crypto.c 60 KB

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