crypto.c 58 KB

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