crypto.c 67 KB

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