crypto.c 57 KB

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