main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 1997-2003 Erez Zadok
  5. * Copyright (C) 2001-2003 Stony Brook University
  6. * Copyright (C) 2004-2007 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <[email protected]>
  8. * Michael C. Thompson <[email protected]>
  9. * Tyler Hicks <[email protected]>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. * 02111-1307, USA.
  25. */
  26. #include <linux/dcache.h>
  27. #include <linux/file.h>
  28. #include <linux/module.h>
  29. #include <linux/namei.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/mount.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/key.h>
  34. #include <linux/parser.h>
  35. #include <linux/fs_stack.h>
  36. #include <linux/slab.h>
  37. #include <linux/magic.h>
  38. #include "ecryptfs_kernel.h"
  39. /**
  40. * Module parameter that defines the ecryptfs_verbosity level.
  41. */
  42. int ecryptfs_verbosity = 0;
  43. module_param(ecryptfs_verbosity, int, 0);
  44. MODULE_PARM_DESC(ecryptfs_verbosity,
  45. "Initial verbosity level (0 or 1; defaults to "
  46. "0, which is Quiet)");
  47. /**
  48. * Module parameter that defines the number of message buffer elements
  49. */
  50. unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
  51. module_param(ecryptfs_message_buf_len, uint, 0);
  52. MODULE_PARM_DESC(ecryptfs_message_buf_len,
  53. "Number of message buffer elements");
  54. /**
  55. * Module parameter that defines the maximum guaranteed amount of time to wait
  56. * for a response from ecryptfsd. The actual sleep time will be, more than
  57. * likely, a small amount greater than this specified value, but only less if
  58. * the message successfully arrives.
  59. */
  60. signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
  61. module_param(ecryptfs_message_wait_timeout, long, 0);
  62. MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
  63. "Maximum number of seconds that an operation will "
  64. "sleep while waiting for a message response from "
  65. "userspace");
  66. /**
  67. * Module parameter that is an estimate of the maximum number of users
  68. * that will be concurrently using eCryptfs. Set this to the right
  69. * value to balance performance and memory use.
  70. */
  71. unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
  72. module_param(ecryptfs_number_of_users, uint, 0);
  73. MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
  74. "concurrent users of eCryptfs");
  75. void __ecryptfs_printk(const char *fmt, ...)
  76. {
  77. va_list args;
  78. va_start(args, fmt);
  79. if (fmt[1] == '7') { /* KERN_DEBUG */
  80. if (ecryptfs_verbosity >= 1)
  81. vprintk(fmt, args);
  82. } else
  83. vprintk(fmt, args);
  84. va_end(args);
  85. }
  86. /**
  87. * ecryptfs_init_lower_file
  88. * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
  89. * the lower dentry and the lower mount set
  90. *
  91. * eCryptfs only ever keeps a single open file for every lower
  92. * inode. All I/O operations to the lower inode occur through that
  93. * file. When the first eCryptfs dentry that interposes with the first
  94. * lower dentry for that inode is created, this function creates the
  95. * lower file struct and associates it with the eCryptfs
  96. * inode. When all eCryptfs files associated with the inode are released, the
  97. * file is closed.
  98. *
  99. * The lower file will be opened with read/write permissions, if
  100. * possible. Otherwise, it is opened read-only.
  101. *
  102. * This function does nothing if a lower file is already
  103. * associated with the eCryptfs inode.
  104. *
  105. * Returns zero on success; non-zero otherwise
  106. */
  107. static int ecryptfs_init_lower_file(struct dentry *dentry,
  108. struct file **lower_file)
  109. {
  110. const struct cred *cred = current_cred();
  111. struct path *path = ecryptfs_dentry_to_lower_path(dentry);
  112. int rc;
  113. rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
  114. cred);
  115. if (rc) {
  116. printk(KERN_ERR "Error opening lower file "
  117. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  118. "rc = [%d]\n", path->dentry, path->mnt, rc);
  119. (*lower_file) = NULL;
  120. }
  121. return rc;
  122. }
  123. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
  124. {
  125. struct ecryptfs_inode_info *inode_info;
  126. int count, rc = 0;
  127. inode_info = ecryptfs_inode_to_private(inode);
  128. mutex_lock(&inode_info->lower_file_mutex);
  129. count = atomic_inc_return(&inode_info->lower_file_count);
  130. if (WARN_ON_ONCE(count < 1))
  131. rc = -EINVAL;
  132. else if (count == 1) {
  133. rc = ecryptfs_init_lower_file(dentry,
  134. &inode_info->lower_file);
  135. if (rc)
  136. atomic_set(&inode_info->lower_file_count, 0);
  137. }
  138. mutex_unlock(&inode_info->lower_file_mutex);
  139. return rc;
  140. }
  141. void ecryptfs_put_lower_file(struct inode *inode)
  142. {
  143. int ret = 0;
  144. struct ecryptfs_inode_info *inode_info;
  145. bool clear_cache_needed = false;
  146. inode_info = ecryptfs_inode_to_private(inode);
  147. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  148. &inode_info->lower_file_mutex)) {
  149. if (get_events() && get_events()->is_hw_crypt_cb &&
  150. get_events()->is_hw_crypt_cb())
  151. clear_cache_needed = true;
  152. if (clear_cache_needed) {
  153. ret = vfs_fsync(inode_info->lower_file, false);
  154. if (ret)
  155. pr_err("failed to sync file ret = %d.\n", ret);
  156. }
  157. filemap_write_and_wait(inode->i_mapping);
  158. fput(inode_info->lower_file);
  159. inode_info->lower_file = NULL;
  160. mutex_unlock(&inode_info->lower_file_mutex);
  161. if (clear_cache_needed) {
  162. truncate_inode_pages_fill_zero(inode->i_mapping, 0);
  163. truncate_inode_pages_fill_zero(
  164. ecryptfs_inode_to_lower(inode)->i_mapping, 0);
  165. }
  166. if (get_events() && get_events()->release_cb)
  167. get_events()->release_cb(
  168. ecryptfs_inode_to_lower(inode));
  169. }
  170. }
  171. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  172. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  173. ecryptfs_opt_ecryptfs_key_bytes,
  174. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  175. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  176. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  177. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  178. ecryptfs_opt_check_dev_ruid,
  179. ecryptfs_opt_err };
  180. static const match_table_t tokens = {
  181. {ecryptfs_opt_sig, "sig=%s"},
  182. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  183. {ecryptfs_opt_cipher, "cipher=%s"},
  184. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  185. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  186. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  187. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  188. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  189. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  190. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  191. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  192. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  193. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  194. {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
  195. {ecryptfs_opt_err, NULL}
  196. };
  197. static int ecryptfs_init_global_auth_toks(
  198. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  199. {
  200. struct ecryptfs_global_auth_tok *global_auth_tok;
  201. struct ecryptfs_auth_tok *auth_tok;
  202. int rc = 0;
  203. list_for_each_entry(global_auth_tok,
  204. &mount_crypt_stat->global_auth_tok_list,
  205. mount_crypt_stat_list) {
  206. rc = ecryptfs_keyring_auth_tok_for_sig(
  207. &global_auth_tok->global_auth_tok_key, &auth_tok,
  208. global_auth_tok->sig);
  209. if (rc) {
  210. printk(KERN_ERR "Could not find valid key in user "
  211. "session keyring for sig specified in mount "
  212. "option: [%s]\n", global_auth_tok->sig);
  213. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  214. goto out;
  215. } else {
  216. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  217. up_write(&(global_auth_tok->global_auth_tok_key)->sem);
  218. }
  219. }
  220. out:
  221. return rc;
  222. }
  223. static void ecryptfs_init_mount_crypt_stat(
  224. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  225. {
  226. memset((void *)mount_crypt_stat, 0,
  227. sizeof(struct ecryptfs_mount_crypt_stat));
  228. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  229. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  230. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  231. }
  232. /**
  233. * ecryptfs_parse_options
  234. * @sb: The ecryptfs super block
  235. * @options: The options passed to the kernel
  236. * @check_ruid: set to 1 if device uid should be checked against the ruid
  237. *
  238. * Parse mount options:
  239. * debug=N - ecryptfs_verbosity level for debug output
  240. * sig=XXX - description(signature) of the key to use
  241. *
  242. * Returns the dentry object of the lower-level (lower/interposed)
  243. * directory; We want to mount our stackable file system on top of
  244. * that lower directory.
  245. *
  246. * The signature of the key to use must be the description of a key
  247. * already in the keyring. Mounting will fail if the key can not be
  248. * found.
  249. *
  250. * Returns zero on success; non-zero on error
  251. */
  252. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
  253. uid_t *check_ruid)
  254. {
  255. char *p;
  256. int rc = 0;
  257. int sig_set = 0;
  258. int cipher_name_set = 0;
  259. int fn_cipher_name_set = 0;
  260. int cipher_key_bytes;
  261. int cipher_key_bytes_set = 0;
  262. int fn_cipher_key_bytes;
  263. int fn_cipher_key_bytes_set = 0;
  264. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  265. &sbi->mount_crypt_stat;
  266. substring_t args[MAX_OPT_ARGS];
  267. int token;
  268. char *sig_src;
  269. char *cipher_name_dst;
  270. char *cipher_name_src;
  271. char *fn_cipher_name_dst;
  272. char *fn_cipher_name_src;
  273. char *fnek_dst;
  274. char *fnek_src;
  275. char *cipher_key_bytes_src;
  276. char *fn_cipher_key_bytes_src;
  277. u8 cipher_code;
  278. unsigned char final[2*ECRYPTFS_MAX_CIPHER_NAME_SIZE+1];
  279. *check_ruid = 0;
  280. if (!options) {
  281. rc = -EINVAL;
  282. goto out;
  283. }
  284. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  285. while ((p = strsep(&options, ",")) != NULL) {
  286. if (!*p)
  287. continue;
  288. token = match_token(p, tokens, args);
  289. switch (token) {
  290. case ecryptfs_opt_sig:
  291. case ecryptfs_opt_ecryptfs_sig:
  292. sig_src = args[0].from;
  293. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  294. sig_src, 0);
  295. if (rc) {
  296. printk(KERN_ERR "Error attempting to register "
  297. "global sig; rc = [%d]\n", rc);
  298. goto out;
  299. }
  300. sig_set = 1;
  301. break;
  302. case ecryptfs_opt_cipher:
  303. case ecryptfs_opt_ecryptfs_cipher:
  304. cipher_name_src = args[0].from;
  305. cipher_name_dst =
  306. mount_crypt_stat->global_default_cipher_name;
  307. ecryptfs_parse_full_cipher(cipher_name_src,
  308. mount_crypt_stat->global_default_cipher_name,
  309. mount_crypt_stat->global_default_cipher_mode);
  310. cipher_name_set = 1;
  311. break;
  312. case ecryptfs_opt_ecryptfs_key_bytes:
  313. cipher_key_bytes_src = args[0].from;
  314. cipher_key_bytes =
  315. (int)simple_strtol(cipher_key_bytes_src,
  316. &cipher_key_bytes_src, 0);
  317. mount_crypt_stat->global_default_cipher_key_size =
  318. cipher_key_bytes;
  319. cipher_key_bytes_set = 1;
  320. break;
  321. case ecryptfs_opt_passthrough:
  322. mount_crypt_stat->flags |=
  323. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  324. break;
  325. case ecryptfs_opt_xattr_metadata:
  326. mount_crypt_stat->flags |=
  327. ECRYPTFS_XATTR_METADATA_ENABLED;
  328. break;
  329. case ecryptfs_opt_encrypted_view:
  330. mount_crypt_stat->flags |=
  331. ECRYPTFS_XATTR_METADATA_ENABLED;
  332. mount_crypt_stat->flags |=
  333. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  334. break;
  335. case ecryptfs_opt_fnek_sig:
  336. fnek_src = args[0].from;
  337. fnek_dst =
  338. mount_crypt_stat->global_default_fnek_sig;
  339. strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
  340. mount_crypt_stat->global_default_fnek_sig[
  341. ECRYPTFS_SIG_SIZE_HEX] = '\0';
  342. rc = ecryptfs_add_global_auth_tok(
  343. mount_crypt_stat,
  344. mount_crypt_stat->global_default_fnek_sig,
  345. ECRYPTFS_AUTH_TOK_FNEK);
  346. if (rc) {
  347. printk(KERN_ERR "Error attempting to register "
  348. "global fnek sig [%s]; rc = [%d]\n",
  349. mount_crypt_stat->global_default_fnek_sig,
  350. rc);
  351. goto out;
  352. }
  353. mount_crypt_stat->flags |=
  354. (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
  355. | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
  356. break;
  357. case ecryptfs_opt_fn_cipher:
  358. fn_cipher_name_src = args[0].from;
  359. fn_cipher_name_dst =
  360. mount_crypt_stat->global_default_fn_cipher_name;
  361. strncpy(fn_cipher_name_dst, fn_cipher_name_src,
  362. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  363. mount_crypt_stat->global_default_fn_cipher_name[
  364. ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  365. fn_cipher_name_set = 1;
  366. break;
  367. case ecryptfs_opt_fn_cipher_key_bytes:
  368. fn_cipher_key_bytes_src = args[0].from;
  369. fn_cipher_key_bytes =
  370. (int)simple_strtol(fn_cipher_key_bytes_src,
  371. &fn_cipher_key_bytes_src, 0);
  372. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  373. fn_cipher_key_bytes;
  374. fn_cipher_key_bytes_set = 1;
  375. break;
  376. case ecryptfs_opt_unlink_sigs:
  377. mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
  378. break;
  379. case ecryptfs_opt_mount_auth_tok_only:
  380. mount_crypt_stat->flags |=
  381. ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
  382. break;
  383. case ecryptfs_opt_check_dev_ruid:
  384. *check_ruid = 1;
  385. break;
  386. case ecryptfs_opt_err:
  387. default:
  388. printk(KERN_WARNING
  389. "%s: eCryptfs: unrecognized option [%s]\n",
  390. __func__, p);
  391. }
  392. }
  393. if (!sig_set) {
  394. rc = -EINVAL;
  395. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  396. "auth tok signature as a mount "
  397. "parameter; see the eCryptfs README\n");
  398. goto out;
  399. }
  400. if (!cipher_name_set) {
  401. int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  402. BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  403. strcpy(mount_crypt_stat->global_default_cipher_name,
  404. ECRYPTFS_DEFAULT_CIPHER);
  405. }
  406. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  407. && !fn_cipher_name_set)
  408. strcpy(mount_crypt_stat->global_default_fn_cipher_name,
  409. mount_crypt_stat->global_default_cipher_name);
  410. if (!cipher_key_bytes_set)
  411. mount_crypt_stat->global_default_cipher_key_size = 0;
  412. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  413. && !fn_cipher_key_bytes_set)
  414. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  415. mount_crypt_stat->global_default_cipher_key_size;
  416. cipher_code = ecryptfs_code_for_cipher_string(
  417. ecryptfs_get_full_cipher(
  418. mount_crypt_stat->global_default_cipher_name,
  419. mount_crypt_stat->global_default_cipher_mode,
  420. final, sizeof(final)),
  421. mount_crypt_stat->global_default_cipher_key_size);
  422. if (!cipher_code) {
  423. ecryptfs_printk(
  424. KERN_ERR,
  425. "eCryptfs doesn't support cipher: %s and key size %zu",
  426. ecryptfs_get_full_cipher(
  427. mount_crypt_stat->global_default_cipher_name,
  428. mount_crypt_stat->global_default_cipher_mode,
  429. final, sizeof(final)),
  430. mount_crypt_stat->global_default_cipher_key_size);
  431. rc = -EINVAL;
  432. goto out;
  433. }
  434. mutex_lock(&key_tfm_list_mutex);
  435. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  436. NULL)) {
  437. rc = ecryptfs_add_new_key_tfm(
  438. NULL, mount_crypt_stat->global_default_cipher_name,
  439. mount_crypt_stat->global_default_cipher_key_size);
  440. if (rc) {
  441. printk(KERN_ERR "Error attempting to initialize "
  442. "cipher with name = [%s] and key size = [%td]; "
  443. "rc = [%d]\n",
  444. mount_crypt_stat->global_default_cipher_name,
  445. mount_crypt_stat->global_default_cipher_key_size,
  446. rc);
  447. rc = -EINVAL;
  448. mutex_unlock(&key_tfm_list_mutex);
  449. goto out;
  450. }
  451. }
  452. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  453. && !ecryptfs_tfm_exists(
  454. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  455. rc = ecryptfs_add_new_key_tfm(
  456. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  457. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  458. if (rc) {
  459. printk(KERN_ERR "Error attempting to initialize "
  460. "cipher with name = [%s] and key size = [%td]; "
  461. "rc = [%d]\n",
  462. mount_crypt_stat->global_default_fn_cipher_name,
  463. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  464. rc);
  465. rc = -EINVAL;
  466. mutex_unlock(&key_tfm_list_mutex);
  467. goto out;
  468. }
  469. }
  470. mutex_unlock(&key_tfm_list_mutex);
  471. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  472. if (rc)
  473. printk(KERN_WARNING "One or more global auth toks could not "
  474. "properly register; rc = [%d]\n", rc);
  475. out:
  476. return rc;
  477. }
  478. struct kmem_cache *ecryptfs_sb_info_cache;
  479. static struct file_system_type ecryptfs_fs_type;
  480. /**
  481. * ecryptfs_get_sb
  482. * @fs_type
  483. * @flags
  484. * @dev_name: The path to mount over
  485. * @raw_data: The options passed into the kernel
  486. */
  487. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  488. const char *dev_name, void *raw_data)
  489. {
  490. struct super_block *s;
  491. struct ecryptfs_sb_info *sbi;
  492. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  493. struct ecryptfs_dentry_info *root_info;
  494. const char *err = "Getting sb failed";
  495. struct inode *inode;
  496. struct path path;
  497. uid_t check_ruid;
  498. int rc;
  499. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  500. if (!sbi) {
  501. rc = -ENOMEM;
  502. goto out;
  503. }
  504. rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
  505. if (rc) {
  506. err = "Error parsing options";
  507. goto out;
  508. }
  509. mount_crypt_stat = &sbi->mount_crypt_stat;
  510. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  511. if (IS_ERR(s)) {
  512. rc = PTR_ERR(s);
  513. goto out;
  514. }
  515. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs");
  516. if (rc)
  517. goto out1;
  518. ecryptfs_set_superblock_private(s, sbi);
  519. s->s_bdi = &sbi->bdi;
  520. /* ->kill_sb() will take care of sbi after that point */
  521. sbi = NULL;
  522. s->s_op = &ecryptfs_sops;
  523. s->s_xattr = ecryptfs_xattr_handlers;
  524. s->s_d_op = &ecryptfs_dops;
  525. err = "Reading sb failed";
  526. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  527. if (rc) {
  528. ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
  529. goto out1;
  530. }
  531. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  532. rc = -EINVAL;
  533. printk(KERN_ERR "Mount on filesystem of type "
  534. "eCryptfs explicitly disallowed due to "
  535. "known incompatibilities\n");
  536. goto out_free;
  537. }
  538. if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
  539. rc = -EPERM;
  540. printk(KERN_ERR "Mount of device (uid: %d) not owned by "
  541. "requested user (uid: %d)\n",
  542. i_uid_read(d_inode(path.dentry)),
  543. from_kuid(&init_user_ns, current_uid()));
  544. goto out_free;
  545. }
  546. ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
  547. if (get_events() && get_events()->is_hw_crypt_cb &&
  548. get_events()->is_hw_crypt_cb())
  549. drop_pagecache_sb(ecryptfs_superblock_to_lower(s), 0);
  550. /**
  551. * Set the POSIX ACL flag based on whether they're enabled in the lower
  552. * mount.
  553. */
  554. s->s_flags = flags & ~MS_POSIXACL;
  555. s->s_flags |= path.dentry->d_sb->s_flags & MS_POSIXACL;
  556. /**
  557. * Force a read-only eCryptfs mount when:
  558. * 1) The lower mount is ro
  559. * 2) The ecryptfs_encrypted_view mount option is specified
  560. */
  561. if (path.dentry->d_sb->s_flags & MS_RDONLY ||
  562. mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  563. s->s_flags |= MS_RDONLY;
  564. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  565. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  566. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  567. s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
  568. rc = -EINVAL;
  569. if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  570. pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
  571. goto out_free;
  572. }
  573. inode = ecryptfs_get_inode(d_inode(path.dentry), s);
  574. rc = PTR_ERR(inode);
  575. if (IS_ERR(inode))
  576. goto out_free;
  577. s->s_root = d_make_root(inode);
  578. if (!s->s_root) {
  579. rc = -ENOMEM;
  580. goto out_free;
  581. }
  582. rc = -ENOMEM;
  583. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  584. if (!root_info)
  585. goto out_free;
  586. /* ->kill_sb() will take care of root_info */
  587. ecryptfs_set_dentry_private(s->s_root, root_info);
  588. root_info->lower_path = path;
  589. s->s_flags |= MS_ACTIVE;
  590. return dget(s->s_root);
  591. out_free:
  592. path_put(&path);
  593. out1:
  594. deactivate_locked_super(s);
  595. out:
  596. if (sbi) {
  597. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  598. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  599. }
  600. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  601. return ERR_PTR(rc);
  602. }
  603. /**
  604. * ecryptfs_kill_block_super
  605. * @sb: The ecryptfs super block
  606. *
  607. * Used to bring the superblock down and free the private data.
  608. */
  609. static void ecryptfs_kill_block_super(struct super_block *sb)
  610. {
  611. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  612. kill_anon_super(sb);
  613. if (!sb_info)
  614. return;
  615. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  616. bdi_destroy(&sb_info->bdi);
  617. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  618. }
  619. static struct file_system_type ecryptfs_fs_type = {
  620. .owner = THIS_MODULE,
  621. .name = "ecryptfs",
  622. .mount = ecryptfs_mount,
  623. .kill_sb = ecryptfs_kill_block_super,
  624. .fs_flags = 0
  625. };
  626. MODULE_ALIAS_FS("ecryptfs");
  627. /**
  628. * inode_info_init_once
  629. *
  630. * Initializes the ecryptfs_inode_info_cache when it is created
  631. */
  632. static void
  633. inode_info_init_once(void *vptr)
  634. {
  635. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  636. inode_init_once(&ei->vfs_inode);
  637. }
  638. static struct ecryptfs_cache_info {
  639. struct kmem_cache **cache;
  640. const char *name;
  641. size_t size;
  642. unsigned long flags;
  643. void (*ctor)(void *obj);
  644. } ecryptfs_cache_infos[] = {
  645. {
  646. .cache = &ecryptfs_auth_tok_list_item_cache,
  647. .name = "ecryptfs_auth_tok_list_item",
  648. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  649. },
  650. {
  651. .cache = &ecryptfs_file_info_cache,
  652. .name = "ecryptfs_file_cache",
  653. .size = sizeof(struct ecryptfs_file_info),
  654. },
  655. {
  656. .cache = &ecryptfs_dentry_info_cache,
  657. .name = "ecryptfs_dentry_info_cache",
  658. .size = sizeof(struct ecryptfs_dentry_info),
  659. },
  660. {
  661. .cache = &ecryptfs_inode_info_cache,
  662. .name = "ecryptfs_inode_cache",
  663. .size = sizeof(struct ecryptfs_inode_info),
  664. .flags = SLAB_ACCOUNT,
  665. .ctor = inode_info_init_once,
  666. },
  667. {
  668. .cache = &ecryptfs_sb_info_cache,
  669. .name = "ecryptfs_sb_cache",
  670. .size = sizeof(struct ecryptfs_sb_info),
  671. },
  672. {
  673. .cache = &ecryptfs_header_cache,
  674. .name = "ecryptfs_headers",
  675. .size = PAGE_SIZE,
  676. },
  677. {
  678. .cache = &ecryptfs_xattr_cache,
  679. .name = "ecryptfs_xattr_cache",
  680. .size = PAGE_SIZE,
  681. },
  682. {
  683. .cache = &ecryptfs_key_record_cache,
  684. .name = "ecryptfs_key_record_cache",
  685. .size = sizeof(struct ecryptfs_key_record),
  686. },
  687. {
  688. .cache = &ecryptfs_key_sig_cache,
  689. .name = "ecryptfs_key_sig_cache",
  690. .size = sizeof(struct ecryptfs_key_sig),
  691. },
  692. {
  693. .cache = &ecryptfs_global_auth_tok_cache,
  694. .name = "ecryptfs_global_auth_tok_cache",
  695. .size = sizeof(struct ecryptfs_global_auth_tok),
  696. },
  697. {
  698. .cache = &ecryptfs_key_tfm_cache,
  699. .name = "ecryptfs_key_tfm_cache",
  700. .size = sizeof(struct ecryptfs_key_tfm),
  701. },
  702. };
  703. static void ecryptfs_free_kmem_caches(void)
  704. {
  705. int i;
  706. /*
  707. * Make sure all delayed rcu free inodes are flushed before we
  708. * destroy cache.
  709. */
  710. rcu_barrier();
  711. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  712. struct ecryptfs_cache_info *info;
  713. info = &ecryptfs_cache_infos[i];
  714. kmem_cache_destroy(*(info->cache));
  715. }
  716. }
  717. /**
  718. * ecryptfs_init_kmem_caches
  719. *
  720. * Returns zero on success; non-zero otherwise
  721. */
  722. static int ecryptfs_init_kmem_caches(void)
  723. {
  724. int i;
  725. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  726. struct ecryptfs_cache_info *info;
  727. info = &ecryptfs_cache_infos[i];
  728. *(info->cache) = kmem_cache_create(info->name, info->size, 0,
  729. SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
  730. if (!*(info->cache)) {
  731. ecryptfs_free_kmem_caches();
  732. ecryptfs_printk(KERN_WARNING, "%s: "
  733. "kmem_cache_create failed\n",
  734. info->name);
  735. return -ENOMEM;
  736. }
  737. }
  738. return 0;
  739. }
  740. static struct kobject *ecryptfs_kobj;
  741. static ssize_t version_show(struct kobject *kobj,
  742. struct kobj_attribute *attr, char *buff)
  743. {
  744. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  745. }
  746. static struct kobj_attribute version_attr = __ATTR_RO(version);
  747. static struct attribute *attributes[] = {
  748. &version_attr.attr,
  749. NULL,
  750. };
  751. static struct attribute_group attr_group = {
  752. .attrs = attributes,
  753. };
  754. static int do_sysfs_registration(void)
  755. {
  756. int rc;
  757. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  758. if (!ecryptfs_kobj) {
  759. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  760. rc = -ENOMEM;
  761. goto out;
  762. }
  763. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  764. if (rc) {
  765. printk(KERN_ERR
  766. "Unable to create ecryptfs version attributes\n");
  767. kobject_put(ecryptfs_kobj);
  768. }
  769. out:
  770. return rc;
  771. }
  772. static void do_sysfs_unregistration(void)
  773. {
  774. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  775. kobject_put(ecryptfs_kobj);
  776. }
  777. static int __init ecryptfs_init(void)
  778. {
  779. int rc;
  780. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
  781. rc = -EINVAL;
  782. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  783. "larger than the host's page size, and so "
  784. "eCryptfs cannot run on this system. The "
  785. "default eCryptfs extent size is [%u] bytes; "
  786. "the page size is [%lu] bytes.\n",
  787. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  788. (unsigned long)PAGE_SIZE);
  789. goto out;
  790. }
  791. rc = ecryptfs_init_kmem_caches();
  792. if (rc) {
  793. printk(KERN_ERR
  794. "Failed to allocate one or more kmem_cache objects\n");
  795. goto out;
  796. }
  797. rc = do_sysfs_registration();
  798. if (rc) {
  799. printk(KERN_ERR "sysfs registration failed\n");
  800. goto out_free_kmem_caches;
  801. }
  802. rc = ecryptfs_init_kthread();
  803. if (rc) {
  804. printk(KERN_ERR "%s: kthread initialization failed; "
  805. "rc = [%d]\n", __func__, rc);
  806. goto out_do_sysfs_unregistration;
  807. }
  808. rc = ecryptfs_init_messaging();
  809. if (rc) {
  810. printk(KERN_ERR "Failure occurred while attempting to "
  811. "initialize the communications channel to "
  812. "ecryptfsd\n");
  813. goto out_destroy_kthread;
  814. }
  815. rc = ecryptfs_init_crypto();
  816. if (rc) {
  817. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  818. "rc = [%d]\n", rc);
  819. goto out_release_messaging;
  820. }
  821. rc = register_filesystem(&ecryptfs_fs_type);
  822. if (rc) {
  823. printk(KERN_ERR "Failed to register filesystem\n");
  824. goto out_destroy_crypto;
  825. }
  826. if (ecryptfs_verbosity > 0)
  827. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  828. "will be written to the syslog!\n", ecryptfs_verbosity);
  829. goto out;
  830. out_destroy_crypto:
  831. ecryptfs_destroy_crypto();
  832. out_release_messaging:
  833. ecryptfs_release_messaging();
  834. out_destroy_kthread:
  835. ecryptfs_destroy_kthread();
  836. out_do_sysfs_unregistration:
  837. do_sysfs_unregistration();
  838. out_free_kmem_caches:
  839. ecryptfs_free_kmem_caches();
  840. out:
  841. return rc;
  842. }
  843. static void __exit ecryptfs_exit(void)
  844. {
  845. int rc;
  846. rc = ecryptfs_destroy_crypto();
  847. if (rc)
  848. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  849. "rc = [%d]\n", rc);
  850. ecryptfs_release_messaging();
  851. ecryptfs_destroy_kthread();
  852. do_sysfs_unregistration();
  853. unregister_filesystem(&ecryptfs_fs_type);
  854. ecryptfs_free_kmem_caches();
  855. ecryptfs_free_events();
  856. }
  857. MODULE_AUTHOR("Michael A. Halcrow <[email protected]>");
  858. MODULE_DESCRIPTION("eCryptfs");
  859. MODULE_LICENSE("GPL");
  860. module_init(ecryptfs_init)
  861. module_exit(ecryptfs_exit)