crypto.c 62 KB

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