crypto.c 60 KB

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