crypto.c 75 KB

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