crypto.c 67 KB

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