main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 <mahalcro@us.ibm.com>
  8. * Michael C. Thompson <mcthomps@us.ibm.com>
  9. * Tyler Hicks <tyhicks@ou.edu>
  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. struct ecryptfs_inode_info *inode_info;
  144. inode_info = ecryptfs_inode_to_private(inode);
  145. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  146. &inode_info->lower_file_mutex)) {
  147. filemap_write_and_wait(inode->i_mapping);
  148. fput(inode_info->lower_file);
  149. inode_info->lower_file = NULL;
  150. mutex_unlock(&inode_info->lower_file_mutex);
  151. }
  152. }
  153. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  154. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  155. ecryptfs_opt_ecryptfs_key_bytes,
  156. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  157. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  158. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  159. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  160. ecryptfs_opt_check_dev_ruid,
  161. ecryptfs_opt_err };
  162. static const match_table_t tokens = {
  163. {ecryptfs_opt_sig, "sig=%s"},
  164. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  165. {ecryptfs_opt_cipher, "cipher=%s"},
  166. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  167. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  168. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  169. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  170. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  171. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  172. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  173. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  174. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  175. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  176. {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
  177. {ecryptfs_opt_err, NULL}
  178. };
  179. static int ecryptfs_init_global_auth_toks(
  180. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  181. {
  182. struct ecryptfs_global_auth_tok *global_auth_tok;
  183. struct ecryptfs_auth_tok *auth_tok;
  184. int rc = 0;
  185. list_for_each_entry(global_auth_tok,
  186. &mount_crypt_stat->global_auth_tok_list,
  187. mount_crypt_stat_list) {
  188. rc = ecryptfs_keyring_auth_tok_for_sig(
  189. &global_auth_tok->global_auth_tok_key, &auth_tok,
  190. global_auth_tok->sig);
  191. if (rc) {
  192. printk(KERN_ERR "Could not find valid key in user "
  193. "session keyring for sig specified in mount "
  194. "option: [%s]\n", global_auth_tok->sig);
  195. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  196. goto out;
  197. } else {
  198. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  199. up_write(&(global_auth_tok->global_auth_tok_key)->sem);
  200. }
  201. }
  202. out:
  203. return rc;
  204. }
  205. static void ecryptfs_init_mount_crypt_stat(
  206. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  207. {
  208. memset((void *)mount_crypt_stat, 0,
  209. sizeof(struct ecryptfs_mount_crypt_stat));
  210. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  211. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  212. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  213. }
  214. /**
  215. * ecryptfs_parse_options
  216. * @sb: The ecryptfs super block
  217. * @options: The options passed to the kernel
  218. * @check_ruid: set to 1 if device uid should be checked against the ruid
  219. *
  220. * Parse mount options:
  221. * debug=N - ecryptfs_verbosity level for debug output
  222. * sig=XXX - description(signature) of the key to use
  223. *
  224. * Returns the dentry object of the lower-level (lower/interposed)
  225. * directory; We want to mount our stackable file system on top of
  226. * that lower directory.
  227. *
  228. * The signature of the key to use must be the description of a key
  229. * already in the keyring. Mounting will fail if the key can not be
  230. * found.
  231. *
  232. * Returns zero on success; non-zero on error
  233. */
  234. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
  235. uid_t *check_ruid)
  236. {
  237. char *p;
  238. int rc = 0;
  239. int sig_set = 0;
  240. int cipher_name_set = 0;
  241. int fn_cipher_name_set = 0;
  242. int cipher_key_bytes;
  243. int cipher_key_bytes_set = 0;
  244. int fn_cipher_key_bytes;
  245. int fn_cipher_key_bytes_set = 0;
  246. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  247. &sbi->mount_crypt_stat;
  248. substring_t args[MAX_OPT_ARGS];
  249. int token;
  250. char *sig_src;
  251. char *cipher_name_dst;
  252. char *cipher_name_src;
  253. char *fn_cipher_name_dst;
  254. char *fn_cipher_name_src;
  255. char *fnek_dst;
  256. char *fnek_src;
  257. char *cipher_key_bytes_src;
  258. char *fn_cipher_key_bytes_src;
  259. u8 cipher_code;
  260. *check_ruid = 0;
  261. if (!options) {
  262. rc = -EINVAL;
  263. goto out;
  264. }
  265. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  266. while ((p = strsep(&options, ",")) != NULL) {
  267. if (!*p)
  268. continue;
  269. token = match_token(p, tokens, args);
  270. switch (token) {
  271. case ecryptfs_opt_sig:
  272. case ecryptfs_opt_ecryptfs_sig:
  273. sig_src = args[0].from;
  274. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  275. sig_src, 0);
  276. if (rc) {
  277. printk(KERN_ERR "Error attempting to register "
  278. "global sig; rc = [%d]\n", rc);
  279. goto out;
  280. }
  281. sig_set = 1;
  282. break;
  283. case ecryptfs_opt_cipher:
  284. case ecryptfs_opt_ecryptfs_cipher:
  285. cipher_name_src = args[0].from;
  286. cipher_name_dst =
  287. mount_crypt_stat->
  288. global_default_cipher_name;
  289. strncpy(cipher_name_dst, cipher_name_src,
  290. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  291. cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  292. cipher_name_set = 1;
  293. break;
  294. case ecryptfs_opt_ecryptfs_key_bytes:
  295. cipher_key_bytes_src = args[0].from;
  296. cipher_key_bytes =
  297. (int)simple_strtol(cipher_key_bytes_src,
  298. &cipher_key_bytes_src, 0);
  299. mount_crypt_stat->global_default_cipher_key_size =
  300. cipher_key_bytes;
  301. cipher_key_bytes_set = 1;
  302. break;
  303. case ecryptfs_opt_passthrough:
  304. mount_crypt_stat->flags |=
  305. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  306. break;
  307. case ecryptfs_opt_xattr_metadata:
  308. mount_crypt_stat->flags |=
  309. ECRYPTFS_XATTR_METADATA_ENABLED;
  310. break;
  311. case ecryptfs_opt_encrypted_view:
  312. mount_crypt_stat->flags |=
  313. ECRYPTFS_XATTR_METADATA_ENABLED;
  314. mount_crypt_stat->flags |=
  315. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  316. break;
  317. case ecryptfs_opt_fnek_sig:
  318. fnek_src = args[0].from;
  319. fnek_dst =
  320. mount_crypt_stat->global_default_fnek_sig;
  321. strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
  322. mount_crypt_stat->global_default_fnek_sig[
  323. ECRYPTFS_SIG_SIZE_HEX] = '\0';
  324. rc = ecryptfs_add_global_auth_tok(
  325. mount_crypt_stat,
  326. mount_crypt_stat->global_default_fnek_sig,
  327. ECRYPTFS_AUTH_TOK_FNEK);
  328. if (rc) {
  329. printk(KERN_ERR "Error attempting to register "
  330. "global fnek sig [%s]; rc = [%d]\n",
  331. mount_crypt_stat->global_default_fnek_sig,
  332. rc);
  333. goto out;
  334. }
  335. mount_crypt_stat->flags |=
  336. (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
  337. | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
  338. break;
  339. case ecryptfs_opt_fn_cipher:
  340. fn_cipher_name_src = args[0].from;
  341. fn_cipher_name_dst =
  342. mount_crypt_stat->global_default_fn_cipher_name;
  343. strncpy(fn_cipher_name_dst, fn_cipher_name_src,
  344. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  345. mount_crypt_stat->global_default_fn_cipher_name[
  346. ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  347. fn_cipher_name_set = 1;
  348. break;
  349. case ecryptfs_opt_fn_cipher_key_bytes:
  350. fn_cipher_key_bytes_src = args[0].from;
  351. fn_cipher_key_bytes =
  352. (int)simple_strtol(fn_cipher_key_bytes_src,
  353. &fn_cipher_key_bytes_src, 0);
  354. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  355. fn_cipher_key_bytes;
  356. fn_cipher_key_bytes_set = 1;
  357. break;
  358. case ecryptfs_opt_unlink_sigs:
  359. mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
  360. break;
  361. case ecryptfs_opt_mount_auth_tok_only:
  362. mount_crypt_stat->flags |=
  363. ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
  364. break;
  365. case ecryptfs_opt_check_dev_ruid:
  366. *check_ruid = 1;
  367. break;
  368. case ecryptfs_opt_err:
  369. default:
  370. printk(KERN_WARNING
  371. "%s: eCryptfs: unrecognized option [%s]\n",
  372. __func__, p);
  373. }
  374. }
  375. if (!sig_set) {
  376. rc = -EINVAL;
  377. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  378. "auth tok signature as a mount "
  379. "parameter; see the eCryptfs README\n");
  380. goto out;
  381. }
  382. if (!cipher_name_set) {
  383. int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  384. BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  385. strcpy(mount_crypt_stat->global_default_cipher_name,
  386. ECRYPTFS_DEFAULT_CIPHER);
  387. }
  388. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  389. && !fn_cipher_name_set)
  390. strcpy(mount_crypt_stat->global_default_fn_cipher_name,
  391. mount_crypt_stat->global_default_cipher_name);
  392. if (!cipher_key_bytes_set)
  393. mount_crypt_stat->global_default_cipher_key_size = 0;
  394. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  395. && !fn_cipher_key_bytes_set)
  396. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  397. mount_crypt_stat->global_default_cipher_key_size;
  398. cipher_code = ecryptfs_code_for_cipher_string(
  399. mount_crypt_stat->global_default_cipher_name,
  400. mount_crypt_stat->global_default_cipher_key_size);
  401. if (!cipher_code) {
  402. ecryptfs_printk(KERN_ERR,
  403. "eCryptfs doesn't support cipher: %s\n",
  404. mount_crypt_stat->global_default_cipher_name);
  405. rc = -EINVAL;
  406. goto out;
  407. }
  408. mutex_lock(&key_tfm_list_mutex);
  409. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  410. NULL)) {
  411. rc = ecryptfs_add_new_key_tfm(
  412. NULL, mount_crypt_stat->global_default_cipher_name,
  413. mount_crypt_stat->global_default_cipher_key_size);
  414. if (rc) {
  415. printk(KERN_ERR "Error attempting to initialize "
  416. "cipher with name = [%s] and key size = [%td]; "
  417. "rc = [%d]\n",
  418. mount_crypt_stat->global_default_cipher_name,
  419. mount_crypt_stat->global_default_cipher_key_size,
  420. rc);
  421. rc = -EINVAL;
  422. mutex_unlock(&key_tfm_list_mutex);
  423. goto out;
  424. }
  425. }
  426. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  427. && !ecryptfs_tfm_exists(
  428. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  429. rc = ecryptfs_add_new_key_tfm(
  430. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  431. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  432. if (rc) {
  433. printk(KERN_ERR "Error attempting to initialize "
  434. "cipher with name = [%s] and key size = [%td]; "
  435. "rc = [%d]\n",
  436. mount_crypt_stat->global_default_fn_cipher_name,
  437. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  438. rc);
  439. rc = -EINVAL;
  440. mutex_unlock(&key_tfm_list_mutex);
  441. goto out;
  442. }
  443. }
  444. mutex_unlock(&key_tfm_list_mutex);
  445. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  446. if (rc)
  447. printk(KERN_WARNING "One or more global auth toks could not "
  448. "properly register; rc = [%d]\n", rc);
  449. out:
  450. return rc;
  451. }
  452. struct kmem_cache *ecryptfs_sb_info_cache;
  453. static struct file_system_type ecryptfs_fs_type;
  454. /**
  455. * ecryptfs_get_sb
  456. * @fs_type
  457. * @flags
  458. * @dev_name: The path to mount over
  459. * @raw_data: The options passed into the kernel
  460. */
  461. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  462. const char *dev_name, void *raw_data)
  463. {
  464. struct super_block *s;
  465. struct ecryptfs_sb_info *sbi;
  466. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  467. struct ecryptfs_dentry_info *root_info;
  468. const char *err = "Getting sb failed";
  469. struct inode *inode;
  470. struct path path;
  471. uid_t check_ruid;
  472. int rc;
  473. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  474. if (!sbi) {
  475. rc = -ENOMEM;
  476. goto out;
  477. }
  478. rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
  479. if (rc) {
  480. err = "Error parsing options";
  481. goto out;
  482. }
  483. mount_crypt_stat = &sbi->mount_crypt_stat;
  484. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  485. if (IS_ERR(s)) {
  486. rc = PTR_ERR(s);
  487. goto out;
  488. }
  489. rc = super_setup_bdi(s);
  490. if (rc)
  491. goto out1;
  492. ecryptfs_set_superblock_private(s, sbi);
  493. /* ->kill_sb() will take care of sbi after that point */
  494. sbi = NULL;
  495. s->s_op = &ecryptfs_sops;
  496. s->s_xattr = ecryptfs_xattr_handlers;
  497. s->s_d_op = &ecryptfs_dops;
  498. err = "Reading sb failed";
  499. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  500. if (rc) {
  501. ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
  502. goto out1;
  503. }
  504. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  505. rc = -EINVAL;
  506. printk(KERN_ERR "Mount on filesystem of type "
  507. "eCryptfs explicitly disallowed due to "
  508. "known incompatibilities\n");
  509. goto out_free;
  510. }
  511. if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
  512. rc = -EPERM;
  513. printk(KERN_ERR "Mount of device (uid: %d) not owned by "
  514. "requested user (uid: %d)\n",
  515. i_uid_read(d_inode(path.dentry)),
  516. from_kuid(&init_user_ns, current_uid()));
  517. goto out_free;
  518. }
  519. ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
  520. /**
  521. * Set the POSIX ACL flag based on whether they're enabled in the lower
  522. * mount.
  523. */
  524. s->s_flags = flags & ~SB_POSIXACL;
  525. s->s_flags |= path.dentry->d_sb->s_flags & SB_POSIXACL;
  526. /**
  527. * Force a read-only eCryptfs mount when:
  528. * 1) The lower mount is ro
  529. * 2) The ecryptfs_encrypted_view mount option is specified
  530. */
  531. if (sb_rdonly(path.dentry->d_sb) || mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  532. s->s_flags |= SB_RDONLY;
  533. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  534. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  535. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  536. s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
  537. rc = -EINVAL;
  538. if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  539. pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
  540. goto out_free;
  541. }
  542. inode = ecryptfs_get_inode(d_inode(path.dentry), s);
  543. rc = PTR_ERR(inode);
  544. if (IS_ERR(inode))
  545. goto out_free;
  546. s->s_root = d_make_root(inode);
  547. if (!s->s_root) {
  548. rc = -ENOMEM;
  549. goto out_free;
  550. }
  551. rc = -ENOMEM;
  552. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  553. if (!root_info)
  554. goto out_free;
  555. /* ->kill_sb() will take care of root_info */
  556. ecryptfs_set_dentry_private(s->s_root, root_info);
  557. root_info->lower_path = path;
  558. s->s_flags |= SB_ACTIVE;
  559. return dget(s->s_root);
  560. out_free:
  561. path_put(&path);
  562. out1:
  563. deactivate_locked_super(s);
  564. out:
  565. if (sbi) {
  566. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  567. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  568. }
  569. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  570. return ERR_PTR(rc);
  571. }
  572. /**
  573. * ecryptfs_kill_block_super
  574. * @sb: The ecryptfs super block
  575. *
  576. * Used to bring the superblock down and free the private data.
  577. */
  578. static void ecryptfs_kill_block_super(struct super_block *sb)
  579. {
  580. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  581. kill_anon_super(sb);
  582. if (!sb_info)
  583. return;
  584. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  585. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  586. }
  587. static struct file_system_type ecryptfs_fs_type = {
  588. .owner = THIS_MODULE,
  589. .name = "ecryptfs",
  590. .mount = ecryptfs_mount,
  591. .kill_sb = ecryptfs_kill_block_super,
  592. .fs_flags = 0
  593. };
  594. MODULE_ALIAS_FS("ecryptfs");
  595. /**
  596. * inode_info_init_once
  597. *
  598. * Initializes the ecryptfs_inode_info_cache when it is created
  599. */
  600. static void
  601. inode_info_init_once(void *vptr)
  602. {
  603. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  604. inode_init_once(&ei->vfs_inode);
  605. }
  606. static struct ecryptfs_cache_info {
  607. struct kmem_cache **cache;
  608. const char *name;
  609. size_t size;
  610. slab_flags_t flags;
  611. void (*ctor)(void *obj);
  612. } ecryptfs_cache_infos[] = {
  613. {
  614. .cache = &ecryptfs_auth_tok_list_item_cache,
  615. .name = "ecryptfs_auth_tok_list_item",
  616. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  617. },
  618. {
  619. .cache = &ecryptfs_file_info_cache,
  620. .name = "ecryptfs_file_cache",
  621. .size = sizeof(struct ecryptfs_file_info),
  622. },
  623. {
  624. .cache = &ecryptfs_dentry_info_cache,
  625. .name = "ecryptfs_dentry_info_cache",
  626. .size = sizeof(struct ecryptfs_dentry_info),
  627. },
  628. {
  629. .cache = &ecryptfs_inode_info_cache,
  630. .name = "ecryptfs_inode_cache",
  631. .size = sizeof(struct ecryptfs_inode_info),
  632. .flags = SLAB_ACCOUNT,
  633. .ctor = inode_info_init_once,
  634. },
  635. {
  636. .cache = &ecryptfs_sb_info_cache,
  637. .name = "ecryptfs_sb_cache",
  638. .size = sizeof(struct ecryptfs_sb_info),
  639. },
  640. {
  641. .cache = &ecryptfs_header_cache,
  642. .name = "ecryptfs_headers",
  643. .size = PAGE_SIZE,
  644. },
  645. {
  646. .cache = &ecryptfs_xattr_cache,
  647. .name = "ecryptfs_xattr_cache",
  648. .size = PAGE_SIZE,
  649. },
  650. {
  651. .cache = &ecryptfs_key_record_cache,
  652. .name = "ecryptfs_key_record_cache",
  653. .size = sizeof(struct ecryptfs_key_record),
  654. },
  655. {
  656. .cache = &ecryptfs_key_sig_cache,
  657. .name = "ecryptfs_key_sig_cache",
  658. .size = sizeof(struct ecryptfs_key_sig),
  659. },
  660. {
  661. .cache = &ecryptfs_global_auth_tok_cache,
  662. .name = "ecryptfs_global_auth_tok_cache",
  663. .size = sizeof(struct ecryptfs_global_auth_tok),
  664. },
  665. {
  666. .cache = &ecryptfs_key_tfm_cache,
  667. .name = "ecryptfs_key_tfm_cache",
  668. .size = sizeof(struct ecryptfs_key_tfm),
  669. },
  670. };
  671. static void ecryptfs_free_kmem_caches(void)
  672. {
  673. int i;
  674. /*
  675. * Make sure all delayed rcu free inodes are flushed before we
  676. * destroy cache.
  677. */
  678. rcu_barrier();
  679. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  680. struct ecryptfs_cache_info *info;
  681. info = &ecryptfs_cache_infos[i];
  682. kmem_cache_destroy(*(info->cache));
  683. }
  684. }
  685. /**
  686. * ecryptfs_init_kmem_caches
  687. *
  688. * Returns zero on success; non-zero otherwise
  689. */
  690. static int ecryptfs_init_kmem_caches(void)
  691. {
  692. int i;
  693. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  694. struct ecryptfs_cache_info *info;
  695. info = &ecryptfs_cache_infos[i];
  696. *(info->cache) = kmem_cache_create(info->name, info->size, 0,
  697. SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
  698. if (!*(info->cache)) {
  699. ecryptfs_free_kmem_caches();
  700. ecryptfs_printk(KERN_WARNING, "%s: "
  701. "kmem_cache_create failed\n",
  702. info->name);
  703. return -ENOMEM;
  704. }
  705. }
  706. return 0;
  707. }
  708. static struct kobject *ecryptfs_kobj;
  709. static ssize_t version_show(struct kobject *kobj,
  710. struct kobj_attribute *attr, char *buff)
  711. {
  712. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  713. }
  714. static struct kobj_attribute version_attr = __ATTR_RO(version);
  715. static struct attribute *attributes[] = {
  716. &version_attr.attr,
  717. NULL,
  718. };
  719. static const struct attribute_group attr_group = {
  720. .attrs = attributes,
  721. };
  722. static int do_sysfs_registration(void)
  723. {
  724. int rc;
  725. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  726. if (!ecryptfs_kobj) {
  727. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  728. rc = -ENOMEM;
  729. goto out;
  730. }
  731. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  732. if (rc) {
  733. printk(KERN_ERR
  734. "Unable to create ecryptfs version attributes\n");
  735. kobject_put(ecryptfs_kobj);
  736. }
  737. out:
  738. return rc;
  739. }
  740. static void do_sysfs_unregistration(void)
  741. {
  742. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  743. kobject_put(ecryptfs_kobj);
  744. }
  745. static int __init ecryptfs_init(void)
  746. {
  747. int rc;
  748. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
  749. rc = -EINVAL;
  750. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  751. "larger than the host's page size, and so "
  752. "eCryptfs cannot run on this system. The "
  753. "default eCryptfs extent size is [%u] bytes; "
  754. "the page size is [%lu] bytes.\n",
  755. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  756. (unsigned long)PAGE_SIZE);
  757. goto out;
  758. }
  759. rc = ecryptfs_init_kmem_caches();
  760. if (rc) {
  761. printk(KERN_ERR
  762. "Failed to allocate one or more kmem_cache objects\n");
  763. goto out;
  764. }
  765. rc = do_sysfs_registration();
  766. if (rc) {
  767. printk(KERN_ERR "sysfs registration failed\n");
  768. goto out_free_kmem_caches;
  769. }
  770. rc = ecryptfs_init_kthread();
  771. if (rc) {
  772. printk(KERN_ERR "%s: kthread initialization failed; "
  773. "rc = [%d]\n", __func__, rc);
  774. goto out_do_sysfs_unregistration;
  775. }
  776. rc = ecryptfs_init_messaging();
  777. if (rc) {
  778. printk(KERN_ERR "Failure occurred while attempting to "
  779. "initialize the communications channel to "
  780. "ecryptfsd\n");
  781. goto out_destroy_kthread;
  782. }
  783. rc = ecryptfs_init_crypto();
  784. if (rc) {
  785. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  786. "rc = [%d]\n", rc);
  787. goto out_release_messaging;
  788. }
  789. rc = register_filesystem(&ecryptfs_fs_type);
  790. if (rc) {
  791. printk(KERN_ERR "Failed to register filesystem\n");
  792. goto out_destroy_crypto;
  793. }
  794. if (ecryptfs_verbosity > 0)
  795. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  796. "will be written to the syslog!\n", ecryptfs_verbosity);
  797. goto out;
  798. out_destroy_crypto:
  799. ecryptfs_destroy_crypto();
  800. out_release_messaging:
  801. ecryptfs_release_messaging();
  802. out_destroy_kthread:
  803. ecryptfs_destroy_kthread();
  804. out_do_sysfs_unregistration:
  805. do_sysfs_unregistration();
  806. out_free_kmem_caches:
  807. ecryptfs_free_kmem_caches();
  808. out:
  809. return rc;
  810. }
  811. static void __exit ecryptfs_exit(void)
  812. {
  813. int rc;
  814. rc = ecryptfs_destroy_crypto();
  815. if (rc)
  816. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  817. "rc = [%d]\n", rc);
  818. ecryptfs_release_messaging();
  819. ecryptfs_destroy_kthread();
  820. do_sysfs_unregistration();
  821. unregister_filesystem(&ecryptfs_fs_type);
  822. ecryptfs_free_kmem_caches();
  823. }
  824. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  825. MODULE_DESCRIPTION("eCryptfs");
  826. MODULE_LICENSE("GPL");
  827. module_init(ecryptfs_init)
  828. module_exit(ecryptfs_exit)