crypto.c 56 KB

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