crypto.c 64 KB

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