crypto.c 56 KB

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