crypto.c 64 KB

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