crypto.c 64 KB

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