crypto.c 54 KB

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