crypto.c 67 KB

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