crypto.c 63 KB

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