main.c 24 KB

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