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