crypto.c 67 KB

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