crypto.c 65 KB

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