crypto.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 1997-2004 Erez Zadok
  5. * Copyright (C) 2001-2004 Stony Brook University
  6. * Copyright (C) 2004-2007 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  8. * Michael C. Thompson <mcthomps@us.ibm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/random.h>
  29. #include <linux/compiler.h>
  30. #include <linux/key.h>
  31. #include <linux/namei.h>
  32. #include <linux/crypto.h>
  33. #include <linux/file.h>
  34. #include <linux/scatterlist.h>
  35. #include <linux/slab.h>
  36. #include <asm/unaligned.h>
  37. #include "ecryptfs_kernel.h"
  38. static int
  39. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  40. struct page *dst_page, int dst_offset,
  41. struct page *src_page, int src_offset, int size,
  42. unsigned char *iv);
  43. static int
  44. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  45. struct page *dst_page, int dst_offset,
  46. struct page *src_page, int src_offset, int size,
  47. unsigned char *iv);
  48. /**
  49. * ecryptfs_to_hex
  50. * @dst: Buffer to take hex character representation of contents of
  51. * src; must be at least of size (src_size * 2)
  52. * @src: Buffer to be converted to a hex string respresentation
  53. * @src_size: number of bytes to convert
  54. */
  55. void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
  56. {
  57. int x;
  58. for (x = 0; x < src_size; x++)
  59. sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
  60. }
  61. /**
  62. * ecryptfs_from_hex
  63. * @dst: Buffer to take the bytes from src hex; must be at least of
  64. * size (src_size / 2)
  65. * @src: Buffer to be converted from a hex string respresentation to raw value
  66. * @dst_size: size of dst buffer, or number of hex characters pairs to convert
  67. */
  68. void ecryptfs_from_hex(char *dst, char *src, int dst_size)
  69. {
  70. int x;
  71. char tmp[3] = { 0, };
  72. for (x = 0; x < dst_size; x++) {
  73. tmp[0] = src[x * 2];
  74. tmp[1] = src[x * 2 + 1];
  75. dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
  76. }
  77. }
  78. /**
  79. * ecryptfs_calculate_md5 - calculates the md5 of @src
  80. * @dst: Pointer to 16 bytes of allocated memory
  81. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  82. * @src: Data to be md5'd
  83. * @len: Length of @src
  84. *
  85. * Uses the allocated crypto context that crypt_stat references to
  86. * generate the MD5 sum of the contents of src.
  87. */
  88. static int ecryptfs_calculate_md5(char *dst,
  89. struct ecryptfs_crypt_stat *crypt_stat,
  90. char *src, int len)
  91. {
  92. struct scatterlist sg;
  93. struct hash_desc desc = {
  94. .tfm = crypt_stat->hash_tfm,
  95. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  96. };
  97. int rc = 0;
  98. mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
  99. sg_init_one(&sg, (u8 *)src, len);
  100. if (!desc.tfm) {
  101. desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
  102. CRYPTO_ALG_ASYNC);
  103. if (IS_ERR(desc.tfm)) {
  104. rc = PTR_ERR(desc.tfm);
  105. ecryptfs_printk(KERN_ERR, "Error attempting to "
  106. "allocate crypto context; rc = [%d]\n",
  107. rc);
  108. goto out;
  109. }
  110. crypt_stat->hash_tfm = desc.tfm;
  111. }
  112. rc = crypto_hash_init(&desc);
  113. if (rc) {
  114. printk(KERN_ERR
  115. "%s: Error initializing crypto hash; rc = [%d]\n",
  116. __func__, rc);
  117. goto out;
  118. }
  119. rc = crypto_hash_update(&desc, &sg, len);
  120. if (rc) {
  121. printk(KERN_ERR
  122. "%s: Error updating crypto hash; rc = [%d]\n",
  123. __func__, rc);
  124. goto out;
  125. }
  126. rc = crypto_hash_final(&desc, dst);
  127. if (rc) {
  128. printk(KERN_ERR
  129. "%s: Error finalizing crypto hash; rc = [%d]\n",
  130. __func__, rc);
  131. goto out;
  132. }
  133. out:
  134. mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
  135. return rc;
  136. }
  137. static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
  138. char *cipher_name,
  139. char *chaining_modifier)
  140. {
  141. int cipher_name_len = strlen(cipher_name);
  142. int chaining_modifier_len = strlen(chaining_modifier);
  143. int algified_name_len;
  144. int rc;
  145. algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
  146. (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
  147. if (!(*algified_name)) {
  148. rc = -ENOMEM;
  149. goto out;
  150. }
  151. snprintf((*algified_name), algified_name_len, "%s(%s)",
  152. chaining_modifier, cipher_name);
  153. rc = 0;
  154. out:
  155. return rc;
  156. }
  157. /**
  158. * ecryptfs_derive_iv
  159. * @iv: destination for the derived iv vale
  160. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  161. * @offset: Offset of the extent whose IV we are to derive
  162. *
  163. * Generate the initialization vector from the given root IV and page
  164. * offset.
  165. *
  166. * Returns zero on success; non-zero on error.
  167. */
  168. int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
  169. loff_t offset)
  170. {
  171. int rc = 0;
  172. char dst[MD5_DIGEST_SIZE];
  173. char src[ECRYPTFS_MAX_IV_BYTES + 16];
  174. if (unlikely(ecryptfs_verbosity > 0)) {
  175. ecryptfs_printk(KERN_DEBUG, "root iv:\n");
  176. ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
  177. }
  178. /* TODO: It is probably secure to just cast the least
  179. * significant bits of the root IV into an unsigned long and
  180. * add the offset to that rather than go through all this
  181. * hashing business. -Halcrow */
  182. memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
  183. memset((src + crypt_stat->iv_bytes), 0, 16);
  184. snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
  185. if (unlikely(ecryptfs_verbosity > 0)) {
  186. ecryptfs_printk(KERN_DEBUG, "source:\n");
  187. ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
  188. }
  189. rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
  190. (crypt_stat->iv_bytes + 16));
  191. if (rc) {
  192. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  193. "MD5 while generating IV for a page\n");
  194. goto out;
  195. }
  196. memcpy(iv, dst, crypt_stat->iv_bytes);
  197. if (unlikely(ecryptfs_verbosity > 0)) {
  198. ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
  199. ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
  200. }
  201. out:
  202. return rc;
  203. }
  204. /**
  205. * ecryptfs_init_crypt_stat
  206. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  207. *
  208. * Initialize the crypt_stat structure.
  209. */
  210. void
  211. ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  212. {
  213. memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  214. INIT_LIST_HEAD(&crypt_stat->keysig_list);
  215. mutex_init(&crypt_stat->keysig_list_mutex);
  216. mutex_init(&crypt_stat->cs_mutex);
  217. mutex_init(&crypt_stat->cs_tfm_mutex);
  218. mutex_init(&crypt_stat->cs_hash_tfm_mutex);
  219. crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
  220. }
  221. /**
  222. * ecryptfs_destroy_crypt_stat
  223. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  224. *
  225. * Releases all memory associated with a crypt_stat struct.
  226. */
  227. void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  228. {
  229. struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
  230. if (crypt_stat->tfm)
  231. crypto_free_ablkcipher(crypt_stat->tfm);
  232. if (crypt_stat->hash_tfm)
  233. crypto_free_hash(crypt_stat->hash_tfm);
  234. list_for_each_entry_safe(key_sig, key_sig_tmp,
  235. &crypt_stat->keysig_list, crypt_stat_list) {
  236. list_del(&key_sig->crypt_stat_list);
  237. kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
  238. }
  239. memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  240. }
  241. void ecryptfs_destroy_mount_crypt_stat(
  242. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  243. {
  244. struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
  245. if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
  246. return;
  247. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  248. list_for_each_entry_safe(auth_tok, auth_tok_tmp,
  249. &mount_crypt_stat->global_auth_tok_list,
  250. mount_crypt_stat_list) {
  251. list_del(&auth_tok->mount_crypt_stat_list);
  252. if (auth_tok->global_auth_tok_key
  253. && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
  254. key_put(auth_tok->global_auth_tok_key);
  255. kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
  256. }
  257. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  258. memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
  259. }
  260. /**
  261. * virt_to_scatterlist
  262. * @addr: Virtual address
  263. * @size: Size of data; should be an even multiple of the block size
  264. * @sg: Pointer to scatterlist array; set to NULL to obtain only
  265. * the number of scatterlist structs required in array
  266. * @sg_size: Max array size
  267. *
  268. * Fills in a scatterlist array with page references for a passed
  269. * virtual address.
  270. *
  271. * Returns the number of scatterlist structs in array used
  272. */
  273. int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
  274. int sg_size)
  275. {
  276. int i = 0;
  277. struct page *pg;
  278. int offset;
  279. int remainder_of_page;
  280. sg_init_table(sg, sg_size);
  281. while (size > 0 && i < sg_size) {
  282. pg = virt_to_page(addr);
  283. offset = offset_in_page(addr);
  284. sg_set_page(&sg[i], pg, 0, offset);
  285. remainder_of_page = PAGE_CACHE_SIZE - offset;
  286. if (size >= remainder_of_page) {
  287. sg[i].length = remainder_of_page;
  288. addr += remainder_of_page;
  289. size -= remainder_of_page;
  290. } else {
  291. sg[i].length = size;
  292. addr += size;
  293. size = 0;
  294. }
  295. i++;
  296. }
  297. if (size > 0)
  298. return -ENOMEM;
  299. return i;
  300. }
  301. struct extent_crypt_result {
  302. struct completion completion;
  303. int rc;
  304. };
  305. static void extent_crypt_complete(struct crypto_async_request *req, int rc)
  306. {
  307. struct extent_crypt_result *ecr = req->data;
  308. if (rc == -EINPROGRESS)
  309. return;
  310. ecr->rc = rc;
  311. complete(&ecr->completion);
  312. }
  313. /**
  314. * encrypt_scatterlist
  315. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  316. * @dest_sg: Destination of encrypted data
  317. * @src_sg: Data to be encrypted
  318. * @size: Length of data to be encrypted
  319. * @iv: iv to use during encryption
  320. *
  321. * Returns the number of bytes encrypted; negative value on error
  322. */
  323. static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  324. struct scatterlist *dest_sg,
  325. struct scatterlist *src_sg, int size,
  326. unsigned char *iv)
  327. {
  328. struct ablkcipher_request *req = NULL;
  329. struct extent_crypt_result ecr;
  330. int rc = 0;
  331. BUG_ON(!crypt_stat || !crypt_stat->tfm
  332. || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
  333. if (unlikely(ecryptfs_verbosity > 0)) {
  334. ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
  335. crypt_stat->key_size);
  336. ecryptfs_dump_hex(crypt_stat->key,
  337. crypt_stat->key_size);
  338. }
  339. init_completion(&ecr.completion);
  340. mutex_lock(&crypt_stat->cs_tfm_mutex);
  341. req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
  342. if (!req) {
  343. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  344. rc = -ENOMEM;
  345. goto out;
  346. }
  347. ablkcipher_request_set_callback(req,
  348. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  349. extent_crypt_complete, &ecr);
  350. /* Consider doing this once, when the file is opened */
  351. if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
  352. rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  353. crypt_stat->key_size);
  354. if (rc) {
  355. ecryptfs_printk(KERN_ERR,
  356. "Error setting key; rc = [%d]\n",
  357. rc);
  358. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  359. rc = -EINVAL;
  360. goto out;
  361. }
  362. crypt_stat->flags |= ECRYPTFS_KEY_SET;
  363. }
  364. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  365. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
  366. ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
  367. rc = crypto_ablkcipher_encrypt(req);
  368. if (rc == -EINPROGRESS || rc == -EBUSY) {
  369. struct extent_crypt_result *ecr = req->base.data;
  370. wait_for_completion(&ecr->completion);
  371. rc = ecr->rc;
  372. INIT_COMPLETION(ecr->completion);
  373. }
  374. out:
  375. ablkcipher_request_free(req);
  376. return rc;
  377. }
  378. /**
  379. * ecryptfs_lower_offset_for_extent
  380. *
  381. * Convert an eCryptfs page index into a lower byte offset
  382. */
  383. static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
  384. struct ecryptfs_crypt_stat *crypt_stat)
  385. {
  386. (*offset) = ecryptfs_lower_header_size(crypt_stat)
  387. + (crypt_stat->extent_size * extent_num);
  388. }
  389. /**
  390. * ecryptfs_encrypt_extent
  391. * @enc_extent_page: Allocated page into which to encrypt the data in
  392. * @page
  393. * @crypt_stat: crypt_stat containing cryptographic context for the
  394. * encryption operation
  395. * @page: Page containing plaintext data extent to encrypt
  396. * @extent_offset: Page extent offset for use in generating IV
  397. *
  398. * Encrypts one extent of data.
  399. *
  400. * Return zero on success; non-zero otherwise
  401. */
  402. static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
  403. struct ecryptfs_crypt_stat *crypt_stat,
  404. struct page *page,
  405. unsigned long extent_offset)
  406. {
  407. loff_t extent_base;
  408. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  409. int rc;
  410. extent_base = (((loff_t)page->index)
  411. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  412. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  413. (extent_base + extent_offset));
  414. if (rc) {
  415. ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
  416. "extent [0x%.16llx]; rc = [%d]\n",
  417. (unsigned long long)(extent_base + extent_offset), rc);
  418. goto out;
  419. }
  420. rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page,
  421. extent_offset * crypt_stat->extent_size,
  422. page,
  423. extent_offset * crypt_stat->extent_size,
  424. crypt_stat->extent_size, extent_iv);
  425. if (rc < 0) {
  426. printk(KERN_ERR "%s: Error attempting to encrypt page with "
  427. "page->index = [%ld], extent_offset = [%ld]; "
  428. "rc = [%d]\n", __func__, page->index, extent_offset,
  429. rc);
  430. goto out;
  431. }
  432. rc = 0;
  433. out:
  434. return rc;
  435. }
  436. /**
  437. * ecryptfs_encrypt_page
  438. * @page: Page mapped from the eCryptfs inode for the file; contains
  439. * decrypted content that needs to be encrypted (to a temporary
  440. * page; not in place) and written out to the lower file
  441. *
  442. * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
  443. * that eCryptfs pages may straddle the lower pages -- for instance,
  444. * if the file was created on a machine with an 8K page size
  445. * (resulting in an 8K header), and then the file is copied onto a
  446. * host with a 32K page size, then when reading page 0 of the eCryptfs
  447. * file, 24K of page 0 of the lower file will be read and decrypted,
  448. * and then 8K of page 1 of the lower file will be read and decrypted.
  449. *
  450. * Returns zero on success; negative on error
  451. */
  452. int ecryptfs_encrypt_page(struct page *page)
  453. {
  454. struct inode *ecryptfs_inode;
  455. struct ecryptfs_crypt_stat *crypt_stat;
  456. char *enc_extent_virt;
  457. struct page *enc_extent_page = NULL;
  458. loff_t extent_offset;
  459. int rc = 0;
  460. ecryptfs_inode = page->mapping->host;
  461. crypt_stat =
  462. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  463. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  464. enc_extent_page = alloc_page(GFP_USER);
  465. if (!enc_extent_page) {
  466. rc = -ENOMEM;
  467. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  468. "encrypted extent\n");
  469. goto out;
  470. }
  471. enc_extent_virt = kmap(enc_extent_page);
  472. for (extent_offset = 0;
  473. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  474. extent_offset++) {
  475. loff_t offset;
  476. rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
  477. extent_offset);
  478. if (rc) {
  479. printk(KERN_ERR "%s: Error encrypting extent; "
  480. "rc = [%d]\n", __func__, rc);
  481. goto out;
  482. }
  483. ecryptfs_lower_offset_for_extent(
  484. &offset, ((((loff_t)page->index)
  485. * (PAGE_CACHE_SIZE
  486. / crypt_stat->extent_size))
  487. + extent_offset), crypt_stat);
  488. rc = ecryptfs_write_lower(ecryptfs_inode, (enc_extent_virt +
  489. extent_offset * crypt_stat->extent_size),
  490. offset, crypt_stat->extent_size);
  491. if (rc < 0) {
  492. ecryptfs_printk(KERN_ERR, "Error attempting "
  493. "to write lower page; rc = [%d]"
  494. "\n", rc);
  495. goto out;
  496. }
  497. }
  498. rc = 0;
  499. out:
  500. if (enc_extent_page) {
  501. kunmap(enc_extent_page);
  502. __free_page(enc_extent_page);
  503. }
  504. return rc;
  505. }
  506. static int ecryptfs_decrypt_extent(struct page *page,
  507. struct ecryptfs_crypt_stat *crypt_stat,
  508. struct page *enc_extent_page,
  509. unsigned long extent_offset)
  510. {
  511. loff_t extent_base;
  512. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  513. int rc;
  514. extent_base = (((loff_t)page->index)
  515. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  516. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  517. (extent_base + extent_offset));
  518. if (rc) {
  519. ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
  520. "extent [0x%.16llx]; rc = [%d]\n",
  521. (unsigned long long)(extent_base + extent_offset), rc);
  522. goto out;
  523. }
  524. rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
  525. extent_offset * crypt_stat->extent_size,
  526. enc_extent_page,
  527. extent_offset * crypt_stat->extent_size,
  528. crypt_stat->extent_size, extent_iv);
  529. if (rc < 0) {
  530. printk(KERN_ERR "%s: Error attempting to decrypt to page with "
  531. "page->index = [%ld], extent_offset = [%ld]; "
  532. "rc = [%d]\n", __func__, page->index, extent_offset,
  533. rc);
  534. goto out;
  535. }
  536. rc = 0;
  537. out:
  538. return rc;
  539. }
  540. /**
  541. * ecryptfs_decrypt_page
  542. * @page: Page mapped from the eCryptfs inode for the file; data read
  543. * and decrypted from the lower file will be written into this
  544. * page
  545. *
  546. * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
  547. * that eCryptfs pages may straddle the lower pages -- for instance,
  548. * if the file was created on a machine with an 8K page size
  549. * (resulting in an 8K header), and then the file is copied onto a
  550. * host with a 32K page size, then when reading page 0 of the eCryptfs
  551. * file, 24K of page 0 of the lower file will be read and decrypted,
  552. * and then 8K of page 1 of the lower file will be read and decrypted.
  553. *
  554. * Returns zero on success; negative on error
  555. */
  556. int ecryptfs_decrypt_page(struct page *page)
  557. {
  558. struct inode *ecryptfs_inode;
  559. struct ecryptfs_crypt_stat *crypt_stat;
  560. char *enc_extent_virt;
  561. struct page *enc_extent_page = NULL;
  562. unsigned long extent_offset;
  563. int rc = 0;
  564. ecryptfs_inode = page->mapping->host;
  565. crypt_stat =
  566. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  567. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  568. enc_extent_page = alloc_page(GFP_USER);
  569. if (!enc_extent_page) {
  570. rc = -ENOMEM;
  571. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  572. "encrypted extent\n");
  573. goto out;
  574. }
  575. enc_extent_virt = kmap(enc_extent_page);
  576. for (extent_offset = 0;
  577. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  578. extent_offset++) {
  579. loff_t offset;
  580. ecryptfs_lower_offset_for_extent(
  581. &offset, ((page->index * (PAGE_CACHE_SIZE
  582. / crypt_stat->extent_size))
  583. + extent_offset), crypt_stat);
  584. rc = ecryptfs_read_lower((enc_extent_virt +
  585. extent_offset * crypt_stat->extent_size),
  586. offset, crypt_stat->extent_size,
  587. ecryptfs_inode);
  588. if (rc < 0) {
  589. ecryptfs_printk(KERN_ERR, "Error attempting "
  590. "to read lower page; rc = [%d]"
  591. "\n", rc);
  592. goto out;
  593. }
  594. rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
  595. extent_offset);
  596. if (rc) {
  597. printk(KERN_ERR "%s: Error encrypting extent; "
  598. "rc = [%d]\n", __func__, rc);
  599. goto out;
  600. }
  601. }
  602. out:
  603. if (enc_extent_page) {
  604. kunmap(enc_extent_page);
  605. __free_page(enc_extent_page);
  606. }
  607. return rc;
  608. }
  609. /**
  610. * decrypt_scatterlist
  611. * @crypt_stat: Cryptographic context
  612. * @dest_sg: The destination scatterlist to decrypt into
  613. * @src_sg: The source scatterlist to decrypt from
  614. * @size: The number of bytes to decrypt
  615. * @iv: The initialization vector to use for the decryption
  616. *
  617. * Returns the number of bytes decrypted; negative value on error
  618. */
  619. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  620. struct scatterlist *dest_sg,
  621. struct scatterlist *src_sg, int size,
  622. unsigned char *iv)
  623. {
  624. struct ablkcipher_request *req = NULL;
  625. struct extent_crypt_result ecr;
  626. int rc = 0;
  627. BUG_ON(!crypt_stat || !crypt_stat->tfm
  628. || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
  629. if (unlikely(ecryptfs_verbosity > 0)) {
  630. ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
  631. crypt_stat->key_size);
  632. ecryptfs_dump_hex(crypt_stat->key,
  633. crypt_stat->key_size);
  634. }
  635. init_completion(&ecr.completion);
  636. mutex_lock(&crypt_stat->cs_tfm_mutex);
  637. req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
  638. if (!req) {
  639. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  640. rc = -ENOMEM;
  641. goto out;
  642. }
  643. ablkcipher_request_set_callback(req,
  644. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  645. extent_crypt_complete, &ecr);
  646. /* Consider doing this once, when the file is opened */
  647. if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
  648. rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  649. crypt_stat->key_size);
  650. if (rc) {
  651. ecryptfs_printk(KERN_ERR,
  652. "Error setting key; rc = [%d]\n",
  653. rc);
  654. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  655. rc = -EINVAL;
  656. goto out;
  657. }
  658. crypt_stat->flags |= ECRYPTFS_KEY_SET;
  659. }
  660. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  661. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  662. ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
  663. rc = crypto_ablkcipher_decrypt(req);
  664. if (rc == -EINPROGRESS || rc == -EBUSY) {
  665. struct extent_crypt_result *ecr = req->base.data;
  666. wait_for_completion(&ecr->completion);
  667. rc = ecr->rc;
  668. INIT_COMPLETION(ecr->completion);
  669. }
  670. out:
  671. ablkcipher_request_free(req);
  672. return rc;
  673. }
  674. /**
  675. * ecryptfs_encrypt_page_offset
  676. * @crypt_stat: The cryptographic context
  677. * @dst_page: The page to encrypt into
  678. * @dst_offset: The offset in the page to encrypt into
  679. * @src_page: The page to encrypt from
  680. * @src_offset: The offset in the page to encrypt from
  681. * @size: The number of bytes to encrypt
  682. * @iv: The initialization vector to use for the encryption
  683. *
  684. * Returns the number of bytes encrypted
  685. */
  686. static int
  687. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  688. struct page *dst_page, int dst_offset,
  689. struct page *src_page, int src_offset, int size,
  690. unsigned char *iv)
  691. {
  692. struct scatterlist src_sg, dst_sg;
  693. sg_init_table(&src_sg, 1);
  694. sg_init_table(&dst_sg, 1);
  695. sg_set_page(&src_sg, src_page, size, src_offset);
  696. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  697. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  698. }
  699. /**
  700. * ecryptfs_decrypt_page_offset
  701. * @crypt_stat: The cryptographic context
  702. * @dst_page: The page to decrypt into
  703. * @dst_offset: The offset in the page to decrypt into
  704. * @src_page: The page to decrypt from
  705. * @src_offset: The offset in the page to decrypt from
  706. * @size: The number of bytes to decrypt
  707. * @iv: The initialization vector to use for the decryption
  708. *
  709. * Returns the number of bytes decrypted
  710. */
  711. static int
  712. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  713. struct page *dst_page, int dst_offset,
  714. struct page *src_page, int src_offset, int size,
  715. unsigned char *iv)
  716. {
  717. struct scatterlist src_sg, dst_sg;
  718. sg_init_table(&src_sg, 1);
  719. sg_set_page(&src_sg, src_page, size, src_offset);
  720. sg_init_table(&dst_sg, 1);
  721. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  722. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  723. }
  724. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  725. /**
  726. * ecryptfs_init_crypt_ctx
  727. * @crypt_stat: Uninitialized crypt stats structure
  728. *
  729. * Initialize the crypto context.
  730. *
  731. * TODO: Performance: Keep a cache of initialized cipher contexts;
  732. * only init if needed
  733. */
  734. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  735. {
  736. char *full_alg_name;
  737. int rc = -EINVAL;
  738. if (!crypt_stat->cipher) {
  739. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  740. goto out;
  741. }
  742. ecryptfs_printk(KERN_DEBUG,
  743. "Initializing cipher [%s]; strlen = [%d]; "
  744. "key_size_bits = [%zd]\n",
  745. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  746. crypt_stat->key_size << 3);
  747. if (crypt_stat->tfm) {
  748. rc = 0;
  749. goto out;
  750. }
  751. mutex_lock(&crypt_stat->cs_tfm_mutex);
  752. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  753. crypt_stat->cipher, "cbc");
  754. if (rc)
  755. goto out_unlock;
  756. crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
  757. kfree(full_alg_name);
  758. if (IS_ERR(crypt_stat->tfm)) {
  759. rc = PTR_ERR(crypt_stat->tfm);
  760. crypt_stat->tfm = NULL;
  761. ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
  762. "Error initializing cipher [%s]\n",
  763. crypt_stat->cipher);
  764. goto out_unlock;
  765. }
  766. crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  767. rc = 0;
  768. out_unlock:
  769. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  770. out:
  771. return rc;
  772. }
  773. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  774. {
  775. int extent_size_tmp;
  776. crypt_stat->extent_mask = 0xFFFFFFFF;
  777. crypt_stat->extent_shift = 0;
  778. if (crypt_stat->extent_size == 0)
  779. return;
  780. extent_size_tmp = crypt_stat->extent_size;
  781. while ((extent_size_tmp & 0x01) == 0) {
  782. extent_size_tmp >>= 1;
  783. crypt_stat->extent_mask <<= 1;
  784. crypt_stat->extent_shift++;
  785. }
  786. }
  787. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  788. {
  789. /* Default values; may be overwritten as we are parsing the
  790. * packets. */
  791. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  792. set_extent_mask_and_shift(crypt_stat);
  793. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  794. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  795. crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  796. else {
  797. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
  798. crypt_stat->metadata_size =
  799. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  800. else
  801. crypt_stat->metadata_size = PAGE_CACHE_SIZE;
  802. }
  803. }
  804. /**
  805. * ecryptfs_compute_root_iv
  806. * @crypt_stats
  807. *
  808. * On error, sets the root IV to all 0's.
  809. */
  810. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
  811. {
  812. int rc = 0;
  813. char dst[MD5_DIGEST_SIZE];
  814. BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
  815. BUG_ON(crypt_stat->iv_bytes <= 0);
  816. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  817. rc = -EINVAL;
  818. ecryptfs_printk(KERN_WARNING, "Session key not valid; "
  819. "cannot generate root IV\n");
  820. goto out;
  821. }
  822. rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
  823. crypt_stat->key_size);
  824. if (rc) {
  825. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  826. "MD5 while generating root IV\n");
  827. goto out;
  828. }
  829. memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
  830. out:
  831. if (rc) {
  832. memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
  833. crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
  834. }
  835. return rc;
  836. }
  837. static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
  838. {
  839. get_random_bytes(crypt_stat->key, crypt_stat->key_size);
  840. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  841. ecryptfs_compute_root_iv(crypt_stat);
  842. if (unlikely(ecryptfs_verbosity > 0)) {
  843. ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
  844. ecryptfs_dump_hex(crypt_stat->key,
  845. crypt_stat->key_size);
  846. }
  847. }
  848. /**
  849. * ecryptfs_copy_mount_wide_flags_to_inode_flags
  850. * @crypt_stat: The inode's cryptographic context
  851. * @mount_crypt_stat: The mount point's cryptographic context
  852. *
  853. * This function propagates the mount-wide flags to individual inode
  854. * flags.
  855. */
  856. static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
  857. struct ecryptfs_crypt_stat *crypt_stat,
  858. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  859. {
  860. if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
  861. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  862. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  863. crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
  864. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  865. crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
  866. if (mount_crypt_stat->flags
  867. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
  868. crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
  869. else if (mount_crypt_stat->flags
  870. & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
  871. crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
  872. }
  873. }
  874. static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
  875. struct ecryptfs_crypt_stat *crypt_stat,
  876. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  877. {
  878. struct ecryptfs_global_auth_tok *global_auth_tok;
  879. int rc = 0;
  880. mutex_lock(&crypt_stat->keysig_list_mutex);
  881. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  882. list_for_each_entry(global_auth_tok,
  883. &mount_crypt_stat->global_auth_tok_list,
  884. mount_crypt_stat_list) {
  885. if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
  886. continue;
  887. rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
  888. if (rc) {
  889. printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
  890. goto out;
  891. }
  892. }
  893. out:
  894. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  895. mutex_unlock(&crypt_stat->keysig_list_mutex);
  896. return rc;
  897. }
  898. /**
  899. * ecryptfs_set_default_crypt_stat_vals
  900. * @crypt_stat: The inode's cryptographic context
  901. * @mount_crypt_stat: The mount point's cryptographic context
  902. *
  903. * Default values in the event that policy does not override them.
  904. */
  905. static void ecryptfs_set_default_crypt_stat_vals(
  906. struct ecryptfs_crypt_stat *crypt_stat,
  907. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  908. {
  909. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  910. mount_crypt_stat);
  911. ecryptfs_set_default_sizes(crypt_stat);
  912. strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
  913. crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
  914. crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
  915. crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
  916. crypt_stat->mount_crypt_stat = mount_crypt_stat;
  917. }
  918. /**
  919. * ecryptfs_new_file_context
  920. * @ecryptfs_inode: The eCryptfs inode
  921. *
  922. * If the crypto context for the file has not yet been established,
  923. * this is where we do that. Establishing a new crypto context
  924. * involves the following decisions:
  925. * - What cipher to use?
  926. * - What set of authentication tokens to use?
  927. * Here we just worry about getting enough information into the
  928. * authentication tokens so that we know that they are available.
  929. * We associate the available authentication tokens with the new file
  930. * via the set of signatures in the crypt_stat struct. Later, when
  931. * the headers are actually written out, we may again defer to
  932. * userspace to perform the encryption of the session key; for the
  933. * foreseeable future, this will be the case with public key packets.
  934. *
  935. * Returns zero on success; non-zero otherwise
  936. */
  937. int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
  938. {
  939. struct ecryptfs_crypt_stat *crypt_stat =
  940. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  941. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  942. &ecryptfs_superblock_to_private(
  943. ecryptfs_inode->i_sb)->mount_crypt_stat;
  944. int cipher_name_len;
  945. int rc = 0;
  946. ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
  947. crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
  948. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  949. mount_crypt_stat);
  950. rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
  951. mount_crypt_stat);
  952. if (rc) {
  953. printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
  954. "to the inode key sigs; rc = [%d]\n", rc);
  955. goto out;
  956. }
  957. cipher_name_len =
  958. strlen(mount_crypt_stat->global_default_cipher_name);
  959. memcpy(crypt_stat->cipher,
  960. mount_crypt_stat->global_default_cipher_name,
  961. cipher_name_len);
  962. crypt_stat->cipher[cipher_name_len] = '\0';
  963. crypt_stat->key_size =
  964. mount_crypt_stat->global_default_cipher_key_size;
  965. ecryptfs_generate_new_key(crypt_stat);
  966. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  967. if (rc)
  968. ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
  969. "context for cipher [%s]: rc = [%d]\n",
  970. crypt_stat->cipher, rc);
  971. out:
  972. return rc;
  973. }
  974. /**
  975. * ecryptfs_validate_marker - check for the ecryptfs marker
  976. * @data: The data block in which to check
  977. *
  978. * Returns zero if marker found; -EINVAL if not found
  979. */
  980. static int ecryptfs_validate_marker(char *data)
  981. {
  982. u32 m_1, m_2;
  983. m_1 = get_unaligned_be32(data);
  984. m_2 = get_unaligned_be32(data + 4);
  985. if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
  986. return 0;
  987. ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
  988. "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
  989. MAGIC_ECRYPTFS_MARKER);
  990. ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
  991. "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
  992. return -EINVAL;
  993. }
  994. struct ecryptfs_flag_map_elem {
  995. u32 file_flag;
  996. u32 local_flag;
  997. };
  998. /* Add support for additional flags by adding elements here. */
  999. static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
  1000. {0x00000001, ECRYPTFS_ENABLE_HMAC},
  1001. {0x00000002, ECRYPTFS_ENCRYPTED},
  1002. {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
  1003. {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
  1004. };
  1005. /**
  1006. * ecryptfs_process_flags
  1007. * @crypt_stat: The cryptographic context
  1008. * @page_virt: Source data to be parsed
  1009. * @bytes_read: Updated with the number of bytes read
  1010. *
  1011. * Returns zero on success; non-zero if the flag set is invalid
  1012. */
  1013. static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
  1014. char *page_virt, int *bytes_read)
  1015. {
  1016. int rc = 0;
  1017. int i;
  1018. u32 flags;
  1019. flags = get_unaligned_be32(page_virt);
  1020. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1021. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1022. if (flags & ecryptfs_flag_map[i].file_flag) {
  1023. crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
  1024. } else
  1025. crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
  1026. /* Version is in top 8 bits of the 32-bit flag vector */
  1027. crypt_stat->file_version = ((flags >> 24) & 0xFF);
  1028. (*bytes_read) = 4;
  1029. return rc;
  1030. }
  1031. /**
  1032. * write_ecryptfs_marker
  1033. * @page_virt: The pointer to in a page to begin writing the marker
  1034. * @written: Number of bytes written
  1035. *
  1036. * Marker = 0x3c81b7f5
  1037. */
  1038. static void write_ecryptfs_marker(char *page_virt, size_t *written)
  1039. {
  1040. u32 m_1, m_2;
  1041. get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1042. m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
  1043. put_unaligned_be32(m_1, page_virt);
  1044. page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
  1045. put_unaligned_be32(m_2, page_virt);
  1046. (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1047. }
  1048. void ecryptfs_write_crypt_stat_flags(char *page_virt,
  1049. struct ecryptfs_crypt_stat *crypt_stat,
  1050. size_t *written)
  1051. {
  1052. u32 flags = 0;
  1053. int i;
  1054. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1055. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1056. if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
  1057. flags |= ecryptfs_flag_map[i].file_flag;
  1058. /* Version is in top 8 bits of the 32-bit flag vector */
  1059. flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
  1060. put_unaligned_be32(flags, page_virt);
  1061. (*written) = 4;
  1062. }
  1063. struct ecryptfs_cipher_code_str_map_elem {
  1064. char cipher_str[16];
  1065. u8 cipher_code;
  1066. };
  1067. /* Add support for additional ciphers by adding elements here. The
  1068. * cipher_code is whatever OpenPGP applicatoins use to identify the
  1069. * ciphers. List in order of probability. */
  1070. static struct ecryptfs_cipher_code_str_map_elem
  1071. ecryptfs_cipher_code_str_map[] = {
  1072. {"aes",RFC2440_CIPHER_AES_128 },
  1073. {"blowfish", RFC2440_CIPHER_BLOWFISH},
  1074. {"des3_ede", RFC2440_CIPHER_DES3_EDE},
  1075. {"cast5", RFC2440_CIPHER_CAST_5},
  1076. {"twofish", RFC2440_CIPHER_TWOFISH},
  1077. {"cast6", RFC2440_CIPHER_CAST_6},
  1078. {"aes", RFC2440_CIPHER_AES_192},
  1079. {"aes", RFC2440_CIPHER_AES_256}
  1080. };
  1081. /**
  1082. * ecryptfs_code_for_cipher_string
  1083. * @cipher_name: The string alias for the cipher
  1084. * @key_bytes: Length of key in bytes; used for AES code selection
  1085. *
  1086. * Returns zero on no match, or the cipher code on match
  1087. */
  1088. u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
  1089. {
  1090. int i;
  1091. u8 code = 0;
  1092. struct ecryptfs_cipher_code_str_map_elem *map =
  1093. ecryptfs_cipher_code_str_map;
  1094. if (strcmp(cipher_name, "aes") == 0) {
  1095. switch (key_bytes) {
  1096. case 16:
  1097. code = RFC2440_CIPHER_AES_128;
  1098. break;
  1099. case 24:
  1100. code = RFC2440_CIPHER_AES_192;
  1101. break;
  1102. case 32:
  1103. code = RFC2440_CIPHER_AES_256;
  1104. }
  1105. } else {
  1106. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1107. if (strcmp(cipher_name, map[i].cipher_str) == 0) {
  1108. code = map[i].cipher_code;
  1109. break;
  1110. }
  1111. }
  1112. return code;
  1113. }
  1114. /**
  1115. * ecryptfs_cipher_code_to_string
  1116. * @str: Destination to write out the cipher name
  1117. * @cipher_code: The code to convert to cipher name string
  1118. *
  1119. * Returns zero on success
  1120. */
  1121. int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
  1122. {
  1123. int rc = 0;
  1124. int i;
  1125. str[0] = '\0';
  1126. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1127. if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
  1128. strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
  1129. if (str[0] == '\0') {
  1130. ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
  1131. "[%d]\n", cipher_code);
  1132. rc = -EINVAL;
  1133. }
  1134. return rc;
  1135. }
  1136. int ecryptfs_read_and_validate_header_region(struct inode *inode)
  1137. {
  1138. u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
  1139. u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
  1140. int rc;
  1141. rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
  1142. inode);
  1143. if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
  1144. return rc >= 0 ? -EINVAL : rc;
  1145. rc = ecryptfs_validate_marker(marker);
  1146. if (!rc)
  1147. ecryptfs_i_size_init(file_size, inode);
  1148. return rc;
  1149. }
  1150. void
  1151. ecryptfs_write_header_metadata(char *virt,
  1152. struct ecryptfs_crypt_stat *crypt_stat,
  1153. size_t *written)
  1154. {
  1155. u32 header_extent_size;
  1156. u16 num_header_extents_at_front;
  1157. header_extent_size = (u32)crypt_stat->extent_size;
  1158. num_header_extents_at_front =
  1159. (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
  1160. put_unaligned_be32(header_extent_size, virt);
  1161. virt += 4;
  1162. put_unaligned_be16(num_header_extents_at_front, virt);
  1163. (*written) = 6;
  1164. }
  1165. struct kmem_cache *ecryptfs_header_cache;
  1166. /**
  1167. * ecryptfs_write_headers_virt
  1168. * @page_virt: The virtual address to write the headers to
  1169. * @max: The size of memory allocated at page_virt
  1170. * @size: Set to the number of bytes written by this function
  1171. * @crypt_stat: The cryptographic context
  1172. * @ecryptfs_dentry: The eCryptfs dentry
  1173. *
  1174. * Format version: 1
  1175. *
  1176. * Header Extent:
  1177. * Octets 0-7: Unencrypted file size (big-endian)
  1178. * Octets 8-15: eCryptfs special marker
  1179. * Octets 16-19: Flags
  1180. * Octet 16: File format version number (between 0 and 255)
  1181. * Octets 17-18: Reserved
  1182. * Octet 19: Bit 1 (lsb): Reserved
  1183. * Bit 2: Encrypted?
  1184. * Bits 3-8: Reserved
  1185. * Octets 20-23: Header extent size (big-endian)
  1186. * Octets 24-25: Number of header extents at front of file
  1187. * (big-endian)
  1188. * Octet 26: Begin RFC 2440 authentication token packet set
  1189. * Data Extent 0:
  1190. * Lower data (CBC encrypted)
  1191. * Data Extent 1:
  1192. * Lower data (CBC encrypted)
  1193. * ...
  1194. *
  1195. * Returns zero on success
  1196. */
  1197. static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
  1198. size_t *size,
  1199. struct ecryptfs_crypt_stat *crypt_stat,
  1200. struct dentry *ecryptfs_dentry)
  1201. {
  1202. int rc;
  1203. size_t written;
  1204. size_t offset;
  1205. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1206. write_ecryptfs_marker((page_virt + offset), &written);
  1207. offset += written;
  1208. ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
  1209. &written);
  1210. offset += written;
  1211. ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
  1212. &written);
  1213. offset += written;
  1214. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1215. ecryptfs_dentry, &written,
  1216. max - offset);
  1217. if (rc)
  1218. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1219. "set; rc = [%d]\n", rc);
  1220. if (size) {
  1221. offset += written;
  1222. *size = offset;
  1223. }
  1224. return rc;
  1225. }
  1226. static int
  1227. ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
  1228. char *virt, size_t virt_len)
  1229. {
  1230. int rc;
  1231. rc = ecryptfs_write_lower(ecryptfs_inode, virt,
  1232. 0, virt_len);
  1233. if (rc < 0)
  1234. printk(KERN_ERR "%s: Error attempting to write header "
  1235. "information to lower file; rc = [%d]\n", __func__, rc);
  1236. else
  1237. rc = 0;
  1238. return rc;
  1239. }
  1240. static int
  1241. ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
  1242. char *page_virt, size_t size)
  1243. {
  1244. int rc;
  1245. rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
  1246. size, 0);
  1247. return rc;
  1248. }
  1249. static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
  1250. unsigned int order)
  1251. {
  1252. struct page *page;
  1253. page = alloc_pages(gfp_mask | __GFP_ZERO, order);
  1254. if (page)
  1255. return (unsigned long) page_address(page);
  1256. return 0;
  1257. }
  1258. /**
  1259. * ecryptfs_write_metadata
  1260. * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
  1261. * @ecryptfs_inode: The newly created eCryptfs inode
  1262. *
  1263. * Write the file headers out. This will likely involve a userspace
  1264. * callout, in which the session key is encrypted with one or more
  1265. * public keys and/or the passphrase necessary to do the encryption is
  1266. * retrieved via a prompt. Exactly what happens at this point should
  1267. * be policy-dependent.
  1268. *
  1269. * Returns zero on success; non-zero on error
  1270. */
  1271. int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
  1272. struct inode *ecryptfs_inode)
  1273. {
  1274. struct ecryptfs_crypt_stat *crypt_stat =
  1275. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1276. unsigned int order;
  1277. char *virt;
  1278. size_t virt_len;
  1279. size_t size = 0;
  1280. int rc = 0;
  1281. if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  1282. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  1283. printk(KERN_ERR "Key is invalid; bailing out\n");
  1284. rc = -EINVAL;
  1285. goto out;
  1286. }
  1287. } else {
  1288. printk(KERN_WARNING "%s: Encrypted flag not set\n",
  1289. __func__);
  1290. rc = -EINVAL;
  1291. goto out;
  1292. }
  1293. virt_len = crypt_stat->metadata_size;
  1294. order = get_order(virt_len);
  1295. /* Released in this function */
  1296. virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
  1297. if (!virt) {
  1298. printk(KERN_ERR "%s: Out of memory\n", __func__);
  1299. rc = -ENOMEM;
  1300. goto out;
  1301. }
  1302. /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
  1303. rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
  1304. ecryptfs_dentry);
  1305. if (unlikely(rc)) {
  1306. printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
  1307. __func__, rc);
  1308. goto out_free;
  1309. }
  1310. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1311. rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
  1312. size);
  1313. else
  1314. rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
  1315. virt_len);
  1316. if (rc) {
  1317. printk(KERN_ERR "%s: Error writing metadata out to lower file; "
  1318. "rc = [%d]\n", __func__, rc);
  1319. goto out_free;
  1320. }
  1321. out_free:
  1322. free_pages((unsigned long)virt, order);
  1323. out:
  1324. return rc;
  1325. }
  1326. #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
  1327. #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
  1328. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1329. char *virt, int *bytes_read,
  1330. int validate_header_size)
  1331. {
  1332. int rc = 0;
  1333. u32 header_extent_size;
  1334. u16 num_header_extents_at_front;
  1335. header_extent_size = get_unaligned_be32(virt);
  1336. virt += sizeof(__be32);
  1337. num_header_extents_at_front = get_unaligned_be16(virt);
  1338. crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
  1339. * (size_t)header_extent_size));
  1340. (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
  1341. if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
  1342. && (crypt_stat->metadata_size
  1343. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
  1344. rc = -EINVAL;
  1345. printk(KERN_WARNING "Invalid header size: [%zd]\n",
  1346. crypt_stat->metadata_size);
  1347. }
  1348. return rc;
  1349. }
  1350. /**
  1351. * set_default_header_data
  1352. * @crypt_stat: The cryptographic context
  1353. *
  1354. * For version 0 file format; this function is only for backwards
  1355. * compatibility for files created with the prior versions of
  1356. * eCryptfs.
  1357. */
  1358. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1359. {
  1360. crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  1361. }
  1362. void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
  1363. {
  1364. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  1365. struct ecryptfs_crypt_stat *crypt_stat;
  1366. u64 file_size;
  1367. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  1368. mount_crypt_stat =
  1369. &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
  1370. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  1371. file_size = i_size_read(ecryptfs_inode_to_lower(inode));
  1372. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1373. file_size += crypt_stat->metadata_size;
  1374. } else
  1375. file_size = get_unaligned_be64(page_virt);
  1376. i_size_write(inode, (loff_t)file_size);
  1377. crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
  1378. }
  1379. /**
  1380. * ecryptfs_read_headers_virt
  1381. * @page_virt: The virtual address into which to read the headers
  1382. * @crypt_stat: The cryptographic context
  1383. * @ecryptfs_dentry: The eCryptfs dentry
  1384. * @validate_header_size: Whether to validate the header size while reading
  1385. *
  1386. * Read/parse the header data. The header format is detailed in the
  1387. * comment block for the ecryptfs_write_headers_virt() function.
  1388. *
  1389. * Returns zero on success
  1390. */
  1391. static int ecryptfs_read_headers_virt(char *page_virt,
  1392. struct ecryptfs_crypt_stat *crypt_stat,
  1393. struct dentry *ecryptfs_dentry,
  1394. int validate_header_size)
  1395. {
  1396. int rc = 0;
  1397. int offset;
  1398. int bytes_read;
  1399. ecryptfs_set_default_sizes(crypt_stat);
  1400. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1401. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1402. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1403. rc = ecryptfs_validate_marker(page_virt + offset);
  1404. if (rc)
  1405. goto out;
  1406. if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
  1407. ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
  1408. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1409. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1410. &bytes_read);
  1411. if (rc) {
  1412. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1413. goto out;
  1414. }
  1415. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1416. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1417. "file version [%d] is supported by this "
  1418. "version of eCryptfs\n",
  1419. crypt_stat->file_version,
  1420. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1421. rc = -EINVAL;
  1422. goto out;
  1423. }
  1424. offset += bytes_read;
  1425. if (crypt_stat->file_version >= 1) {
  1426. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1427. &bytes_read, validate_header_size);
  1428. if (rc) {
  1429. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1430. "metadata; rc = [%d]\n", rc);
  1431. }
  1432. offset += bytes_read;
  1433. } else
  1434. set_default_header_data(crypt_stat);
  1435. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1436. ecryptfs_dentry);
  1437. out:
  1438. return rc;
  1439. }
  1440. /**
  1441. * ecryptfs_read_xattr_region
  1442. * @page_virt: The vitual address into which to read the xattr data
  1443. * @ecryptfs_inode: The eCryptfs inode
  1444. *
  1445. * Attempts to read the crypto metadata from the extended attribute
  1446. * region of the lower file.
  1447. *
  1448. * Returns zero on success; non-zero on error
  1449. */
  1450. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
  1451. {
  1452. struct dentry *lower_dentry =
  1453. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  1454. ssize_t size;
  1455. int rc = 0;
  1456. size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
  1457. page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
  1458. if (size < 0) {
  1459. if (unlikely(ecryptfs_verbosity > 0))
  1460. printk(KERN_INFO "Error attempting to read the [%s] "
  1461. "xattr from the lower file; return value = "
  1462. "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
  1463. rc = -EINVAL;
  1464. goto out;
  1465. }
  1466. out:
  1467. return rc;
  1468. }
  1469. int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
  1470. struct inode *inode)
  1471. {
  1472. u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
  1473. u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
  1474. int rc;
  1475. rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
  1476. ECRYPTFS_XATTR_NAME, file_size,
  1477. ECRYPTFS_SIZE_AND_MARKER_BYTES);
  1478. if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
  1479. return rc >= 0 ? -EINVAL : rc;
  1480. rc = ecryptfs_validate_marker(marker);
  1481. if (!rc)
  1482. ecryptfs_i_size_init(file_size, inode);
  1483. return rc;
  1484. }
  1485. /**
  1486. * ecryptfs_read_metadata
  1487. *
  1488. * Common entry point for reading file metadata. From here, we could
  1489. * retrieve the header information from the header region of the file,
  1490. * the xattr region of the file, or some other repostory that is
  1491. * stored separately from the file itself. The current implementation
  1492. * supports retrieving the metadata information from the file contents
  1493. * and from the xattr region.
  1494. *
  1495. * Returns zero if valid headers found and parsed; non-zero otherwise
  1496. */
  1497. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
  1498. {
  1499. int rc;
  1500. char *page_virt;
  1501. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  1502. struct ecryptfs_crypt_stat *crypt_stat =
  1503. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1504. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1505. &ecryptfs_superblock_to_private(
  1506. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1507. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  1508. mount_crypt_stat);
  1509. /* Read the first page from the underlying file */
  1510. page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
  1511. if (!page_virt) {
  1512. rc = -ENOMEM;
  1513. printk(KERN_ERR "%s: Unable to allocate page_virt\n",
  1514. __func__);
  1515. goto out;
  1516. }
  1517. rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
  1518. ecryptfs_inode);
  1519. if (rc >= 0)
  1520. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1521. ecryptfs_dentry,
  1522. ECRYPTFS_VALIDATE_HEADER_SIZE);
  1523. if (rc) {
  1524. /* metadata is not in the file header, so try xattrs */
  1525. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1526. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
  1527. if (rc) {
  1528. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1529. "file header region or xattr region, inode %lu\n",
  1530. ecryptfs_inode->i_ino);
  1531. rc = -EINVAL;
  1532. goto out;
  1533. }
  1534. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1535. ecryptfs_dentry,
  1536. ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
  1537. if (rc) {
  1538. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1539. "file xattr region either, inode %lu\n",
  1540. ecryptfs_inode->i_ino);
  1541. rc = -EINVAL;
  1542. }
  1543. if (crypt_stat->mount_crypt_stat->flags
  1544. & ECRYPTFS_XATTR_METADATA_ENABLED) {
  1545. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  1546. } else {
  1547. printk(KERN_WARNING "Attempt to access file with "
  1548. "crypto metadata only in the extended attribute "
  1549. "region, but eCryptfs was mounted without "
  1550. "xattr support enabled. eCryptfs will not treat "
  1551. "this like an encrypted file, inode %lu\n",
  1552. ecryptfs_inode->i_ino);
  1553. rc = -EINVAL;
  1554. }
  1555. }
  1556. out:
  1557. if (page_virt) {
  1558. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1559. kmem_cache_free(ecryptfs_header_cache, page_virt);
  1560. }
  1561. return rc;
  1562. }
  1563. /**
  1564. * ecryptfs_encrypt_filename - encrypt filename
  1565. *
  1566. * CBC-encrypts the filename. We do not want to encrypt the same
  1567. * filename with the same key and IV, which may happen with hard
  1568. * links, so we prepend random bits to each filename.
  1569. *
  1570. * Returns zero on success; non-zero otherwise
  1571. */
  1572. static int
  1573. ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
  1574. struct ecryptfs_crypt_stat *crypt_stat,
  1575. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  1576. {
  1577. int rc = 0;
  1578. filename->encrypted_filename = NULL;
  1579. filename->encrypted_filename_size = 0;
  1580. if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
  1581. || (mount_crypt_stat && (mount_crypt_stat->flags
  1582. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
  1583. size_t packet_size;
  1584. size_t remaining_bytes;
  1585. rc = ecryptfs_write_tag_70_packet(
  1586. NULL, NULL,
  1587. &filename->encrypted_filename_size,
  1588. mount_crypt_stat, NULL,
  1589. filename->filename_size);
  1590. if (rc) {
  1591. printk(KERN_ERR "%s: Error attempting to get packet "
  1592. "size for tag 72; rc = [%d]\n", __func__,
  1593. rc);
  1594. filename->encrypted_filename_size = 0;
  1595. goto out;
  1596. }
  1597. filename->encrypted_filename =
  1598. kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
  1599. if (!filename->encrypted_filename) {
  1600. printk(KERN_ERR "%s: Out of memory whilst attempting "
  1601. "to kmalloc [%zd] bytes\n", __func__,
  1602. filename->encrypted_filename_size);
  1603. rc = -ENOMEM;
  1604. goto out;
  1605. }
  1606. remaining_bytes = filename->encrypted_filename_size;
  1607. rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
  1608. &remaining_bytes,
  1609. &packet_size,
  1610. mount_crypt_stat,
  1611. filename->filename,
  1612. filename->filename_size);
  1613. if (rc) {
  1614. printk(KERN_ERR "%s: Error attempting to generate "
  1615. "tag 70 packet; rc = [%d]\n", __func__,
  1616. rc);
  1617. kfree(filename->encrypted_filename);
  1618. filename->encrypted_filename = NULL;
  1619. filename->encrypted_filename_size = 0;
  1620. goto out;
  1621. }
  1622. filename->encrypted_filename_size = packet_size;
  1623. } else {
  1624. printk(KERN_ERR "%s: No support for requested filename "
  1625. "encryption method in this release\n", __func__);
  1626. rc = -EOPNOTSUPP;
  1627. goto out;
  1628. }
  1629. out:
  1630. return rc;
  1631. }
  1632. static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
  1633. const char *name, size_t name_size)
  1634. {
  1635. int rc = 0;
  1636. (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
  1637. if (!(*copied_name)) {
  1638. rc = -ENOMEM;
  1639. goto out;
  1640. }
  1641. memcpy((void *)(*copied_name), (void *)name, name_size);
  1642. (*copied_name)[(name_size)] = '\0'; /* Only for convenience
  1643. * in printing out the
  1644. * string in debug
  1645. * messages */
  1646. (*copied_name_size) = name_size;
  1647. out:
  1648. return rc;
  1649. }
  1650. /**
  1651. * ecryptfs_process_key_cipher - Perform key cipher initialization.
  1652. * @key_tfm: Crypto context for key material, set by this function
  1653. * @cipher_name: Name of the cipher
  1654. * @key_size: Size of the key in bytes
  1655. *
  1656. * Returns zero on success. Any crypto_tfm structs allocated here
  1657. * should be released by other functions, such as on a superblock put
  1658. * event, regardless of whether this function succeeds for fails.
  1659. */
  1660. static int
  1661. ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
  1662. char *cipher_name, size_t *key_size)
  1663. {
  1664. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1665. char *full_alg_name = NULL;
  1666. int rc;
  1667. *key_tfm = NULL;
  1668. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1669. rc = -EINVAL;
  1670. printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
  1671. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1672. goto out;
  1673. }
  1674. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1675. "ecb");
  1676. if (rc)
  1677. goto out;
  1678. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1679. if (IS_ERR(*key_tfm)) {
  1680. rc = PTR_ERR(*key_tfm);
  1681. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1682. "[%s]; rc = [%d]\n", full_alg_name, rc);
  1683. goto out;
  1684. }
  1685. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1686. if (*key_size == 0) {
  1687. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1688. *key_size = alg->max_keysize;
  1689. }
  1690. get_random_bytes(dummy_key, *key_size);
  1691. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1692. if (rc) {
  1693. printk(KERN_ERR "Error attempting to set key of size [%zd] for "
  1694. "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
  1695. rc);
  1696. rc = -EINVAL;
  1697. goto out;
  1698. }
  1699. out:
  1700. kfree(full_alg_name);
  1701. return rc;
  1702. }
  1703. struct kmem_cache *ecryptfs_key_tfm_cache;
  1704. static struct list_head key_tfm_list;
  1705. struct mutex key_tfm_list_mutex;
  1706. int __init ecryptfs_init_crypto(void)
  1707. {
  1708. mutex_init(&key_tfm_list_mutex);
  1709. INIT_LIST_HEAD(&key_tfm_list);
  1710. return 0;
  1711. }
  1712. /**
  1713. * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
  1714. *
  1715. * Called only at module unload time
  1716. */
  1717. int ecryptfs_destroy_crypto(void)
  1718. {
  1719. struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
  1720. mutex_lock(&key_tfm_list_mutex);
  1721. list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
  1722. key_tfm_list) {
  1723. list_del(&key_tfm->key_tfm_list);
  1724. if (key_tfm->key_tfm)
  1725. crypto_free_blkcipher(key_tfm->key_tfm);
  1726. kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
  1727. }
  1728. mutex_unlock(&key_tfm_list_mutex);
  1729. return 0;
  1730. }
  1731. int
  1732. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  1733. size_t key_size)
  1734. {
  1735. struct ecryptfs_key_tfm *tmp_tfm;
  1736. int rc = 0;
  1737. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1738. tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
  1739. if (key_tfm != NULL)
  1740. (*key_tfm) = tmp_tfm;
  1741. if (!tmp_tfm) {
  1742. rc = -ENOMEM;
  1743. printk(KERN_ERR "Error attempting to allocate from "
  1744. "ecryptfs_key_tfm_cache\n");
  1745. goto out;
  1746. }
  1747. mutex_init(&tmp_tfm->key_tfm_mutex);
  1748. strncpy(tmp_tfm->cipher_name, cipher_name,
  1749. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  1750. tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  1751. tmp_tfm->key_size = key_size;
  1752. rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
  1753. tmp_tfm->cipher_name,
  1754. &tmp_tfm->key_size);
  1755. if (rc) {
  1756. printk(KERN_ERR "Error attempting to initialize key TFM "
  1757. "cipher with name = [%s]; rc = [%d]\n",
  1758. tmp_tfm->cipher_name, rc);
  1759. kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
  1760. if (key_tfm != NULL)
  1761. (*key_tfm) = NULL;
  1762. goto out;
  1763. }
  1764. list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
  1765. out:
  1766. return rc;
  1767. }
  1768. /**
  1769. * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
  1770. * @cipher_name: the name of the cipher to search for
  1771. * @key_tfm: set to corresponding tfm if found
  1772. *
  1773. * Searches for cached key_tfm matching @cipher_name
  1774. * Must be called with &key_tfm_list_mutex held
  1775. * Returns 1 if found, with @key_tfm set
  1776. * Returns 0 if not found, with @key_tfm set to NULL
  1777. */
  1778. int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
  1779. {
  1780. struct ecryptfs_key_tfm *tmp_key_tfm;
  1781. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1782. list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
  1783. if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
  1784. if (key_tfm)
  1785. (*key_tfm) = tmp_key_tfm;
  1786. return 1;
  1787. }
  1788. }
  1789. if (key_tfm)
  1790. (*key_tfm) = NULL;
  1791. return 0;
  1792. }
  1793. /**
  1794. * ecryptfs_get_tfm_and_mutex_for_cipher_name
  1795. *
  1796. * @tfm: set to cached tfm found, or new tfm created
  1797. * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
  1798. * @cipher_name: the name of the cipher to search for and/or add
  1799. *
  1800. * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
  1801. * Searches for cached item first, and creates new if not found.
  1802. * Returns 0 on success, non-zero if adding new cipher failed
  1803. */
  1804. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
  1805. struct mutex **tfm_mutex,
  1806. char *cipher_name)
  1807. {
  1808. struct ecryptfs_key_tfm *key_tfm;
  1809. int rc = 0;
  1810. (*tfm) = NULL;
  1811. (*tfm_mutex) = NULL;
  1812. mutex_lock(&key_tfm_list_mutex);
  1813. if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
  1814. rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
  1815. if (rc) {
  1816. printk(KERN_ERR "Error adding new key_tfm to list; "
  1817. "rc = [%d]\n", rc);
  1818. goto out;
  1819. }
  1820. }
  1821. (*tfm) = key_tfm->key_tfm;
  1822. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1823. out:
  1824. mutex_unlock(&key_tfm_list_mutex);
  1825. return rc;
  1826. }
  1827. /* 64 characters forming a 6-bit target field */
  1828. static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
  1829. "EFGHIJKLMNOPQRST"
  1830. "UVWXYZabcdefghij"
  1831. "klmnopqrstuvwxyz");
  1832. /* We could either offset on every reverse map or just pad some 0x00's
  1833. * at the front here */
  1834. static const unsigned char filename_rev_map[256] = {
  1835. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
  1836. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
  1837. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
  1838. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
  1839. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
  1840. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
  1841. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
  1842. 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
  1843. 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
  1844. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
  1845. 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
  1846. 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
  1847. 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
  1848. 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
  1849. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
  1850. 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
  1851. };
  1852. /**
  1853. * ecryptfs_encode_for_filename
  1854. * @dst: Destination location for encoded filename
  1855. * @dst_size: Size of the encoded filename in bytes
  1856. * @src: Source location for the filename to encode
  1857. * @src_size: Size of the source in bytes
  1858. */
  1859. static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
  1860. unsigned char *src, size_t src_size)
  1861. {
  1862. size_t num_blocks;
  1863. size_t block_num = 0;
  1864. size_t dst_offset = 0;
  1865. unsigned char last_block[3];
  1866. if (src_size == 0) {
  1867. (*dst_size) = 0;
  1868. goto out;
  1869. }
  1870. num_blocks = (src_size / 3);
  1871. if ((src_size % 3) == 0) {
  1872. memcpy(last_block, (&src[src_size - 3]), 3);
  1873. } else {
  1874. num_blocks++;
  1875. last_block[2] = 0x00;
  1876. switch (src_size % 3) {
  1877. case 1:
  1878. last_block[0] = src[src_size - 1];
  1879. last_block[1] = 0x00;
  1880. break;
  1881. case 2:
  1882. last_block[0] = src[src_size - 2];
  1883. last_block[1] = src[src_size - 1];
  1884. }
  1885. }
  1886. (*dst_size) = (num_blocks * 4);
  1887. if (!dst)
  1888. goto out;
  1889. while (block_num < num_blocks) {
  1890. unsigned char *src_block;
  1891. unsigned char dst_block[4];
  1892. if (block_num == (num_blocks - 1))
  1893. src_block = last_block;
  1894. else
  1895. src_block = &src[block_num * 3];
  1896. dst_block[0] = ((src_block[0] >> 2) & 0x3F);
  1897. dst_block[1] = (((src_block[0] << 4) & 0x30)
  1898. | ((src_block[1] >> 4) & 0x0F));
  1899. dst_block[2] = (((src_block[1] << 2) & 0x3C)
  1900. | ((src_block[2] >> 6) & 0x03));
  1901. dst_block[3] = (src_block[2] & 0x3F);
  1902. dst[dst_offset++] = portable_filename_chars[dst_block[0]];
  1903. dst[dst_offset++] = portable_filename_chars[dst_block[1]];
  1904. dst[dst_offset++] = portable_filename_chars[dst_block[2]];
  1905. dst[dst_offset++] = portable_filename_chars[dst_block[3]];
  1906. block_num++;
  1907. }
  1908. out:
  1909. return;
  1910. }
  1911. static size_t ecryptfs_max_decoded_size(size_t encoded_size)
  1912. {
  1913. /* Not exact; conservatively long. Every block of 4
  1914. * encoded characters decodes into a block of 3
  1915. * decoded characters. This segment of code provides
  1916. * the caller with the maximum amount of allocated
  1917. * space that @dst will need to point to in a
  1918. * subsequent call. */
  1919. return ((encoded_size + 1) * 3) / 4;
  1920. }
  1921. /**
  1922. * ecryptfs_decode_from_filename
  1923. * @dst: If NULL, this function only sets @dst_size and returns. If
  1924. * non-NULL, this function decodes the encoded octets in @src
  1925. * into the memory that @dst points to.
  1926. * @dst_size: Set to the size of the decoded string.
  1927. * @src: The encoded set of octets to decode.
  1928. * @src_size: The size of the encoded set of octets to decode.
  1929. */
  1930. static void
  1931. ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
  1932. const unsigned char *src, size_t src_size)
  1933. {
  1934. u8 current_bit_offset = 0;
  1935. size_t src_byte_offset = 0;
  1936. size_t dst_byte_offset = 0;
  1937. if (dst == NULL) {
  1938. (*dst_size) = ecryptfs_max_decoded_size(src_size);
  1939. goto out;
  1940. }
  1941. while (src_byte_offset < src_size) {
  1942. unsigned char src_byte =
  1943. filename_rev_map[(int)src[src_byte_offset]];
  1944. switch (current_bit_offset) {
  1945. case 0:
  1946. dst[dst_byte_offset] = (src_byte << 2);
  1947. current_bit_offset = 6;
  1948. break;
  1949. case 6:
  1950. dst[dst_byte_offset++] |= (src_byte >> 4);
  1951. dst[dst_byte_offset] = ((src_byte & 0xF)
  1952. << 4);
  1953. current_bit_offset = 4;
  1954. break;
  1955. case 4:
  1956. dst[dst_byte_offset++] |= (src_byte >> 2);
  1957. dst[dst_byte_offset] = (src_byte << 6);
  1958. current_bit_offset = 2;
  1959. break;
  1960. case 2:
  1961. dst[dst_byte_offset++] |= (src_byte);
  1962. dst[dst_byte_offset] = 0;
  1963. current_bit_offset = 0;
  1964. break;
  1965. }
  1966. src_byte_offset++;
  1967. }
  1968. (*dst_size) = dst_byte_offset;
  1969. out:
  1970. return;
  1971. }
  1972. /**
  1973. * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
  1974. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1975. * @name: The plaintext name
  1976. * @length: The length of the plaintext
  1977. * @encoded_name: The encypted name
  1978. *
  1979. * Encrypts and encodes a filename into something that constitutes a
  1980. * valid filename for a filesystem, with printable characters.
  1981. *
  1982. * We assume that we have a properly initialized crypto context,
  1983. * pointed to by crypt_stat->tfm.
  1984. *
  1985. * Returns zero on success; non-zero on otherwise
  1986. */
  1987. int ecryptfs_encrypt_and_encode_filename(
  1988. char **encoded_name,
  1989. size_t *encoded_name_size,
  1990. struct ecryptfs_crypt_stat *crypt_stat,
  1991. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  1992. const char *name, size_t name_size)
  1993. {
  1994. size_t encoded_name_no_prefix_size;
  1995. int rc = 0;
  1996. (*encoded_name) = NULL;
  1997. (*encoded_name_size) = 0;
  1998. if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
  1999. || (mount_crypt_stat && (mount_crypt_stat->flags
  2000. & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
  2001. struct ecryptfs_filename *filename;
  2002. filename = kzalloc(sizeof(*filename), GFP_KERNEL);
  2003. if (!filename) {
  2004. printk(KERN_ERR "%s: Out of memory whilst attempting "
  2005. "to kzalloc [%zd] bytes\n", __func__,
  2006. sizeof(*filename));
  2007. rc = -ENOMEM;
  2008. goto out;
  2009. }
  2010. filename->filename = (char *)name;
  2011. filename->filename_size = name_size;
  2012. rc = ecryptfs_encrypt_filename(filename, crypt_stat,
  2013. mount_crypt_stat);
  2014. if (rc) {
  2015. printk(KERN_ERR "%s: Error attempting to encrypt "
  2016. "filename; rc = [%d]\n", __func__, rc);
  2017. kfree(filename);
  2018. goto out;
  2019. }
  2020. ecryptfs_encode_for_filename(
  2021. NULL, &encoded_name_no_prefix_size,
  2022. filename->encrypted_filename,
  2023. filename->encrypted_filename_size);
  2024. if ((crypt_stat && (crypt_stat->flags
  2025. & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
  2026. || (mount_crypt_stat
  2027. && (mount_crypt_stat->flags
  2028. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
  2029. (*encoded_name_size) =
  2030. (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
  2031. + encoded_name_no_prefix_size);
  2032. else
  2033. (*encoded_name_size) =
  2034. (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
  2035. + encoded_name_no_prefix_size);
  2036. (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
  2037. if (!(*encoded_name)) {
  2038. printk(KERN_ERR "%s: Out of memory whilst attempting "
  2039. "to kzalloc [%zd] bytes\n", __func__,
  2040. (*encoded_name_size));
  2041. rc = -ENOMEM;
  2042. kfree(filename->encrypted_filename);
  2043. kfree(filename);
  2044. goto out;
  2045. }
  2046. if ((crypt_stat && (crypt_stat->flags
  2047. & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
  2048. || (mount_crypt_stat
  2049. && (mount_crypt_stat->flags
  2050. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
  2051. memcpy((*encoded_name),
  2052. ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
  2053. ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
  2054. ecryptfs_encode_for_filename(
  2055. ((*encoded_name)
  2056. + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
  2057. &encoded_name_no_prefix_size,
  2058. filename->encrypted_filename,
  2059. filename->encrypted_filename_size);
  2060. (*encoded_name_size) =
  2061. (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
  2062. + encoded_name_no_prefix_size);
  2063. (*encoded_name)[(*encoded_name_size)] = '\0';
  2064. } else {
  2065. rc = -EOPNOTSUPP;
  2066. }
  2067. if (rc) {
  2068. printk(KERN_ERR "%s: Error attempting to encode "
  2069. "encrypted filename; rc = [%d]\n", __func__,
  2070. rc);
  2071. kfree((*encoded_name));
  2072. (*encoded_name) = NULL;
  2073. (*encoded_name_size) = 0;
  2074. }
  2075. kfree(filename->encrypted_filename);
  2076. kfree(filename);
  2077. } else {
  2078. rc = ecryptfs_copy_filename(encoded_name,
  2079. encoded_name_size,
  2080. name, name_size);
  2081. }
  2082. out:
  2083. return rc;
  2084. }
  2085. /**
  2086. * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
  2087. * @plaintext_name: The plaintext name
  2088. * @plaintext_name_size: The plaintext name size
  2089. * @ecryptfs_dir_dentry: eCryptfs directory dentry
  2090. * @name: The filename in cipher text
  2091. * @name_size: The cipher text name size
  2092. *
  2093. * Decrypts and decodes the filename.
  2094. *
  2095. * Returns zero on error; non-zero otherwise
  2096. */
  2097. int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
  2098. size_t *plaintext_name_size,
  2099. struct dentry *ecryptfs_dir_dentry,
  2100. const char *name, size_t name_size)
  2101. {
  2102. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  2103. &ecryptfs_superblock_to_private(
  2104. ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
  2105. char *decoded_name;
  2106. size_t decoded_name_size;
  2107. size_t packet_size;
  2108. int rc = 0;
  2109. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  2110. && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  2111. && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
  2112. && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
  2113. ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
  2114. const char *orig_name = name;
  2115. size_t orig_name_size = name_size;
  2116. name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
  2117. name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
  2118. ecryptfs_decode_from_filename(NULL, &decoded_name_size,
  2119. name, name_size);
  2120. decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
  2121. if (!decoded_name) {
  2122. printk(KERN_ERR "%s: Out of memory whilst attempting "
  2123. "to kmalloc [%zd] bytes\n", __func__,
  2124. decoded_name_size);
  2125. rc = -ENOMEM;
  2126. goto out;
  2127. }
  2128. ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
  2129. name, name_size);
  2130. rc = ecryptfs_parse_tag_70_packet(plaintext_name,
  2131. plaintext_name_size,
  2132. &packet_size,
  2133. mount_crypt_stat,
  2134. decoded_name,
  2135. decoded_name_size);
  2136. if (rc) {
  2137. printk(KERN_INFO "%s: Could not parse tag 70 packet "
  2138. "from filename; copying through filename "
  2139. "as-is\n", __func__);
  2140. rc = ecryptfs_copy_filename(plaintext_name,
  2141. plaintext_name_size,
  2142. orig_name, orig_name_size);
  2143. goto out_free;
  2144. }
  2145. } else {
  2146. rc = ecryptfs_copy_filename(plaintext_name,
  2147. plaintext_name_size,
  2148. name, name_size);
  2149. goto out;
  2150. }
  2151. out_free:
  2152. kfree(decoded_name);
  2153. out:
  2154. return rc;
  2155. }
  2156. #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
  2157. int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
  2158. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  2159. {
  2160. struct blkcipher_desc desc;
  2161. struct mutex *tfm_mutex;
  2162. size_t cipher_blocksize;
  2163. int rc;
  2164. if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
  2165. (*namelen) = lower_namelen;
  2166. return 0;
  2167. }
  2168. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  2169. mount_crypt_stat->global_default_fn_cipher_name);
  2170. if (unlikely(rc)) {
  2171. (*namelen) = 0;
  2172. return rc;
  2173. }
  2174. mutex_lock(tfm_mutex);
  2175. cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm);
  2176. mutex_unlock(tfm_mutex);
  2177. /* Return an exact amount for the common cases */
  2178. if (lower_namelen == NAME_MAX
  2179. && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
  2180. (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
  2181. return 0;
  2182. }
  2183. /* Return a safe estimate for the uncommon cases */
  2184. (*namelen) = lower_namelen;
  2185. (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
  2186. /* Since this is the max decoded size, subtract 1 "decoded block" len */
  2187. (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
  2188. (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
  2189. (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
  2190. /* Worst case is that the filename is padded nearly a full block size */
  2191. (*namelen) -= cipher_blocksize - 1;
  2192. if ((*namelen) < 0)
  2193. (*namelen) = 0;
  2194. return 0;
  2195. }