crypto.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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 "ecryptfs_kernel.h"
  36. static int
  37. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  38. struct page *dst_page, int dst_offset,
  39. struct page *src_page, int src_offset, int size,
  40. unsigned char *iv);
  41. static int
  42. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  43. struct page *dst_page, int dst_offset,
  44. struct page *src_page, int src_offset, int size,
  45. unsigned char *iv);
  46. /**
  47. * ecryptfs_to_hex
  48. * @dst: Buffer to take hex character representation of contents of
  49. * src; must be at least of size (src_size * 2)
  50. * @src: Buffer to be converted to a hex string respresentation
  51. * @src_size: number of bytes to convert
  52. */
  53. void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
  54. {
  55. int x;
  56. for (x = 0; x < src_size; x++)
  57. sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
  58. }
  59. /**
  60. * ecryptfs_from_hex
  61. * @dst: Buffer to take the bytes from src hex; must be at least of
  62. * size (src_size / 2)
  63. * @src: Buffer to be converted from a hex string respresentation to raw value
  64. * @dst_size: size of dst buffer, or number of hex characters pairs to convert
  65. */
  66. void ecryptfs_from_hex(char *dst, char *src, int dst_size)
  67. {
  68. int x;
  69. char tmp[3] = { 0, };
  70. for (x = 0; x < dst_size; x++) {
  71. tmp[0] = src[x * 2];
  72. tmp[1] = src[x * 2 + 1];
  73. dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
  74. }
  75. }
  76. /**
  77. * ecryptfs_calculate_md5 - calculates the md5 of @src
  78. * @dst: Pointer to 16 bytes of allocated memory
  79. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  80. * @src: Data to be md5'd
  81. * @len: Length of @src
  82. *
  83. * Uses the allocated crypto context that crypt_stat references to
  84. * generate the MD5 sum of the contents of src.
  85. */
  86. static int ecryptfs_calculate_md5(char *dst,
  87. struct ecryptfs_crypt_stat *crypt_stat,
  88. char *src, int len)
  89. {
  90. struct scatterlist sg;
  91. struct hash_desc desc = {
  92. .tfm = crypt_stat->hash_tfm,
  93. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  94. };
  95. int rc = 0;
  96. mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
  97. sg_init_one(&sg, (u8 *)src, len);
  98. if (!desc.tfm) {
  99. desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
  100. CRYPTO_ALG_ASYNC);
  101. if (IS_ERR(desc.tfm)) {
  102. rc = PTR_ERR(desc.tfm);
  103. ecryptfs_printk(KERN_ERR, "Error attempting to "
  104. "allocate crypto context; rc = [%d]\n",
  105. rc);
  106. goto out;
  107. }
  108. crypt_stat->hash_tfm = desc.tfm;
  109. }
  110. crypto_hash_init(&desc);
  111. crypto_hash_update(&desc, &sg, len);
  112. crypto_hash_final(&desc, dst);
  113. mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
  114. out:
  115. return rc;
  116. }
  117. static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
  118. char *cipher_name,
  119. char *chaining_modifier)
  120. {
  121. int cipher_name_len = strlen(cipher_name);
  122. int chaining_modifier_len = strlen(chaining_modifier);
  123. int algified_name_len;
  124. int rc;
  125. algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
  126. (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
  127. if (!(*algified_name)) {
  128. rc = -ENOMEM;
  129. goto out;
  130. }
  131. snprintf((*algified_name), algified_name_len, "%s(%s)",
  132. chaining_modifier, cipher_name);
  133. rc = 0;
  134. out:
  135. return rc;
  136. }
  137. /**
  138. * ecryptfs_derive_iv
  139. * @iv: destination for the derived iv vale
  140. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  141. * @offset: Offset of the extent whose IV we are to derive
  142. *
  143. * Generate the initialization vector from the given root IV and page
  144. * offset.
  145. *
  146. * Returns zero on success; non-zero on error.
  147. */
  148. static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
  149. loff_t offset)
  150. {
  151. int rc = 0;
  152. char dst[MD5_DIGEST_SIZE];
  153. char src[ECRYPTFS_MAX_IV_BYTES + 16];
  154. if (unlikely(ecryptfs_verbosity > 0)) {
  155. ecryptfs_printk(KERN_DEBUG, "root iv:\n");
  156. ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
  157. }
  158. /* TODO: It is probably secure to just cast the least
  159. * significant bits of the root IV into an unsigned long and
  160. * add the offset to that rather than go through all this
  161. * hashing business. -Halcrow */
  162. memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
  163. memset((src + crypt_stat->iv_bytes), 0, 16);
  164. snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
  165. if (unlikely(ecryptfs_verbosity > 0)) {
  166. ecryptfs_printk(KERN_DEBUG, "source:\n");
  167. ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
  168. }
  169. rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
  170. (crypt_stat->iv_bytes + 16));
  171. if (rc) {
  172. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  173. "MD5 while generating IV for a page\n");
  174. goto out;
  175. }
  176. memcpy(iv, dst, crypt_stat->iv_bytes);
  177. if (unlikely(ecryptfs_verbosity > 0)) {
  178. ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
  179. ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
  180. }
  181. out:
  182. return rc;
  183. }
  184. /**
  185. * ecryptfs_init_crypt_stat
  186. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  187. *
  188. * Initialize the crypt_stat structure.
  189. */
  190. void
  191. ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  192. {
  193. memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  194. INIT_LIST_HEAD(&crypt_stat->keysig_list);
  195. mutex_init(&crypt_stat->keysig_list_mutex);
  196. mutex_init(&crypt_stat->cs_mutex);
  197. mutex_init(&crypt_stat->cs_tfm_mutex);
  198. mutex_init(&crypt_stat->cs_hash_tfm_mutex);
  199. crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
  200. }
  201. /**
  202. * ecryptfs_destroy_crypt_stat
  203. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  204. *
  205. * Releases all memory associated with a crypt_stat struct.
  206. */
  207. void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  208. {
  209. struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
  210. if (crypt_stat->tfm)
  211. crypto_free_blkcipher(crypt_stat->tfm);
  212. if (crypt_stat->hash_tfm)
  213. crypto_free_hash(crypt_stat->hash_tfm);
  214. mutex_lock(&crypt_stat->keysig_list_mutex);
  215. list_for_each_entry_safe(key_sig, key_sig_tmp,
  216. &crypt_stat->keysig_list, crypt_stat_list) {
  217. list_del(&key_sig->crypt_stat_list);
  218. kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
  219. }
  220. mutex_unlock(&crypt_stat->keysig_list_mutex);
  221. memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  222. }
  223. void ecryptfs_destroy_mount_crypt_stat(
  224. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  225. {
  226. struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
  227. if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
  228. return;
  229. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  230. list_for_each_entry_safe(auth_tok, auth_tok_tmp,
  231. &mount_crypt_stat->global_auth_tok_list,
  232. mount_crypt_stat_list) {
  233. list_del(&auth_tok->mount_crypt_stat_list);
  234. mount_crypt_stat->num_global_auth_toks--;
  235. if (auth_tok->global_auth_tok_key
  236. && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
  237. key_put(auth_tok->global_auth_tok_key);
  238. kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
  239. }
  240. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  241. memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
  242. }
  243. /**
  244. * virt_to_scatterlist
  245. * @addr: Virtual address
  246. * @size: Size of data; should be an even multiple of the block size
  247. * @sg: Pointer to scatterlist array; set to NULL to obtain only
  248. * the number of scatterlist structs required in array
  249. * @sg_size: Max array size
  250. *
  251. * Fills in a scatterlist array with page references for a passed
  252. * virtual address.
  253. *
  254. * Returns the number of scatterlist structs in array used
  255. */
  256. int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
  257. int sg_size)
  258. {
  259. int i = 0;
  260. struct page *pg;
  261. int offset;
  262. int remainder_of_page;
  263. sg_init_table(sg, sg_size);
  264. while (size > 0 && i < sg_size) {
  265. pg = virt_to_page(addr);
  266. offset = offset_in_page(addr);
  267. if (sg)
  268. sg_set_page(&sg[i], pg, 0, offset);
  269. remainder_of_page = PAGE_CACHE_SIZE - offset;
  270. if (size >= remainder_of_page) {
  271. if (sg)
  272. sg[i].length = remainder_of_page;
  273. addr += remainder_of_page;
  274. size -= remainder_of_page;
  275. } else {
  276. if (sg)
  277. sg[i].length = size;
  278. addr += size;
  279. size = 0;
  280. }
  281. i++;
  282. }
  283. if (size > 0)
  284. return -ENOMEM;
  285. return i;
  286. }
  287. /**
  288. * encrypt_scatterlist
  289. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  290. * @dest_sg: Destination of encrypted data
  291. * @src_sg: Data to be encrypted
  292. * @size: Length of data to be encrypted
  293. * @iv: iv to use during encryption
  294. *
  295. * Returns the number of bytes encrypted; negative value on error
  296. */
  297. static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  298. struct scatterlist *dest_sg,
  299. struct scatterlist *src_sg, int size,
  300. unsigned char *iv)
  301. {
  302. struct blkcipher_desc desc = {
  303. .tfm = crypt_stat->tfm,
  304. .info = iv,
  305. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  306. };
  307. int rc = 0;
  308. BUG_ON(!crypt_stat || !crypt_stat->tfm
  309. || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
  310. if (unlikely(ecryptfs_verbosity > 0)) {
  311. ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
  312. crypt_stat->key_size);
  313. ecryptfs_dump_hex(crypt_stat->key,
  314. crypt_stat->key_size);
  315. }
  316. /* Consider doing this once, when the file is opened */
  317. mutex_lock(&crypt_stat->cs_tfm_mutex);
  318. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  319. crypt_stat->key_size);
  320. if (rc) {
  321. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  322. rc);
  323. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  324. rc = -EINVAL;
  325. goto out;
  326. }
  327. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
  328. crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
  329. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  330. out:
  331. return rc;
  332. }
  333. /**
  334. * ecryptfs_lower_offset_for_extent
  335. *
  336. * Convert an eCryptfs page index into a lower byte offset
  337. */
  338. void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
  339. struct ecryptfs_crypt_stat *crypt_stat)
  340. {
  341. (*offset) = ((crypt_stat->extent_size
  342. * crypt_stat->num_header_extents_at_front)
  343. + (crypt_stat->extent_size * extent_num));
  344. }
  345. /**
  346. * ecryptfs_encrypt_extent
  347. * @enc_extent_page: Allocated page into which to encrypt the data in
  348. * @page
  349. * @crypt_stat: crypt_stat containing cryptographic context for the
  350. * encryption operation
  351. * @page: Page containing plaintext data extent to encrypt
  352. * @extent_offset: Page extent offset for use in generating IV
  353. *
  354. * Encrypts one extent of data.
  355. *
  356. * Return zero on success; non-zero otherwise
  357. */
  358. static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
  359. struct ecryptfs_crypt_stat *crypt_stat,
  360. struct page *page,
  361. unsigned long extent_offset)
  362. {
  363. loff_t extent_base;
  364. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  365. int rc;
  366. extent_base = (((loff_t)page->index)
  367. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  368. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  369. (extent_base + extent_offset));
  370. if (rc) {
  371. ecryptfs_printk(KERN_ERR, "Error attempting to "
  372. "derive IV for extent [0x%.16x]; "
  373. "rc = [%d]\n", (extent_base + extent_offset),
  374. rc);
  375. goto out;
  376. }
  377. if (unlikely(ecryptfs_verbosity > 0)) {
  378. ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
  379. "with iv:\n");
  380. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  381. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  382. "encryption:\n");
  383. ecryptfs_dump_hex((char *)
  384. (page_address(page)
  385. + (extent_offset * crypt_stat->extent_size)),
  386. 8);
  387. }
  388. rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
  389. page, (extent_offset
  390. * crypt_stat->extent_size),
  391. crypt_stat->extent_size, extent_iv);
  392. if (rc < 0) {
  393. printk(KERN_ERR "%s: Error attempting to encrypt page with "
  394. "page->index = [%ld], extent_offset = [%ld]; "
  395. "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
  396. rc);
  397. goto out;
  398. }
  399. rc = 0;
  400. if (unlikely(ecryptfs_verbosity > 0)) {
  401. ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
  402. "rc = [%d]\n", (extent_base + extent_offset),
  403. rc);
  404. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  405. "encryption:\n");
  406. ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
  407. }
  408. out:
  409. return rc;
  410. }
  411. /**
  412. * ecryptfs_encrypt_page
  413. * @page: Page mapped from the eCryptfs inode for the file; contains
  414. * decrypted content that needs to be encrypted (to a temporary
  415. * page; not in place) and written out to the lower file
  416. *
  417. * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
  418. * that eCryptfs pages may straddle the lower pages -- for instance,
  419. * if the file was created on a machine with an 8K page size
  420. * (resulting in an 8K header), and then the file is copied onto a
  421. * host with a 32K page size, then when reading page 0 of the eCryptfs
  422. * file, 24K of page 0 of the lower file will be read and decrypted,
  423. * and then 8K of page 1 of the lower file will be read and decrypted.
  424. *
  425. * Returns zero on success; negative on error
  426. */
  427. int ecryptfs_encrypt_page(struct page *page)
  428. {
  429. struct inode *ecryptfs_inode;
  430. struct ecryptfs_crypt_stat *crypt_stat;
  431. char *enc_extent_virt = NULL;
  432. struct page *enc_extent_page;
  433. loff_t extent_offset;
  434. int rc = 0;
  435. ecryptfs_inode = page->mapping->host;
  436. crypt_stat =
  437. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  438. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  439. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
  440. 0, PAGE_CACHE_SIZE);
  441. if (rc)
  442. printk(KERN_ERR "%s: Error attempting to copy "
  443. "page at index [%ld]\n", __FUNCTION__,
  444. page->index);
  445. goto out;
  446. }
  447. enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
  448. if (!enc_extent_virt) {
  449. rc = -ENOMEM;
  450. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  451. "encrypted extent\n");
  452. goto out;
  453. }
  454. enc_extent_page = virt_to_page(enc_extent_virt);
  455. for (extent_offset = 0;
  456. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  457. extent_offset++) {
  458. loff_t offset;
  459. rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
  460. extent_offset);
  461. if (rc) {
  462. printk(KERN_ERR "%s: Error encrypting extent; "
  463. "rc = [%d]\n", __FUNCTION__, rc);
  464. goto out;
  465. }
  466. ecryptfs_lower_offset_for_extent(
  467. &offset, ((((loff_t)page->index)
  468. * (PAGE_CACHE_SIZE
  469. / crypt_stat->extent_size))
  470. + extent_offset), crypt_stat);
  471. rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
  472. offset, crypt_stat->extent_size);
  473. if (rc) {
  474. ecryptfs_printk(KERN_ERR, "Error attempting "
  475. "to write lower page; rc = [%d]"
  476. "\n", rc);
  477. goto out;
  478. }
  479. extent_offset++;
  480. }
  481. out:
  482. kfree(enc_extent_virt);
  483. return rc;
  484. }
  485. static int ecryptfs_decrypt_extent(struct page *page,
  486. struct ecryptfs_crypt_stat *crypt_stat,
  487. struct page *enc_extent_page,
  488. unsigned long extent_offset)
  489. {
  490. loff_t extent_base;
  491. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  492. int rc;
  493. extent_base = (((loff_t)page->index)
  494. * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
  495. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  496. (extent_base + extent_offset));
  497. if (rc) {
  498. ecryptfs_printk(KERN_ERR, "Error attempting to "
  499. "derive IV for extent [0x%.16x]; "
  500. "rc = [%d]\n", (extent_base + extent_offset),
  501. rc);
  502. goto out;
  503. }
  504. if (unlikely(ecryptfs_verbosity > 0)) {
  505. ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
  506. "with iv:\n");
  507. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  508. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  509. "decryption:\n");
  510. ecryptfs_dump_hex((char *)
  511. (page_address(enc_extent_page)
  512. + (extent_offset * crypt_stat->extent_size)),
  513. 8);
  514. }
  515. rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
  516. (extent_offset
  517. * crypt_stat->extent_size),
  518. enc_extent_page, 0,
  519. crypt_stat->extent_size, extent_iv);
  520. if (rc < 0) {
  521. printk(KERN_ERR "%s: Error attempting to decrypt to page with "
  522. "page->index = [%ld], extent_offset = [%ld]; "
  523. "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
  524. rc);
  525. goto out;
  526. }
  527. rc = 0;
  528. if (unlikely(ecryptfs_verbosity > 0)) {
  529. ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
  530. "rc = [%d]\n", (extent_base + extent_offset),
  531. rc);
  532. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  533. "decryption:\n");
  534. ecryptfs_dump_hex((char *)(page_address(page)
  535. + (extent_offset
  536. * crypt_stat->extent_size)), 8);
  537. }
  538. out:
  539. return rc;
  540. }
  541. /**
  542. * ecryptfs_decrypt_page
  543. * @page: Page mapped from the eCryptfs inode for the file; data read
  544. * and decrypted from the lower file will be written into this
  545. * page
  546. *
  547. * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
  548. * that eCryptfs pages may straddle the lower pages -- for instance,
  549. * if the file was created on a machine with an 8K page size
  550. * (resulting in an 8K header), and then the file is copied onto a
  551. * host with a 32K page size, then when reading page 0 of the eCryptfs
  552. * file, 24K of page 0 of the lower file will be read and decrypted,
  553. * and then 8K of page 1 of the lower file will be read and decrypted.
  554. *
  555. * Returns zero on success; negative on error
  556. */
  557. int ecryptfs_decrypt_page(struct page *page)
  558. {
  559. struct inode *ecryptfs_inode;
  560. struct ecryptfs_crypt_stat *crypt_stat;
  561. char *enc_extent_virt = NULL;
  562. struct page *enc_extent_page;
  563. unsigned long extent_offset;
  564. int rc = 0;
  565. ecryptfs_inode = page->mapping->host;
  566. crypt_stat =
  567. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  568. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  569. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  570. PAGE_CACHE_SIZE,
  571. ecryptfs_inode);
  572. if (rc)
  573. printk(KERN_ERR "%s: Error attempting to copy "
  574. "page at index [%ld]\n", __FUNCTION__,
  575. page->index);
  576. goto out;
  577. }
  578. enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
  579. if (!enc_extent_virt) {
  580. rc = -ENOMEM;
  581. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  582. "encrypted extent\n");
  583. goto out;
  584. }
  585. enc_extent_page = virt_to_page(enc_extent_virt);
  586. for (extent_offset = 0;
  587. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  588. extent_offset++) {
  589. loff_t offset;
  590. ecryptfs_lower_offset_for_extent(
  591. &offset, ((page->index * (PAGE_CACHE_SIZE
  592. / crypt_stat->extent_size))
  593. + extent_offset), crypt_stat);
  594. rc = ecryptfs_read_lower(enc_extent_virt, offset,
  595. crypt_stat->extent_size,
  596. ecryptfs_inode);
  597. if (rc) {
  598. ecryptfs_printk(KERN_ERR, "Error attempting "
  599. "to read lower page; rc = [%d]"
  600. "\n", rc);
  601. goto out;
  602. }
  603. rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
  604. extent_offset);
  605. if (rc) {
  606. printk(KERN_ERR "%s: Error encrypting extent; "
  607. "rc = [%d]\n", __FUNCTION__, rc);
  608. goto out;
  609. }
  610. extent_offset++;
  611. }
  612. out:
  613. kfree(enc_extent_virt);
  614. return rc;
  615. }
  616. /**
  617. * decrypt_scatterlist
  618. * @crypt_stat: Cryptographic context
  619. * @dest_sg: The destination scatterlist to decrypt into
  620. * @src_sg: The source scatterlist to decrypt from
  621. * @size: The number of bytes to decrypt
  622. * @iv: The initialization vector to use for the decryption
  623. *
  624. * Returns the number of bytes decrypted; negative value on error
  625. */
  626. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  627. struct scatterlist *dest_sg,
  628. struct scatterlist *src_sg, int size,
  629. unsigned char *iv)
  630. {
  631. struct blkcipher_desc desc = {
  632. .tfm = crypt_stat->tfm,
  633. .info = iv,
  634. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  635. };
  636. int rc = 0;
  637. /* Consider doing this once, when the file is opened */
  638. mutex_lock(&crypt_stat->cs_tfm_mutex);
  639. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  640. crypt_stat->key_size);
  641. if (rc) {
  642. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  643. rc);
  644. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  645. rc = -EINVAL;
  646. goto out;
  647. }
  648. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  649. rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
  650. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  651. if (rc) {
  652. ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
  653. rc);
  654. goto out;
  655. }
  656. rc = size;
  657. out:
  658. return rc;
  659. }
  660. /**
  661. * ecryptfs_encrypt_page_offset
  662. * @crypt_stat: The cryptographic context
  663. * @dst_page: The page to encrypt into
  664. * @dst_offset: The offset in the page to encrypt into
  665. * @src_page: The page to encrypt from
  666. * @src_offset: The offset in the page to encrypt from
  667. * @size: The number of bytes to encrypt
  668. * @iv: The initialization vector to use for the encryption
  669. *
  670. * Returns the number of bytes encrypted
  671. */
  672. static int
  673. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  674. struct page *dst_page, int dst_offset,
  675. struct page *src_page, int src_offset, int size,
  676. unsigned char *iv)
  677. {
  678. struct scatterlist src_sg, dst_sg;
  679. sg_init_table(&src_sg, 1);
  680. sg_init_table(&dst_sg, 1);
  681. sg_set_page(&src_sg, src_page, size, src_offset);
  682. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  683. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  684. }
  685. /**
  686. * ecryptfs_decrypt_page_offset
  687. * @crypt_stat: The cryptographic context
  688. * @dst_page: The page to decrypt into
  689. * @dst_offset: The offset in the page to decrypt into
  690. * @src_page: The page to decrypt from
  691. * @src_offset: The offset in the page to decrypt from
  692. * @size: The number of bytes to decrypt
  693. * @iv: The initialization vector to use for the decryption
  694. *
  695. * Returns the number of bytes decrypted
  696. */
  697. static int
  698. ecryptfs_decrypt_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_set_page(&src_sg, src_page, size, src_offset);
  706. sg_init_table(&dst_sg, 1);
  707. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  708. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  709. }
  710. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  711. /**
  712. * ecryptfs_init_crypt_ctx
  713. * @crypt_stat: Uninitilized crypt stats structure
  714. *
  715. * Initialize the crypto context.
  716. *
  717. * TODO: Performance: Keep a cache of initialized cipher contexts;
  718. * only init if needed
  719. */
  720. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  721. {
  722. char *full_alg_name;
  723. int rc = -EINVAL;
  724. if (!crypt_stat->cipher) {
  725. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  726. goto out;
  727. }
  728. ecryptfs_printk(KERN_DEBUG,
  729. "Initializing cipher [%s]; strlen = [%d]; "
  730. "key_size_bits = [%d]\n",
  731. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  732. crypt_stat->key_size << 3);
  733. if (crypt_stat->tfm) {
  734. rc = 0;
  735. goto out;
  736. }
  737. mutex_lock(&crypt_stat->cs_tfm_mutex);
  738. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  739. crypt_stat->cipher, "cbc");
  740. if (rc)
  741. goto out;
  742. crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  743. CRYPTO_ALG_ASYNC);
  744. kfree(full_alg_name);
  745. if (IS_ERR(crypt_stat->tfm)) {
  746. rc = PTR_ERR(crypt_stat->tfm);
  747. ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
  748. "Error initializing cipher [%s]\n",
  749. crypt_stat->cipher);
  750. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  751. goto out;
  752. }
  753. crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  754. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  755. rc = 0;
  756. out:
  757. return rc;
  758. }
  759. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  760. {
  761. int extent_size_tmp;
  762. crypt_stat->extent_mask = 0xFFFFFFFF;
  763. crypt_stat->extent_shift = 0;
  764. if (crypt_stat->extent_size == 0)
  765. return;
  766. extent_size_tmp = crypt_stat->extent_size;
  767. while ((extent_size_tmp & 0x01) == 0) {
  768. extent_size_tmp >>= 1;
  769. crypt_stat->extent_mask <<= 1;
  770. crypt_stat->extent_shift++;
  771. }
  772. }
  773. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  774. {
  775. /* Default values; may be overwritten as we are parsing the
  776. * packets. */
  777. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  778. set_extent_mask_and_shift(crypt_stat);
  779. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  780. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  781. crypt_stat->num_header_extents_at_front = 0;
  782. else {
  783. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
  784. crypt_stat->num_header_extents_at_front =
  785. (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
  786. / crypt_stat->extent_size);
  787. else
  788. crypt_stat->num_header_extents_at_front =
  789. (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  790. }
  791. }
  792. /**
  793. * ecryptfs_compute_root_iv
  794. * @crypt_stats
  795. *
  796. * On error, sets the root IV to all 0's.
  797. */
  798. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
  799. {
  800. int rc = 0;
  801. char dst[MD5_DIGEST_SIZE];
  802. BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
  803. BUG_ON(crypt_stat->iv_bytes <= 0);
  804. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  805. rc = -EINVAL;
  806. ecryptfs_printk(KERN_WARNING, "Session key not valid; "
  807. "cannot generate root IV\n");
  808. goto out;
  809. }
  810. rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
  811. crypt_stat->key_size);
  812. if (rc) {
  813. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  814. "MD5 while generating root IV\n");
  815. goto out;
  816. }
  817. memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
  818. out:
  819. if (rc) {
  820. memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
  821. crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
  822. }
  823. return rc;
  824. }
  825. static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
  826. {
  827. get_random_bytes(crypt_stat->key, crypt_stat->key_size);
  828. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  829. ecryptfs_compute_root_iv(crypt_stat);
  830. if (unlikely(ecryptfs_verbosity > 0)) {
  831. ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
  832. ecryptfs_dump_hex(crypt_stat->key,
  833. crypt_stat->key_size);
  834. }
  835. }
  836. /**
  837. * ecryptfs_copy_mount_wide_flags_to_inode_flags
  838. * @crypt_stat: The inode's cryptographic context
  839. * @mount_crypt_stat: The mount point's cryptographic context
  840. *
  841. * This function propagates the mount-wide flags to individual inode
  842. * flags.
  843. */
  844. static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
  845. struct ecryptfs_crypt_stat *crypt_stat,
  846. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  847. {
  848. if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
  849. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  850. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  851. crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
  852. }
  853. static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
  854. struct ecryptfs_crypt_stat *crypt_stat,
  855. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  856. {
  857. struct ecryptfs_global_auth_tok *global_auth_tok;
  858. int rc = 0;
  859. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  860. list_for_each_entry(global_auth_tok,
  861. &mount_crypt_stat->global_auth_tok_list,
  862. mount_crypt_stat_list) {
  863. rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
  864. if (rc) {
  865. printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
  866. mutex_unlock(
  867. &mount_crypt_stat->global_auth_tok_list_mutex);
  868. goto out;
  869. }
  870. }
  871. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  872. out:
  873. return rc;
  874. }
  875. /**
  876. * ecryptfs_set_default_crypt_stat_vals
  877. * @crypt_stat: The inode's cryptographic context
  878. * @mount_crypt_stat: The mount point's cryptographic context
  879. *
  880. * Default values in the event that policy does not override them.
  881. */
  882. static void ecryptfs_set_default_crypt_stat_vals(
  883. struct ecryptfs_crypt_stat *crypt_stat,
  884. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  885. {
  886. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  887. mount_crypt_stat);
  888. ecryptfs_set_default_sizes(crypt_stat);
  889. strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
  890. crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
  891. crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
  892. crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
  893. crypt_stat->mount_crypt_stat = mount_crypt_stat;
  894. }
  895. /**
  896. * ecryptfs_new_file_context
  897. * @ecryptfs_dentry: The eCryptfs dentry
  898. *
  899. * If the crypto context for the file has not yet been established,
  900. * this is where we do that. Establishing a new crypto context
  901. * involves the following decisions:
  902. * - What cipher to use?
  903. * - What set of authentication tokens to use?
  904. * Here we just worry about getting enough information into the
  905. * authentication tokens so that we know that they are available.
  906. * We associate the available authentication tokens with the new file
  907. * via the set of signatures in the crypt_stat struct. Later, when
  908. * the headers are actually written out, we may again defer to
  909. * userspace to perform the encryption of the session key; for the
  910. * foreseeable future, this will be the case with public key packets.
  911. *
  912. * Returns zero on success; non-zero otherwise
  913. */
  914. int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
  915. {
  916. struct ecryptfs_crypt_stat *crypt_stat =
  917. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  918. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  919. &ecryptfs_superblock_to_private(
  920. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  921. int cipher_name_len;
  922. int rc = 0;
  923. ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
  924. crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
  925. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  926. mount_crypt_stat);
  927. rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
  928. mount_crypt_stat);
  929. if (rc) {
  930. printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
  931. "to the inode key sigs; rc = [%d]\n", rc);
  932. goto out;
  933. }
  934. cipher_name_len =
  935. strlen(mount_crypt_stat->global_default_cipher_name);
  936. memcpy(crypt_stat->cipher,
  937. mount_crypt_stat->global_default_cipher_name,
  938. cipher_name_len);
  939. crypt_stat->cipher[cipher_name_len] = '\0';
  940. crypt_stat->key_size =
  941. mount_crypt_stat->global_default_cipher_key_size;
  942. ecryptfs_generate_new_key(crypt_stat);
  943. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  944. if (rc)
  945. ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
  946. "context for cipher [%s]: rc = [%d]\n",
  947. crypt_stat->cipher, rc);
  948. out:
  949. return rc;
  950. }
  951. /**
  952. * contains_ecryptfs_marker - check for the ecryptfs marker
  953. * @data: The data block in which to check
  954. *
  955. * Returns one if marker found; zero if not found
  956. */
  957. static int contains_ecryptfs_marker(char *data)
  958. {
  959. u32 m_1, m_2;
  960. memcpy(&m_1, data, 4);
  961. m_1 = be32_to_cpu(m_1);
  962. memcpy(&m_2, (data + 4), 4);
  963. m_2 = be32_to_cpu(m_2);
  964. if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
  965. return 1;
  966. ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
  967. "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
  968. MAGIC_ECRYPTFS_MARKER);
  969. ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
  970. "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
  971. return 0;
  972. }
  973. struct ecryptfs_flag_map_elem {
  974. u32 file_flag;
  975. u32 local_flag;
  976. };
  977. /* Add support for additional flags by adding elements here. */
  978. static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
  979. {0x00000001, ECRYPTFS_ENABLE_HMAC},
  980. {0x00000002, ECRYPTFS_ENCRYPTED},
  981. {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
  982. };
  983. /**
  984. * ecryptfs_process_flags
  985. * @crypt_stat: The cryptographic context
  986. * @page_virt: Source data to be parsed
  987. * @bytes_read: Updated with the number of bytes read
  988. *
  989. * Returns zero on success; non-zero if the flag set is invalid
  990. */
  991. static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
  992. char *page_virt, int *bytes_read)
  993. {
  994. int rc = 0;
  995. int i;
  996. u32 flags;
  997. memcpy(&flags, page_virt, 4);
  998. flags = be32_to_cpu(flags);
  999. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1000. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1001. if (flags & ecryptfs_flag_map[i].file_flag) {
  1002. crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
  1003. } else
  1004. crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
  1005. /* Version is in top 8 bits of the 32-bit flag vector */
  1006. crypt_stat->file_version = ((flags >> 24) & 0xFF);
  1007. (*bytes_read) = 4;
  1008. return rc;
  1009. }
  1010. /**
  1011. * write_ecryptfs_marker
  1012. * @page_virt: The pointer to in a page to begin writing the marker
  1013. * @written: Number of bytes written
  1014. *
  1015. * Marker = 0x3c81b7f5
  1016. */
  1017. static void write_ecryptfs_marker(char *page_virt, size_t *written)
  1018. {
  1019. u32 m_1, m_2;
  1020. get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1021. m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
  1022. m_1 = cpu_to_be32(m_1);
  1023. memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1024. m_2 = cpu_to_be32(m_2);
  1025. memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
  1026. (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1027. (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1028. }
  1029. static void
  1030. write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
  1031. size_t *written)
  1032. {
  1033. u32 flags = 0;
  1034. int i;
  1035. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1036. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1037. if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
  1038. flags |= ecryptfs_flag_map[i].file_flag;
  1039. /* Version is in top 8 bits of the 32-bit flag vector */
  1040. flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
  1041. flags = cpu_to_be32(flags);
  1042. memcpy(page_virt, &flags, 4);
  1043. (*written) = 4;
  1044. }
  1045. struct ecryptfs_cipher_code_str_map_elem {
  1046. char cipher_str[16];
  1047. u16 cipher_code;
  1048. };
  1049. /* Add support for additional ciphers by adding elements here. The
  1050. * cipher_code is whatever OpenPGP applicatoins use to identify the
  1051. * ciphers. List in order of probability. */
  1052. static struct ecryptfs_cipher_code_str_map_elem
  1053. ecryptfs_cipher_code_str_map[] = {
  1054. {"aes",RFC2440_CIPHER_AES_128 },
  1055. {"blowfish", RFC2440_CIPHER_BLOWFISH},
  1056. {"des3_ede", RFC2440_CIPHER_DES3_EDE},
  1057. {"cast5", RFC2440_CIPHER_CAST_5},
  1058. {"twofish", RFC2440_CIPHER_TWOFISH},
  1059. {"cast6", RFC2440_CIPHER_CAST_6},
  1060. {"aes", RFC2440_CIPHER_AES_192},
  1061. {"aes", RFC2440_CIPHER_AES_256}
  1062. };
  1063. /**
  1064. * ecryptfs_code_for_cipher_string
  1065. * @crypt_stat: The cryptographic context
  1066. *
  1067. * Returns zero on no match, or the cipher code on match
  1068. */
  1069. u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
  1070. {
  1071. int i;
  1072. u16 code = 0;
  1073. struct ecryptfs_cipher_code_str_map_elem *map =
  1074. ecryptfs_cipher_code_str_map;
  1075. if (strcmp(crypt_stat->cipher, "aes") == 0) {
  1076. switch (crypt_stat->key_size) {
  1077. case 16:
  1078. code = RFC2440_CIPHER_AES_128;
  1079. break;
  1080. case 24:
  1081. code = RFC2440_CIPHER_AES_192;
  1082. break;
  1083. case 32:
  1084. code = RFC2440_CIPHER_AES_256;
  1085. }
  1086. } else {
  1087. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1088. if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
  1089. code = map[i].cipher_code;
  1090. break;
  1091. }
  1092. }
  1093. return code;
  1094. }
  1095. /**
  1096. * ecryptfs_cipher_code_to_string
  1097. * @str: Destination to write out the cipher name
  1098. * @cipher_code: The code to convert to cipher name string
  1099. *
  1100. * Returns zero on success
  1101. */
  1102. int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
  1103. {
  1104. int rc = 0;
  1105. int i;
  1106. str[0] = '\0';
  1107. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1108. if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
  1109. strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
  1110. if (str[0] == '\0') {
  1111. ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
  1112. "[%d]\n", cipher_code);
  1113. rc = -EINVAL;
  1114. }
  1115. return rc;
  1116. }
  1117. int ecryptfs_read_and_validate_header_region(char *data,
  1118. struct inode *ecryptfs_inode)
  1119. {
  1120. struct ecryptfs_crypt_stat *crypt_stat =
  1121. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  1122. int rc;
  1123. rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
  1124. ecryptfs_inode);
  1125. if (rc) {
  1126. printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
  1127. __FUNCTION__, rc);
  1128. goto out;
  1129. }
  1130. if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
  1131. rc = -EINVAL;
  1132. ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
  1133. }
  1134. out:
  1135. return rc;
  1136. }
  1137. void
  1138. ecryptfs_write_header_metadata(char *virt,
  1139. struct ecryptfs_crypt_stat *crypt_stat,
  1140. size_t *written)
  1141. {
  1142. u32 header_extent_size;
  1143. u16 num_header_extents_at_front;
  1144. header_extent_size = (u32)crypt_stat->extent_size;
  1145. num_header_extents_at_front =
  1146. (u16)crypt_stat->num_header_extents_at_front;
  1147. header_extent_size = cpu_to_be32(header_extent_size);
  1148. memcpy(virt, &header_extent_size, 4);
  1149. virt += 4;
  1150. num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
  1151. memcpy(virt, &num_header_extents_at_front, 2);
  1152. (*written) = 6;
  1153. }
  1154. struct kmem_cache *ecryptfs_header_cache_0;
  1155. struct kmem_cache *ecryptfs_header_cache_1;
  1156. struct kmem_cache *ecryptfs_header_cache_2;
  1157. /**
  1158. * ecryptfs_write_headers_virt
  1159. * @page_virt: The virtual address to write the headers to
  1160. * @size: Set to the number of bytes written by this function
  1161. * @crypt_stat: The cryptographic context
  1162. * @ecryptfs_dentry: The eCryptfs dentry
  1163. *
  1164. * Format version: 1
  1165. *
  1166. * Header Extent:
  1167. * Octets 0-7: Unencrypted file size (big-endian)
  1168. * Octets 8-15: eCryptfs special marker
  1169. * Octets 16-19: Flags
  1170. * Octet 16: File format version number (between 0 and 255)
  1171. * Octets 17-18: Reserved
  1172. * Octet 19: Bit 1 (lsb): Reserved
  1173. * Bit 2: Encrypted?
  1174. * Bits 3-8: Reserved
  1175. * Octets 20-23: Header extent size (big-endian)
  1176. * Octets 24-25: Number of header extents at front of file
  1177. * (big-endian)
  1178. * Octet 26: Begin RFC 2440 authentication token packet set
  1179. * Data Extent 0:
  1180. * Lower data (CBC encrypted)
  1181. * Data Extent 1:
  1182. * Lower data (CBC encrypted)
  1183. * ...
  1184. *
  1185. * Returns zero on success
  1186. */
  1187. static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
  1188. struct ecryptfs_crypt_stat *crypt_stat,
  1189. struct dentry *ecryptfs_dentry)
  1190. {
  1191. int rc;
  1192. size_t written;
  1193. size_t offset;
  1194. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1195. write_ecryptfs_marker((page_virt + offset), &written);
  1196. offset += written;
  1197. write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
  1198. offset += written;
  1199. ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
  1200. &written);
  1201. offset += written;
  1202. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1203. ecryptfs_dentry, &written,
  1204. PAGE_CACHE_SIZE - offset);
  1205. if (rc)
  1206. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1207. "set; rc = [%d]\n", rc);
  1208. if (size) {
  1209. offset += written;
  1210. *size = offset;
  1211. }
  1212. return rc;
  1213. }
  1214. static int
  1215. ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
  1216. struct dentry *ecryptfs_dentry,
  1217. char *page_virt)
  1218. {
  1219. int current_header_page;
  1220. int header_pages;
  1221. int rc;
  1222. rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
  1223. 0, PAGE_CACHE_SIZE);
  1224. if (rc) {
  1225. printk(KERN_ERR "%s: Error attempting to write header "
  1226. "information to lower file; rc = [%d]\n", __FUNCTION__,
  1227. rc);
  1228. goto out;
  1229. }
  1230. header_pages = ((crypt_stat->extent_size
  1231. * crypt_stat->num_header_extents_at_front)
  1232. / PAGE_CACHE_SIZE);
  1233. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1234. current_header_page = 1;
  1235. while (current_header_page < header_pages) {
  1236. loff_t offset;
  1237. offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT);
  1238. if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
  1239. page_virt, offset,
  1240. PAGE_CACHE_SIZE))) {
  1241. printk(KERN_ERR "%s: Error attempting to write header "
  1242. "information to lower file; rc = [%d]\n",
  1243. __FUNCTION__, rc);
  1244. goto out;
  1245. }
  1246. current_header_page++;
  1247. }
  1248. out:
  1249. return rc;
  1250. }
  1251. static int
  1252. ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
  1253. struct ecryptfs_crypt_stat *crypt_stat,
  1254. char *page_virt, size_t size)
  1255. {
  1256. int rc;
  1257. rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
  1258. size, 0);
  1259. return rc;
  1260. }
  1261. /**
  1262. * ecryptfs_write_metadata
  1263. * @ecryptfs_dentry: The eCryptfs dentry
  1264. *
  1265. * Write the file headers out. This will likely involve a userspace
  1266. * callout, in which the session key is encrypted with one or more
  1267. * public keys and/or the passphrase necessary to do the encryption is
  1268. * retrieved via a prompt. Exactly what happens at this point should
  1269. * be policy-dependent.
  1270. *
  1271. * TODO: Support header information spanning multiple pages
  1272. *
  1273. * Returns zero on success; non-zero on error
  1274. */
  1275. int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
  1276. {
  1277. struct ecryptfs_crypt_stat *crypt_stat =
  1278. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  1279. char *page_virt;
  1280. size_t size = 0;
  1281. int rc = 0;
  1282. if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  1283. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  1284. printk(KERN_ERR "Key is invalid; bailing out\n");
  1285. rc = -EINVAL;
  1286. goto out;
  1287. }
  1288. } else {
  1289. rc = -EINVAL;
  1290. ecryptfs_printk(KERN_WARNING,
  1291. "Called with crypt_stat->encrypted == 0\n");
  1292. goto out;
  1293. }
  1294. /* Released in this function */
  1295. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
  1296. if (!page_virt) {
  1297. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  1298. rc = -ENOMEM;
  1299. goto out;
  1300. }
  1301. rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
  1302. ecryptfs_dentry);
  1303. if (unlikely(rc)) {
  1304. ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
  1305. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1306. goto out_free;
  1307. }
  1308. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1309. rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
  1310. crypt_stat, page_virt,
  1311. size);
  1312. else
  1313. rc = ecryptfs_write_metadata_to_contents(crypt_stat,
  1314. ecryptfs_dentry,
  1315. page_virt);
  1316. if (rc) {
  1317. printk(KERN_ERR "Error writing metadata out to lower file; "
  1318. "rc = [%d]\n", rc);
  1319. goto out_free;
  1320. }
  1321. out_free:
  1322. kmem_cache_free(ecryptfs_header_cache_0, page_virt);
  1323. out:
  1324. return rc;
  1325. }
  1326. #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
  1327. #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
  1328. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1329. char *virt, int *bytes_read,
  1330. int validate_header_size)
  1331. {
  1332. int rc = 0;
  1333. u32 header_extent_size;
  1334. u16 num_header_extents_at_front;
  1335. memcpy(&header_extent_size, virt, sizeof(u32));
  1336. header_extent_size = be32_to_cpu(header_extent_size);
  1337. virt += sizeof(u32);
  1338. memcpy(&num_header_extents_at_front, virt, sizeof(u16));
  1339. num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
  1340. crypt_stat->num_header_extents_at_front =
  1341. (int)num_header_extents_at_front;
  1342. (*bytes_read) = (sizeof(u32) + sizeof(u16));
  1343. if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
  1344. && ((crypt_stat->extent_size
  1345. * crypt_stat->num_header_extents_at_front)
  1346. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
  1347. rc = -EINVAL;
  1348. printk(KERN_WARNING "Invalid number of header extents: [%zd]\n",
  1349. crypt_stat->num_header_extents_at_front);
  1350. }
  1351. return rc;
  1352. }
  1353. /**
  1354. * set_default_header_data
  1355. * @crypt_stat: The cryptographic context
  1356. *
  1357. * For version 0 file format; this function is only for backwards
  1358. * compatibility for files created with the prior versions of
  1359. * eCryptfs.
  1360. */
  1361. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1362. {
  1363. crypt_stat->num_header_extents_at_front = 2;
  1364. }
  1365. /**
  1366. * ecryptfs_read_headers_virt
  1367. * @page_virt: The virtual address into which to read the headers
  1368. * @crypt_stat: The cryptographic context
  1369. * @ecryptfs_dentry: The eCryptfs dentry
  1370. * @validate_header_size: Whether to validate the header size while reading
  1371. *
  1372. * Read/parse the header data. The header format is detailed in the
  1373. * comment block for the ecryptfs_write_headers_virt() function.
  1374. *
  1375. * Returns zero on success
  1376. */
  1377. static int ecryptfs_read_headers_virt(char *page_virt,
  1378. struct ecryptfs_crypt_stat *crypt_stat,
  1379. struct dentry *ecryptfs_dentry,
  1380. int validate_header_size)
  1381. {
  1382. int rc = 0;
  1383. int offset;
  1384. int bytes_read;
  1385. ecryptfs_set_default_sizes(crypt_stat);
  1386. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1387. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1388. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1389. rc = contains_ecryptfs_marker(page_virt + offset);
  1390. if (rc == 0) {
  1391. rc = -EINVAL;
  1392. goto out;
  1393. }
  1394. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1395. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1396. &bytes_read);
  1397. if (rc) {
  1398. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1399. goto out;
  1400. }
  1401. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1402. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1403. "file version [%d] is supported by this "
  1404. "version of eCryptfs\n",
  1405. crypt_stat->file_version,
  1406. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1407. rc = -EINVAL;
  1408. goto out;
  1409. }
  1410. offset += bytes_read;
  1411. if (crypt_stat->file_version >= 1) {
  1412. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1413. &bytes_read, validate_header_size);
  1414. if (rc) {
  1415. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1416. "metadata; rc = [%d]\n", rc);
  1417. }
  1418. offset += bytes_read;
  1419. } else
  1420. set_default_header_data(crypt_stat);
  1421. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1422. ecryptfs_dentry);
  1423. out:
  1424. return rc;
  1425. }
  1426. /**
  1427. * ecryptfs_read_xattr_region
  1428. * @page_virt: The vitual address into which to read the xattr data
  1429. * @ecryptfs_inode: The eCryptfs inode
  1430. *
  1431. * Attempts to read the crypto metadata from the extended attribute
  1432. * region of the lower file.
  1433. *
  1434. * Returns zero on success; non-zero on error
  1435. */
  1436. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
  1437. {
  1438. struct dentry *lower_dentry =
  1439. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  1440. ssize_t size;
  1441. int rc = 0;
  1442. size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
  1443. page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
  1444. if (size < 0) {
  1445. printk(KERN_ERR "Error attempting to read the [%s] "
  1446. "xattr from the lower file; return value = [%zd]\n",
  1447. ECRYPTFS_XATTR_NAME, size);
  1448. rc = -EINVAL;
  1449. goto out;
  1450. }
  1451. out:
  1452. return rc;
  1453. }
  1454. int ecryptfs_read_and_validate_xattr_region(char *page_virt,
  1455. struct dentry *ecryptfs_dentry)
  1456. {
  1457. int rc;
  1458. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
  1459. if (rc)
  1460. goto out;
  1461. if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
  1462. printk(KERN_WARNING "Valid data found in [%s] xattr, but "
  1463. "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
  1464. rc = -EINVAL;
  1465. }
  1466. out:
  1467. return rc;
  1468. }
  1469. /**
  1470. * ecryptfs_read_metadata
  1471. *
  1472. * Common entry point for reading file metadata. From here, we could
  1473. * retrieve the header information from the header region of the file,
  1474. * the xattr region of the file, or some other repostory that is
  1475. * stored separately from the file itself. The current implementation
  1476. * supports retrieving the metadata information from the file contents
  1477. * and from the xattr region.
  1478. *
  1479. * Returns zero if valid headers found and parsed; non-zero otherwise
  1480. */
  1481. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
  1482. {
  1483. int rc = 0;
  1484. char *page_virt = NULL;
  1485. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  1486. struct ecryptfs_crypt_stat *crypt_stat =
  1487. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1488. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1489. &ecryptfs_superblock_to_private(
  1490. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1491. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  1492. mount_crypt_stat);
  1493. /* Read the first page from the underlying file */
  1494. page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
  1495. if (!page_virt) {
  1496. rc = -ENOMEM;
  1497. printk(KERN_ERR "%s: Unable to allocate page_virt\n",
  1498. __FUNCTION__);
  1499. goto out;
  1500. }
  1501. rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
  1502. ecryptfs_inode);
  1503. if (!rc)
  1504. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1505. ecryptfs_dentry,
  1506. ECRYPTFS_VALIDATE_HEADER_SIZE);
  1507. if (rc) {
  1508. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
  1509. if (rc) {
  1510. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1511. "file header region or xattr region\n");
  1512. rc = -EINVAL;
  1513. goto out;
  1514. }
  1515. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1516. ecryptfs_dentry,
  1517. ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
  1518. if (rc) {
  1519. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1520. "file xattr region either\n");
  1521. rc = -EINVAL;
  1522. }
  1523. if (crypt_stat->mount_crypt_stat->flags
  1524. & ECRYPTFS_XATTR_METADATA_ENABLED) {
  1525. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  1526. } else {
  1527. printk(KERN_WARNING "Attempt to access file with "
  1528. "crypto metadata only in the extended attribute "
  1529. "region, but eCryptfs was mounted without "
  1530. "xattr support enabled. eCryptfs will not treat "
  1531. "this like an encrypted file.\n");
  1532. rc = -EINVAL;
  1533. }
  1534. }
  1535. out:
  1536. if (page_virt) {
  1537. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1538. kmem_cache_free(ecryptfs_header_cache_1, page_virt);
  1539. }
  1540. return rc;
  1541. }
  1542. /**
  1543. * ecryptfs_encode_filename - converts a plaintext file name to cipher text
  1544. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1545. * @name: The plaintext name
  1546. * @length: The length of the plaintext
  1547. * @encoded_name: The encypted name
  1548. *
  1549. * Encrypts and encodes a filename into something that constitutes a
  1550. * valid filename for a filesystem, with printable characters.
  1551. *
  1552. * We assume that we have a properly initialized crypto context,
  1553. * pointed to by crypt_stat->tfm.
  1554. *
  1555. * TODO: Implement filename decoding and decryption here, in place of
  1556. * memcpy. We are keeping the framework around for now to (1)
  1557. * facilitate testing of the components needed to implement filename
  1558. * encryption and (2) to provide a code base from which other
  1559. * developers in the community can easily implement this feature.
  1560. *
  1561. * Returns the length of encoded filename; negative if error
  1562. */
  1563. int
  1564. ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1565. const char *name, int length, char **encoded_name)
  1566. {
  1567. int error = 0;
  1568. (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
  1569. if (!(*encoded_name)) {
  1570. error = -ENOMEM;
  1571. goto out;
  1572. }
  1573. /* TODO: Filename encryption is a scheduled feature for a
  1574. * future version of eCryptfs. This function is here only for
  1575. * the purpose of providing a framework for other developers
  1576. * to easily implement filename encryption. Hint: Replace this
  1577. * memcpy() with a call to encrypt and encode the
  1578. * filename, the set the length accordingly. */
  1579. memcpy((void *)(*encoded_name), (void *)name, length);
  1580. (*encoded_name)[length] = '\0';
  1581. error = length + 1;
  1582. out:
  1583. return error;
  1584. }
  1585. /**
  1586. * ecryptfs_decode_filename - converts the cipher text name to plaintext
  1587. * @crypt_stat: The crypt_stat struct associated with the file
  1588. * @name: The filename in cipher text
  1589. * @length: The length of the cipher text name
  1590. * @decrypted_name: The plaintext name
  1591. *
  1592. * Decodes and decrypts the filename.
  1593. *
  1594. * We assume that we have a properly initialized crypto context,
  1595. * pointed to by crypt_stat->tfm.
  1596. *
  1597. * TODO: Implement filename decoding and decryption here, in place of
  1598. * memcpy. We are keeping the framework around for now to (1)
  1599. * facilitate testing of the components needed to implement filename
  1600. * encryption and (2) to provide a code base from which other
  1601. * developers in the community can easily implement this feature.
  1602. *
  1603. * Returns the length of decoded filename; negative if error
  1604. */
  1605. int
  1606. ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1607. const char *name, int length, char **decrypted_name)
  1608. {
  1609. int error = 0;
  1610. (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
  1611. if (!(*decrypted_name)) {
  1612. error = -ENOMEM;
  1613. goto out;
  1614. }
  1615. /* TODO: Filename encryption is a scheduled feature for a
  1616. * future version of eCryptfs. This function is here only for
  1617. * the purpose of providing a framework for other developers
  1618. * to easily implement filename encryption. Hint: Replace this
  1619. * memcpy() with a call to decode and decrypt the
  1620. * filename, the set the length accordingly. */
  1621. memcpy((void *)(*decrypted_name), (void *)name, length);
  1622. (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
  1623. * in printing out the
  1624. * string in debug
  1625. * messages */
  1626. error = length;
  1627. out:
  1628. return error;
  1629. }
  1630. /**
  1631. * ecryptfs_process_key_cipher - Perform key cipher initialization.
  1632. * @key_tfm: Crypto context for key material, set by this function
  1633. * @cipher_name: Name of the cipher
  1634. * @key_size: Size of the key in bytes
  1635. *
  1636. * Returns zero on success. Any crypto_tfm structs allocated here
  1637. * should be released by other functions, such as on a superblock put
  1638. * event, regardless of whether this function succeeds for fails.
  1639. */
  1640. static int
  1641. ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
  1642. char *cipher_name, size_t *key_size)
  1643. {
  1644. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1645. char *full_alg_name;
  1646. int rc;
  1647. *key_tfm = NULL;
  1648. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1649. rc = -EINVAL;
  1650. printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
  1651. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1652. goto out;
  1653. }
  1654. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1655. "ecb");
  1656. if (rc)
  1657. goto out;
  1658. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1659. kfree(full_alg_name);
  1660. if (IS_ERR(*key_tfm)) {
  1661. rc = PTR_ERR(*key_tfm);
  1662. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1663. "[%s]; rc = [%d]\n", cipher_name, rc);
  1664. goto out;
  1665. }
  1666. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1667. if (*key_size == 0) {
  1668. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1669. *key_size = alg->max_keysize;
  1670. }
  1671. get_random_bytes(dummy_key, *key_size);
  1672. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1673. if (rc) {
  1674. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1675. "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
  1676. rc = -EINVAL;
  1677. goto out;
  1678. }
  1679. out:
  1680. return rc;
  1681. }
  1682. struct kmem_cache *ecryptfs_key_tfm_cache;
  1683. struct list_head key_tfm_list;
  1684. struct mutex key_tfm_list_mutex;
  1685. int ecryptfs_init_crypto(void)
  1686. {
  1687. mutex_init(&key_tfm_list_mutex);
  1688. INIT_LIST_HEAD(&key_tfm_list);
  1689. return 0;
  1690. }
  1691. int ecryptfs_destroy_crypto(void)
  1692. {
  1693. struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
  1694. mutex_lock(&key_tfm_list_mutex);
  1695. list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
  1696. key_tfm_list) {
  1697. list_del(&key_tfm->key_tfm_list);
  1698. if (key_tfm->key_tfm)
  1699. crypto_free_blkcipher(key_tfm->key_tfm);
  1700. kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
  1701. }
  1702. mutex_unlock(&key_tfm_list_mutex);
  1703. return 0;
  1704. }
  1705. int
  1706. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  1707. size_t key_size)
  1708. {
  1709. struct ecryptfs_key_tfm *tmp_tfm;
  1710. int rc = 0;
  1711. tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
  1712. if (key_tfm != NULL)
  1713. (*key_tfm) = tmp_tfm;
  1714. if (!tmp_tfm) {
  1715. rc = -ENOMEM;
  1716. printk(KERN_ERR "Error attempting to allocate from "
  1717. "ecryptfs_key_tfm_cache\n");
  1718. goto out;
  1719. }
  1720. mutex_init(&tmp_tfm->key_tfm_mutex);
  1721. strncpy(tmp_tfm->cipher_name, cipher_name,
  1722. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  1723. tmp_tfm->key_size = key_size;
  1724. rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
  1725. tmp_tfm->cipher_name,
  1726. &tmp_tfm->key_size);
  1727. if (rc) {
  1728. printk(KERN_ERR "Error attempting to initialize key TFM "
  1729. "cipher with name = [%s]; rc = [%d]\n",
  1730. tmp_tfm->cipher_name, rc);
  1731. kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
  1732. if (key_tfm != NULL)
  1733. (*key_tfm) = NULL;
  1734. goto out;
  1735. }
  1736. mutex_lock(&key_tfm_list_mutex);
  1737. list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
  1738. mutex_unlock(&key_tfm_list_mutex);
  1739. out:
  1740. return rc;
  1741. }
  1742. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
  1743. struct mutex **tfm_mutex,
  1744. char *cipher_name)
  1745. {
  1746. struct ecryptfs_key_tfm *key_tfm;
  1747. int rc = 0;
  1748. (*tfm) = NULL;
  1749. (*tfm_mutex) = NULL;
  1750. mutex_lock(&key_tfm_list_mutex);
  1751. list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
  1752. if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
  1753. (*tfm) = key_tfm->key_tfm;
  1754. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1755. mutex_unlock(&key_tfm_list_mutex);
  1756. goto out;
  1757. }
  1758. }
  1759. mutex_unlock(&key_tfm_list_mutex);
  1760. rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
  1761. if (rc) {
  1762. printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
  1763. rc);
  1764. goto out;
  1765. }
  1766. (*tfm) = key_tfm->key_tfm;
  1767. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1768. out:
  1769. return rc;
  1770. }