crypto.c 68 KB

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