crypto.c 56 KB

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