crypto.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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 <asm/unaligned.h>
  36. #include "ecryptfs_kernel.h"
  37. static int
  38. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  39. struct page *dst_page, int dst_offset,
  40. struct page *src_page, int src_offset, int size,
  41. unsigned char *iv);
  42. static int
  43. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  44. struct page *dst_page, int dst_offset,
  45. struct page *src_page, int src_offset, int size,
  46. unsigned char *iv);
  47. /**
  48. * ecryptfs_to_hex
  49. * @dst: Buffer to take hex character representation of contents of
  50. * src; must be at least of size (src_size * 2)
  51. * @src: Buffer to be converted to a hex string respresentation
  52. * @src_size: number of bytes to convert
  53. */
  54. void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
  55. {
  56. int x;
  57. for (x = 0; x < src_size; x++)
  58. sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
  59. }
  60. /**
  61. * ecryptfs_from_hex
  62. * @dst: Buffer to take the bytes from src hex; must be at least of
  63. * size (src_size / 2)
  64. * @src: Buffer to be converted from a hex string respresentation to raw value
  65. * @dst_size: size of dst buffer, or number of hex characters pairs to convert
  66. */
  67. void ecryptfs_from_hex(char *dst, char *src, int dst_size)
  68. {
  69. int x;
  70. char tmp[3] = { 0, };
  71. for (x = 0; x < dst_size; x++) {
  72. tmp[0] = src[x * 2];
  73. tmp[1] = src[x * 2 + 1];
  74. dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
  75. }
  76. }
  77. /**
  78. * ecryptfs_calculate_md5 - calculates the md5 of @src
  79. * @dst: Pointer to 16 bytes of allocated memory
  80. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  81. * @src: Data to be md5'd
  82. * @len: Length of @src
  83. *
  84. * Uses the allocated crypto context that crypt_stat references to
  85. * generate the MD5 sum of the contents of src.
  86. */
  87. static int ecryptfs_calculate_md5(char *dst,
  88. struct ecryptfs_crypt_stat *crypt_stat,
  89. char *src, int len)
  90. {
  91. struct scatterlist sg;
  92. struct hash_desc desc = {
  93. .tfm = crypt_stat->hash_tfm,
  94. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  95. };
  96. int rc = 0;
  97. mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
  98. sg_init_one(&sg, (u8 *)src, len);
  99. if (!desc.tfm) {
  100. desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
  101. CRYPTO_ALG_ASYNC);
  102. if (IS_ERR(desc.tfm)) {
  103. rc = PTR_ERR(desc.tfm);
  104. ecryptfs_printk(KERN_ERR, "Error attempting to "
  105. "allocate crypto context; rc = [%d]\n",
  106. rc);
  107. goto out;
  108. }
  109. crypt_stat->hash_tfm = desc.tfm;
  110. }
  111. rc = crypto_hash_init(&desc);
  112. if (rc) {
  113. printk(KERN_ERR
  114. "%s: Error initializing crypto hash; rc = [%d]\n",
  115. __func__, rc);
  116. goto out;
  117. }
  118. rc = crypto_hash_update(&desc, &sg, len);
  119. if (rc) {
  120. printk(KERN_ERR
  121. "%s: Error updating crypto hash; rc = [%d]\n",
  122. __func__, rc);
  123. goto out;
  124. }
  125. rc = crypto_hash_final(&desc, dst);
  126. if (rc) {
  127. printk(KERN_ERR
  128. "%s: Error finalizing crypto hash; rc = [%d]\n",
  129. __func__, rc);
  130. goto out;
  131. }
  132. out:
  133. mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
  134. return rc;
  135. }
  136. static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
  137. char *cipher_name,
  138. char *chaining_modifier)
  139. {
  140. int cipher_name_len = strlen(cipher_name);
  141. int chaining_modifier_len = strlen(chaining_modifier);
  142. int algified_name_len;
  143. int rc;
  144. algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
  145. (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
  146. if (!(*algified_name)) {
  147. rc = -ENOMEM;
  148. goto out;
  149. }
  150. snprintf((*algified_name), algified_name_len, "%s(%s)",
  151. chaining_modifier, cipher_name);
  152. rc = 0;
  153. out:
  154. return rc;
  155. }
  156. /**
  157. * ecryptfs_derive_iv
  158. * @iv: destination for the derived iv vale
  159. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  160. * @offset: Offset of the extent whose IV we are to derive
  161. *
  162. * Generate the initialization vector from the given root IV and page
  163. * offset.
  164. *
  165. * Returns zero on success; non-zero on error.
  166. */
  167. static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
  168. loff_t offset)
  169. {
  170. int rc = 0;
  171. char dst[MD5_DIGEST_SIZE];
  172. char src[ECRYPTFS_MAX_IV_BYTES + 16];
  173. if (unlikely(ecryptfs_verbosity > 0)) {
  174. ecryptfs_printk(KERN_DEBUG, "root iv:\n");
  175. ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
  176. }
  177. /* TODO: It is probably secure to just cast the least
  178. * significant bits of the root IV into an unsigned long and
  179. * add the offset to that rather than go through all this
  180. * hashing business. -Halcrow */
  181. memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
  182. memset((src + crypt_stat->iv_bytes), 0, 16);
  183. snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
  184. if (unlikely(ecryptfs_verbosity > 0)) {
  185. ecryptfs_printk(KERN_DEBUG, "source:\n");
  186. ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
  187. }
  188. rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
  189. (crypt_stat->iv_bytes + 16));
  190. if (rc) {
  191. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  192. "MD5 while generating IV for a page\n");
  193. goto out;
  194. }
  195. memcpy(iv, dst, crypt_stat->iv_bytes);
  196. if (unlikely(ecryptfs_verbosity > 0)) {
  197. ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
  198. ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
  199. }
  200. out:
  201. return rc;
  202. }
  203. /**
  204. * ecryptfs_init_crypt_stat
  205. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  206. *
  207. * Initialize the crypt_stat structure.
  208. */
  209. void
  210. ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  211. {
  212. memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  213. INIT_LIST_HEAD(&crypt_stat->keysig_list);
  214. mutex_init(&crypt_stat->keysig_list_mutex);
  215. mutex_init(&crypt_stat->cs_mutex);
  216. mutex_init(&crypt_stat->cs_tfm_mutex);
  217. mutex_init(&crypt_stat->cs_hash_tfm_mutex);
  218. crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
  219. }
  220. /**
  221. * ecryptfs_destroy_crypt_stat
  222. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  223. *
  224. * Releases all memory associated with a crypt_stat struct.
  225. */
  226. void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  227. {
  228. struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
  229. if (crypt_stat->tfm)
  230. crypto_free_blkcipher(crypt_stat->tfm);
  231. if (crypt_stat->hash_tfm)
  232. crypto_free_hash(crypt_stat->hash_tfm);
  233. mutex_lock(&crypt_stat->keysig_list_mutex);
  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. mutex_unlock(&crypt_stat->keysig_list_mutex);
  240. memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  241. }
  242. void ecryptfs_destroy_mount_crypt_stat(
  243. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  244. {
  245. struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
  246. if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
  247. return;
  248. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  249. list_for_each_entry_safe(auth_tok, auth_tok_tmp,
  250. &mount_crypt_stat->global_auth_tok_list,
  251. mount_crypt_stat_list) {
  252. list_del(&auth_tok->mount_crypt_stat_list);
  253. mount_crypt_stat->num_global_auth_toks--;
  254. if (auth_tok->global_auth_tok_key
  255. && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
  256. key_put(auth_tok->global_auth_tok_key);
  257. kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
  258. }
  259. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  260. memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
  261. }
  262. /**
  263. * virt_to_scatterlist
  264. * @addr: Virtual address
  265. * @size: Size of data; should be an even multiple of the block size
  266. * @sg: Pointer to scatterlist array; set to NULL to obtain only
  267. * the number of scatterlist structs required in array
  268. * @sg_size: Max array size
  269. *
  270. * Fills in a scatterlist array with page references for a passed
  271. * virtual address.
  272. *
  273. * Returns the number of scatterlist structs in array used
  274. */
  275. int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
  276. int sg_size)
  277. {
  278. int i = 0;
  279. struct page *pg;
  280. int offset;
  281. int remainder_of_page;
  282. sg_init_table(sg, sg_size);
  283. while (size > 0 && i < sg_size) {
  284. pg = virt_to_page(addr);
  285. offset = offset_in_page(addr);
  286. if (sg)
  287. sg_set_page(&sg[i], pg, 0, offset);
  288. remainder_of_page = PAGE_CACHE_SIZE - offset;
  289. if (size >= remainder_of_page) {
  290. if (sg)
  291. sg[i].length = remainder_of_page;
  292. addr += remainder_of_page;
  293. size -= remainder_of_page;
  294. } else {
  295. if (sg)
  296. sg[i].length = size;
  297. addr += size;
  298. size = 0;
  299. }
  300. i++;
  301. }
  302. if (size > 0)
  303. return -ENOMEM;
  304. return i;
  305. }
  306. /**
  307. * encrypt_scatterlist
  308. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  309. * @dest_sg: Destination of encrypted data
  310. * @src_sg: Data to be encrypted
  311. * @size: Length of data to be encrypted
  312. * @iv: iv to use during encryption
  313. *
  314. * Returns the number of bytes encrypted; negative value on error
  315. */
  316. static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  317. struct scatterlist *dest_sg,
  318. struct scatterlist *src_sg, int size,
  319. unsigned char *iv)
  320. {
  321. struct blkcipher_desc desc = {
  322. .tfm = crypt_stat->tfm,
  323. .info = iv,
  324. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  325. };
  326. int rc = 0;
  327. BUG_ON(!crypt_stat || !crypt_stat->tfm
  328. || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
  329. if (unlikely(ecryptfs_verbosity > 0)) {
  330. ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
  331. crypt_stat->key_size);
  332. ecryptfs_dump_hex(crypt_stat->key,
  333. crypt_stat->key_size);
  334. }
  335. /* Consider doing this once, when the file is opened */
  336. mutex_lock(&crypt_stat->cs_tfm_mutex);
  337. if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
  338. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  339. crypt_stat->key_size);
  340. crypt_stat->flags |= ECRYPTFS_KEY_SET;
  341. }
  342. if (rc) {
  343. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  344. rc);
  345. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  346. rc = -EINVAL;
  347. goto out;
  348. }
  349. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
  350. crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
  351. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  352. out:
  353. return rc;
  354. }
  355. /**
  356. * ecryptfs_lower_offset_for_extent
  357. *
  358. * Convert an eCryptfs page index into a lower byte offset
  359. */
  360. static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
  361. struct ecryptfs_crypt_stat *crypt_stat)
  362. {
  363. (*offset) = (crypt_stat->num_header_bytes_at_front
  364. + (crypt_stat->extent_size * extent_num));
  365. }
  366. /**
  367. * ecryptfs_encrypt_extent
  368. * @enc_extent_page: Allocated page into which to encrypt the data in
  369. * @page
  370. * @crypt_stat: crypt_stat containing cryptographic context for the
  371. * encryption operation
  372. * @page: Page containing plaintext data extent to encrypt
  373. * @extent_offset: Page extent offset for use in generating IV
  374. *
  375. * Encrypts one extent of data.
  376. *
  377. * Return zero on success; non-zero otherwise
  378. */
  379. static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
  380. struct ecryptfs_crypt_stat *crypt_stat,
  381. struct page *page,
  382. unsigned long extent_offset)
  383. {
  384. loff_t extent_base;
  385. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  386. int rc;
  387. extent_base = (((loff_t)page->index)
  388. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  389. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  390. (extent_base + extent_offset));
  391. if (rc) {
  392. ecryptfs_printk(KERN_ERR, "Error attempting to "
  393. "derive IV for extent [0x%.16x]; "
  394. "rc = [%d]\n", (extent_base + extent_offset),
  395. rc);
  396. goto out;
  397. }
  398. if (unlikely(ecryptfs_verbosity > 0)) {
  399. ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
  400. "with iv:\n");
  401. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  402. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  403. "encryption:\n");
  404. ecryptfs_dump_hex((char *)
  405. (page_address(page)
  406. + (extent_offset * crypt_stat->extent_size)),
  407. 8);
  408. }
  409. rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
  410. page, (extent_offset
  411. * crypt_stat->extent_size),
  412. crypt_stat->extent_size, extent_iv);
  413. if (rc < 0) {
  414. printk(KERN_ERR "%s: Error attempting to encrypt page with "
  415. "page->index = [%ld], extent_offset = [%ld]; "
  416. "rc = [%d]\n", __func__, page->index, extent_offset,
  417. rc);
  418. goto out;
  419. }
  420. rc = 0;
  421. if (unlikely(ecryptfs_verbosity > 0)) {
  422. ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
  423. "rc = [%d]\n", (extent_base + extent_offset),
  424. rc);
  425. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  426. "encryption:\n");
  427. ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
  428. }
  429. out:
  430. return rc;
  431. }
  432. /**
  433. * ecryptfs_encrypt_page
  434. * @page: Page mapped from the eCryptfs inode for the file; contains
  435. * decrypted content that needs to be encrypted (to a temporary
  436. * page; not in place) and written out to the lower file
  437. *
  438. * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
  439. * that eCryptfs pages may straddle the lower pages -- for instance,
  440. * if the file was created on a machine with an 8K page size
  441. * (resulting in an 8K header), and then the file is copied onto a
  442. * host with a 32K page size, then when reading page 0 of the eCryptfs
  443. * file, 24K of page 0 of the lower file will be read and decrypted,
  444. * and then 8K of page 1 of the lower file will be read and decrypted.
  445. *
  446. * Returns zero on success; negative on error
  447. */
  448. int ecryptfs_encrypt_page(struct page *page)
  449. {
  450. struct inode *ecryptfs_inode;
  451. struct ecryptfs_crypt_stat *crypt_stat;
  452. char *enc_extent_virt;
  453. struct page *enc_extent_page = NULL;
  454. loff_t extent_offset;
  455. int rc = 0;
  456. ecryptfs_inode = page->mapping->host;
  457. crypt_stat =
  458. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  459. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  460. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
  461. 0, PAGE_CACHE_SIZE);
  462. if (rc)
  463. printk(KERN_ERR "%s: Error attempting to copy "
  464. "page at index [%ld]\n", __func__,
  465. page->index);
  466. goto out;
  467. }
  468. enc_extent_page = alloc_page(GFP_USER);
  469. if (!enc_extent_page) {
  470. rc = -ENOMEM;
  471. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  472. "encrypted extent\n");
  473. goto out;
  474. }
  475. enc_extent_virt = kmap(enc_extent_page);
  476. for (extent_offset = 0;
  477. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  478. extent_offset++) {
  479. loff_t offset;
  480. rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
  481. extent_offset);
  482. if (rc) {
  483. printk(KERN_ERR "%s: Error encrypting extent; "
  484. "rc = [%d]\n", __func__, rc);
  485. goto out;
  486. }
  487. ecryptfs_lower_offset_for_extent(
  488. &offset, ((((loff_t)page->index)
  489. * (PAGE_CACHE_SIZE
  490. / crypt_stat->extent_size))
  491. + extent_offset), crypt_stat);
  492. rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
  493. offset, crypt_stat->extent_size);
  494. if (rc) {
  495. ecryptfs_printk(KERN_ERR, "Error attempting "
  496. "to write lower page; rc = [%d]"
  497. "\n", rc);
  498. goto out;
  499. }
  500. }
  501. out:
  502. if (enc_extent_page) {
  503. kunmap(enc_extent_page);
  504. __free_page(enc_extent_page);
  505. }
  506. return rc;
  507. }
  508. static int ecryptfs_decrypt_extent(struct page *page,
  509. struct ecryptfs_crypt_stat *crypt_stat,
  510. struct page *enc_extent_page,
  511. unsigned long extent_offset)
  512. {
  513. loff_t extent_base;
  514. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  515. int rc;
  516. extent_base = (((loff_t)page->index)
  517. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  518. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  519. (extent_base + extent_offset));
  520. if (rc) {
  521. ecryptfs_printk(KERN_ERR, "Error attempting to "
  522. "derive IV for extent [0x%.16x]; "
  523. "rc = [%d]\n", (extent_base + extent_offset),
  524. rc);
  525. goto out;
  526. }
  527. if (unlikely(ecryptfs_verbosity > 0)) {
  528. ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
  529. "with iv:\n");
  530. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  531. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  532. "decryption:\n");
  533. ecryptfs_dump_hex((char *)
  534. (page_address(enc_extent_page)
  535. + (extent_offset * crypt_stat->extent_size)),
  536. 8);
  537. }
  538. rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
  539. (extent_offset
  540. * crypt_stat->extent_size),
  541. enc_extent_page, 0,
  542. crypt_stat->extent_size, extent_iv);
  543. if (rc < 0) {
  544. printk(KERN_ERR "%s: Error attempting to decrypt to page with "
  545. "page->index = [%ld], extent_offset = [%ld]; "
  546. "rc = [%d]\n", __func__, page->index, extent_offset,
  547. rc);
  548. goto out;
  549. }
  550. rc = 0;
  551. if (unlikely(ecryptfs_verbosity > 0)) {
  552. ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
  553. "rc = [%d]\n", (extent_base + extent_offset),
  554. rc);
  555. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  556. "decryption:\n");
  557. ecryptfs_dump_hex((char *)(page_address(page)
  558. + (extent_offset
  559. * crypt_stat->extent_size)), 8);
  560. }
  561. out:
  562. return rc;
  563. }
  564. /**
  565. * ecryptfs_decrypt_page
  566. * @page: Page mapped from the eCryptfs inode for the file; data read
  567. * and decrypted from the lower file will be written into this
  568. * page
  569. *
  570. * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
  571. * that eCryptfs pages may straddle the lower pages -- for instance,
  572. * if the file was created on a machine with an 8K page size
  573. * (resulting in an 8K header), and then the file is copied onto a
  574. * host with a 32K page size, then when reading page 0 of the eCryptfs
  575. * file, 24K of page 0 of the lower file will be read and decrypted,
  576. * and then 8K of page 1 of the lower file will be read and decrypted.
  577. *
  578. * Returns zero on success; negative on error
  579. */
  580. int ecryptfs_decrypt_page(struct page *page)
  581. {
  582. struct inode *ecryptfs_inode;
  583. struct ecryptfs_crypt_stat *crypt_stat;
  584. char *enc_extent_virt;
  585. struct page *enc_extent_page = NULL;
  586. unsigned long extent_offset;
  587. int rc = 0;
  588. ecryptfs_inode = page->mapping->host;
  589. crypt_stat =
  590. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  591. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  592. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  593. PAGE_CACHE_SIZE,
  594. ecryptfs_inode);
  595. if (rc)
  596. printk(KERN_ERR "%s: Error attempting to copy "
  597. "page at index [%ld]\n", __func__,
  598. page->index);
  599. goto out;
  600. }
  601. enc_extent_page = alloc_page(GFP_USER);
  602. if (!enc_extent_page) {
  603. rc = -ENOMEM;
  604. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  605. "encrypted extent\n");
  606. goto out;
  607. }
  608. enc_extent_virt = kmap(enc_extent_page);
  609. for (extent_offset = 0;
  610. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  611. extent_offset++) {
  612. loff_t offset;
  613. ecryptfs_lower_offset_for_extent(
  614. &offset, ((page->index * (PAGE_CACHE_SIZE
  615. / crypt_stat->extent_size))
  616. + extent_offset), crypt_stat);
  617. rc = ecryptfs_read_lower(enc_extent_virt, offset,
  618. crypt_stat->extent_size,
  619. ecryptfs_inode);
  620. if (rc) {
  621. ecryptfs_printk(KERN_ERR, "Error attempting "
  622. "to read lower page; rc = [%d]"
  623. "\n", rc);
  624. goto out;
  625. }
  626. rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
  627. extent_offset);
  628. if (rc) {
  629. printk(KERN_ERR "%s: Error encrypting extent; "
  630. "rc = [%d]\n", __func__, rc);
  631. goto out;
  632. }
  633. }
  634. out:
  635. if (enc_extent_page) {
  636. kunmap(enc_extent_page);
  637. __free_page(enc_extent_page);
  638. }
  639. return rc;
  640. }
  641. /**
  642. * decrypt_scatterlist
  643. * @crypt_stat: Cryptographic context
  644. * @dest_sg: The destination scatterlist to decrypt into
  645. * @src_sg: The source scatterlist to decrypt from
  646. * @size: The number of bytes to decrypt
  647. * @iv: The initialization vector to use for the decryption
  648. *
  649. * Returns the number of bytes decrypted; negative value on error
  650. */
  651. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  652. struct scatterlist *dest_sg,
  653. struct scatterlist *src_sg, int size,
  654. unsigned char *iv)
  655. {
  656. struct blkcipher_desc desc = {
  657. .tfm = crypt_stat->tfm,
  658. .info = iv,
  659. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  660. };
  661. int rc = 0;
  662. /* Consider doing this once, when the file is opened */
  663. mutex_lock(&crypt_stat->cs_tfm_mutex);
  664. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  665. crypt_stat->key_size);
  666. if (rc) {
  667. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  668. rc);
  669. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  670. rc = -EINVAL;
  671. goto out;
  672. }
  673. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  674. rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
  675. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  676. if (rc) {
  677. ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
  678. rc);
  679. goto out;
  680. }
  681. rc = size;
  682. out:
  683. return rc;
  684. }
  685. /**
  686. * ecryptfs_encrypt_page_offset
  687. * @crypt_stat: The cryptographic context
  688. * @dst_page: The page to encrypt into
  689. * @dst_offset: The offset in the page to encrypt into
  690. * @src_page: The page to encrypt from
  691. * @src_offset: The offset in the page to encrypt from
  692. * @size: The number of bytes to encrypt
  693. * @iv: The initialization vector to use for the encryption
  694. *
  695. * Returns the number of bytes encrypted
  696. */
  697. static int
  698. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  699. struct page *dst_page, int dst_offset,
  700. struct page *src_page, int src_offset, int size,
  701. unsigned char *iv)
  702. {
  703. struct scatterlist src_sg, dst_sg;
  704. sg_init_table(&src_sg, 1);
  705. sg_init_table(&dst_sg, 1);
  706. sg_set_page(&src_sg, src_page, size, src_offset);
  707. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  708. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  709. }
  710. /**
  711. * ecryptfs_decrypt_page_offset
  712. * @crypt_stat: The cryptographic context
  713. * @dst_page: The page to decrypt into
  714. * @dst_offset: The offset in the page to decrypt into
  715. * @src_page: The page to decrypt from
  716. * @src_offset: The offset in the page to decrypt from
  717. * @size: The number of bytes to decrypt
  718. * @iv: The initialization vector to use for the decryption
  719. *
  720. * Returns the number of bytes decrypted
  721. */
  722. static int
  723. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  724. struct page *dst_page, int dst_offset,
  725. struct page *src_page, int src_offset, int size,
  726. unsigned char *iv)
  727. {
  728. struct scatterlist src_sg, dst_sg;
  729. sg_init_table(&src_sg, 1);
  730. sg_set_page(&src_sg, src_page, size, src_offset);
  731. sg_init_table(&dst_sg, 1);
  732. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  733. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  734. }
  735. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  736. /**
  737. * ecryptfs_init_crypt_ctx
  738. * @crypt_stat: Uninitilized crypt stats structure
  739. *
  740. * Initialize the crypto context.
  741. *
  742. * TODO: Performance: Keep a cache of initialized cipher contexts;
  743. * only init if needed
  744. */
  745. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  746. {
  747. char *full_alg_name;
  748. int rc = -EINVAL;
  749. if (!crypt_stat->cipher) {
  750. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  751. goto out;
  752. }
  753. ecryptfs_printk(KERN_DEBUG,
  754. "Initializing cipher [%s]; strlen = [%d]; "
  755. "key_size_bits = [%d]\n",
  756. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  757. crypt_stat->key_size << 3);
  758. if (crypt_stat->tfm) {
  759. rc = 0;
  760. goto out;
  761. }
  762. mutex_lock(&crypt_stat->cs_tfm_mutex);
  763. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  764. crypt_stat->cipher, "cbc");
  765. if (rc)
  766. goto out_unlock;
  767. crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  768. CRYPTO_ALG_ASYNC);
  769. kfree(full_alg_name);
  770. if (IS_ERR(crypt_stat->tfm)) {
  771. rc = PTR_ERR(crypt_stat->tfm);
  772. ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
  773. "Error initializing cipher [%s]\n",
  774. crypt_stat->cipher);
  775. goto out_unlock;
  776. }
  777. crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  778. rc = 0;
  779. out_unlock:
  780. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  781. out:
  782. return rc;
  783. }
  784. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  785. {
  786. int extent_size_tmp;
  787. crypt_stat->extent_mask = 0xFFFFFFFF;
  788. crypt_stat->extent_shift = 0;
  789. if (crypt_stat->extent_size == 0)
  790. return;
  791. extent_size_tmp = crypt_stat->extent_size;
  792. while ((extent_size_tmp & 0x01) == 0) {
  793. extent_size_tmp >>= 1;
  794. crypt_stat->extent_mask <<= 1;
  795. crypt_stat->extent_shift++;
  796. }
  797. }
  798. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  799. {
  800. /* Default values; may be overwritten as we are parsing the
  801. * packets. */
  802. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  803. set_extent_mask_and_shift(crypt_stat);
  804. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  805. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  806. crypt_stat->num_header_bytes_at_front = 0;
  807. else {
  808. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
  809. crypt_stat->num_header_bytes_at_front =
  810. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  811. else
  812. crypt_stat->num_header_bytes_at_front = PAGE_CACHE_SIZE;
  813. }
  814. }
  815. /**
  816. * ecryptfs_compute_root_iv
  817. * @crypt_stats
  818. *
  819. * On error, sets the root IV to all 0's.
  820. */
  821. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
  822. {
  823. int rc = 0;
  824. char dst[MD5_DIGEST_SIZE];
  825. BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
  826. BUG_ON(crypt_stat->iv_bytes <= 0);
  827. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  828. rc = -EINVAL;
  829. ecryptfs_printk(KERN_WARNING, "Session key not valid; "
  830. "cannot generate root IV\n");
  831. goto out;
  832. }
  833. rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
  834. crypt_stat->key_size);
  835. if (rc) {
  836. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  837. "MD5 while generating root IV\n");
  838. goto out;
  839. }
  840. memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
  841. out:
  842. if (rc) {
  843. memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
  844. crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
  845. }
  846. return rc;
  847. }
  848. static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
  849. {
  850. get_random_bytes(crypt_stat->key, crypt_stat->key_size);
  851. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  852. ecryptfs_compute_root_iv(crypt_stat);
  853. if (unlikely(ecryptfs_verbosity > 0)) {
  854. ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
  855. ecryptfs_dump_hex(crypt_stat->key,
  856. crypt_stat->key_size);
  857. }
  858. }
  859. /**
  860. * ecryptfs_copy_mount_wide_flags_to_inode_flags
  861. * @crypt_stat: The inode's cryptographic context
  862. * @mount_crypt_stat: The mount point's cryptographic context
  863. *
  864. * This function propagates the mount-wide flags to individual inode
  865. * flags.
  866. */
  867. static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
  868. struct ecryptfs_crypt_stat *crypt_stat,
  869. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  870. {
  871. if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
  872. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  873. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  874. crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
  875. }
  876. static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
  877. struct ecryptfs_crypt_stat *crypt_stat,
  878. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  879. {
  880. struct ecryptfs_global_auth_tok *global_auth_tok;
  881. int rc = 0;
  882. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  883. list_for_each_entry(global_auth_tok,
  884. &mount_crypt_stat->global_auth_tok_list,
  885. mount_crypt_stat_list) {
  886. rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
  887. if (rc) {
  888. printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
  889. mutex_unlock(
  890. &mount_crypt_stat->global_auth_tok_list_mutex);
  891. goto out;
  892. }
  893. }
  894. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  895. out:
  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_dentry: The eCryptfs dentry
  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 dentry *ecryptfs_dentry)
  938. {
  939. struct ecryptfs_crypt_stat *crypt_stat =
  940. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  941. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  942. &ecryptfs_superblock_to_private(
  943. ecryptfs_dentry->d_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. * contains_ecryptfs_marker - check for the ecryptfs marker
  976. * @data: The data block in which to check
  977. *
  978. * Returns one if marker found; zero if not found
  979. */
  980. static int contains_ecryptfs_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 1;
  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 0;
  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. };
  1004. /**
  1005. * ecryptfs_process_flags
  1006. * @crypt_stat: The cryptographic context
  1007. * @page_virt: Source data to be parsed
  1008. * @bytes_read: Updated with the number of bytes read
  1009. *
  1010. * Returns zero on success; non-zero if the flag set is invalid
  1011. */
  1012. static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
  1013. char *page_virt, int *bytes_read)
  1014. {
  1015. int rc = 0;
  1016. int i;
  1017. u32 flags;
  1018. flags = get_unaligned_be32(page_virt);
  1019. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1020. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1021. if (flags & ecryptfs_flag_map[i].file_flag) {
  1022. crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
  1023. } else
  1024. crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
  1025. /* Version is in top 8 bits of the 32-bit flag vector */
  1026. crypt_stat->file_version = ((flags >> 24) & 0xFF);
  1027. (*bytes_read) = 4;
  1028. return rc;
  1029. }
  1030. /**
  1031. * write_ecryptfs_marker
  1032. * @page_virt: The pointer to in a page to begin writing the marker
  1033. * @written: Number of bytes written
  1034. *
  1035. * Marker = 0x3c81b7f5
  1036. */
  1037. static void write_ecryptfs_marker(char *page_virt, size_t *written)
  1038. {
  1039. u32 m_1, m_2;
  1040. get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1041. m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
  1042. put_unaligned_be32(m_1, page_virt);
  1043. page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
  1044. put_unaligned_be32(m_2, page_virt);
  1045. (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1046. }
  1047. static void
  1048. write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
  1049. size_t *written)
  1050. {
  1051. u32 flags = 0;
  1052. int i;
  1053. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1054. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1055. if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
  1056. flags |= ecryptfs_flag_map[i].file_flag;
  1057. /* Version is in top 8 bits of the 32-bit flag vector */
  1058. flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
  1059. put_unaligned_be32(flags, page_virt);
  1060. (*written) = 4;
  1061. }
  1062. struct ecryptfs_cipher_code_str_map_elem {
  1063. char cipher_str[16];
  1064. u8 cipher_code;
  1065. };
  1066. /* Add support for additional ciphers by adding elements here. The
  1067. * cipher_code is whatever OpenPGP applicatoins use to identify the
  1068. * ciphers. List in order of probability. */
  1069. static struct ecryptfs_cipher_code_str_map_elem
  1070. ecryptfs_cipher_code_str_map[] = {
  1071. {"aes",RFC2440_CIPHER_AES_128 },
  1072. {"blowfish", RFC2440_CIPHER_BLOWFISH},
  1073. {"des3_ede", RFC2440_CIPHER_DES3_EDE},
  1074. {"cast5", RFC2440_CIPHER_CAST_5},
  1075. {"twofish", RFC2440_CIPHER_TWOFISH},
  1076. {"cast6", RFC2440_CIPHER_CAST_6},
  1077. {"aes", RFC2440_CIPHER_AES_192},
  1078. {"aes", RFC2440_CIPHER_AES_256}
  1079. };
  1080. /**
  1081. * ecryptfs_code_for_cipher_string
  1082. * @crypt_stat: The cryptographic context
  1083. *
  1084. * Returns zero on no match, or the cipher code on match
  1085. */
  1086. u8 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
  1087. {
  1088. int i;
  1089. u8 code = 0;
  1090. struct ecryptfs_cipher_code_str_map_elem *map =
  1091. ecryptfs_cipher_code_str_map;
  1092. if (strcmp(crypt_stat->cipher, "aes") == 0) {
  1093. switch (crypt_stat->key_size) {
  1094. case 16:
  1095. code = RFC2440_CIPHER_AES_128;
  1096. break;
  1097. case 24:
  1098. code = RFC2440_CIPHER_AES_192;
  1099. break;
  1100. case 32:
  1101. code = RFC2440_CIPHER_AES_256;
  1102. }
  1103. } else {
  1104. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1105. if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
  1106. code = map[i].cipher_code;
  1107. break;
  1108. }
  1109. }
  1110. return code;
  1111. }
  1112. /**
  1113. * ecryptfs_cipher_code_to_string
  1114. * @str: Destination to write out the cipher name
  1115. * @cipher_code: The code to convert to cipher name string
  1116. *
  1117. * Returns zero on success
  1118. */
  1119. int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
  1120. {
  1121. int rc = 0;
  1122. int i;
  1123. str[0] = '\0';
  1124. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1125. if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
  1126. strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
  1127. if (str[0] == '\0') {
  1128. ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
  1129. "[%d]\n", cipher_code);
  1130. rc = -EINVAL;
  1131. }
  1132. return rc;
  1133. }
  1134. int ecryptfs_read_and_validate_header_region(char *data,
  1135. struct inode *ecryptfs_inode)
  1136. {
  1137. struct ecryptfs_crypt_stat *crypt_stat =
  1138. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  1139. int rc;
  1140. rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
  1141. ecryptfs_inode);
  1142. if (rc) {
  1143. printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
  1144. __func__, rc);
  1145. goto out;
  1146. }
  1147. if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
  1148. rc = -EINVAL;
  1149. ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
  1150. }
  1151. out:
  1152. return rc;
  1153. }
  1154. void
  1155. ecryptfs_write_header_metadata(char *virt,
  1156. struct ecryptfs_crypt_stat *crypt_stat,
  1157. size_t *written)
  1158. {
  1159. u32 header_extent_size;
  1160. u16 num_header_extents_at_front;
  1161. header_extent_size = (u32)crypt_stat->extent_size;
  1162. num_header_extents_at_front =
  1163. (u16)(crypt_stat->num_header_bytes_at_front
  1164. / crypt_stat->extent_size);
  1165. put_unaligned_be32(header_extent_size, virt);
  1166. virt += 4;
  1167. put_unaligned_be16(num_header_extents_at_front, virt);
  1168. (*written) = 6;
  1169. }
  1170. struct kmem_cache *ecryptfs_header_cache_1;
  1171. struct kmem_cache *ecryptfs_header_cache_2;
  1172. /**
  1173. * ecryptfs_write_headers_virt
  1174. * @page_virt: The virtual address to write the headers to
  1175. * @max: The size of memory allocated at page_virt
  1176. * @size: Set to the number of bytes written by this function
  1177. * @crypt_stat: The cryptographic context
  1178. * @ecryptfs_dentry: The eCryptfs dentry
  1179. *
  1180. * Format version: 1
  1181. *
  1182. * Header Extent:
  1183. * Octets 0-7: Unencrypted file size (big-endian)
  1184. * Octets 8-15: eCryptfs special marker
  1185. * Octets 16-19: Flags
  1186. * Octet 16: File format version number (between 0 and 255)
  1187. * Octets 17-18: Reserved
  1188. * Octet 19: Bit 1 (lsb): Reserved
  1189. * Bit 2: Encrypted?
  1190. * Bits 3-8: Reserved
  1191. * Octets 20-23: Header extent size (big-endian)
  1192. * Octets 24-25: Number of header extents at front of file
  1193. * (big-endian)
  1194. * Octet 26: Begin RFC 2440 authentication token packet set
  1195. * Data Extent 0:
  1196. * Lower data (CBC encrypted)
  1197. * Data Extent 1:
  1198. * Lower data (CBC encrypted)
  1199. * ...
  1200. *
  1201. * Returns zero on success
  1202. */
  1203. static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
  1204. size_t *size,
  1205. struct ecryptfs_crypt_stat *crypt_stat,
  1206. struct dentry *ecryptfs_dentry)
  1207. {
  1208. int rc;
  1209. size_t written;
  1210. size_t offset;
  1211. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1212. write_ecryptfs_marker((page_virt + offset), &written);
  1213. offset += written;
  1214. write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
  1215. offset += written;
  1216. ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
  1217. &written);
  1218. offset += written;
  1219. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1220. ecryptfs_dentry, &written,
  1221. max - offset);
  1222. if (rc)
  1223. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1224. "set; rc = [%d]\n", rc);
  1225. if (size) {
  1226. offset += written;
  1227. *size = offset;
  1228. }
  1229. return rc;
  1230. }
  1231. static int
  1232. ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
  1233. struct dentry *ecryptfs_dentry,
  1234. char *virt)
  1235. {
  1236. int rc;
  1237. rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
  1238. 0, crypt_stat->num_header_bytes_at_front);
  1239. if (rc)
  1240. printk(KERN_ERR "%s: Error attempting to write header "
  1241. "information to lower file; rc = [%d]\n", __func__,
  1242. rc);
  1243. return rc;
  1244. }
  1245. static int
  1246. ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
  1247. struct ecryptfs_crypt_stat *crypt_stat,
  1248. char *page_virt, size_t size)
  1249. {
  1250. int rc;
  1251. rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
  1252. size, 0);
  1253. return rc;
  1254. }
  1255. /**
  1256. * ecryptfs_write_metadata
  1257. * @ecryptfs_dentry: The eCryptfs dentry
  1258. *
  1259. * Write the file headers out. This will likely involve a userspace
  1260. * callout, in which the session key is encrypted with one or more
  1261. * public keys and/or the passphrase necessary to do the encryption is
  1262. * retrieved via a prompt. Exactly what happens at this point should
  1263. * be policy-dependent.
  1264. *
  1265. * Returns zero on success; non-zero on error
  1266. */
  1267. int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
  1268. {
  1269. struct ecryptfs_crypt_stat *crypt_stat =
  1270. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  1271. char *virt;
  1272. size_t size = 0;
  1273. int rc = 0;
  1274. if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  1275. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  1276. printk(KERN_ERR "Key is invalid; bailing out\n");
  1277. rc = -EINVAL;
  1278. goto out;
  1279. }
  1280. } else {
  1281. printk(KERN_WARNING "%s: Encrypted flag not set\n",
  1282. __func__);
  1283. rc = -EINVAL;
  1284. goto out;
  1285. }
  1286. /* Released in this function */
  1287. virt = (char *)get_zeroed_page(GFP_KERNEL);
  1288. if (!virt) {
  1289. printk(KERN_ERR "%s: Out of memory\n", __func__);
  1290. rc = -ENOMEM;
  1291. goto out;
  1292. }
  1293. rc = ecryptfs_write_headers_virt(virt, PAGE_CACHE_SIZE, &size,
  1294. crypt_stat, ecryptfs_dentry);
  1295. if (unlikely(rc)) {
  1296. printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
  1297. __func__, rc);
  1298. goto out_free;
  1299. }
  1300. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1301. rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
  1302. crypt_stat, virt, size);
  1303. else
  1304. rc = ecryptfs_write_metadata_to_contents(crypt_stat,
  1305. ecryptfs_dentry, virt);
  1306. if (rc) {
  1307. printk(KERN_ERR "%s: Error writing metadata out to lower file; "
  1308. "rc = [%d]\n", __func__, rc);
  1309. goto out_free;
  1310. }
  1311. out_free:
  1312. free_page((unsigned long)virt);
  1313. out:
  1314. return rc;
  1315. }
  1316. #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
  1317. #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
  1318. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1319. char *virt, int *bytes_read,
  1320. int validate_header_size)
  1321. {
  1322. int rc = 0;
  1323. u32 header_extent_size;
  1324. u16 num_header_extents_at_front;
  1325. header_extent_size = get_unaligned_be32(virt);
  1326. virt += sizeof(__be32);
  1327. num_header_extents_at_front = get_unaligned_be16(virt);
  1328. crypt_stat->num_header_bytes_at_front =
  1329. (((size_t)num_header_extents_at_front
  1330. * (size_t)header_extent_size));
  1331. (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
  1332. if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
  1333. && (crypt_stat->num_header_bytes_at_front
  1334. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
  1335. rc = -EINVAL;
  1336. printk(KERN_WARNING "Invalid header size: [%zd]\n",
  1337. crypt_stat->num_header_bytes_at_front);
  1338. }
  1339. return rc;
  1340. }
  1341. /**
  1342. * set_default_header_data
  1343. * @crypt_stat: The cryptographic context
  1344. *
  1345. * For version 0 file format; this function is only for backwards
  1346. * compatibility for files created with the prior versions of
  1347. * eCryptfs.
  1348. */
  1349. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1350. {
  1351. crypt_stat->num_header_bytes_at_front =
  1352. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  1353. }
  1354. /**
  1355. * ecryptfs_read_headers_virt
  1356. * @page_virt: The virtual address into which to read the headers
  1357. * @crypt_stat: The cryptographic context
  1358. * @ecryptfs_dentry: The eCryptfs dentry
  1359. * @validate_header_size: Whether to validate the header size while reading
  1360. *
  1361. * Read/parse the header data. The header format is detailed in the
  1362. * comment block for the ecryptfs_write_headers_virt() function.
  1363. *
  1364. * Returns zero on success
  1365. */
  1366. static int ecryptfs_read_headers_virt(char *page_virt,
  1367. struct ecryptfs_crypt_stat *crypt_stat,
  1368. struct dentry *ecryptfs_dentry,
  1369. int validate_header_size)
  1370. {
  1371. int rc = 0;
  1372. int offset;
  1373. int bytes_read;
  1374. ecryptfs_set_default_sizes(crypt_stat);
  1375. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1376. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1377. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1378. rc = contains_ecryptfs_marker(page_virt + offset);
  1379. if (rc == 0) {
  1380. rc = -EINVAL;
  1381. goto out;
  1382. }
  1383. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1384. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1385. &bytes_read);
  1386. if (rc) {
  1387. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1388. goto out;
  1389. }
  1390. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1391. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1392. "file version [%d] is supported by this "
  1393. "version of eCryptfs\n",
  1394. crypt_stat->file_version,
  1395. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1396. rc = -EINVAL;
  1397. goto out;
  1398. }
  1399. offset += bytes_read;
  1400. if (crypt_stat->file_version >= 1) {
  1401. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1402. &bytes_read, validate_header_size);
  1403. if (rc) {
  1404. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1405. "metadata; rc = [%d]\n", rc);
  1406. }
  1407. offset += bytes_read;
  1408. } else
  1409. set_default_header_data(crypt_stat);
  1410. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1411. ecryptfs_dentry);
  1412. out:
  1413. return rc;
  1414. }
  1415. /**
  1416. * ecryptfs_read_xattr_region
  1417. * @page_virt: The vitual address into which to read the xattr data
  1418. * @ecryptfs_inode: The eCryptfs inode
  1419. *
  1420. * Attempts to read the crypto metadata from the extended attribute
  1421. * region of the lower file.
  1422. *
  1423. * Returns zero on success; non-zero on error
  1424. */
  1425. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
  1426. {
  1427. struct dentry *lower_dentry =
  1428. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  1429. ssize_t size;
  1430. int rc = 0;
  1431. size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
  1432. page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
  1433. if (size < 0) {
  1434. if (unlikely(ecryptfs_verbosity > 0))
  1435. printk(KERN_INFO "Error attempting to read the [%s] "
  1436. "xattr from the lower file; return value = "
  1437. "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
  1438. rc = -EINVAL;
  1439. goto out;
  1440. }
  1441. out:
  1442. return rc;
  1443. }
  1444. int ecryptfs_read_and_validate_xattr_region(char *page_virt,
  1445. struct dentry *ecryptfs_dentry)
  1446. {
  1447. int rc;
  1448. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
  1449. if (rc)
  1450. goto out;
  1451. if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
  1452. printk(KERN_WARNING "Valid data found in [%s] xattr, but "
  1453. "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
  1454. rc = -EINVAL;
  1455. }
  1456. out:
  1457. return rc;
  1458. }
  1459. /**
  1460. * ecryptfs_read_metadata
  1461. *
  1462. * Common entry point for reading file metadata. From here, we could
  1463. * retrieve the header information from the header region of the file,
  1464. * the xattr region of the file, or some other repostory that is
  1465. * stored separately from the file itself. The current implementation
  1466. * supports retrieving the metadata information from the file contents
  1467. * and from the xattr region.
  1468. *
  1469. * Returns zero if valid headers found and parsed; non-zero otherwise
  1470. */
  1471. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
  1472. {
  1473. int rc = 0;
  1474. char *page_virt = NULL;
  1475. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  1476. struct ecryptfs_crypt_stat *crypt_stat =
  1477. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1478. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1479. &ecryptfs_superblock_to_private(
  1480. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1481. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  1482. mount_crypt_stat);
  1483. /* Read the first page from the underlying file */
  1484. page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
  1485. if (!page_virt) {
  1486. rc = -ENOMEM;
  1487. printk(KERN_ERR "%s: Unable to allocate page_virt\n",
  1488. __func__);
  1489. goto out;
  1490. }
  1491. rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
  1492. ecryptfs_inode);
  1493. if (!rc)
  1494. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1495. ecryptfs_dentry,
  1496. ECRYPTFS_VALIDATE_HEADER_SIZE);
  1497. if (rc) {
  1498. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
  1499. if (rc) {
  1500. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1501. "file header region or xattr region\n");
  1502. rc = -EINVAL;
  1503. goto out;
  1504. }
  1505. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1506. ecryptfs_dentry,
  1507. ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
  1508. if (rc) {
  1509. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1510. "file xattr region either\n");
  1511. rc = -EINVAL;
  1512. }
  1513. if (crypt_stat->mount_crypt_stat->flags
  1514. & ECRYPTFS_XATTR_METADATA_ENABLED) {
  1515. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  1516. } else {
  1517. printk(KERN_WARNING "Attempt to access file with "
  1518. "crypto metadata only in the extended attribute "
  1519. "region, but eCryptfs was mounted without "
  1520. "xattr support enabled. eCryptfs will not treat "
  1521. "this like an encrypted file.\n");
  1522. rc = -EINVAL;
  1523. }
  1524. }
  1525. out:
  1526. if (page_virt) {
  1527. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1528. kmem_cache_free(ecryptfs_header_cache_1, page_virt);
  1529. }
  1530. return rc;
  1531. }
  1532. /**
  1533. * ecryptfs_encode_filename - converts a plaintext file name to cipher text
  1534. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1535. * @name: The plaintext name
  1536. * @length: The length of the plaintext
  1537. * @encoded_name: The encypted name
  1538. *
  1539. * Encrypts and encodes a filename into something that constitutes a
  1540. * valid filename for a filesystem, with printable characters.
  1541. *
  1542. * We assume that we have a properly initialized crypto context,
  1543. * pointed to by crypt_stat->tfm.
  1544. *
  1545. * TODO: Implement filename decoding and decryption here, in place of
  1546. * memcpy. We are keeping the framework around for now to (1)
  1547. * facilitate testing of the components needed to implement filename
  1548. * encryption and (2) to provide a code base from which other
  1549. * developers in the community can easily implement this feature.
  1550. *
  1551. * Returns the length of encoded filename; negative if error
  1552. */
  1553. int
  1554. ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1555. const char *name, int length, char **encoded_name)
  1556. {
  1557. int error = 0;
  1558. (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
  1559. if (!(*encoded_name)) {
  1560. error = -ENOMEM;
  1561. goto out;
  1562. }
  1563. /* TODO: Filename encryption is a scheduled feature for a
  1564. * future version of eCryptfs. This function is here only for
  1565. * the purpose of providing a framework for other developers
  1566. * to easily implement filename encryption. Hint: Replace this
  1567. * memcpy() with a call to encrypt and encode the
  1568. * filename, the set the length accordingly. */
  1569. memcpy((void *)(*encoded_name), (void *)name, length);
  1570. (*encoded_name)[length] = '\0';
  1571. error = length + 1;
  1572. out:
  1573. return error;
  1574. }
  1575. /**
  1576. * ecryptfs_decode_filename - converts the cipher text name to plaintext
  1577. * @crypt_stat: The crypt_stat struct associated with the file
  1578. * @name: The filename in cipher text
  1579. * @length: The length of the cipher text name
  1580. * @decrypted_name: The plaintext name
  1581. *
  1582. * Decodes and decrypts the filename.
  1583. *
  1584. * We assume that we have a properly initialized crypto context,
  1585. * pointed to by crypt_stat->tfm.
  1586. *
  1587. * TODO: Implement filename decoding and decryption here, in place of
  1588. * memcpy. We are keeping the framework around for now to (1)
  1589. * facilitate testing of the components needed to implement filename
  1590. * encryption and (2) to provide a code base from which other
  1591. * developers in the community can easily implement this feature.
  1592. *
  1593. * Returns the length of decoded filename; negative if error
  1594. */
  1595. int
  1596. ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1597. const char *name, int length, char **decrypted_name)
  1598. {
  1599. int error = 0;
  1600. (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
  1601. if (!(*decrypted_name)) {
  1602. error = -ENOMEM;
  1603. goto out;
  1604. }
  1605. /* TODO: Filename encryption is a scheduled feature for a
  1606. * future version of eCryptfs. This function is here only for
  1607. * the purpose of providing a framework for other developers
  1608. * to easily implement filename encryption. Hint: Replace this
  1609. * memcpy() with a call to decode and decrypt the
  1610. * filename, the set the length accordingly. */
  1611. memcpy((void *)(*decrypted_name), (void *)name, length);
  1612. (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
  1613. * in printing out the
  1614. * string in debug
  1615. * messages */
  1616. error = length;
  1617. out:
  1618. return error;
  1619. }
  1620. /**
  1621. * ecryptfs_process_key_cipher - Perform key cipher initialization.
  1622. * @key_tfm: Crypto context for key material, set by this function
  1623. * @cipher_name: Name of the cipher
  1624. * @key_size: Size of the key in bytes
  1625. *
  1626. * Returns zero on success. Any crypto_tfm structs allocated here
  1627. * should be released by other functions, such as on a superblock put
  1628. * event, regardless of whether this function succeeds for fails.
  1629. */
  1630. static int
  1631. ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
  1632. char *cipher_name, size_t *key_size)
  1633. {
  1634. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1635. char *full_alg_name;
  1636. int rc;
  1637. *key_tfm = NULL;
  1638. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1639. rc = -EINVAL;
  1640. printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
  1641. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1642. goto out;
  1643. }
  1644. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1645. "ecb");
  1646. if (rc)
  1647. goto out;
  1648. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1649. kfree(full_alg_name);
  1650. if (IS_ERR(*key_tfm)) {
  1651. rc = PTR_ERR(*key_tfm);
  1652. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1653. "[%s]; rc = [%d]\n", cipher_name, rc);
  1654. goto out;
  1655. }
  1656. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1657. if (*key_size == 0) {
  1658. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1659. *key_size = alg->max_keysize;
  1660. }
  1661. get_random_bytes(dummy_key, *key_size);
  1662. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1663. if (rc) {
  1664. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1665. "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
  1666. rc = -EINVAL;
  1667. goto out;
  1668. }
  1669. out:
  1670. return rc;
  1671. }
  1672. struct kmem_cache *ecryptfs_key_tfm_cache;
  1673. static struct list_head key_tfm_list;
  1674. struct mutex key_tfm_list_mutex;
  1675. int ecryptfs_init_crypto(void)
  1676. {
  1677. mutex_init(&key_tfm_list_mutex);
  1678. INIT_LIST_HEAD(&key_tfm_list);
  1679. return 0;
  1680. }
  1681. /**
  1682. * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
  1683. *
  1684. * Called only at module unload time
  1685. */
  1686. int ecryptfs_destroy_crypto(void)
  1687. {
  1688. struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
  1689. mutex_lock(&key_tfm_list_mutex);
  1690. list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
  1691. key_tfm_list) {
  1692. list_del(&key_tfm->key_tfm_list);
  1693. if (key_tfm->key_tfm)
  1694. crypto_free_blkcipher(key_tfm->key_tfm);
  1695. kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
  1696. }
  1697. mutex_unlock(&key_tfm_list_mutex);
  1698. return 0;
  1699. }
  1700. int
  1701. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  1702. size_t key_size)
  1703. {
  1704. struct ecryptfs_key_tfm *tmp_tfm;
  1705. int rc = 0;
  1706. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1707. tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
  1708. if (key_tfm != NULL)
  1709. (*key_tfm) = tmp_tfm;
  1710. if (!tmp_tfm) {
  1711. rc = -ENOMEM;
  1712. printk(KERN_ERR "Error attempting to allocate from "
  1713. "ecryptfs_key_tfm_cache\n");
  1714. goto out;
  1715. }
  1716. mutex_init(&tmp_tfm->key_tfm_mutex);
  1717. strncpy(tmp_tfm->cipher_name, cipher_name,
  1718. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  1719. tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  1720. tmp_tfm->key_size = key_size;
  1721. rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
  1722. tmp_tfm->cipher_name,
  1723. &tmp_tfm->key_size);
  1724. if (rc) {
  1725. printk(KERN_ERR "Error attempting to initialize key TFM "
  1726. "cipher with name = [%s]; rc = [%d]\n",
  1727. tmp_tfm->cipher_name, rc);
  1728. kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
  1729. if (key_tfm != NULL)
  1730. (*key_tfm) = NULL;
  1731. goto out;
  1732. }
  1733. list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
  1734. out:
  1735. return rc;
  1736. }
  1737. /**
  1738. * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
  1739. * @cipher_name: the name of the cipher to search for
  1740. * @key_tfm: set to corresponding tfm if found
  1741. *
  1742. * Searches for cached key_tfm matching @cipher_name
  1743. * Must be called with &key_tfm_list_mutex held
  1744. * Returns 1 if found, with @key_tfm set
  1745. * Returns 0 if not found, with @key_tfm set to NULL
  1746. */
  1747. int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
  1748. {
  1749. struct ecryptfs_key_tfm *tmp_key_tfm;
  1750. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1751. list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
  1752. if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
  1753. if (key_tfm)
  1754. (*key_tfm) = tmp_key_tfm;
  1755. return 1;
  1756. }
  1757. }
  1758. if (key_tfm)
  1759. (*key_tfm) = NULL;
  1760. return 0;
  1761. }
  1762. /**
  1763. * ecryptfs_get_tfm_and_mutex_for_cipher_name
  1764. *
  1765. * @tfm: set to cached tfm found, or new tfm created
  1766. * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
  1767. * @cipher_name: the name of the cipher to search for and/or add
  1768. *
  1769. * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
  1770. * Searches for cached item first, and creates new if not found.
  1771. * Returns 0 on success, non-zero if adding new cipher failed
  1772. */
  1773. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
  1774. struct mutex **tfm_mutex,
  1775. char *cipher_name)
  1776. {
  1777. struct ecryptfs_key_tfm *key_tfm;
  1778. int rc = 0;
  1779. (*tfm) = NULL;
  1780. (*tfm_mutex) = NULL;
  1781. mutex_lock(&key_tfm_list_mutex);
  1782. if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
  1783. rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
  1784. if (rc) {
  1785. printk(KERN_ERR "Error adding new key_tfm to list; "
  1786. "rc = [%d]\n", rc);
  1787. goto out;
  1788. }
  1789. }
  1790. (*tfm) = key_tfm->key_tfm;
  1791. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1792. out:
  1793. mutex_unlock(&key_tfm_list_mutex);
  1794. return rc;
  1795. }