crypto.c 66 KB

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