crypto.c 54 KB

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