crypto.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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;
  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;
  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;
  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;
  610. }
  611. extent_offset++;
  612. }
  613. out:
  614. kfree(enc_extent_virt);
  615. return rc;
  616. }
  617. /**
  618. * decrypt_scatterlist
  619. * @crypt_stat: Cryptographic context
  620. * @dest_sg: The destination scatterlist to decrypt into
  621. * @src_sg: The source scatterlist to decrypt from
  622. * @size: The number of bytes to decrypt
  623. * @iv: The initialization vector to use for the decryption
  624. *
  625. * Returns the number of bytes decrypted; negative value on error
  626. */
  627. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  628. struct scatterlist *dest_sg,
  629. struct scatterlist *src_sg, int size,
  630. unsigned char *iv)
  631. {
  632. struct blkcipher_desc desc = {
  633. .tfm = crypt_stat->tfm,
  634. .info = iv,
  635. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  636. };
  637. int rc = 0;
  638. /* Consider doing this once, when the file is opened */
  639. mutex_lock(&crypt_stat->cs_tfm_mutex);
  640. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  641. crypt_stat->key_size);
  642. if (rc) {
  643. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  644. rc);
  645. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  646. rc = -EINVAL;
  647. goto out;
  648. }
  649. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  650. rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
  651. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  652. if (rc) {
  653. ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
  654. rc);
  655. goto out;
  656. }
  657. rc = size;
  658. out:
  659. return rc;
  660. }
  661. /**
  662. * ecryptfs_encrypt_page_offset
  663. * @crypt_stat: The cryptographic context
  664. * @dst_page: The page to encrypt into
  665. * @dst_offset: The offset in the page to encrypt into
  666. * @src_page: The page to encrypt from
  667. * @src_offset: The offset in the page to encrypt from
  668. * @size: The number of bytes to encrypt
  669. * @iv: The initialization vector to use for the encryption
  670. *
  671. * Returns the number of bytes encrypted
  672. */
  673. static int
  674. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  675. struct page *dst_page, int dst_offset,
  676. struct page *src_page, int src_offset, int size,
  677. unsigned char *iv)
  678. {
  679. struct scatterlist src_sg, dst_sg;
  680. src_sg.page = src_page;
  681. src_sg.offset = src_offset;
  682. src_sg.length = size;
  683. dst_sg.page = dst_page;
  684. dst_sg.offset = dst_offset;
  685. dst_sg.length = size;
  686. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  687. }
  688. /**
  689. * ecryptfs_decrypt_page_offset
  690. * @crypt_stat: The cryptographic context
  691. * @dst_page: The page to decrypt into
  692. * @dst_offset: The offset in the page to decrypt into
  693. * @src_page: The page to decrypt from
  694. * @src_offset: The offset in the page to decrypt from
  695. * @size: The number of bytes to decrypt
  696. * @iv: The initialization vector to use for the decryption
  697. *
  698. * Returns the number of bytes decrypted
  699. */
  700. static int
  701. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  702. struct page *dst_page, int dst_offset,
  703. struct page *src_page, int src_offset, int size,
  704. unsigned char *iv)
  705. {
  706. struct scatterlist src_sg, dst_sg;
  707. src_sg.page = src_page;
  708. src_sg.offset = src_offset;
  709. src_sg.length = size;
  710. dst_sg.page = dst_page;
  711. dst_sg.offset = dst_offset;
  712. dst_sg.length = size;
  713. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  714. }
  715. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  716. /**
  717. * ecryptfs_init_crypt_ctx
  718. * @crypt_stat: Uninitilized crypt stats structure
  719. *
  720. * Initialize the crypto context.
  721. *
  722. * TODO: Performance: Keep a cache of initialized cipher contexts;
  723. * only init if needed
  724. */
  725. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  726. {
  727. char *full_alg_name;
  728. int rc = -EINVAL;
  729. if (!crypt_stat->cipher) {
  730. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  731. goto out;
  732. }
  733. ecryptfs_printk(KERN_DEBUG,
  734. "Initializing cipher [%s]; strlen = [%d]; "
  735. "key_size_bits = [%d]\n",
  736. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  737. crypt_stat->key_size << 3);
  738. if (crypt_stat->tfm) {
  739. rc = 0;
  740. goto out;
  741. }
  742. mutex_lock(&crypt_stat->cs_tfm_mutex);
  743. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  744. crypt_stat->cipher, "cbc");
  745. if (rc)
  746. goto out;
  747. crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  748. CRYPTO_ALG_ASYNC);
  749. kfree(full_alg_name);
  750. if (IS_ERR(crypt_stat->tfm)) {
  751. rc = PTR_ERR(crypt_stat->tfm);
  752. ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
  753. "Error initializing cipher [%s]\n",
  754. crypt_stat->cipher);
  755. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  756. goto out;
  757. }
  758. crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  759. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  760. rc = 0;
  761. out:
  762. return rc;
  763. }
  764. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  765. {
  766. int extent_size_tmp;
  767. crypt_stat->extent_mask = 0xFFFFFFFF;
  768. crypt_stat->extent_shift = 0;
  769. if (crypt_stat->extent_size == 0)
  770. return;
  771. extent_size_tmp = crypt_stat->extent_size;
  772. while ((extent_size_tmp & 0x01) == 0) {
  773. extent_size_tmp >>= 1;
  774. crypt_stat->extent_mask <<= 1;
  775. crypt_stat->extent_shift++;
  776. }
  777. }
  778. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  779. {
  780. /* Default values; may be overwritten as we are parsing the
  781. * packets. */
  782. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  783. set_extent_mask_and_shift(crypt_stat);
  784. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  785. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  786. crypt_stat->num_header_extents_at_front = 0;
  787. else {
  788. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
  789. crypt_stat->num_header_extents_at_front =
  790. (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
  791. / crypt_stat->extent_size);
  792. else
  793. crypt_stat->num_header_extents_at_front =
  794. (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  795. }
  796. }
  797. /**
  798. * ecryptfs_compute_root_iv
  799. * @crypt_stats
  800. *
  801. * On error, sets the root IV to all 0's.
  802. */
  803. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
  804. {
  805. int rc = 0;
  806. char dst[MD5_DIGEST_SIZE];
  807. BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
  808. BUG_ON(crypt_stat->iv_bytes <= 0);
  809. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  810. rc = -EINVAL;
  811. ecryptfs_printk(KERN_WARNING, "Session key not valid; "
  812. "cannot generate root IV\n");
  813. goto out;
  814. }
  815. rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
  816. crypt_stat->key_size);
  817. if (rc) {
  818. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  819. "MD5 while generating root IV\n");
  820. goto out;
  821. }
  822. memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
  823. out:
  824. if (rc) {
  825. memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
  826. crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
  827. }
  828. return rc;
  829. }
  830. static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
  831. {
  832. get_random_bytes(crypt_stat->key, crypt_stat->key_size);
  833. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  834. ecryptfs_compute_root_iv(crypt_stat);
  835. if (unlikely(ecryptfs_verbosity > 0)) {
  836. ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
  837. ecryptfs_dump_hex(crypt_stat->key,
  838. crypt_stat->key_size);
  839. }
  840. }
  841. /**
  842. * ecryptfs_copy_mount_wide_flags_to_inode_flags
  843. * @crypt_stat: The inode's cryptographic context
  844. * @mount_crypt_stat: The mount point's cryptographic context
  845. *
  846. * This function propagates the mount-wide flags to individual inode
  847. * flags.
  848. */
  849. static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
  850. struct ecryptfs_crypt_stat *crypt_stat,
  851. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  852. {
  853. if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
  854. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  855. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  856. crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
  857. }
  858. static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
  859. struct ecryptfs_crypt_stat *crypt_stat,
  860. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  861. {
  862. struct ecryptfs_global_auth_tok *global_auth_tok;
  863. int rc = 0;
  864. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  865. list_for_each_entry(global_auth_tok,
  866. &mount_crypt_stat->global_auth_tok_list,
  867. mount_crypt_stat_list) {
  868. rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
  869. if (rc) {
  870. printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
  871. mutex_unlock(
  872. &mount_crypt_stat->global_auth_tok_list_mutex);
  873. goto out;
  874. }
  875. }
  876. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  877. out:
  878. return rc;
  879. }
  880. /**
  881. * ecryptfs_set_default_crypt_stat_vals
  882. * @crypt_stat: The inode's cryptographic context
  883. * @mount_crypt_stat: The mount point's cryptographic context
  884. *
  885. * Default values in the event that policy does not override them.
  886. */
  887. static void ecryptfs_set_default_crypt_stat_vals(
  888. struct ecryptfs_crypt_stat *crypt_stat,
  889. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  890. {
  891. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  892. mount_crypt_stat);
  893. ecryptfs_set_default_sizes(crypt_stat);
  894. strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
  895. crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
  896. crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
  897. crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
  898. crypt_stat->mount_crypt_stat = mount_crypt_stat;
  899. }
  900. /**
  901. * ecryptfs_new_file_context
  902. * @ecryptfs_dentry: The eCryptfs dentry
  903. *
  904. * If the crypto context for the file has not yet been established,
  905. * this is where we do that. Establishing a new crypto context
  906. * involves the following decisions:
  907. * - What cipher to use?
  908. * - What set of authentication tokens to use?
  909. * Here we just worry about getting enough information into the
  910. * authentication tokens so that we know that they are available.
  911. * We associate the available authentication tokens with the new file
  912. * via the set of signatures in the crypt_stat struct. Later, when
  913. * the headers are actually written out, we may again defer to
  914. * userspace to perform the encryption of the session key; for the
  915. * foreseeable future, this will be the case with public key packets.
  916. *
  917. * Returns zero on success; non-zero otherwise
  918. */
  919. int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
  920. {
  921. struct ecryptfs_crypt_stat *crypt_stat =
  922. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  923. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  924. &ecryptfs_superblock_to_private(
  925. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  926. int cipher_name_len;
  927. int rc = 0;
  928. ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
  929. crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
  930. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  931. mount_crypt_stat);
  932. rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
  933. mount_crypt_stat);
  934. if (rc) {
  935. printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
  936. "to the inode key sigs; rc = [%d]\n", rc);
  937. goto out;
  938. }
  939. cipher_name_len =
  940. strlen(mount_crypt_stat->global_default_cipher_name);
  941. memcpy(crypt_stat->cipher,
  942. mount_crypt_stat->global_default_cipher_name,
  943. cipher_name_len);
  944. crypt_stat->cipher[cipher_name_len] = '\0';
  945. crypt_stat->key_size =
  946. mount_crypt_stat->global_default_cipher_key_size;
  947. ecryptfs_generate_new_key(crypt_stat);
  948. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  949. if (rc)
  950. ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
  951. "context for cipher [%s]: rc = [%d]\n",
  952. crypt_stat->cipher, rc);
  953. out:
  954. return rc;
  955. }
  956. /**
  957. * contains_ecryptfs_marker - check for the ecryptfs marker
  958. * @data: The data block in which to check
  959. *
  960. * Returns one if marker found; zero if not found
  961. */
  962. static int contains_ecryptfs_marker(char *data)
  963. {
  964. u32 m_1, m_2;
  965. memcpy(&m_1, data, 4);
  966. m_1 = be32_to_cpu(m_1);
  967. memcpy(&m_2, (data + 4), 4);
  968. m_2 = be32_to_cpu(m_2);
  969. if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
  970. return 1;
  971. ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
  972. "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
  973. MAGIC_ECRYPTFS_MARKER);
  974. ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
  975. "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
  976. return 0;
  977. }
  978. struct ecryptfs_flag_map_elem {
  979. u32 file_flag;
  980. u32 local_flag;
  981. };
  982. /* Add support for additional flags by adding elements here. */
  983. static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
  984. {0x00000001, ECRYPTFS_ENABLE_HMAC},
  985. {0x00000002, ECRYPTFS_ENCRYPTED},
  986. {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
  987. };
  988. /**
  989. * ecryptfs_process_flags
  990. * @crypt_stat: The cryptographic context
  991. * @page_virt: Source data to be parsed
  992. * @bytes_read: Updated with the number of bytes read
  993. *
  994. * Returns zero on success; non-zero if the flag set is invalid
  995. */
  996. static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
  997. char *page_virt, int *bytes_read)
  998. {
  999. int rc = 0;
  1000. int i;
  1001. u32 flags;
  1002. memcpy(&flags, page_virt, 4);
  1003. flags = be32_to_cpu(flags);
  1004. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1005. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1006. if (flags & ecryptfs_flag_map[i].file_flag) {
  1007. crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
  1008. } else
  1009. crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
  1010. /* Version is in top 8 bits of the 32-bit flag vector */
  1011. crypt_stat->file_version = ((flags >> 24) & 0xFF);
  1012. (*bytes_read) = 4;
  1013. return rc;
  1014. }
  1015. /**
  1016. * write_ecryptfs_marker
  1017. * @page_virt: The pointer to in a page to begin writing the marker
  1018. * @written: Number of bytes written
  1019. *
  1020. * Marker = 0x3c81b7f5
  1021. */
  1022. static void write_ecryptfs_marker(char *page_virt, size_t *written)
  1023. {
  1024. u32 m_1, m_2;
  1025. get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1026. m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
  1027. m_1 = cpu_to_be32(m_1);
  1028. memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1029. m_2 = cpu_to_be32(m_2);
  1030. memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
  1031. (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1032. (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1033. }
  1034. static void
  1035. write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
  1036. size_t *written)
  1037. {
  1038. u32 flags = 0;
  1039. int i;
  1040. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1041. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1042. if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
  1043. flags |= ecryptfs_flag_map[i].file_flag;
  1044. /* Version is in top 8 bits of the 32-bit flag vector */
  1045. flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
  1046. flags = cpu_to_be32(flags);
  1047. memcpy(page_virt, &flags, 4);
  1048. (*written) = 4;
  1049. }
  1050. struct ecryptfs_cipher_code_str_map_elem {
  1051. char cipher_str[16];
  1052. u16 cipher_code;
  1053. };
  1054. /* Add support for additional ciphers by adding elements here. The
  1055. * cipher_code is whatever OpenPGP applicatoins use to identify the
  1056. * ciphers. List in order of probability. */
  1057. static struct ecryptfs_cipher_code_str_map_elem
  1058. ecryptfs_cipher_code_str_map[] = {
  1059. {"aes",RFC2440_CIPHER_AES_128 },
  1060. {"blowfish", RFC2440_CIPHER_BLOWFISH},
  1061. {"des3_ede", RFC2440_CIPHER_DES3_EDE},
  1062. {"cast5", RFC2440_CIPHER_CAST_5},
  1063. {"twofish", RFC2440_CIPHER_TWOFISH},
  1064. {"cast6", RFC2440_CIPHER_CAST_6},
  1065. {"aes", RFC2440_CIPHER_AES_192},
  1066. {"aes", RFC2440_CIPHER_AES_256}
  1067. };
  1068. /**
  1069. * ecryptfs_code_for_cipher_string
  1070. * @crypt_stat: The cryptographic context
  1071. *
  1072. * Returns zero on no match, or the cipher code on match
  1073. */
  1074. u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
  1075. {
  1076. int i;
  1077. u16 code = 0;
  1078. struct ecryptfs_cipher_code_str_map_elem *map =
  1079. ecryptfs_cipher_code_str_map;
  1080. if (strcmp(crypt_stat->cipher, "aes") == 0) {
  1081. switch (crypt_stat->key_size) {
  1082. case 16:
  1083. code = RFC2440_CIPHER_AES_128;
  1084. break;
  1085. case 24:
  1086. code = RFC2440_CIPHER_AES_192;
  1087. break;
  1088. case 32:
  1089. code = RFC2440_CIPHER_AES_256;
  1090. }
  1091. } else {
  1092. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1093. if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
  1094. code = map[i].cipher_code;
  1095. break;
  1096. }
  1097. }
  1098. return code;
  1099. }
  1100. /**
  1101. * ecryptfs_cipher_code_to_string
  1102. * @str: Destination to write out the cipher name
  1103. * @cipher_code: The code to convert to cipher name string
  1104. *
  1105. * Returns zero on success
  1106. */
  1107. int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
  1108. {
  1109. int rc = 0;
  1110. int i;
  1111. str[0] = '\0';
  1112. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1113. if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
  1114. strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
  1115. if (str[0] == '\0') {
  1116. ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
  1117. "[%d]\n", cipher_code);
  1118. rc = -EINVAL;
  1119. }
  1120. return rc;
  1121. }
  1122. int ecryptfs_read_and_validate_header_region(char *data,
  1123. struct inode *ecryptfs_inode)
  1124. {
  1125. struct ecryptfs_crypt_stat *crypt_stat =
  1126. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  1127. int rc;
  1128. rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
  1129. ecryptfs_inode);
  1130. if (rc) {
  1131. printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
  1132. __FUNCTION__, rc);
  1133. goto out;
  1134. }
  1135. if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
  1136. rc = -EINVAL;
  1137. ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
  1138. }
  1139. out:
  1140. return rc;
  1141. }
  1142. void
  1143. ecryptfs_write_header_metadata(char *virt,
  1144. struct ecryptfs_crypt_stat *crypt_stat,
  1145. size_t *written)
  1146. {
  1147. u32 header_extent_size;
  1148. u16 num_header_extents_at_front;
  1149. header_extent_size = (u32)crypt_stat->extent_size;
  1150. num_header_extents_at_front =
  1151. (u16)crypt_stat->num_header_extents_at_front;
  1152. header_extent_size = cpu_to_be32(header_extent_size);
  1153. memcpy(virt, &header_extent_size, 4);
  1154. virt += 4;
  1155. num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
  1156. memcpy(virt, &num_header_extents_at_front, 2);
  1157. (*written) = 6;
  1158. }
  1159. struct kmem_cache *ecryptfs_header_cache_0;
  1160. struct kmem_cache *ecryptfs_header_cache_1;
  1161. struct kmem_cache *ecryptfs_header_cache_2;
  1162. /**
  1163. * ecryptfs_write_headers_virt
  1164. * @page_virt: The virtual address to write the headers to
  1165. * @size: Set to the number of bytes written by this function
  1166. * @crypt_stat: The cryptographic context
  1167. * @ecryptfs_dentry: The eCryptfs dentry
  1168. *
  1169. * Format version: 1
  1170. *
  1171. * Header Extent:
  1172. * Octets 0-7: Unencrypted file size (big-endian)
  1173. * Octets 8-15: eCryptfs special marker
  1174. * Octets 16-19: Flags
  1175. * Octet 16: File format version number (between 0 and 255)
  1176. * Octets 17-18: Reserved
  1177. * Octet 19: Bit 1 (lsb): Reserved
  1178. * Bit 2: Encrypted?
  1179. * Bits 3-8: Reserved
  1180. * Octets 20-23: Header extent size (big-endian)
  1181. * Octets 24-25: Number of header extents at front of file
  1182. * (big-endian)
  1183. * Octet 26: Begin RFC 2440 authentication token packet set
  1184. * Data Extent 0:
  1185. * Lower data (CBC encrypted)
  1186. * Data Extent 1:
  1187. * Lower data (CBC encrypted)
  1188. * ...
  1189. *
  1190. * Returns zero on success
  1191. */
  1192. static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
  1193. struct ecryptfs_crypt_stat *crypt_stat,
  1194. struct dentry *ecryptfs_dentry)
  1195. {
  1196. int rc;
  1197. size_t written;
  1198. size_t offset;
  1199. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1200. write_ecryptfs_marker((page_virt + offset), &written);
  1201. offset += written;
  1202. write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
  1203. offset += written;
  1204. ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
  1205. &written);
  1206. offset += written;
  1207. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1208. ecryptfs_dentry, &written,
  1209. PAGE_CACHE_SIZE - offset);
  1210. if (rc)
  1211. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1212. "set; rc = [%d]\n", rc);
  1213. if (size) {
  1214. offset += written;
  1215. *size = offset;
  1216. }
  1217. return rc;
  1218. }
  1219. static int
  1220. ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
  1221. struct dentry *ecryptfs_dentry,
  1222. char *page_virt)
  1223. {
  1224. int current_header_page;
  1225. int header_pages;
  1226. int rc;
  1227. rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
  1228. 0, PAGE_CACHE_SIZE);
  1229. if (rc) {
  1230. printk(KERN_ERR "%s: Error attempting to write header "
  1231. "information to lower file; rc = [%d]\n", __FUNCTION__,
  1232. rc);
  1233. goto out;
  1234. }
  1235. header_pages = ((crypt_stat->extent_size
  1236. * crypt_stat->num_header_extents_at_front)
  1237. / PAGE_CACHE_SIZE);
  1238. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1239. current_header_page = 1;
  1240. while (current_header_page < header_pages) {
  1241. loff_t offset;
  1242. offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT);
  1243. if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
  1244. page_virt, offset,
  1245. PAGE_CACHE_SIZE))) {
  1246. printk(KERN_ERR "%s: Error attempting to write header "
  1247. "information to lower file; rc = [%d]\n",
  1248. __FUNCTION__, rc);
  1249. goto out;
  1250. }
  1251. current_header_page++;
  1252. }
  1253. out:
  1254. return rc;
  1255. }
  1256. static int
  1257. ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
  1258. struct ecryptfs_crypt_stat *crypt_stat,
  1259. char *page_virt, size_t size)
  1260. {
  1261. int rc;
  1262. rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
  1263. size, 0);
  1264. return rc;
  1265. }
  1266. /**
  1267. * ecryptfs_write_metadata
  1268. * @ecryptfs_dentry: The eCryptfs dentry
  1269. *
  1270. * Write the file headers out. This will likely involve a userspace
  1271. * callout, in which the session key is encrypted with one or more
  1272. * public keys and/or the passphrase necessary to do the encryption is
  1273. * retrieved via a prompt. Exactly what happens at this point should
  1274. * be policy-dependent.
  1275. *
  1276. * TODO: Support header information spanning multiple pages
  1277. *
  1278. * Returns zero on success; non-zero on error
  1279. */
  1280. int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
  1281. {
  1282. struct ecryptfs_crypt_stat *crypt_stat =
  1283. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  1284. char *page_virt;
  1285. size_t size = 0;
  1286. int rc = 0;
  1287. if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  1288. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  1289. printk(KERN_ERR "Key is invalid; bailing out\n");
  1290. rc = -EINVAL;
  1291. goto out;
  1292. }
  1293. } else {
  1294. rc = -EINVAL;
  1295. ecryptfs_printk(KERN_WARNING,
  1296. "Called with crypt_stat->encrypted == 0\n");
  1297. goto out;
  1298. }
  1299. /* Released in this function */
  1300. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
  1301. if (!page_virt) {
  1302. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  1303. rc = -ENOMEM;
  1304. goto out;
  1305. }
  1306. rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
  1307. ecryptfs_dentry);
  1308. if (unlikely(rc)) {
  1309. ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
  1310. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1311. goto out_free;
  1312. }
  1313. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1314. rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
  1315. crypt_stat, page_virt,
  1316. size);
  1317. else
  1318. rc = ecryptfs_write_metadata_to_contents(crypt_stat,
  1319. ecryptfs_dentry,
  1320. page_virt);
  1321. if (rc) {
  1322. printk(KERN_ERR "Error writing metadata out to lower file; "
  1323. "rc = [%d]\n", rc);
  1324. goto out_free;
  1325. }
  1326. out_free:
  1327. kmem_cache_free(ecryptfs_header_cache_0, page_virt);
  1328. out:
  1329. return rc;
  1330. }
  1331. #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
  1332. #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
  1333. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1334. char *virt, int *bytes_read,
  1335. int validate_header_size)
  1336. {
  1337. int rc = 0;
  1338. u32 header_extent_size;
  1339. u16 num_header_extents_at_front;
  1340. memcpy(&header_extent_size, virt, sizeof(u32));
  1341. header_extent_size = be32_to_cpu(header_extent_size);
  1342. virt += sizeof(u32);
  1343. memcpy(&num_header_extents_at_front, virt, sizeof(u16));
  1344. num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
  1345. crypt_stat->num_header_extents_at_front =
  1346. (int)num_header_extents_at_front;
  1347. (*bytes_read) = (sizeof(u32) + sizeof(u16));
  1348. if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
  1349. && ((crypt_stat->extent_size
  1350. * crypt_stat->num_header_extents_at_front)
  1351. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
  1352. rc = -EINVAL;
  1353. printk(KERN_WARNING "Invalid number of header extents: [%zd]\n",
  1354. crypt_stat->num_header_extents_at_front);
  1355. }
  1356. return rc;
  1357. }
  1358. /**
  1359. * set_default_header_data
  1360. * @crypt_stat: The cryptographic context
  1361. *
  1362. * For version 0 file format; this function is only for backwards
  1363. * compatibility for files created with the prior versions of
  1364. * eCryptfs.
  1365. */
  1366. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1367. {
  1368. crypt_stat->num_header_extents_at_front = 2;
  1369. }
  1370. /**
  1371. * ecryptfs_read_headers_virt
  1372. * @page_virt: The virtual address into which to read the headers
  1373. * @crypt_stat: The cryptographic context
  1374. * @ecryptfs_dentry: The eCryptfs dentry
  1375. * @validate_header_size: Whether to validate the header size while reading
  1376. *
  1377. * Read/parse the header data. The header format is detailed in the
  1378. * comment block for the ecryptfs_write_headers_virt() function.
  1379. *
  1380. * Returns zero on success
  1381. */
  1382. static int ecryptfs_read_headers_virt(char *page_virt,
  1383. struct ecryptfs_crypt_stat *crypt_stat,
  1384. struct dentry *ecryptfs_dentry,
  1385. int validate_header_size)
  1386. {
  1387. int rc = 0;
  1388. int offset;
  1389. int bytes_read;
  1390. ecryptfs_set_default_sizes(crypt_stat);
  1391. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1392. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1393. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1394. rc = contains_ecryptfs_marker(page_virt + offset);
  1395. if (rc == 0) {
  1396. rc = -EINVAL;
  1397. goto out;
  1398. }
  1399. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1400. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1401. &bytes_read);
  1402. if (rc) {
  1403. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1404. goto out;
  1405. }
  1406. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1407. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1408. "file version [%d] is supported by this "
  1409. "version of eCryptfs\n",
  1410. crypt_stat->file_version,
  1411. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1412. rc = -EINVAL;
  1413. goto out;
  1414. }
  1415. offset += bytes_read;
  1416. if (crypt_stat->file_version >= 1) {
  1417. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1418. &bytes_read, validate_header_size);
  1419. if (rc) {
  1420. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1421. "metadata; rc = [%d]\n", rc);
  1422. }
  1423. offset += bytes_read;
  1424. } else
  1425. set_default_header_data(crypt_stat);
  1426. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1427. ecryptfs_dentry);
  1428. out:
  1429. return rc;
  1430. }
  1431. /**
  1432. * ecryptfs_read_xattr_region
  1433. * @page_virt: The vitual address into which to read the xattr data
  1434. * @ecryptfs_inode: The eCryptfs inode
  1435. *
  1436. * Attempts to read the crypto metadata from the extended attribute
  1437. * region of the lower file.
  1438. *
  1439. * Returns zero on success; non-zero on error
  1440. */
  1441. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
  1442. {
  1443. struct dentry *lower_dentry =
  1444. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  1445. ssize_t size;
  1446. int rc = 0;
  1447. size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
  1448. page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
  1449. if (size < 0) {
  1450. printk(KERN_ERR "Error attempting to read the [%s] "
  1451. "xattr from the lower file; return value = [%zd]\n",
  1452. ECRYPTFS_XATTR_NAME, size);
  1453. rc = -EINVAL;
  1454. goto out;
  1455. }
  1456. out:
  1457. return rc;
  1458. }
  1459. int ecryptfs_read_and_validate_xattr_region(char *page_virt,
  1460. struct dentry *ecryptfs_dentry)
  1461. {
  1462. int rc;
  1463. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
  1464. if (rc)
  1465. goto out;
  1466. if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
  1467. printk(KERN_WARNING "Valid data found in [%s] xattr, but "
  1468. "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
  1469. rc = -EINVAL;
  1470. }
  1471. out:
  1472. return rc;
  1473. }
  1474. /**
  1475. * ecryptfs_read_metadata
  1476. *
  1477. * Common entry point for reading file metadata. From here, we could
  1478. * retrieve the header information from the header region of the file,
  1479. * the xattr region of the file, or some other repostory that is
  1480. * stored separately from the file itself. The current implementation
  1481. * supports retrieving the metadata information from the file contents
  1482. * and from the xattr region.
  1483. *
  1484. * Returns zero if valid headers found and parsed; non-zero otherwise
  1485. */
  1486. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
  1487. {
  1488. int rc = 0;
  1489. char *page_virt = NULL;
  1490. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  1491. struct ecryptfs_crypt_stat *crypt_stat =
  1492. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1493. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1494. &ecryptfs_superblock_to_private(
  1495. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1496. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  1497. mount_crypt_stat);
  1498. /* Read the first page from the underlying file */
  1499. page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
  1500. if (!page_virt) {
  1501. rc = -ENOMEM;
  1502. printk(KERN_ERR "%s: Unable to allocate page_virt\n",
  1503. __FUNCTION__);
  1504. goto out;
  1505. }
  1506. rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
  1507. ecryptfs_inode);
  1508. if (!rc)
  1509. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1510. ecryptfs_dentry,
  1511. ECRYPTFS_VALIDATE_HEADER_SIZE);
  1512. if (rc) {
  1513. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
  1514. if (rc) {
  1515. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1516. "file header region or xattr region\n");
  1517. rc = -EINVAL;
  1518. goto out;
  1519. }
  1520. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1521. ecryptfs_dentry,
  1522. ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
  1523. if (rc) {
  1524. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1525. "file xattr region either\n");
  1526. rc = -EINVAL;
  1527. }
  1528. if (crypt_stat->mount_crypt_stat->flags
  1529. & ECRYPTFS_XATTR_METADATA_ENABLED) {
  1530. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  1531. } else {
  1532. printk(KERN_WARNING "Attempt to access file with "
  1533. "crypto metadata only in the extended attribute "
  1534. "region, but eCryptfs was mounted without "
  1535. "xattr support enabled. eCryptfs will not treat "
  1536. "this like an encrypted file.\n");
  1537. rc = -EINVAL;
  1538. }
  1539. }
  1540. out:
  1541. if (page_virt) {
  1542. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1543. kmem_cache_free(ecryptfs_header_cache_1, page_virt);
  1544. }
  1545. return rc;
  1546. }
  1547. /**
  1548. * ecryptfs_encode_filename - converts a plaintext file name to cipher text
  1549. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1550. * @name: The plaintext name
  1551. * @length: The length of the plaintext
  1552. * @encoded_name: The encypted name
  1553. *
  1554. * Encrypts and encodes a filename into something that constitutes a
  1555. * valid filename for a filesystem, with printable characters.
  1556. *
  1557. * We assume that we have a properly initialized crypto context,
  1558. * pointed to by crypt_stat->tfm.
  1559. *
  1560. * TODO: Implement filename decoding and decryption here, in place of
  1561. * memcpy. We are keeping the framework around for now to (1)
  1562. * facilitate testing of the components needed to implement filename
  1563. * encryption and (2) to provide a code base from which other
  1564. * developers in the community can easily implement this feature.
  1565. *
  1566. * Returns the length of encoded filename; negative if error
  1567. */
  1568. int
  1569. ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1570. const char *name, int length, char **encoded_name)
  1571. {
  1572. int error = 0;
  1573. (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
  1574. if (!(*encoded_name)) {
  1575. error = -ENOMEM;
  1576. goto out;
  1577. }
  1578. /* TODO: Filename encryption is a scheduled feature for a
  1579. * future version of eCryptfs. This function is here only for
  1580. * the purpose of providing a framework for other developers
  1581. * to easily implement filename encryption. Hint: Replace this
  1582. * memcpy() with a call to encrypt and encode the
  1583. * filename, the set the length accordingly. */
  1584. memcpy((void *)(*encoded_name), (void *)name, length);
  1585. (*encoded_name)[length] = '\0';
  1586. error = length + 1;
  1587. out:
  1588. return error;
  1589. }
  1590. /**
  1591. * ecryptfs_decode_filename - converts the cipher text name to plaintext
  1592. * @crypt_stat: The crypt_stat struct associated with the file
  1593. * @name: The filename in cipher text
  1594. * @length: The length of the cipher text name
  1595. * @decrypted_name: The plaintext name
  1596. *
  1597. * Decodes and decrypts the filename.
  1598. *
  1599. * We assume that we have a properly initialized crypto context,
  1600. * pointed to by crypt_stat->tfm.
  1601. *
  1602. * TODO: Implement filename decoding and decryption here, in place of
  1603. * memcpy. We are keeping the framework around for now to (1)
  1604. * facilitate testing of the components needed to implement filename
  1605. * encryption and (2) to provide a code base from which other
  1606. * developers in the community can easily implement this feature.
  1607. *
  1608. * Returns the length of decoded filename; negative if error
  1609. */
  1610. int
  1611. ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1612. const char *name, int length, char **decrypted_name)
  1613. {
  1614. int error = 0;
  1615. (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
  1616. if (!(*decrypted_name)) {
  1617. error = -ENOMEM;
  1618. goto out;
  1619. }
  1620. /* TODO: Filename encryption is a scheduled feature for a
  1621. * future version of eCryptfs. This function is here only for
  1622. * the purpose of providing a framework for other developers
  1623. * to easily implement filename encryption. Hint: Replace this
  1624. * memcpy() with a call to decode and decrypt the
  1625. * filename, the set the length accordingly. */
  1626. memcpy((void *)(*decrypted_name), (void *)name, length);
  1627. (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
  1628. * in printing out the
  1629. * string in debug
  1630. * messages */
  1631. error = length;
  1632. out:
  1633. return error;
  1634. }
  1635. /**
  1636. * ecryptfs_process_key_cipher - Perform key cipher initialization.
  1637. * @key_tfm: Crypto context for key material, set by this function
  1638. * @cipher_name: Name of the cipher
  1639. * @key_size: Size of the key in bytes
  1640. *
  1641. * Returns zero on success. Any crypto_tfm structs allocated here
  1642. * should be released by other functions, such as on a superblock put
  1643. * event, regardless of whether this function succeeds for fails.
  1644. */
  1645. static int
  1646. ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
  1647. char *cipher_name, size_t *key_size)
  1648. {
  1649. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1650. char *full_alg_name;
  1651. int rc;
  1652. *key_tfm = NULL;
  1653. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1654. rc = -EINVAL;
  1655. printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
  1656. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1657. goto out;
  1658. }
  1659. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1660. "ecb");
  1661. if (rc)
  1662. goto out;
  1663. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1664. kfree(full_alg_name);
  1665. if (IS_ERR(*key_tfm)) {
  1666. rc = PTR_ERR(*key_tfm);
  1667. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1668. "[%s]; rc = [%d]\n", cipher_name, rc);
  1669. goto out;
  1670. }
  1671. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1672. if (*key_size == 0) {
  1673. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1674. *key_size = alg->max_keysize;
  1675. }
  1676. get_random_bytes(dummy_key, *key_size);
  1677. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1678. if (rc) {
  1679. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1680. "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
  1681. rc = -EINVAL;
  1682. goto out;
  1683. }
  1684. out:
  1685. return rc;
  1686. }
  1687. struct kmem_cache *ecryptfs_key_tfm_cache;
  1688. struct list_head key_tfm_list;
  1689. struct mutex key_tfm_list_mutex;
  1690. int ecryptfs_init_crypto(void)
  1691. {
  1692. mutex_init(&key_tfm_list_mutex);
  1693. INIT_LIST_HEAD(&key_tfm_list);
  1694. return 0;
  1695. }
  1696. int ecryptfs_destroy_crypto(void)
  1697. {
  1698. struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
  1699. mutex_lock(&key_tfm_list_mutex);
  1700. list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
  1701. key_tfm_list) {
  1702. list_del(&key_tfm->key_tfm_list);
  1703. if (key_tfm->key_tfm)
  1704. crypto_free_blkcipher(key_tfm->key_tfm);
  1705. kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
  1706. }
  1707. mutex_unlock(&key_tfm_list_mutex);
  1708. return 0;
  1709. }
  1710. int
  1711. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  1712. size_t key_size)
  1713. {
  1714. struct ecryptfs_key_tfm *tmp_tfm;
  1715. int rc = 0;
  1716. tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
  1717. if (key_tfm != NULL)
  1718. (*key_tfm) = tmp_tfm;
  1719. if (!tmp_tfm) {
  1720. rc = -ENOMEM;
  1721. printk(KERN_ERR "Error attempting to allocate from "
  1722. "ecryptfs_key_tfm_cache\n");
  1723. goto out;
  1724. }
  1725. mutex_init(&tmp_tfm->key_tfm_mutex);
  1726. strncpy(tmp_tfm->cipher_name, cipher_name,
  1727. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  1728. tmp_tfm->key_size = key_size;
  1729. rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
  1730. tmp_tfm->cipher_name,
  1731. &tmp_tfm->key_size);
  1732. if (rc) {
  1733. printk(KERN_ERR "Error attempting to initialize key TFM "
  1734. "cipher with name = [%s]; rc = [%d]\n",
  1735. tmp_tfm->cipher_name, rc);
  1736. kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
  1737. if (key_tfm != NULL)
  1738. (*key_tfm) = NULL;
  1739. goto out;
  1740. }
  1741. mutex_lock(&key_tfm_list_mutex);
  1742. list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
  1743. mutex_unlock(&key_tfm_list_mutex);
  1744. out:
  1745. return rc;
  1746. }
  1747. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
  1748. struct mutex **tfm_mutex,
  1749. char *cipher_name)
  1750. {
  1751. struct ecryptfs_key_tfm *key_tfm;
  1752. int rc = 0;
  1753. (*tfm) = NULL;
  1754. (*tfm_mutex) = NULL;
  1755. mutex_lock(&key_tfm_list_mutex);
  1756. list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
  1757. if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
  1758. (*tfm) = key_tfm->key_tfm;
  1759. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1760. mutex_unlock(&key_tfm_list_mutex);
  1761. goto out;
  1762. }
  1763. }
  1764. mutex_unlock(&key_tfm_list_mutex);
  1765. rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
  1766. if (rc) {
  1767. printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
  1768. rc);
  1769. goto out;
  1770. }
  1771. (*tfm) = key_tfm->key_tfm;
  1772. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1773. out:
  1774. return rc;
  1775. }