crypto.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  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. if (unlikely(ecryptfs_verbosity > 0)) {
  396. ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
  397. "with iv:\n");
  398. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  399. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  400. "encryption:\n");
  401. ecryptfs_dump_hex((char *)
  402. (page_address(page)
  403. + (extent_offset * crypt_stat->extent_size)),
  404. 8);
  405. }
  406. rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
  407. page, (extent_offset
  408. * crypt_stat->extent_size),
  409. crypt_stat->extent_size, extent_iv);
  410. if (rc < 0) {
  411. printk(KERN_ERR "%s: Error attempting to encrypt page with "
  412. "page->index = [%ld], extent_offset = [%ld]; "
  413. "rc = [%d]\n", __func__, page->index, extent_offset,
  414. rc);
  415. goto out;
  416. }
  417. rc = 0;
  418. if (unlikely(ecryptfs_verbosity > 0)) {
  419. ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16llx]; "
  420. "rc = [%d]\n",
  421. (unsigned long long)(extent_base + extent_offset), rc);
  422. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  423. "encryption:\n");
  424. ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
  425. }
  426. out:
  427. return rc;
  428. }
  429. /**
  430. * ecryptfs_encrypt_page
  431. * @page: Page mapped from the eCryptfs inode for the file; contains
  432. * decrypted content that needs to be encrypted (to a temporary
  433. * page; not in place) and written out to the lower file
  434. *
  435. * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
  436. * that eCryptfs pages may straddle the lower pages -- for instance,
  437. * if the file was created on a machine with an 8K page size
  438. * (resulting in an 8K header), and then the file is copied onto a
  439. * host with a 32K page size, then when reading page 0 of the eCryptfs
  440. * file, 24K of page 0 of the lower file will be read and decrypted,
  441. * and then 8K of page 1 of the lower file will be read and decrypted.
  442. *
  443. * Returns zero on success; negative on error
  444. */
  445. int ecryptfs_encrypt_page(struct page *page)
  446. {
  447. struct inode *ecryptfs_inode;
  448. struct ecryptfs_crypt_stat *crypt_stat;
  449. char *enc_extent_virt;
  450. struct page *enc_extent_page = NULL;
  451. loff_t extent_offset;
  452. int rc = 0;
  453. ecryptfs_inode = page->mapping->host;
  454. crypt_stat =
  455. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  456. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  457. enc_extent_page = alloc_page(GFP_USER);
  458. if (!enc_extent_page) {
  459. rc = -ENOMEM;
  460. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  461. "encrypted extent\n");
  462. goto out;
  463. }
  464. enc_extent_virt = kmap(enc_extent_page);
  465. for (extent_offset = 0;
  466. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  467. extent_offset++) {
  468. loff_t offset;
  469. rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
  470. extent_offset);
  471. if (rc) {
  472. printk(KERN_ERR "%s: Error encrypting extent; "
  473. "rc = [%d]\n", __func__, rc);
  474. goto out;
  475. }
  476. ecryptfs_lower_offset_for_extent(
  477. &offset, ((((loff_t)page->index)
  478. * (PAGE_CACHE_SIZE
  479. / crypt_stat->extent_size))
  480. + extent_offset), crypt_stat);
  481. rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
  482. offset, crypt_stat->extent_size);
  483. if (rc < 0) {
  484. ecryptfs_printk(KERN_ERR, "Error attempting "
  485. "to write lower page; rc = [%d]"
  486. "\n", rc);
  487. goto out;
  488. }
  489. }
  490. rc = 0;
  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 derive IV for "
  512. "extent [0x%.16llx]; rc = [%d]\n",
  513. (unsigned long long)(extent_base + extent_offset), rc);
  514. goto out;
  515. }
  516. if (unlikely(ecryptfs_verbosity > 0)) {
  517. ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
  518. "with iv:\n");
  519. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  520. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  521. "decryption:\n");
  522. ecryptfs_dump_hex((char *)
  523. (page_address(enc_extent_page)
  524. + (extent_offset * crypt_stat->extent_size)),
  525. 8);
  526. }
  527. rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
  528. (extent_offset
  529. * crypt_stat->extent_size),
  530. enc_extent_page, 0,
  531. crypt_stat->extent_size, extent_iv);
  532. if (rc < 0) {
  533. printk(KERN_ERR "%s: Error attempting to decrypt to page with "
  534. "page->index = [%ld], extent_offset = [%ld]; "
  535. "rc = [%d]\n", __func__, page->index, extent_offset,
  536. rc);
  537. goto out;
  538. }
  539. rc = 0;
  540. if (unlikely(ecryptfs_verbosity > 0)) {
  541. ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16llx]; "
  542. "rc = [%d]\n",
  543. (unsigned long long)(extent_base + extent_offset), rc);
  544. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  545. "decryption:\n");
  546. ecryptfs_dump_hex((char *)(page_address(page)
  547. + (extent_offset
  548. * crypt_stat->extent_size)), 8);
  549. }
  550. out:
  551. return rc;
  552. }
  553. /**
  554. * ecryptfs_decrypt_page
  555. * @page: Page mapped from the eCryptfs inode for the file; data read
  556. * and decrypted from the lower file will be written into this
  557. * page
  558. *
  559. * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
  560. * that eCryptfs pages may straddle the lower pages -- for instance,
  561. * if the file was created on a machine with an 8K page size
  562. * (resulting in an 8K header), and then the file is copied onto a
  563. * host with a 32K page size, then when reading page 0 of the eCryptfs
  564. * file, 24K of page 0 of the lower file will be read and decrypted,
  565. * and then 8K of page 1 of the lower file will be read and decrypted.
  566. *
  567. * Returns zero on success; negative on error
  568. */
  569. int ecryptfs_decrypt_page(struct page *page)
  570. {
  571. struct inode *ecryptfs_inode;
  572. struct ecryptfs_crypt_stat *crypt_stat;
  573. char *enc_extent_virt;
  574. struct page *enc_extent_page = NULL;
  575. unsigned long extent_offset;
  576. int rc = 0;
  577. ecryptfs_inode = page->mapping->host;
  578. crypt_stat =
  579. &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
  580. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  581. enc_extent_page = alloc_page(GFP_USER);
  582. if (!enc_extent_page) {
  583. rc = -ENOMEM;
  584. ecryptfs_printk(KERN_ERR, "Error allocating memory for "
  585. "encrypted extent\n");
  586. goto out;
  587. }
  588. enc_extent_virt = kmap(enc_extent_page);
  589. for (extent_offset = 0;
  590. extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
  591. extent_offset++) {
  592. loff_t offset;
  593. ecryptfs_lower_offset_for_extent(
  594. &offset, ((page->index * (PAGE_CACHE_SIZE
  595. / crypt_stat->extent_size))
  596. + extent_offset), crypt_stat);
  597. rc = ecryptfs_read_lower(enc_extent_virt, offset,
  598. crypt_stat->extent_size,
  599. ecryptfs_inode);
  600. if (rc < 0) {
  601. ecryptfs_printk(KERN_ERR, "Error attempting "
  602. "to read lower page; rc = [%d]"
  603. "\n", rc);
  604. goto out;
  605. }
  606. rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
  607. extent_offset);
  608. if (rc) {
  609. printk(KERN_ERR "%s: Error encrypting extent; "
  610. "rc = [%d]\n", __func__, rc);
  611. goto out;
  612. }
  613. }
  614. out:
  615. if (enc_extent_page) {
  616. kunmap(enc_extent_page);
  617. __free_page(enc_extent_page);
  618. }
  619. return rc;
  620. }
  621. /**
  622. * decrypt_scatterlist
  623. * @crypt_stat: Cryptographic context
  624. * @dest_sg: The destination scatterlist to decrypt into
  625. * @src_sg: The source scatterlist to decrypt from
  626. * @size: The number of bytes to decrypt
  627. * @iv: The initialization vector to use for the decryption
  628. *
  629. * Returns the number of bytes decrypted; negative value on error
  630. */
  631. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  632. struct scatterlist *dest_sg,
  633. struct scatterlist *src_sg, int size,
  634. unsigned char *iv)
  635. {
  636. struct blkcipher_desc desc = {
  637. .tfm = crypt_stat->tfm,
  638. .info = iv,
  639. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  640. };
  641. int rc = 0;
  642. /* Consider doing this once, when the file is opened */
  643. mutex_lock(&crypt_stat->cs_tfm_mutex);
  644. rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
  645. crypt_stat->key_size);
  646. if (rc) {
  647. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  648. rc);
  649. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  650. rc = -EINVAL;
  651. goto out;
  652. }
  653. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  654. rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
  655. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  656. if (rc) {
  657. ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
  658. rc);
  659. goto out;
  660. }
  661. rc = size;
  662. out:
  663. return rc;
  664. }
  665. /**
  666. * ecryptfs_encrypt_page_offset
  667. * @crypt_stat: The cryptographic context
  668. * @dst_page: The page to encrypt into
  669. * @dst_offset: The offset in the page to encrypt into
  670. * @src_page: The page to encrypt from
  671. * @src_offset: The offset in the page to encrypt from
  672. * @size: The number of bytes to encrypt
  673. * @iv: The initialization vector to use for the encryption
  674. *
  675. * Returns the number of bytes encrypted
  676. */
  677. static int
  678. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  679. struct page *dst_page, int dst_offset,
  680. struct page *src_page, int src_offset, int size,
  681. unsigned char *iv)
  682. {
  683. struct scatterlist src_sg, dst_sg;
  684. sg_init_table(&src_sg, 1);
  685. sg_init_table(&dst_sg, 1);
  686. sg_set_page(&src_sg, src_page, size, src_offset);
  687. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  688. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  689. }
  690. /**
  691. * ecryptfs_decrypt_page_offset
  692. * @crypt_stat: The cryptographic context
  693. * @dst_page: The page to decrypt into
  694. * @dst_offset: The offset in the page to decrypt into
  695. * @src_page: The page to decrypt from
  696. * @src_offset: The offset in the page to decrypt from
  697. * @size: The number of bytes to decrypt
  698. * @iv: The initialization vector to use for the decryption
  699. *
  700. * Returns the number of bytes decrypted
  701. */
  702. static int
  703. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  704. struct page *dst_page, int dst_offset,
  705. struct page *src_page, int src_offset, int size,
  706. unsigned char *iv)
  707. {
  708. struct scatterlist src_sg, dst_sg;
  709. sg_init_table(&src_sg, 1);
  710. sg_set_page(&src_sg, src_page, size, src_offset);
  711. sg_init_table(&dst_sg, 1);
  712. sg_set_page(&dst_sg, dst_page, size, dst_offset);
  713. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  714. }
  715. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  716. /**
  717. * ecryptfs_init_crypt_ctx
  718. * @crypt_stat: Uninitialized crypt stats structure
  719. *
  720. * Initialize the crypto context.
  721. *
  722. * TODO: Performance: Keep a cache of initialized cipher contexts;
  723. * only init if needed
  724. */
  725. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  726. {
  727. char *full_alg_name;
  728. int rc = -EINVAL;
  729. if (!crypt_stat->cipher) {
  730. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  731. goto out;
  732. }
  733. ecryptfs_printk(KERN_DEBUG,
  734. "Initializing cipher [%s]; strlen = [%d]; "
  735. "key_size_bits = [%zd]\n",
  736. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  737. crypt_stat->key_size << 3);
  738. if (crypt_stat->tfm) {
  739. rc = 0;
  740. goto out;
  741. }
  742. mutex_lock(&crypt_stat->cs_tfm_mutex);
  743. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  744. crypt_stat->cipher, "cbc");
  745. if (rc)
  746. goto out_unlock;
  747. crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  748. CRYPTO_ALG_ASYNC);
  749. kfree(full_alg_name);
  750. if (IS_ERR(crypt_stat->tfm)) {
  751. rc = PTR_ERR(crypt_stat->tfm);
  752. crypt_stat->tfm = NULL;
  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->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  788. else {
  789. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
  790. crypt_stat->metadata_size =
  791. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  792. else
  793. crypt_stat->metadata_size = 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. * ecryptfs_validate_marker - check for the ecryptfs marker
  968. * @data: The data block in which to check
  969. *
  970. * Returns zero if marker found; -EINVAL if not found
  971. */
  972. static int ecryptfs_validate_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 0;
  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 -EINVAL;
  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. void ecryptfs_write_crypt_stat_flags(char *page_virt,
  1041. 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(struct inode *inode)
  1129. {
  1130. u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
  1131. u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
  1132. int rc;
  1133. rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
  1134. inode);
  1135. if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
  1136. return rc >= 0 ? -EINVAL : rc;
  1137. rc = ecryptfs_validate_marker(marker);
  1138. if (!rc)
  1139. ecryptfs_i_size_init(file_size, inode);
  1140. return rc;
  1141. }
  1142. void
  1143. ecryptfs_write_header_metadata(char *virt,
  1144. struct ecryptfs_crypt_stat *crypt_stat,
  1145. size_t *written)
  1146. {
  1147. u32 header_extent_size;
  1148. u16 num_header_extents_at_front;
  1149. header_extent_size = (u32)crypt_stat->extent_size;
  1150. num_header_extents_at_front =
  1151. (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
  1152. put_unaligned_be32(header_extent_size, virt);
  1153. virt += 4;
  1154. put_unaligned_be16(num_header_extents_at_front, virt);
  1155. (*written) = 6;
  1156. }
  1157. struct kmem_cache *ecryptfs_header_cache;
  1158. /**
  1159. * ecryptfs_write_headers_virt
  1160. * @page_virt: The virtual address to write the headers to
  1161. * @max: The size of memory allocated at page_virt
  1162. * @size: Set to the number of bytes written by this function
  1163. * @crypt_stat: The cryptographic context
  1164. * @ecryptfs_dentry: The eCryptfs dentry
  1165. *
  1166. * Format version: 1
  1167. *
  1168. * Header Extent:
  1169. * Octets 0-7: Unencrypted file size (big-endian)
  1170. * Octets 8-15: eCryptfs special marker
  1171. * Octets 16-19: Flags
  1172. * Octet 16: File format version number (between 0 and 255)
  1173. * Octets 17-18: Reserved
  1174. * Octet 19: Bit 1 (lsb): Reserved
  1175. * Bit 2: Encrypted?
  1176. * Bits 3-8: Reserved
  1177. * Octets 20-23: Header extent size (big-endian)
  1178. * Octets 24-25: Number of header extents at front of file
  1179. * (big-endian)
  1180. * Octet 26: Begin RFC 2440 authentication token packet set
  1181. * Data Extent 0:
  1182. * Lower data (CBC encrypted)
  1183. * Data Extent 1:
  1184. * Lower data (CBC encrypted)
  1185. * ...
  1186. *
  1187. * Returns zero on success
  1188. */
  1189. static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
  1190. size_t *size,
  1191. struct ecryptfs_crypt_stat *crypt_stat,
  1192. struct dentry *ecryptfs_dentry)
  1193. {
  1194. int rc;
  1195. size_t written;
  1196. size_t offset;
  1197. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1198. write_ecryptfs_marker((page_virt + offset), &written);
  1199. offset += written;
  1200. ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
  1201. &written);
  1202. offset += written;
  1203. ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
  1204. &written);
  1205. offset += written;
  1206. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1207. ecryptfs_dentry, &written,
  1208. max - offset);
  1209. if (rc)
  1210. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1211. "set; rc = [%d]\n", rc);
  1212. if (size) {
  1213. offset += written;
  1214. *size = offset;
  1215. }
  1216. return rc;
  1217. }
  1218. static int
  1219. ecryptfs_write_metadata_to_contents(struct dentry *ecryptfs_dentry,
  1220. char *virt, size_t virt_len)
  1221. {
  1222. int rc;
  1223. rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
  1224. 0, virt_len);
  1225. if (rc < 0)
  1226. printk(KERN_ERR "%s: Error attempting to write header "
  1227. "information to lower file; rc = [%d]\n", __func__, rc);
  1228. else
  1229. rc = 0;
  1230. return rc;
  1231. }
  1232. static int
  1233. ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
  1234. char *page_virt, size_t size)
  1235. {
  1236. int rc;
  1237. rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
  1238. size, 0);
  1239. return rc;
  1240. }
  1241. static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
  1242. unsigned int order)
  1243. {
  1244. struct page *page;
  1245. page = alloc_pages(gfp_mask | __GFP_ZERO, order);
  1246. if (page)
  1247. return (unsigned long) page_address(page);
  1248. return 0;
  1249. }
  1250. /**
  1251. * ecryptfs_write_metadata
  1252. * @ecryptfs_dentry: The eCryptfs dentry
  1253. *
  1254. * Write the file headers out. This will likely involve a userspace
  1255. * callout, in which the session key is encrypted with one or more
  1256. * public keys and/or the passphrase necessary to do the encryption is
  1257. * retrieved via a prompt. Exactly what happens at this point should
  1258. * be policy-dependent.
  1259. *
  1260. * Returns zero on success; non-zero on error
  1261. */
  1262. int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
  1263. {
  1264. struct ecryptfs_crypt_stat *crypt_stat =
  1265. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  1266. unsigned int order;
  1267. char *virt;
  1268. size_t virt_len;
  1269. size_t size = 0;
  1270. int rc = 0;
  1271. if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  1272. if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  1273. printk(KERN_ERR "Key is invalid; bailing out\n");
  1274. rc = -EINVAL;
  1275. goto out;
  1276. }
  1277. } else {
  1278. printk(KERN_WARNING "%s: Encrypted flag not set\n",
  1279. __func__);
  1280. rc = -EINVAL;
  1281. goto out;
  1282. }
  1283. virt_len = crypt_stat->metadata_size;
  1284. order = get_order(virt_len);
  1285. /* Released in this function */
  1286. virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
  1287. if (!virt) {
  1288. printk(KERN_ERR "%s: Out of memory\n", __func__);
  1289. rc = -ENOMEM;
  1290. goto out;
  1291. }
  1292. /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
  1293. rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
  1294. ecryptfs_dentry);
  1295. if (unlikely(rc)) {
  1296. printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
  1297. __func__, rc);
  1298. goto out_free;
  1299. }
  1300. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1301. rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
  1302. size);
  1303. else
  1304. rc = ecryptfs_write_metadata_to_contents(ecryptfs_dentry, virt,
  1305. virt_len);
  1306. if (rc) {
  1307. printk(KERN_ERR "%s: Error writing metadata out to lower file; "
  1308. "rc = [%d]\n", __func__, rc);
  1309. goto out_free;
  1310. }
  1311. out_free:
  1312. free_pages((unsigned long)virt, order);
  1313. out:
  1314. return rc;
  1315. }
  1316. #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
  1317. #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
  1318. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1319. char *virt, int *bytes_read,
  1320. int validate_header_size)
  1321. {
  1322. int rc = 0;
  1323. u32 header_extent_size;
  1324. u16 num_header_extents_at_front;
  1325. header_extent_size = get_unaligned_be32(virt);
  1326. virt += sizeof(__be32);
  1327. num_header_extents_at_front = get_unaligned_be16(virt);
  1328. crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
  1329. * (size_t)header_extent_size));
  1330. (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
  1331. if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
  1332. && (crypt_stat->metadata_size
  1333. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
  1334. rc = -EINVAL;
  1335. printk(KERN_WARNING "Invalid header size: [%zd]\n",
  1336. crypt_stat->metadata_size);
  1337. }
  1338. return rc;
  1339. }
  1340. /**
  1341. * set_default_header_data
  1342. * @crypt_stat: The cryptographic context
  1343. *
  1344. * For version 0 file format; this function is only for backwards
  1345. * compatibility for files created with the prior versions of
  1346. * eCryptfs.
  1347. */
  1348. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1349. {
  1350. crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  1351. }
  1352. void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
  1353. {
  1354. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  1355. struct ecryptfs_crypt_stat *crypt_stat;
  1356. u64 file_size;
  1357. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  1358. mount_crypt_stat =
  1359. &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
  1360. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  1361. file_size = i_size_read(ecryptfs_inode_to_lower(inode));
  1362. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  1363. file_size += crypt_stat->metadata_size;
  1364. } else
  1365. file_size = get_unaligned_be64(page_virt);
  1366. i_size_write(inode, (loff_t)file_size);
  1367. crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
  1368. }
  1369. /**
  1370. * ecryptfs_read_headers_virt
  1371. * @page_virt: The virtual address into which to read the headers
  1372. * @crypt_stat: The cryptographic context
  1373. * @ecryptfs_dentry: The eCryptfs dentry
  1374. * @validate_header_size: Whether to validate the header size while reading
  1375. *
  1376. * Read/parse the header data. The header format is detailed in the
  1377. * comment block for the ecryptfs_write_headers_virt() function.
  1378. *
  1379. * Returns zero on success
  1380. */
  1381. static int ecryptfs_read_headers_virt(char *page_virt,
  1382. struct ecryptfs_crypt_stat *crypt_stat,
  1383. struct dentry *ecryptfs_dentry,
  1384. int validate_header_size)
  1385. {
  1386. int rc = 0;
  1387. int offset;
  1388. int bytes_read;
  1389. ecryptfs_set_default_sizes(crypt_stat);
  1390. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1391. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1392. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1393. rc = ecryptfs_validate_marker(page_virt + offset);
  1394. if (rc)
  1395. goto out;
  1396. if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
  1397. ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
  1398. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1399. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1400. &bytes_read);
  1401. if (rc) {
  1402. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1403. goto out;
  1404. }
  1405. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1406. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1407. "file version [%d] is supported by this "
  1408. "version of eCryptfs\n",
  1409. crypt_stat->file_version,
  1410. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1411. rc = -EINVAL;
  1412. goto out;
  1413. }
  1414. offset += bytes_read;
  1415. if (crypt_stat->file_version >= 1) {
  1416. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1417. &bytes_read, validate_header_size);
  1418. if (rc) {
  1419. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1420. "metadata; rc = [%d]\n", rc);
  1421. }
  1422. offset += bytes_read;
  1423. } else
  1424. set_default_header_data(crypt_stat);
  1425. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1426. ecryptfs_dentry);
  1427. out:
  1428. return rc;
  1429. }
  1430. /**
  1431. * ecryptfs_read_xattr_region
  1432. * @page_virt: The vitual address into which to read the xattr data
  1433. * @ecryptfs_inode: The eCryptfs inode
  1434. *
  1435. * Attempts to read the crypto metadata from the extended attribute
  1436. * region of the lower file.
  1437. *
  1438. * Returns zero on success; non-zero on error
  1439. */
  1440. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
  1441. {
  1442. struct dentry *lower_dentry =
  1443. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  1444. ssize_t size;
  1445. int rc = 0;
  1446. size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
  1447. page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
  1448. if (size < 0) {
  1449. if (unlikely(ecryptfs_verbosity > 0))
  1450. printk(KERN_INFO "Error attempting to read the [%s] "
  1451. "xattr from the lower file; return value = "
  1452. "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
  1453. rc = -EINVAL;
  1454. goto out;
  1455. }
  1456. out:
  1457. return rc;
  1458. }
  1459. int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
  1460. struct inode *inode)
  1461. {
  1462. u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
  1463. u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
  1464. int rc;
  1465. rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
  1466. ECRYPTFS_XATTR_NAME, file_size,
  1467. ECRYPTFS_SIZE_AND_MARKER_BYTES);
  1468. if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
  1469. return rc >= 0 ? -EINVAL : rc;
  1470. rc = ecryptfs_validate_marker(marker);
  1471. if (!rc)
  1472. ecryptfs_i_size_init(file_size, inode);
  1473. return rc;
  1474. }
  1475. /**
  1476. * ecryptfs_read_metadata
  1477. *
  1478. * Common entry point for reading file metadata. From here, we could
  1479. * retrieve the header information from the header region of the file,
  1480. * the xattr region of the file, or some other repostory that is
  1481. * stored separately from the file itself. The current implementation
  1482. * supports retrieving the metadata information from the file contents
  1483. * and from the xattr region.
  1484. *
  1485. * Returns zero if valid headers found and parsed; non-zero otherwise
  1486. */
  1487. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
  1488. {
  1489. int rc = 0;
  1490. char *page_virt = NULL;
  1491. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  1492. struct ecryptfs_crypt_stat *crypt_stat =
  1493. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  1494. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1495. &ecryptfs_superblock_to_private(
  1496. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1497. ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
  1498. mount_crypt_stat);
  1499. /* Read the first page from the underlying file */
  1500. page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
  1501. if (!page_virt) {
  1502. rc = -ENOMEM;
  1503. printk(KERN_ERR "%s: Unable to allocate page_virt\n",
  1504. __func__);
  1505. goto out;
  1506. }
  1507. rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
  1508. ecryptfs_inode);
  1509. if (rc >= 0)
  1510. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1511. ecryptfs_dentry,
  1512. ECRYPTFS_VALIDATE_HEADER_SIZE);
  1513. if (rc) {
  1514. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1515. rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
  1516. if (rc) {
  1517. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1518. "file header region or xattr region\n");
  1519. rc = -EINVAL;
  1520. goto out;
  1521. }
  1522. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1523. ecryptfs_dentry,
  1524. ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
  1525. if (rc) {
  1526. printk(KERN_DEBUG "Valid eCryptfs headers not found in "
  1527. "file xattr region either\n");
  1528. rc = -EINVAL;
  1529. }
  1530. if (crypt_stat->mount_crypt_stat->flags
  1531. & ECRYPTFS_XATTR_METADATA_ENABLED) {
  1532. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  1533. } else {
  1534. printk(KERN_WARNING "Attempt to access file with "
  1535. "crypto metadata only in the extended attribute "
  1536. "region, but eCryptfs was mounted without "
  1537. "xattr support enabled. eCryptfs will not treat "
  1538. "this like an encrypted file.\n");
  1539. rc = -EINVAL;
  1540. }
  1541. }
  1542. out:
  1543. if (page_virt) {
  1544. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1545. kmem_cache_free(ecryptfs_header_cache, page_virt);
  1546. }
  1547. return rc;
  1548. }
  1549. /**
  1550. * ecryptfs_encrypt_filename - encrypt filename
  1551. *
  1552. * CBC-encrypts the filename. We do not want to encrypt the same
  1553. * filename with the same key and IV, which may happen with hard
  1554. * links, so we prepend random bits to each filename.
  1555. *
  1556. * Returns zero on success; non-zero otherwise
  1557. */
  1558. static int
  1559. ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
  1560. struct ecryptfs_crypt_stat *crypt_stat,
  1561. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  1562. {
  1563. int rc = 0;
  1564. filename->encrypted_filename = NULL;
  1565. filename->encrypted_filename_size = 0;
  1566. if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
  1567. || (mount_crypt_stat && (mount_crypt_stat->flags
  1568. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
  1569. size_t packet_size;
  1570. size_t remaining_bytes;
  1571. rc = ecryptfs_write_tag_70_packet(
  1572. NULL, NULL,
  1573. &filename->encrypted_filename_size,
  1574. mount_crypt_stat, NULL,
  1575. filename->filename_size);
  1576. if (rc) {
  1577. printk(KERN_ERR "%s: Error attempting to get packet "
  1578. "size for tag 72; rc = [%d]\n", __func__,
  1579. rc);
  1580. filename->encrypted_filename_size = 0;
  1581. goto out;
  1582. }
  1583. filename->encrypted_filename =
  1584. kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
  1585. if (!filename->encrypted_filename) {
  1586. printk(KERN_ERR "%s: Out of memory whilst attempting "
  1587. "to kmalloc [%zd] bytes\n", __func__,
  1588. filename->encrypted_filename_size);
  1589. rc = -ENOMEM;
  1590. goto out;
  1591. }
  1592. remaining_bytes = filename->encrypted_filename_size;
  1593. rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
  1594. &remaining_bytes,
  1595. &packet_size,
  1596. mount_crypt_stat,
  1597. filename->filename,
  1598. filename->filename_size);
  1599. if (rc) {
  1600. printk(KERN_ERR "%s: Error attempting to generate "
  1601. "tag 70 packet; rc = [%d]\n", __func__,
  1602. rc);
  1603. kfree(filename->encrypted_filename);
  1604. filename->encrypted_filename = NULL;
  1605. filename->encrypted_filename_size = 0;
  1606. goto out;
  1607. }
  1608. filename->encrypted_filename_size = packet_size;
  1609. } else {
  1610. printk(KERN_ERR "%s: No support for requested filename "
  1611. "encryption method in this release\n", __func__);
  1612. rc = -EOPNOTSUPP;
  1613. goto out;
  1614. }
  1615. out:
  1616. return rc;
  1617. }
  1618. static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
  1619. const char *name, size_t name_size)
  1620. {
  1621. int rc = 0;
  1622. (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
  1623. if (!(*copied_name)) {
  1624. rc = -ENOMEM;
  1625. goto out;
  1626. }
  1627. memcpy((void *)(*copied_name), (void *)name, name_size);
  1628. (*copied_name)[(name_size)] = '\0'; /* Only for convenience
  1629. * in printing out the
  1630. * string in debug
  1631. * messages */
  1632. (*copied_name_size) = name_size;
  1633. out:
  1634. return rc;
  1635. }
  1636. /**
  1637. * ecryptfs_process_key_cipher - Perform key cipher initialization.
  1638. * @key_tfm: Crypto context for key material, set by this function
  1639. * @cipher_name: Name of the cipher
  1640. * @key_size: Size of the key in bytes
  1641. *
  1642. * Returns zero on success. Any crypto_tfm structs allocated here
  1643. * should be released by other functions, such as on a superblock put
  1644. * event, regardless of whether this function succeeds for fails.
  1645. */
  1646. static int
  1647. ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
  1648. char *cipher_name, size_t *key_size)
  1649. {
  1650. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1651. char *full_alg_name = NULL;
  1652. int rc;
  1653. *key_tfm = NULL;
  1654. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1655. rc = -EINVAL;
  1656. printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
  1657. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1658. goto out;
  1659. }
  1660. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1661. "ecb");
  1662. if (rc)
  1663. goto out;
  1664. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1665. if (IS_ERR(*key_tfm)) {
  1666. rc = PTR_ERR(*key_tfm);
  1667. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1668. "[%s]; rc = [%d]\n", full_alg_name, rc);
  1669. goto out;
  1670. }
  1671. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1672. if (*key_size == 0) {
  1673. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1674. *key_size = alg->max_keysize;
  1675. }
  1676. get_random_bytes(dummy_key, *key_size);
  1677. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1678. if (rc) {
  1679. printk(KERN_ERR "Error attempting to set key of size [%zd] for "
  1680. "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
  1681. rc);
  1682. rc = -EINVAL;
  1683. goto out;
  1684. }
  1685. out:
  1686. kfree(full_alg_name);
  1687. return rc;
  1688. }
  1689. struct kmem_cache *ecryptfs_key_tfm_cache;
  1690. static struct list_head key_tfm_list;
  1691. struct mutex key_tfm_list_mutex;
  1692. int __init ecryptfs_init_crypto(void)
  1693. {
  1694. mutex_init(&key_tfm_list_mutex);
  1695. INIT_LIST_HEAD(&key_tfm_list);
  1696. return 0;
  1697. }
  1698. /**
  1699. * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
  1700. *
  1701. * Called only at module unload time
  1702. */
  1703. int ecryptfs_destroy_crypto(void)
  1704. {
  1705. struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
  1706. mutex_lock(&key_tfm_list_mutex);
  1707. list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
  1708. key_tfm_list) {
  1709. list_del(&key_tfm->key_tfm_list);
  1710. if (key_tfm->key_tfm)
  1711. crypto_free_blkcipher(key_tfm->key_tfm);
  1712. kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
  1713. }
  1714. mutex_unlock(&key_tfm_list_mutex);
  1715. return 0;
  1716. }
  1717. int
  1718. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  1719. size_t key_size)
  1720. {
  1721. struct ecryptfs_key_tfm *tmp_tfm;
  1722. int rc = 0;
  1723. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1724. tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
  1725. if (key_tfm != NULL)
  1726. (*key_tfm) = tmp_tfm;
  1727. if (!tmp_tfm) {
  1728. rc = -ENOMEM;
  1729. printk(KERN_ERR "Error attempting to allocate from "
  1730. "ecryptfs_key_tfm_cache\n");
  1731. goto out;
  1732. }
  1733. mutex_init(&tmp_tfm->key_tfm_mutex);
  1734. strncpy(tmp_tfm->cipher_name, cipher_name,
  1735. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  1736. tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  1737. tmp_tfm->key_size = key_size;
  1738. rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
  1739. tmp_tfm->cipher_name,
  1740. &tmp_tfm->key_size);
  1741. if (rc) {
  1742. printk(KERN_ERR "Error attempting to initialize key TFM "
  1743. "cipher with name = [%s]; rc = [%d]\n",
  1744. tmp_tfm->cipher_name, rc);
  1745. kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
  1746. if (key_tfm != NULL)
  1747. (*key_tfm) = NULL;
  1748. goto out;
  1749. }
  1750. list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
  1751. out:
  1752. return rc;
  1753. }
  1754. /**
  1755. * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
  1756. * @cipher_name: the name of the cipher to search for
  1757. * @key_tfm: set to corresponding tfm if found
  1758. *
  1759. * Searches for cached key_tfm matching @cipher_name
  1760. * Must be called with &key_tfm_list_mutex held
  1761. * Returns 1 if found, with @key_tfm set
  1762. * Returns 0 if not found, with @key_tfm set to NULL
  1763. */
  1764. int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
  1765. {
  1766. struct ecryptfs_key_tfm *tmp_key_tfm;
  1767. BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
  1768. list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
  1769. if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
  1770. if (key_tfm)
  1771. (*key_tfm) = tmp_key_tfm;
  1772. return 1;
  1773. }
  1774. }
  1775. if (key_tfm)
  1776. (*key_tfm) = NULL;
  1777. return 0;
  1778. }
  1779. /**
  1780. * ecryptfs_get_tfm_and_mutex_for_cipher_name
  1781. *
  1782. * @tfm: set to cached tfm found, or new tfm created
  1783. * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
  1784. * @cipher_name: the name of the cipher to search for and/or add
  1785. *
  1786. * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
  1787. * Searches for cached item first, and creates new if not found.
  1788. * Returns 0 on success, non-zero if adding new cipher failed
  1789. */
  1790. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
  1791. struct mutex **tfm_mutex,
  1792. char *cipher_name)
  1793. {
  1794. struct ecryptfs_key_tfm *key_tfm;
  1795. int rc = 0;
  1796. (*tfm) = NULL;
  1797. (*tfm_mutex) = NULL;
  1798. mutex_lock(&key_tfm_list_mutex);
  1799. if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
  1800. rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
  1801. if (rc) {
  1802. printk(KERN_ERR "Error adding new key_tfm to list; "
  1803. "rc = [%d]\n", rc);
  1804. goto out;
  1805. }
  1806. }
  1807. (*tfm) = key_tfm->key_tfm;
  1808. (*tfm_mutex) = &key_tfm->key_tfm_mutex;
  1809. out:
  1810. mutex_unlock(&key_tfm_list_mutex);
  1811. return rc;
  1812. }
  1813. /* 64 characters forming a 6-bit target field */
  1814. static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
  1815. "EFGHIJKLMNOPQRST"
  1816. "UVWXYZabcdefghij"
  1817. "klmnopqrstuvwxyz");
  1818. /* We could either offset on every reverse map or just pad some 0x00's
  1819. * at the front here */
  1820. static const unsigned char filename_rev_map[] = {
  1821. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
  1822. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
  1823. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
  1824. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
  1825. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
  1826. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
  1827. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
  1828. 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
  1829. 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
  1830. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
  1831. 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
  1832. 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
  1833. 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
  1834. 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
  1835. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
  1836. 0x3D, 0x3E, 0x3F
  1837. };
  1838. /**
  1839. * ecryptfs_encode_for_filename
  1840. * @dst: Destination location for encoded filename
  1841. * @dst_size: Size of the encoded filename in bytes
  1842. * @src: Source location for the filename to encode
  1843. * @src_size: Size of the source in bytes
  1844. */
  1845. void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
  1846. unsigned char *src, size_t src_size)
  1847. {
  1848. size_t num_blocks;
  1849. size_t block_num = 0;
  1850. size_t dst_offset = 0;
  1851. unsigned char last_block[3];
  1852. if (src_size == 0) {
  1853. (*dst_size) = 0;
  1854. goto out;
  1855. }
  1856. num_blocks = (src_size / 3);
  1857. if ((src_size % 3) == 0) {
  1858. memcpy(last_block, (&src[src_size - 3]), 3);
  1859. } else {
  1860. num_blocks++;
  1861. last_block[2] = 0x00;
  1862. switch (src_size % 3) {
  1863. case 1:
  1864. last_block[0] = src[src_size - 1];
  1865. last_block[1] = 0x00;
  1866. break;
  1867. case 2:
  1868. last_block[0] = src[src_size - 2];
  1869. last_block[1] = src[src_size - 1];
  1870. }
  1871. }
  1872. (*dst_size) = (num_blocks * 4);
  1873. if (!dst)
  1874. goto out;
  1875. while (block_num < num_blocks) {
  1876. unsigned char *src_block;
  1877. unsigned char dst_block[4];
  1878. if (block_num == (num_blocks - 1))
  1879. src_block = last_block;
  1880. else
  1881. src_block = &src[block_num * 3];
  1882. dst_block[0] = ((src_block[0] >> 2) & 0x3F);
  1883. dst_block[1] = (((src_block[0] << 4) & 0x30)
  1884. | ((src_block[1] >> 4) & 0x0F));
  1885. dst_block[2] = (((src_block[1] << 2) & 0x3C)
  1886. | ((src_block[2] >> 6) & 0x03));
  1887. dst_block[3] = (src_block[2] & 0x3F);
  1888. dst[dst_offset++] = portable_filename_chars[dst_block[0]];
  1889. dst[dst_offset++] = portable_filename_chars[dst_block[1]];
  1890. dst[dst_offset++] = portable_filename_chars[dst_block[2]];
  1891. dst[dst_offset++] = portable_filename_chars[dst_block[3]];
  1892. block_num++;
  1893. }
  1894. out:
  1895. return;
  1896. }
  1897. /**
  1898. * ecryptfs_decode_from_filename
  1899. * @dst: If NULL, this function only sets @dst_size and returns. If
  1900. * non-NULL, this function decodes the encoded octets in @src
  1901. * into the memory that @dst points to.
  1902. * @dst_size: Set to the size of the decoded string.
  1903. * @src: The encoded set of octets to decode.
  1904. * @src_size: The size of the encoded set of octets to decode.
  1905. */
  1906. static void
  1907. ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
  1908. const unsigned char *src, size_t src_size)
  1909. {
  1910. u8 current_bit_offset = 0;
  1911. size_t src_byte_offset = 0;
  1912. size_t dst_byte_offset = 0;
  1913. if (dst == NULL) {
  1914. /* Not exact; conservatively long. Every block of 4
  1915. * encoded characters decodes into a block of 3
  1916. * decoded characters. This segment of code provides
  1917. * the caller with the maximum amount of allocated
  1918. * space that @dst will need to point to in a
  1919. * subsequent call. */
  1920. (*dst_size) = (((src_size + 1) * 3) / 4);
  1921. goto out;
  1922. }
  1923. while (src_byte_offset < src_size) {
  1924. unsigned char src_byte =
  1925. filename_rev_map[(int)src[src_byte_offset]];
  1926. switch (current_bit_offset) {
  1927. case 0:
  1928. dst[dst_byte_offset] = (src_byte << 2);
  1929. current_bit_offset = 6;
  1930. break;
  1931. case 6:
  1932. dst[dst_byte_offset++] |= (src_byte >> 4);
  1933. dst[dst_byte_offset] = ((src_byte & 0xF)
  1934. << 4);
  1935. current_bit_offset = 4;
  1936. break;
  1937. case 4:
  1938. dst[dst_byte_offset++] |= (src_byte >> 2);
  1939. dst[dst_byte_offset] = (src_byte << 6);
  1940. current_bit_offset = 2;
  1941. break;
  1942. case 2:
  1943. dst[dst_byte_offset++] |= (src_byte);
  1944. dst[dst_byte_offset] = 0;
  1945. current_bit_offset = 0;
  1946. break;
  1947. }
  1948. src_byte_offset++;
  1949. }
  1950. (*dst_size) = dst_byte_offset;
  1951. out:
  1952. return;
  1953. }
  1954. /**
  1955. * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
  1956. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1957. * @name: The plaintext name
  1958. * @length: The length of the plaintext
  1959. * @encoded_name: The encypted name
  1960. *
  1961. * Encrypts and encodes a filename into something that constitutes a
  1962. * valid filename for a filesystem, with printable characters.
  1963. *
  1964. * We assume that we have a properly initialized crypto context,
  1965. * pointed to by crypt_stat->tfm.
  1966. *
  1967. * Returns zero on success; non-zero on otherwise
  1968. */
  1969. int ecryptfs_encrypt_and_encode_filename(
  1970. char **encoded_name,
  1971. size_t *encoded_name_size,
  1972. struct ecryptfs_crypt_stat *crypt_stat,
  1973. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  1974. const char *name, size_t name_size)
  1975. {
  1976. size_t encoded_name_no_prefix_size;
  1977. int rc = 0;
  1978. (*encoded_name) = NULL;
  1979. (*encoded_name_size) = 0;
  1980. if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
  1981. || (mount_crypt_stat && (mount_crypt_stat->flags
  1982. & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
  1983. struct ecryptfs_filename *filename;
  1984. filename = kzalloc(sizeof(*filename), GFP_KERNEL);
  1985. if (!filename) {
  1986. printk(KERN_ERR "%s: Out of memory whilst attempting "
  1987. "to kzalloc [%zd] bytes\n", __func__,
  1988. sizeof(*filename));
  1989. rc = -ENOMEM;
  1990. goto out;
  1991. }
  1992. filename->filename = (char *)name;
  1993. filename->filename_size = name_size;
  1994. rc = ecryptfs_encrypt_filename(filename, crypt_stat,
  1995. mount_crypt_stat);
  1996. if (rc) {
  1997. printk(KERN_ERR "%s: Error attempting to encrypt "
  1998. "filename; rc = [%d]\n", __func__, rc);
  1999. kfree(filename);
  2000. goto out;
  2001. }
  2002. ecryptfs_encode_for_filename(
  2003. NULL, &encoded_name_no_prefix_size,
  2004. filename->encrypted_filename,
  2005. filename->encrypted_filename_size);
  2006. if ((crypt_stat && (crypt_stat->flags
  2007. & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
  2008. || (mount_crypt_stat
  2009. && (mount_crypt_stat->flags
  2010. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
  2011. (*encoded_name_size) =
  2012. (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
  2013. + encoded_name_no_prefix_size);
  2014. else
  2015. (*encoded_name_size) =
  2016. (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
  2017. + encoded_name_no_prefix_size);
  2018. (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
  2019. if (!(*encoded_name)) {
  2020. printk(KERN_ERR "%s: Out of memory whilst attempting "
  2021. "to kzalloc [%zd] bytes\n", __func__,
  2022. (*encoded_name_size));
  2023. rc = -ENOMEM;
  2024. kfree(filename->encrypted_filename);
  2025. kfree(filename);
  2026. goto out;
  2027. }
  2028. if ((crypt_stat && (crypt_stat->flags
  2029. & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
  2030. || (mount_crypt_stat
  2031. && (mount_crypt_stat->flags
  2032. & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
  2033. memcpy((*encoded_name),
  2034. ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
  2035. ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
  2036. ecryptfs_encode_for_filename(
  2037. ((*encoded_name)
  2038. + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
  2039. &encoded_name_no_prefix_size,
  2040. filename->encrypted_filename,
  2041. filename->encrypted_filename_size);
  2042. (*encoded_name_size) =
  2043. (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
  2044. + encoded_name_no_prefix_size);
  2045. (*encoded_name)[(*encoded_name_size)] = '\0';
  2046. } else {
  2047. rc = -EOPNOTSUPP;
  2048. }
  2049. if (rc) {
  2050. printk(KERN_ERR "%s: Error attempting to encode "
  2051. "encrypted filename; rc = [%d]\n", __func__,
  2052. rc);
  2053. kfree((*encoded_name));
  2054. (*encoded_name) = NULL;
  2055. (*encoded_name_size) = 0;
  2056. }
  2057. kfree(filename->encrypted_filename);
  2058. kfree(filename);
  2059. } else {
  2060. rc = ecryptfs_copy_filename(encoded_name,
  2061. encoded_name_size,
  2062. name, name_size);
  2063. }
  2064. out:
  2065. return rc;
  2066. }
  2067. /**
  2068. * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
  2069. * @plaintext_name: The plaintext name
  2070. * @plaintext_name_size: The plaintext name size
  2071. * @ecryptfs_dir_dentry: eCryptfs directory dentry
  2072. * @name: The filename in cipher text
  2073. * @name_size: The cipher text name size
  2074. *
  2075. * Decrypts and decodes the filename.
  2076. *
  2077. * Returns zero on error; non-zero otherwise
  2078. */
  2079. int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
  2080. size_t *plaintext_name_size,
  2081. struct dentry *ecryptfs_dir_dentry,
  2082. const char *name, size_t name_size)
  2083. {
  2084. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  2085. &ecryptfs_superblock_to_private(
  2086. ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
  2087. char *decoded_name;
  2088. size_t decoded_name_size;
  2089. size_t packet_size;
  2090. int rc = 0;
  2091. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  2092. && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  2093. && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
  2094. && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
  2095. ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
  2096. const char *orig_name = name;
  2097. size_t orig_name_size = name_size;
  2098. name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
  2099. name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
  2100. ecryptfs_decode_from_filename(NULL, &decoded_name_size,
  2101. name, name_size);
  2102. decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
  2103. if (!decoded_name) {
  2104. printk(KERN_ERR "%s: Out of memory whilst attempting "
  2105. "to kmalloc [%zd] bytes\n", __func__,
  2106. decoded_name_size);
  2107. rc = -ENOMEM;
  2108. goto out;
  2109. }
  2110. ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
  2111. name, name_size);
  2112. rc = ecryptfs_parse_tag_70_packet(plaintext_name,
  2113. plaintext_name_size,
  2114. &packet_size,
  2115. mount_crypt_stat,
  2116. decoded_name,
  2117. decoded_name_size);
  2118. if (rc) {
  2119. printk(KERN_INFO "%s: Could not parse tag 70 packet "
  2120. "from filename; copying through filename "
  2121. "as-is\n", __func__);
  2122. rc = ecryptfs_copy_filename(plaintext_name,
  2123. plaintext_name_size,
  2124. orig_name, orig_name_size);
  2125. goto out_free;
  2126. }
  2127. } else {
  2128. rc = ecryptfs_copy_filename(plaintext_name,
  2129. plaintext_name_size,
  2130. name, name_size);
  2131. goto out;
  2132. }
  2133. out_free:
  2134. kfree(decoded_name);
  2135. out:
  2136. return rc;
  2137. }