crypto.c 67 KB

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