crypto.c 63 KB

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