crypto.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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. if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
  337. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  338. crypt_stat->key_size);
  339. crypt_stat->flags |= ECRYPTFS_KEY_SET;
  340. }
  341. if (rc) {
  342. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  343. rc);
  344. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  345. rc = -EINVAL;
  346. goto out;
  347. }
  348. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
  349. crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
  350. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  351. out:
  352. return rc;
  353. }
  354. /**
  355. * ecryptfs_lower_offset_for_extent
  356. *
  357. * Convert an eCryptfs page index into a lower byte offset
  358. */
  359. static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
  360. struct ecryptfs_crypt_stat *crypt_stat)
  361. {
  362. (*offset) = (crypt_stat->num_header_bytes_at_front
  363. + (crypt_stat->extent_size * extent_num));
  364. }
  365. /**
  366. * ecryptfs_encrypt_extent
  367. * @enc_extent_page: Allocated page into which to encrypt the data in
  368. * @page
  369. * @crypt_stat: crypt_stat containing cryptographic context for the
  370. * encryption operation
  371. * @page: Page containing plaintext data extent to encrypt
  372. * @extent_offset: Page extent offset for use in generating IV
  373. *
  374. * Encrypts one extent of data.
  375. *
  376. * Return zero on success; non-zero otherwise
  377. */
  378. static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
  379. struct ecryptfs_crypt_stat *crypt_stat,
  380. struct page *page,
  381. unsigned long extent_offset)
  382. {
  383. loff_t extent_base;
  384. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  385. int rc;
  386. extent_base = (((loff_t)page->index)
  387. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  388. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  389. (extent_base + extent_offset));
  390. if (rc) {
  391. ecryptfs_printk(KERN_ERR, "Error attempting to "
  392. "derive IV for extent [0x%.16x]; "
  393. "rc = [%d]\n", (extent_base + extent_offset),
  394. rc);
  395. goto out;
  396. }
  397. if (unlikely(ecryptfs_verbosity > 0)) {
  398. ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
  399. "with iv:\n");
  400. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  401. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  402. "encryption:\n");
  403. ecryptfs_dump_hex((char *)
  404. (page_address(page)
  405. + (extent_offset * crypt_stat->extent_size)),
  406. 8);
  407. }
  408. rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
  409. page, (extent_offset
  410. * crypt_stat->extent_size),
  411. crypt_stat->extent_size, extent_iv);
  412. if (rc < 0) {
  413. printk(KERN_ERR "%s: Error attempting to encrypt page with "
  414. "page->index = [%ld], extent_offset = [%ld]; "
  415. "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
  416. rc);
  417. goto out;
  418. }
  419. rc = 0;
  420. if (unlikely(ecryptfs_verbosity > 0)) {
  421. ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
  422. "rc = [%d]\n", (extent_base + extent_offset),
  423. rc);
  424. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  425. "encryption:\n");
  426. ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
  427. }
  428. out:
  429. return rc;
  430. }
  431. /**
  432. * ecryptfs_encrypt_page
  433. * @page: Page mapped from the eCryptfs inode for the file; contains
  434. * decrypted content that needs to be encrypted (to a temporary
  435. * page; not in place) and written out to the lower file
  436. *
  437. * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
  438. * that eCryptfs pages may straddle the lower pages -- for instance,
  439. * if the file was created on a machine with an 8K page size
  440. * (resulting in an 8K header), and then the file is copied onto a
  441. * host with a 32K page size, then when reading page 0 of the eCryptfs
  442. * file, 24K of page 0 of the lower file will be read and decrypted,
  443. * and then 8K of page 1 of the lower file will be read and decrypted.
  444. *
  445. * Returns zero on success; negative on error
  446. */
  447. int ecryptfs_encrypt_page(struct page *page)
  448. {
  449. struct inode *ecryptfs_inode;
  450. struct ecryptfs_crypt_stat *crypt_stat;
  451. char *enc_extent_virt = NULL;
  452. struct page *enc_extent_page;
  453. loff_t extent_offset;
  454. int rc = 0;
  455. ecryptfs_inode = page->mapping->host;
  456. crypt_stat =
  457. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  458. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  459. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
  460. 0, PAGE_CACHE_SIZE);
  461. if (rc)
  462. printk(KERN_ERR "%s: Error attempting to copy "
  463. "page at index [%ld]\n", __FUNCTION__,
  464. page->index);
  465. goto out;
  466. }
  467. enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
  468. if (!enc_extent_virt) {
  469. rc = -ENOMEM;
  470. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  471. "encrypted extent\n");
  472. goto out;
  473. }
  474. enc_extent_page = virt_to_page(enc_extent_virt);
  475. for (extent_offset = 0;
  476. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  477. extent_offset++) {
  478. loff_t offset;
  479. rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
  480. extent_offset);
  481. if (rc) {
  482. printk(KERN_ERR "%s: Error encrypting extent; "
  483. "rc = [%d]\n", __FUNCTION__, rc);
  484. goto out;
  485. }
  486. ecryptfs_lower_offset_for_extent(
  487. &offset, ((((loff_t)page->index)
  488. * (PAGE_CACHE_SIZE
  489. / crypt_stat->extent_size))
  490. + extent_offset), crypt_stat);
  491. rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
  492. offset, crypt_stat->extent_size);
  493. if (rc) {
  494. ecryptfs_printk(KERN_ERR, "Error attempting "
  495. "to write lower page; rc = [%d]"
  496. "\n", rc);
  497. goto out;
  498. }
  499. }
  500. out:
  501. kfree(enc_extent_virt);
  502. return rc;
  503. }
  504. static int ecryptfs_decrypt_extent(struct page *page,
  505. struct ecryptfs_crypt_stat *crypt_stat,
  506. struct page *enc_extent_page,
  507. unsigned long extent_offset)
  508. {
  509. loff_t extent_base;
  510. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  511. int rc;
  512. extent_base = (((loff_t)page->index)
  513. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  514. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  515. (extent_base + extent_offset));
  516. if (rc) {
  517. ecryptfs_printk(KERN_ERR, "Error attempting to "
  518. "derive IV for extent [0x%.16x]; "
  519. "rc = [%d]\n", (extent_base + extent_offset),
  520. rc);
  521. goto out;
  522. }
  523. if (unlikely(ecryptfs_verbosity > 0)) {
  524. ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
  525. "with iv:\n");
  526. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  527. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  528. "decryption:\n");
  529. ecryptfs_dump_hex((char *)
  530. (page_address(enc_extent_page)
  531. + (extent_offset * crypt_stat->extent_size)),
  532. 8);
  533. }
  534. rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
  535. (extent_offset
  536. * crypt_stat->extent_size),
  537. enc_extent_page, 0,
  538. crypt_stat->extent_size, extent_iv);
  539. if (rc < 0) {
  540. printk(KERN_ERR "%s: Error attempting to decrypt to page with "
  541. "page->index = [%ld], extent_offset = [%ld]; "
  542. "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
  543. rc);
  544. goto out;
  545. }
  546. rc = 0;
  547. if (unlikely(ecryptfs_verbosity > 0)) {
  548. ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
  549. "rc = [%d]\n", (extent_base + extent_offset),
  550. rc);
  551. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  552. "decryption:\n");
  553. ecryptfs_dump_hex((char *)(page_address(page)
  554. + (extent_offset
  555. * crypt_stat->extent_size)), 8);
  556. }
  557. out:
  558. return rc;
  559. }
  560. /**
  561. * ecryptfs_decrypt_page
  562. * @page: Page mapped from the eCryptfs inode for the file; data read
  563. * and decrypted from the lower file will be written into this
  564. * page
  565. *
  566. * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
  567. * that eCryptfs pages may straddle the lower pages -- for instance,
  568. * if the file was created on a machine with an 8K page size
  569. * (resulting in an 8K header), and then the file is copied onto a
  570. * host with a 32K page size, then when reading page 0 of the eCryptfs
  571. * file, 24K of page 0 of the lower file will be read and decrypted,
  572. * and then 8K of page 1 of the lower file will be read and decrypted.
  573. *
  574. * Returns zero on success; negative on error
  575. */
  576. int ecryptfs_decrypt_page(struct page *page)
  577. {
  578. struct inode *ecryptfs_inode;
  579. struct ecryptfs_crypt_stat *crypt_stat;
  580. char *enc_extent_virt = NULL;
  581. struct page *enc_extent_page;
  582. unsigned long extent_offset;
  583. int rc = 0;
  584. ecryptfs_inode = page->mapping->host;
  585. crypt_stat =
  586. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  587. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  588. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  589. PAGE_CACHE_SIZE,
  590. ecryptfs_inode);
  591. if (rc)
  592. printk(KERN_ERR "%s: Error attempting to copy "
  593. "page at index [%ld]\n", __FUNCTION__,
  594. page->index);
  595. goto out;
  596. }
  597. enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
  598. if (!enc_extent_virt) {
  599. rc = -ENOMEM;
  600. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  601. "encrypted extent\n");
  602. goto out;
  603. }
  604. enc_extent_page = virt_to_page(enc_extent_virt);
  605. for (extent_offset = 0;
  606. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  607. extent_offset++) {
  608. loff_t offset;
  609. ecryptfs_lower_offset_for_extent(
  610. &offset, ((page->index * (PAGE_CACHE_SIZE
  611. / crypt_stat->extent_size))
  612. + extent_offset), crypt_stat);
  613. rc = ecryptfs_read_lower(enc_extent_virt, offset,
  614. crypt_stat->extent_size,
  615. ecryptfs_inode);
  616. if (rc) {
  617. ecryptfs_printk(KERN_ERR, "Error attempting "
  618. "to read lower page; rc = [%d]"
  619. "\n", rc);
  620. goto out;
  621. }
  622. rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
  623. extent_offset);
  624. if (rc) {
  625. printk(KERN_ERR "%s: Error encrypting extent; "
  626. "rc = [%d]\n", __FUNCTION__, rc);
  627. goto out;
  628. }
  629. }
  630. out:
  631. kfree(enc_extent_virt);
  632. return rc;
  633. }
  634. /**
  635. * decrypt_scatterlist
  636. * @crypt_stat: Cryptographic context
  637. * @dest_sg: The destination scatterlist to decrypt into
  638. * @src_sg: The source scatterlist to decrypt from
  639. * @size: The number of bytes to decrypt
  640. * @iv: The initialization vector to use for the decryption
  641. *
  642. * Returns the number of bytes decrypted; negative value on error
  643. */
  644. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  645. struct scatterlist *dest_sg,
  646. struct scatterlist *src_sg, int size,
  647. unsigned char *iv)
  648. {
  649. struct blkcipher_desc desc = {
  650. .tfm = crypt_stat->tfm,
  651. .info = iv,
  652. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  653. };
  654. int rc = 0;
  655. /* Consider doing this once, when the file is opened */
  656. mutex_lock(&crypt_stat->cs_tfm_mutex);
  657. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  658. crypt_stat->key_size);
  659. if (rc) {
  660. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  661. rc);
  662. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  663. rc = -EINVAL;
  664. goto out;
  665. }
  666. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  667. rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
  668. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  669. if (rc) {
  670. ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
  671. rc);
  672. goto out;
  673. }
  674. rc = size;
  675. out:
  676. return rc;
  677. }
  678. /**
  679. * ecryptfs_encrypt_page_offset
  680. * @crypt_stat: The cryptographic context
  681. * @dst_page: The page to encrypt into
  682. * @dst_offset: The offset in the page to encrypt into
  683. * @src_page: The page to encrypt from
  684. * @src_offset: The offset in the page to encrypt from
  685. * @size: The number of bytes to encrypt
  686. * @iv: The initialization vector to use for the encryption
  687. *
  688. * Returns the number of bytes encrypted
  689. */
  690. static int
  691. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  692. struct page *dst_page, int dst_offset,
  693. struct page *src_page, int src_offset, int size,
  694. unsigned char *iv)
  695. {
  696. struct scatterlist src_sg, dst_sg;
  697. sg_init_table(&src_sg, 1);
  698. sg_init_table(&dst_sg, 1);
  699. sg_set_page(&src_sg, src_page, size, src_offset);
  700. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  701. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  702. }
  703. /**
  704. * ecryptfs_decrypt_page_offset
  705. * @crypt_stat: The cryptographic context
  706. * @dst_page: The page to decrypt into
  707. * @dst_offset: The offset in the page to decrypt into
  708. * @src_page: The page to decrypt from
  709. * @src_offset: The offset in the page to decrypt from
  710. * @size: The number of bytes to decrypt
  711. * @iv: The initialization vector to use for the decryption
  712. *
  713. * Returns the number of bytes decrypted
  714. */
  715. static int
  716. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  717. struct page *dst_page, int dst_offset,
  718. struct page *src_page, int src_offset, int size,
  719. unsigned char *iv)
  720. {
  721. struct scatterlist src_sg, dst_sg;
  722. sg_init_table(&src_sg, 1);
  723. sg_set_page(&src_sg, src_page, size, src_offset);
  724. sg_init_table(&dst_sg, 1);
  725. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  726. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  727. }
  728. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  729. /**
  730. * ecryptfs_init_crypt_ctx
  731. * @crypt_stat: Uninitilized crypt stats structure
  732. *
  733. * Initialize the crypto context.
  734. *
  735. * TODO: Performance: Keep a cache of initialized cipher contexts;
  736. * only init if needed
  737. */
  738. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  739. {
  740. char *full_alg_name;
  741. int rc = -EINVAL;
  742. if (!crypt_stat->cipher) {
  743. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  744. goto out;
  745. }
  746. ecryptfs_printk(KERN_DEBUG,
  747. "Initializing cipher [%s]; strlen = [%d]; "
  748. "key_size_bits = [%d]\n",
  749. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  750. crypt_stat->key_size << 3);
  751. if (crypt_stat->tfm) {
  752. rc = 0;
  753. goto out;
  754. }
  755. mutex_lock(&crypt_stat->cs_tfm_mutex);
  756. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  757. crypt_stat->cipher, "cbc");
  758. if (rc)
  759. goto out_unlock;
  760. crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  761. CRYPTO_ALG_ASYNC);
  762. kfree(full_alg_name);
  763. if (IS_ERR(crypt_stat->tfm)) {
  764. rc = PTR_ERR(crypt_stat->tfm);
  765. ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
  766. "Error initializing cipher [%s]\n",
  767. crypt_stat->cipher);
  768. goto out_unlock;
  769. }
  770. crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  771. rc = 0;
  772. out_unlock:
  773. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  774. out:
  775. return rc;
  776. }
  777. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  778. {
  779. int extent_size_tmp;
  780. crypt_stat->extent_mask = 0xFFFFFFFF;
  781. crypt_stat->extent_shift = 0;
  782. if (crypt_stat->extent_size == 0)
  783. return;
  784. extent_size_tmp = crypt_stat->extent_size;
  785. while ((extent_size_tmp & 0x01) == 0) {
  786. extent_size_tmp >>= 1;
  787. crypt_stat->extent_mask <<= 1;
  788. crypt_stat->extent_shift++;
  789. }
  790. }
  791. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  792. {
  793. /* Default values; may be overwritten as we are parsing the
  794. * packets. */
  795. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  796. set_extent_mask_and_shift(crypt_stat);
  797. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  798. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  799. crypt_stat->num_header_bytes_at_front = 0;
  800. else {
  801. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
  802. crypt_stat->num_header_bytes_at_front =
  803. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  804. else
  805. crypt_stat->num_header_bytes_at_front = PAGE_CACHE_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. u8 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. u8 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
  1086. {
  1087. int i;
  1088. u8 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, u8 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_bytes_at_front
  1163. / crypt_stat->extent_size);
  1164. header_extent_size = cpu_to_be32(header_extent_size);
  1165. memcpy(virt, &header_extent_size, 4);
  1166. virt += 4;
  1167. num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
  1168. memcpy(virt, &num_header_extents_at_front, 2);
  1169. (*written) = 6;
  1170. }
  1171. struct kmem_cache *ecryptfs_header_cache_0;
  1172. struct kmem_cache *ecryptfs_header_cache_1;
  1173. struct kmem_cache *ecryptfs_header_cache_2;
  1174. /**
  1175. * ecryptfs_write_headers_virt
  1176. * @page_virt: The virtual address to write the headers to
  1177. * @size: Set to the number of bytes written by this function
  1178. * @crypt_stat: The cryptographic context
  1179. * @ecryptfs_dentry: The eCryptfs dentry
  1180. *
  1181. * Format version: 1
  1182. *
  1183. * Header Extent:
  1184. * Octets 0-7: Unencrypted file size (big-endian)
  1185. * Octets 8-15: eCryptfs special marker
  1186. * Octets 16-19: Flags
  1187. * Octet 16: File format version number (between 0 and 255)
  1188. * Octets 17-18: Reserved
  1189. * Octet 19: Bit 1 (lsb): Reserved
  1190. * Bit 2: Encrypted?
  1191. * Bits 3-8: Reserved
  1192. * Octets 20-23: Header extent size (big-endian)
  1193. * Octets 24-25: Number of header extents at front of file
  1194. * (big-endian)
  1195. * Octet 26: Begin RFC 2440 authentication token packet set
  1196. * Data Extent 0:
  1197. * Lower data (CBC encrypted)
  1198. * Data Extent 1:
  1199. * Lower data (CBC encrypted)
  1200. * ...
  1201. *
  1202. * Returns zero on success
  1203. */
  1204. static int ecryptfs_write_headers_virt(char *page_virt, 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. PAGE_CACHE_SIZE - 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", __FUNCTION__,
  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. __FUNCTION__);
  1283. rc = -EINVAL;
  1284. goto out;
  1285. }
  1286. /* Released in this function */
  1287. virt = kzalloc(crypt_stat->num_header_bytes_at_front, GFP_KERNEL);
  1288. if (!virt) {
  1289. printk(KERN_ERR "%s: Out of memory\n", __FUNCTION__);
  1290. rc = -ENOMEM;
  1291. goto out;
  1292. }
  1293. rc = ecryptfs_write_headers_virt(virt, &size, crypt_stat,
  1294. ecryptfs_dentry);
  1295. if (unlikely(rc)) {
  1296. printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
  1297. __FUNCTION__, 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", __FUNCTION__, rc);
  1309. goto out_free;
  1310. }
  1311. out_free:
  1312. memset(virt, 0, crypt_stat->num_header_bytes_at_front);
  1313. kfree(virt);
  1314. out:
  1315. return rc;
  1316. }
  1317. #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
  1318. #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
  1319. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1320. char *virt, int *bytes_read,
  1321. int validate_header_size)
  1322. {
  1323. int rc = 0;
  1324. u32 header_extent_size;
  1325. u16 num_header_extents_at_front;
  1326. memcpy(&header_extent_size, virt, sizeof(u32));
  1327. header_extent_size = be32_to_cpu(header_extent_size);
  1328. virt += sizeof(u32);
  1329. memcpy(&num_header_extents_at_front, virt, sizeof(u16));
  1330. num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
  1331. crypt_stat->num_header_bytes_at_front =
  1332. (((size_t)num_header_extents_at_front
  1333. * (size_t)header_extent_size));
  1334. (*bytes_read) = (sizeof(u32) + sizeof(u16));
  1335. if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
  1336. && (crypt_stat->num_header_bytes_at_front
  1337. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
  1338. rc = -EINVAL;
  1339. printk(KERN_WARNING "Invalid header size: [%zd]\n",
  1340. crypt_stat->num_header_bytes_at_front);
  1341. }
  1342. return rc;
  1343. }
  1344. /**
  1345. * set_default_header_data
  1346. * @crypt_stat: The cryptographic context
  1347. *
  1348. * For version 0 file format; this function is only for backwards
  1349. * compatibility for files created with the prior versions of
  1350. * eCryptfs.
  1351. */
  1352. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1353. {
  1354. crypt_stat->num_header_bytes_at_front =
  1355. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  1356. }
  1357. /**
  1358. * ecryptfs_read_headers_virt
  1359. * @page_virt: The virtual address into which to read the headers
  1360. * @crypt_stat: The cryptographic context
  1361. * @ecryptfs_dentry: The eCryptfs dentry
  1362. * @validate_header_size: Whether to validate the header size while reading
  1363. *
  1364. * Read/parse the header data. The header format is detailed in the
  1365. * comment block for the ecryptfs_write_headers_virt() function.
  1366. *
  1367. * Returns zero on success
  1368. */
  1369. static int ecryptfs_read_headers_virt(char *page_virt,
  1370. struct ecryptfs_crypt_stat *crypt_stat,
  1371. struct dentry *ecryptfs_dentry,
  1372. int validate_header_size)
  1373. {
  1374. int rc = 0;
  1375. int offset;
  1376. int bytes_read;
  1377. ecryptfs_set_default_sizes(crypt_stat);
  1378. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1379. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1380. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1381. rc = contains_ecryptfs_marker(page_virt + offset);
  1382. if (rc == 0) {
  1383. rc = -EINVAL;
  1384. goto out;
  1385. }
  1386. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1387. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1388. &bytes_read);
  1389. if (rc) {
  1390. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1391. goto out;
  1392. }
  1393. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1394. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1395. "file version [%d] is supported by this "
  1396. "version of eCryptfs\n",
  1397. crypt_stat->file_version,
  1398. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1399. rc = -EINVAL;
  1400. goto out;
  1401. }
  1402. offset += bytes_read;
  1403. if (crypt_stat->file_version >= 1) {
  1404. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1405. &bytes_read, validate_header_size);
  1406. if (rc) {
  1407. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1408. "metadata; rc = [%d]\n", rc);
  1409. }
  1410. offset += bytes_read;
  1411. } else
  1412. set_default_header_data(crypt_stat);
  1413. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1414. ecryptfs_dentry);
  1415. out:
  1416. return rc;
  1417. }
  1418. /**
  1419. * ecryptfs_read_xattr_region
  1420. * @page_virt: The vitual address into which to read the xattr data
  1421. * @ecryptfs_inode: The eCryptfs inode
  1422. *
  1423. * Attempts to read the crypto metadata from the extended attribute
  1424. * region of the lower file.
  1425. *
  1426. * Returns zero on success; non-zero on error
  1427. */
  1428. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
  1429. {
  1430. struct dentry *lower_dentry =
  1431. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  1432. ssize_t size;
  1433. int rc = 0;
  1434. size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
  1435. page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
  1436. if (size < 0) {
  1437. if (unlikely(ecryptfs_verbosity > 0))
  1438. printk(KERN_INFO "Error attempting to read the [%s] "
  1439. "xattr from the lower file; return value = "
  1440. "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
  1441. rc = -EINVAL;
  1442. goto out;
  1443. }
  1444. out:
  1445. return rc;
  1446. }
  1447. int ecryptfs_read_and_validate_xattr_region(char *page_virt,
  1448. struct dentry *ecryptfs_dentry)
  1449. {
  1450. int rc;
  1451. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
  1452. if (rc)
  1453. goto out;
  1454. if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
  1455. printk(KERN_WARNING "Valid data found in [%s] xattr, but "
  1456. "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
  1457. rc = -EINVAL;
  1458. }
  1459. out:
  1460. return rc;
  1461. }
  1462. /**
  1463. * ecryptfs_read_metadata
  1464. *
  1465. * Common entry point for reading file metadata. From here, we could
  1466. * retrieve the header information from the header region of the file,
  1467. * the xattr region of the file, or some other repostory that is
  1468. * stored separately from the file itself. The current implementation
  1469. * supports retrieving the metadata information from the file contents
  1470. * and from the xattr region.
  1471. *
  1472. * Returns zero if valid headers found and parsed; non-zero otherwise
  1473. */
  1474. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
  1475. {
  1476. int rc = 0;
  1477. char *page_virt = NULL;
  1478. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  1479. struct ecryptfs_crypt_stat *crypt_stat =
  1480. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1481. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1482. &ecryptfs_superblock_to_private(
  1483. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1484. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  1485. mount_crypt_stat);
  1486. /* Read the first page from the underlying file */
  1487. page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
  1488. if (!page_virt) {
  1489. rc = -ENOMEM;
  1490. printk(KERN_ERR "%s: Unable to allocate page_virt\n",
  1491. __FUNCTION__);
  1492. goto out;
  1493. }
  1494. rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
  1495. ecryptfs_inode);
  1496. if (!rc)
  1497. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1498. ecryptfs_dentry,
  1499. ECRYPTFS_VALIDATE_HEADER_SIZE);
  1500. if (rc) {
  1501. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
  1502. if (rc) {
  1503. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1504. "file header region or xattr region\n");
  1505. rc = -EINVAL;
  1506. goto out;
  1507. }
  1508. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1509. ecryptfs_dentry,
  1510. ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
  1511. if (rc) {
  1512. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1513. "file xattr region either\n");
  1514. rc = -EINVAL;
  1515. }
  1516. if (crypt_stat->mount_crypt_stat->flags
  1517. & ECRYPTFS_XATTR_METADATA_ENABLED) {
  1518. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  1519. } else {
  1520. printk(KERN_WARNING "Attempt to access file with "
  1521. "crypto metadata only in the extended attribute "
  1522. "region, but eCryptfs was mounted without "
  1523. "xattr support enabled. eCryptfs will not treat "
  1524. "this like an encrypted file.\n");
  1525. rc = -EINVAL;
  1526. }
  1527. }
  1528. out:
  1529. if (page_virt) {
  1530. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1531. kmem_cache_free(ecryptfs_header_cache_1, page_virt);
  1532. }
  1533. return rc;
  1534. }
  1535. /**
  1536. * ecryptfs_encode_filename - converts a plaintext file name to cipher text
  1537. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1538. * @name: The plaintext name
  1539. * @length: The length of the plaintext
  1540. * @encoded_name: The encypted name
  1541. *
  1542. * Encrypts and encodes a filename into something that constitutes a
  1543. * valid filename for a filesystem, with printable characters.
  1544. *
  1545. * We assume that we have a properly initialized crypto context,
  1546. * pointed to by crypt_stat->tfm.
  1547. *
  1548. * TODO: Implement filename decoding and decryption here, in place of
  1549. * memcpy. We are keeping the framework around for now to (1)
  1550. * facilitate testing of the components needed to implement filename
  1551. * encryption and (2) to provide a code base from which other
  1552. * developers in the community can easily implement this feature.
  1553. *
  1554. * Returns the length of encoded filename; negative if error
  1555. */
  1556. int
  1557. ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1558. const char *name, int length, char **encoded_name)
  1559. {
  1560. int error = 0;
  1561. (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
  1562. if (!(*encoded_name)) {
  1563. error = -ENOMEM;
  1564. goto out;
  1565. }
  1566. /* TODO: Filename encryption is a scheduled feature for a
  1567. * future version of eCryptfs. This function is here only for
  1568. * the purpose of providing a framework for other developers
  1569. * to easily implement filename encryption. Hint: Replace this
  1570. * memcpy() with a call to encrypt and encode the
  1571. * filename, the set the length accordingly. */
  1572. memcpy((void *)(*encoded_name), (void *)name, length);
  1573. (*encoded_name)[length] = '\0';
  1574. error = length + 1;
  1575. out:
  1576. return error;
  1577. }
  1578. /**
  1579. * ecryptfs_decode_filename - converts the cipher text name to plaintext
  1580. * @crypt_stat: The crypt_stat struct associated with the file
  1581. * @name: The filename in cipher text
  1582. * @length: The length of the cipher text name
  1583. * @decrypted_name: The plaintext name
  1584. *
  1585. * Decodes and decrypts the filename.
  1586. *
  1587. * We assume that we have a properly initialized crypto context,
  1588. * pointed to by crypt_stat->tfm.
  1589. *
  1590. * TODO: Implement filename decoding and decryption here, in place of
  1591. * memcpy. We are keeping the framework around for now to (1)
  1592. * facilitate testing of the components needed to implement filename
  1593. * encryption and (2) to provide a code base from which other
  1594. * developers in the community can easily implement this feature.
  1595. *
  1596. * Returns the length of decoded filename; negative if error
  1597. */
  1598. int
  1599. ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1600. const char *name, int length, char **decrypted_name)
  1601. {
  1602. int error = 0;
  1603. (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
  1604. if (!(*decrypted_name)) {
  1605. error = -ENOMEM;
  1606. goto out;
  1607. }
  1608. /* TODO: Filename encryption is a scheduled feature for a
  1609. * future version of eCryptfs. This function is here only for
  1610. * the purpose of providing a framework for other developers
  1611. * to easily implement filename encryption. Hint: Replace this
  1612. * memcpy() with a call to decode and decrypt the
  1613. * filename, the set the length accordingly. */
  1614. memcpy((void *)(*decrypted_name), (void *)name, length);
  1615. (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
  1616. * in printing out the
  1617. * string in debug
  1618. * messages */
  1619. error = length;
  1620. out:
  1621. return error;
  1622. }
  1623. /**
  1624. * ecryptfs_process_key_cipher - Perform key cipher initialization.
  1625. * @key_tfm: Crypto context for key material, set by this function
  1626. * @cipher_name: Name of the cipher
  1627. * @key_size: Size of the key in bytes
  1628. *
  1629. * Returns zero on success. Any crypto_tfm structs allocated here
  1630. * should be released by other functions, such as on a superblock put
  1631. * event, regardless of whether this function succeeds for fails.
  1632. */
  1633. static int
  1634. ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
  1635. char *cipher_name, size_t *key_size)
  1636. {
  1637. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1638. char *full_alg_name;
  1639. int rc;
  1640. *key_tfm = NULL;
  1641. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1642. rc = -EINVAL;
  1643. printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
  1644. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1645. goto out;
  1646. }
  1647. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1648. "ecb");
  1649. if (rc)
  1650. goto out;
  1651. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1652. kfree(full_alg_name);
  1653. if (IS_ERR(*key_tfm)) {
  1654. rc = PTR_ERR(*key_tfm);
  1655. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1656. "[%s]; rc = [%d]\n", cipher_name, rc);
  1657. goto out;
  1658. }
  1659. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1660. if (*key_size == 0) {
  1661. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1662. *key_size = alg->max_keysize;
  1663. }
  1664. get_random_bytes(dummy_key, *key_size);
  1665. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1666. if (rc) {
  1667. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1668. "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
  1669. rc = -EINVAL;
  1670. goto out;
  1671. }
  1672. out:
  1673. return rc;
  1674. }
  1675. struct kmem_cache *ecryptfs_key_tfm_cache;
  1676. static struct list_head key_tfm_list;
  1677. struct mutex key_tfm_list_mutex;
  1678. int ecryptfs_init_crypto(void)
  1679. {
  1680. mutex_init(&key_tfm_list_mutex);
  1681. INIT_LIST_HEAD(&key_tfm_list);
  1682. return 0;
  1683. }
  1684. /**
  1685. * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
  1686. *
  1687. * Called only at module unload time
  1688. */
  1689. int ecryptfs_destroy_crypto(void)
  1690. {
  1691. struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
  1692. mutex_lock(&key_tfm_list_mutex);
  1693. list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
  1694. key_tfm_list) {
  1695. list_del(&key_tfm->key_tfm_list);
  1696. if (key_tfm->key_tfm)
  1697. crypto_free_blkcipher(key_tfm->key_tfm);
  1698. kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
  1699. }
  1700. mutex_unlock(&key_tfm_list_mutex);
  1701. return 0;
  1702. }
  1703. int
  1704. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  1705. size_t key_size)
  1706. {
  1707. struct ecryptfs_key_tfm *tmp_tfm;
  1708. int rc = 0;
  1709. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1710. tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
  1711. if (key_tfm != NULL)
  1712. (*key_tfm) = tmp_tfm;
  1713. if (!tmp_tfm) {
  1714. rc = -ENOMEM;
  1715. printk(KERN_ERR "Error attempting to allocate from "
  1716. "ecryptfs_key_tfm_cache\n");
  1717. goto out;
  1718. }
  1719. mutex_init(&tmp_tfm->key_tfm_mutex);
  1720. strncpy(tmp_tfm->cipher_name, cipher_name,
  1721. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  1722. tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  1723. tmp_tfm->key_size = key_size;
  1724. rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
  1725. tmp_tfm->cipher_name,
  1726. &tmp_tfm->key_size);
  1727. if (rc) {
  1728. printk(KERN_ERR "Error attempting to initialize key TFM "
  1729. "cipher with name = [%s]; rc = [%d]\n",
  1730. tmp_tfm->cipher_name, rc);
  1731. kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
  1732. if (key_tfm != NULL)
  1733. (*key_tfm) = NULL;
  1734. goto out;
  1735. }
  1736. list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
  1737. out:
  1738. return rc;
  1739. }
  1740. /**
  1741. * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
  1742. * @cipher_name: the name of the cipher to search for
  1743. * @key_tfm: set to corresponding tfm if found
  1744. *
  1745. * Searches for cached key_tfm matching @cipher_name
  1746. * Must be called with &key_tfm_list_mutex held
  1747. * Returns 1 if found, with @key_tfm set
  1748. * Returns 0 if not found, with @key_tfm set to NULL
  1749. */
  1750. int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
  1751. {
  1752. struct ecryptfs_key_tfm *tmp_key_tfm;
  1753. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1754. list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
  1755. if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
  1756. if (key_tfm)
  1757. (*key_tfm) = tmp_key_tfm;
  1758. return 1;
  1759. }
  1760. }
  1761. if (key_tfm)
  1762. (*key_tfm) = NULL;
  1763. return 0;
  1764. }
  1765. /**
  1766. * ecryptfs_get_tfm_and_mutex_for_cipher_name
  1767. *
  1768. * @tfm: set to cached tfm found, or new tfm created
  1769. * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
  1770. * @cipher_name: the name of the cipher to search for and/or add
  1771. *
  1772. * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
  1773. * Searches for cached item first, and creates new if not found.
  1774. * Returns 0 on success, non-zero if adding new cipher failed
  1775. */
  1776. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
  1777. struct mutex **tfm_mutex,
  1778. char *cipher_name)
  1779. {
  1780. struct ecryptfs_key_tfm *key_tfm;
  1781. int rc = 0;
  1782. (*tfm) = NULL;
  1783. (*tfm_mutex) = NULL;
  1784. mutex_lock(&key_tfm_list_mutex);
  1785. if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
  1786. rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
  1787. if (rc) {
  1788. printk(KERN_ERR "Error adding new key_tfm to list; "
  1789. "rc = [%d]\n", rc);
  1790. goto out;
  1791. }
  1792. }
  1793. mutex_unlock(&key_tfm_list_mutex);
  1794. (*tfm) = key_tfm->key_tfm;
  1795. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1796. out:
  1797. return rc;
  1798. }