crypto.c 67 KB

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