crypto.c 69 KB

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