main.c 24 KB

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