crypto.c 66 KB

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