crypto.c 69 KB

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