crypto.c 62 KB

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