crypto.c 68 KB

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