crypto.c 60 KB

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