main.c 24 KB

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