crypto.c 62 KB

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