main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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/crypto.h>
  32. #include <linux/mount.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/key.h>
  35. #include <linux/parser.h>
  36. #include <linux/fs_stack.h>
  37. #include <linux/slab.h>
  38. #include <linux/magic.h>
  39. #include "ecryptfs_kernel.h"
  40. /**
  41. * Module parameter that defines the ecryptfs_verbosity level.
  42. */
  43. int ecryptfs_verbosity = 0;
  44. module_param(ecryptfs_verbosity, int, 0);
  45. MODULE_PARM_DESC(ecryptfs_verbosity,
  46. "Initial verbosity level (0 or 1; defaults to "
  47. "0, which is Quiet)");
  48. /**
  49. * Module parameter that defines the number of message buffer elements
  50. */
  51. unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
  52. module_param(ecryptfs_message_buf_len, uint, 0);
  53. MODULE_PARM_DESC(ecryptfs_message_buf_len,
  54. "Number of message buffer elements");
  55. /**
  56. * Module parameter that defines the maximum guaranteed amount of time to wait
  57. * for a response from ecryptfsd. The actual sleep time will be, more than
  58. * likely, a small amount greater than this specified value, but only less if
  59. * the message successfully arrives.
  60. */
  61. signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
  62. module_param(ecryptfs_message_wait_timeout, long, 0);
  63. MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
  64. "Maximum number of seconds that an operation will "
  65. "sleep while waiting for a message response from "
  66. "userspace");
  67. /**
  68. * Module parameter that is an estimate of the maximum number of users
  69. * that will be concurrently using eCryptfs. Set this to the right
  70. * value to balance performance and memory use.
  71. */
  72. unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
  73. module_param(ecryptfs_number_of_users, uint, 0);
  74. MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
  75. "concurrent users of eCryptfs");
  76. void __ecryptfs_printk(const char *fmt, ...)
  77. {
  78. va_list args;
  79. va_start(args, fmt);
  80. if (fmt[1] == '7') { /* KERN_DEBUG */
  81. if (ecryptfs_verbosity >= 1)
  82. vprintk(fmt, args);
  83. } else
  84. vprintk(fmt, args);
  85. va_end(args);
  86. }
  87. /**
  88. * ecryptfs_init_lower_file
  89. * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
  90. * the lower dentry and the lower mount set
  91. *
  92. * eCryptfs only ever keeps a single open file for every lower
  93. * inode. All I/O operations to the lower inode occur through that
  94. * file. When the first eCryptfs dentry that interposes with the first
  95. * lower dentry for that inode is created, this function creates the
  96. * lower file struct and associates it with the eCryptfs
  97. * inode. When all eCryptfs files associated with the inode are released, the
  98. * file is closed.
  99. *
  100. * The lower file will be opened with read/write permissions, if
  101. * possible. Otherwise, it is opened read-only.
  102. *
  103. * This function does nothing if a lower file is already
  104. * associated with the eCryptfs inode.
  105. *
  106. * Returns zero on success; non-zero otherwise
  107. */
  108. static int ecryptfs_init_lower_file(struct dentry *dentry,
  109. struct file **lower_file)
  110. {
  111. const struct cred *cred = current_cred();
  112. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  113. struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  114. int rc;
  115. rc = ecryptfs_privileged_open(lower_file, lower_dentry, lower_mnt,
  116. cred);
  117. if (rc) {
  118. printk(KERN_ERR "Error opening lower file "
  119. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  120. "rc = [%d]\n", lower_dentry, lower_mnt, rc);
  121. (*lower_file) = NULL;
  122. }
  123. return rc;
  124. }
  125. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
  126. {
  127. struct ecryptfs_inode_info *inode_info;
  128. int count, rc = 0;
  129. inode_info = ecryptfs_inode_to_private(inode);
  130. mutex_lock(&inode_info->lower_file_mutex);
  131. count = atomic_inc_return(&inode_info->lower_file_count);
  132. if (WARN_ON_ONCE(count < 1))
  133. rc = -EINVAL;
  134. else if (count == 1) {
  135. rc = ecryptfs_init_lower_file(dentry,
  136. &inode_info->lower_file);
  137. if (rc)
  138. atomic_set(&inode_info->lower_file_count, 0);
  139. }
  140. mutex_unlock(&inode_info->lower_file_mutex);
  141. return rc;
  142. }
  143. void ecryptfs_put_lower_file(struct inode *inode)
  144. {
  145. struct ecryptfs_inode_info *inode_info;
  146. inode_info = ecryptfs_inode_to_private(inode);
  147. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  148. &inode_info->lower_file_mutex)) {
  149. fput(inode_info->lower_file);
  150. inode_info->lower_file = NULL;
  151. mutex_unlock(&inode_info->lower_file_mutex);
  152. }
  153. }
  154. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  155. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  156. ecryptfs_opt_ecryptfs_key_bytes,
  157. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  158. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  159. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  160. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  161. ecryptfs_opt_check_dev_ruid,
  162. ecryptfs_opt_err };
  163. static const match_table_t tokens = {
  164. {ecryptfs_opt_sig, "sig=%s"},
  165. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  166. {ecryptfs_opt_cipher, "cipher=%s"},
  167. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  168. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  169. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  170. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  171. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  172. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  173. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  174. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  175. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  176. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  177. {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
  178. {ecryptfs_opt_err, NULL}
  179. };
  180. static int ecryptfs_init_global_auth_toks(
  181. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  182. {
  183. struct ecryptfs_global_auth_tok *global_auth_tok;
  184. struct ecryptfs_auth_tok *auth_tok;
  185. int rc = 0;
  186. list_for_each_entry(global_auth_tok,
  187. &mount_crypt_stat->global_auth_tok_list,
  188. mount_crypt_stat_list) {
  189. rc = ecryptfs_keyring_auth_tok_for_sig(
  190. &global_auth_tok->global_auth_tok_key, &auth_tok,
  191. global_auth_tok->sig);
  192. if (rc) {
  193. printk(KERN_ERR "Could not find valid key in user "
  194. "session keyring for sig specified in mount "
  195. "option: [%s]\n", global_auth_tok->sig);
  196. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  197. goto out;
  198. } else {
  199. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  200. up_write(&(global_auth_tok->global_auth_tok_key)->sem);
  201. }
  202. }
  203. out:
  204. return rc;
  205. }
  206. static void ecryptfs_init_mount_crypt_stat(
  207. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  208. {
  209. memset((void *)mount_crypt_stat, 0,
  210. sizeof(struct ecryptfs_mount_crypt_stat));
  211. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  212. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  213. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  214. }
  215. /**
  216. * ecryptfs_parse_options
  217. * @sb: The ecryptfs super block
  218. * @options: The options passed to the kernel
  219. * @check_ruid: set to 1 if device uid should be checked against the ruid
  220. *
  221. * Parse mount options:
  222. * debug=N - ecryptfs_verbosity level for debug output
  223. * sig=XXX - description(signature) of the key to use
  224. *
  225. * Returns the dentry object of the lower-level (lower/interposed)
  226. * directory; We want to mount our stackable file system on top of
  227. * that lower directory.
  228. *
  229. * The signature of the key to use must be the description of a key
  230. * already in the keyring. Mounting will fail if the key can not be
  231. * found.
  232. *
  233. * Returns zero on success; non-zero on error
  234. */
  235. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
  236. uid_t *check_ruid)
  237. {
  238. char *p;
  239. int rc = 0;
  240. int sig_set = 0;
  241. int cipher_name_set = 0;
  242. int fn_cipher_name_set = 0;
  243. int cipher_key_bytes;
  244. int cipher_key_bytes_set = 0;
  245. int fn_cipher_key_bytes;
  246. int fn_cipher_key_bytes_set = 0;
  247. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  248. &sbi->mount_crypt_stat;
  249. substring_t args[MAX_OPT_ARGS];
  250. int token;
  251. char *sig_src;
  252. char *cipher_name_dst;
  253. char *cipher_name_src;
  254. char *fn_cipher_name_dst;
  255. char *fn_cipher_name_src;
  256. char *fnek_dst;
  257. char *fnek_src;
  258. char *cipher_key_bytes_src;
  259. char *fn_cipher_key_bytes_src;
  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. mutex_lock(&key_tfm_list_mutex);
  399. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  400. NULL)) {
  401. rc = ecryptfs_add_new_key_tfm(
  402. NULL, mount_crypt_stat->global_default_cipher_name,
  403. mount_crypt_stat->global_default_cipher_key_size);
  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_cipher_name,
  409. mount_crypt_stat->global_default_cipher_key_size,
  410. rc);
  411. rc = -EINVAL;
  412. mutex_unlock(&key_tfm_list_mutex);
  413. goto out;
  414. }
  415. }
  416. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  417. && !ecryptfs_tfm_exists(
  418. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  419. rc = ecryptfs_add_new_key_tfm(
  420. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  421. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  422. if (rc) {
  423. printk(KERN_ERR "Error attempting to initialize "
  424. "cipher with name = [%s] and key size = [%td]; "
  425. "rc = [%d]\n",
  426. mount_crypt_stat->global_default_fn_cipher_name,
  427. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  428. rc);
  429. rc = -EINVAL;
  430. mutex_unlock(&key_tfm_list_mutex);
  431. goto out;
  432. }
  433. }
  434. mutex_unlock(&key_tfm_list_mutex);
  435. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  436. if (rc)
  437. printk(KERN_WARNING "One or more global auth toks could not "
  438. "properly register; rc = [%d]\n", rc);
  439. out:
  440. return rc;
  441. }
  442. struct kmem_cache *ecryptfs_sb_info_cache;
  443. static struct file_system_type ecryptfs_fs_type;
  444. /**
  445. * ecryptfs_get_sb
  446. * @fs_type
  447. * @flags
  448. * @dev_name: The path to mount over
  449. * @raw_data: The options passed into the kernel
  450. */
  451. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  452. const char *dev_name, void *raw_data)
  453. {
  454. struct super_block *s;
  455. struct ecryptfs_sb_info *sbi;
  456. struct ecryptfs_dentry_info *root_info;
  457. const char *err = "Getting sb failed";
  458. struct inode *inode;
  459. struct path path;
  460. uid_t check_ruid;
  461. int rc;
  462. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  463. if (!sbi) {
  464. rc = -ENOMEM;
  465. goto out;
  466. }
  467. rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
  468. if (rc) {
  469. err = "Error parsing options";
  470. goto out;
  471. }
  472. s = sget(fs_type, NULL, set_anon_super, NULL);
  473. if (IS_ERR(s)) {
  474. rc = PTR_ERR(s);
  475. goto out;
  476. }
  477. s->s_flags = flags;
  478. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
  479. if (rc)
  480. goto out1;
  481. ecryptfs_set_superblock_private(s, sbi);
  482. s->s_bdi = &sbi->bdi;
  483. /* ->kill_sb() will take care of sbi after that point */
  484. sbi = NULL;
  485. s->s_op = &ecryptfs_sops;
  486. s->s_d_op = &ecryptfs_dops;
  487. err = "Reading sb failed";
  488. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  489. if (rc) {
  490. ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
  491. goto out1;
  492. }
  493. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  494. rc = -EINVAL;
  495. printk(KERN_ERR "Mount on filesystem of type "
  496. "eCryptfs explicitly disallowed due to "
  497. "known incompatibilities\n");
  498. goto out_free;
  499. }
  500. if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
  501. rc = -EPERM;
  502. printk(KERN_ERR "Mount of device (uid: %d) not owned by "
  503. "requested user (uid: %d)\n",
  504. path.dentry->d_inode->i_uid, current_uid());
  505. goto out_free;
  506. }
  507. ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
  508. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  509. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  510. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  511. inode = ecryptfs_get_inode(path.dentry->d_inode, s);
  512. rc = PTR_ERR(inode);
  513. if (IS_ERR(inode))
  514. goto out_free;
  515. s->s_root = d_alloc_root(inode);
  516. if (!s->s_root) {
  517. iput(inode);
  518. rc = -ENOMEM;
  519. goto out_free;
  520. }
  521. rc = -ENOMEM;
  522. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  523. if (!root_info)
  524. goto out_free;
  525. /* ->kill_sb() will take care of root_info */
  526. ecryptfs_set_dentry_private(s->s_root, root_info);
  527. ecryptfs_set_dentry_lower(s->s_root, path.dentry);
  528. ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
  529. s->s_flags |= MS_ACTIVE;
  530. return dget(s->s_root);
  531. out_free:
  532. path_put(&path);
  533. out1:
  534. deactivate_locked_super(s);
  535. out:
  536. if (sbi) {
  537. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  538. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  539. }
  540. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  541. return ERR_PTR(rc);
  542. }
  543. /**
  544. * ecryptfs_kill_block_super
  545. * @sb: The ecryptfs super block
  546. *
  547. * Used to bring the superblock down and free the private data.
  548. */
  549. static void ecryptfs_kill_block_super(struct super_block *sb)
  550. {
  551. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  552. kill_anon_super(sb);
  553. if (!sb_info)
  554. return;
  555. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  556. bdi_destroy(&sb_info->bdi);
  557. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  558. }
  559. static struct file_system_type ecryptfs_fs_type = {
  560. .owner = THIS_MODULE,
  561. .name = "ecryptfs",
  562. .mount = ecryptfs_mount,
  563. .kill_sb = ecryptfs_kill_block_super,
  564. .fs_flags = 0
  565. };
  566. /**
  567. * inode_info_init_once
  568. *
  569. * Initializes the ecryptfs_inode_info_cache when it is created
  570. */
  571. static void
  572. inode_info_init_once(void *vptr)
  573. {
  574. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  575. inode_init_once(&ei->vfs_inode);
  576. }
  577. static struct ecryptfs_cache_info {
  578. struct kmem_cache **cache;
  579. const char *name;
  580. size_t size;
  581. void (*ctor)(void *obj);
  582. } ecryptfs_cache_infos[] = {
  583. {
  584. .cache = &ecryptfs_auth_tok_list_item_cache,
  585. .name = "ecryptfs_auth_tok_list_item",
  586. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  587. },
  588. {
  589. .cache = &ecryptfs_file_info_cache,
  590. .name = "ecryptfs_file_cache",
  591. .size = sizeof(struct ecryptfs_file_info),
  592. },
  593. {
  594. .cache = &ecryptfs_dentry_info_cache,
  595. .name = "ecryptfs_dentry_info_cache",
  596. .size = sizeof(struct ecryptfs_dentry_info),
  597. },
  598. {
  599. .cache = &ecryptfs_inode_info_cache,
  600. .name = "ecryptfs_inode_cache",
  601. .size = sizeof(struct ecryptfs_inode_info),
  602. .ctor = inode_info_init_once,
  603. },
  604. {
  605. .cache = &ecryptfs_sb_info_cache,
  606. .name = "ecryptfs_sb_cache",
  607. .size = sizeof(struct ecryptfs_sb_info),
  608. },
  609. {
  610. .cache = &ecryptfs_header_cache,
  611. .name = "ecryptfs_headers",
  612. .size = PAGE_CACHE_SIZE,
  613. },
  614. {
  615. .cache = &ecryptfs_xattr_cache,
  616. .name = "ecryptfs_xattr_cache",
  617. .size = PAGE_CACHE_SIZE,
  618. },
  619. {
  620. .cache = &ecryptfs_key_record_cache,
  621. .name = "ecryptfs_key_record_cache",
  622. .size = sizeof(struct ecryptfs_key_record),
  623. },
  624. {
  625. .cache = &ecryptfs_key_sig_cache,
  626. .name = "ecryptfs_key_sig_cache",
  627. .size = sizeof(struct ecryptfs_key_sig),
  628. },
  629. {
  630. .cache = &ecryptfs_global_auth_tok_cache,
  631. .name = "ecryptfs_global_auth_tok_cache",
  632. .size = sizeof(struct ecryptfs_global_auth_tok),
  633. },
  634. {
  635. .cache = &ecryptfs_key_tfm_cache,
  636. .name = "ecryptfs_key_tfm_cache",
  637. .size = sizeof(struct ecryptfs_key_tfm),
  638. },
  639. {
  640. .cache = &ecryptfs_open_req_cache,
  641. .name = "ecryptfs_open_req_cache",
  642. .size = sizeof(struct ecryptfs_open_req),
  643. },
  644. };
  645. static void ecryptfs_free_kmem_caches(void)
  646. {
  647. int i;
  648. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  649. struct ecryptfs_cache_info *info;
  650. info = &ecryptfs_cache_infos[i];
  651. if (*(info->cache))
  652. kmem_cache_destroy(*(info->cache));
  653. }
  654. }
  655. /**
  656. * ecryptfs_init_kmem_caches
  657. *
  658. * Returns zero on success; non-zero otherwise
  659. */
  660. static int ecryptfs_init_kmem_caches(void)
  661. {
  662. int i;
  663. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  664. struct ecryptfs_cache_info *info;
  665. info = &ecryptfs_cache_infos[i];
  666. *(info->cache) = kmem_cache_create(info->name, info->size,
  667. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  668. if (!*(info->cache)) {
  669. ecryptfs_free_kmem_caches();
  670. ecryptfs_printk(KERN_WARNING, "%s: "
  671. "kmem_cache_create failed\n",
  672. info->name);
  673. return -ENOMEM;
  674. }
  675. }
  676. return 0;
  677. }
  678. static struct kobject *ecryptfs_kobj;
  679. static ssize_t version_show(struct kobject *kobj,
  680. struct kobj_attribute *attr, char *buff)
  681. {
  682. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  683. }
  684. static struct kobj_attribute version_attr = __ATTR_RO(version);
  685. static struct attribute *attributes[] = {
  686. &version_attr.attr,
  687. NULL,
  688. };
  689. static struct attribute_group attr_group = {
  690. .attrs = attributes,
  691. };
  692. static int do_sysfs_registration(void)
  693. {
  694. int rc;
  695. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  696. if (!ecryptfs_kobj) {
  697. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  698. rc = -ENOMEM;
  699. goto out;
  700. }
  701. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  702. if (rc) {
  703. printk(KERN_ERR
  704. "Unable to create ecryptfs version attributes\n");
  705. kobject_put(ecryptfs_kobj);
  706. }
  707. out:
  708. return rc;
  709. }
  710. static void do_sysfs_unregistration(void)
  711. {
  712. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  713. kobject_put(ecryptfs_kobj);
  714. }
  715. static int __init ecryptfs_init(void)
  716. {
  717. int rc;
  718. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  719. rc = -EINVAL;
  720. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  721. "larger than the host's page size, and so "
  722. "eCryptfs cannot run on this system. The "
  723. "default eCryptfs extent size is [%u] bytes; "
  724. "the page size is [%lu] bytes.\n",
  725. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  726. (unsigned long)PAGE_CACHE_SIZE);
  727. goto out;
  728. }
  729. rc = ecryptfs_init_kmem_caches();
  730. if (rc) {
  731. printk(KERN_ERR
  732. "Failed to allocate one or more kmem_cache objects\n");
  733. goto out;
  734. }
  735. rc = register_filesystem(&ecryptfs_fs_type);
  736. if (rc) {
  737. printk(KERN_ERR "Failed to register filesystem\n");
  738. goto out_free_kmem_caches;
  739. }
  740. rc = do_sysfs_registration();
  741. if (rc) {
  742. printk(KERN_ERR "sysfs registration failed\n");
  743. goto out_unregister_filesystem;
  744. }
  745. rc = ecryptfs_init_kthread();
  746. if (rc) {
  747. printk(KERN_ERR "%s: kthread initialization failed; "
  748. "rc = [%d]\n", __func__, rc);
  749. goto out_do_sysfs_unregistration;
  750. }
  751. rc = ecryptfs_init_messaging();
  752. if (rc) {
  753. printk(KERN_ERR "Failure occurred while attempting to "
  754. "initialize the communications channel to "
  755. "ecryptfsd\n");
  756. goto out_destroy_kthread;
  757. }
  758. rc = ecryptfs_init_crypto();
  759. if (rc) {
  760. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  761. "rc = [%d]\n", rc);
  762. goto out_release_messaging;
  763. }
  764. if (ecryptfs_verbosity > 0)
  765. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  766. "will be written to the syslog!\n", ecryptfs_verbosity);
  767. goto out;
  768. out_release_messaging:
  769. ecryptfs_release_messaging();
  770. out_destroy_kthread:
  771. ecryptfs_destroy_kthread();
  772. out_do_sysfs_unregistration:
  773. do_sysfs_unregistration();
  774. out_unregister_filesystem:
  775. unregister_filesystem(&ecryptfs_fs_type);
  776. out_free_kmem_caches:
  777. ecryptfs_free_kmem_caches();
  778. out:
  779. return rc;
  780. }
  781. static void __exit ecryptfs_exit(void)
  782. {
  783. int rc;
  784. rc = ecryptfs_destroy_crypto();
  785. if (rc)
  786. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  787. "rc = [%d]\n", rc);
  788. ecryptfs_release_messaging();
  789. ecryptfs_destroy_kthread();
  790. do_sysfs_unregistration();
  791. unregister_filesystem(&ecryptfs_fs_type);
  792. ecryptfs_free_kmem_caches();
  793. }
  794. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  795. MODULE_DESCRIPTION("eCryptfs");
  796. MODULE_LICENSE("GPL");
  797. module_init(ecryptfs_init)
  798. module_exit(ecryptfs_exit)