main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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/netlink.h>
  33. #include <linux/mount.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/key.h>
  36. #include <linux/parser.h>
  37. #include <linux/fs_stack.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 netlink message buffer
  49. * 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 through netlink. The actual sleep time will be, more than
  58. * likely, a small amount greater than this specified value, but only less if
  59. * the netlink 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. unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
  77. void __ecryptfs_printk(const char *fmt, ...)
  78. {
  79. va_list args;
  80. va_start(args, fmt);
  81. if (fmt[1] == '7') { /* KERN_DEBUG */
  82. if (ecryptfs_verbosity >= 1)
  83. vprintk(fmt, args);
  84. } else
  85. vprintk(fmt, args);
  86. va_end(args);
  87. }
  88. /**
  89. * ecryptfs_init_persistent_file
  90. * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
  91. * the lower dentry and the lower mount set
  92. *
  93. * eCryptfs only ever keeps a single open file for every lower
  94. * inode. All I/O operations to the lower inode occur through that
  95. * file. When the first eCryptfs dentry that interposes with the first
  96. * lower dentry for that inode is created, this function creates the
  97. * persistent file struct and associates it with the eCryptfs
  98. * inode. When the eCryptfs inode is destroyed, the file is closed.
  99. *
  100. * The persistent 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 persistent file is already
  104. * associated with the eCryptfs inode.
  105. *
  106. * Returns zero on success; non-zero otherwise
  107. */
  108. int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
  109. {
  110. struct ecryptfs_inode_info *inode_info =
  111. ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
  112. int rc = 0;
  113. mutex_lock(&inode_info->lower_file_mutex);
  114. if (!inode_info->lower_file) {
  115. struct dentry *lower_dentry;
  116. struct vfsmount *lower_mnt =
  117. ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  118. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  119. /* Corresponding dput() and mntput() are done when the
  120. * persistent file is fput() when the eCryptfs inode
  121. * is destroyed. */
  122. dget(lower_dentry);
  123. mntget(lower_mnt);
  124. inode_info->lower_file = dentry_open(lower_dentry,
  125. lower_mnt,
  126. (O_RDWR | O_LARGEFILE));
  127. if (IS_ERR(inode_info->lower_file)) {
  128. dget(lower_dentry);
  129. mntget(lower_mnt);
  130. inode_info->lower_file = dentry_open(lower_dentry,
  131. lower_mnt,
  132. (O_RDONLY
  133. | O_LARGEFILE));
  134. }
  135. if (IS_ERR(inode_info->lower_file)) {
  136. printk(KERN_ERR "Error opening lower persistent file "
  137. "for lower_dentry [0x%p] and lower_mnt [0x%p]\n",
  138. lower_dentry, lower_mnt);
  139. rc = PTR_ERR(inode_info->lower_file);
  140. inode_info->lower_file = NULL;
  141. }
  142. }
  143. mutex_unlock(&inode_info->lower_file_mutex);
  144. return rc;
  145. }
  146. /**
  147. * ecryptfs_interpose
  148. * @lower_dentry: Existing dentry in the lower filesystem
  149. * @dentry: ecryptfs' dentry
  150. * @sb: ecryptfs's super_block
  151. * @flag: If set to true, then d_add is called, else d_instantiate is called
  152. *
  153. * Interposes upper and lower dentries.
  154. *
  155. * Returns zero on success; non-zero otherwise
  156. */
  157. int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
  158. struct super_block *sb, int flag)
  159. {
  160. struct inode *lower_inode;
  161. struct inode *inode;
  162. int rc = 0;
  163. lower_inode = lower_dentry->d_inode;
  164. if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
  165. rc = -EXDEV;
  166. goto out;
  167. }
  168. if (!igrab(lower_inode)) {
  169. rc = -ESTALE;
  170. goto out;
  171. }
  172. inode = iget5_locked(sb, (unsigned long)lower_inode,
  173. ecryptfs_inode_test, ecryptfs_inode_set,
  174. lower_inode);
  175. if (!inode) {
  176. rc = -EACCES;
  177. iput(lower_inode);
  178. goto out;
  179. }
  180. if (inode->i_state & I_NEW)
  181. unlock_new_inode(inode);
  182. else
  183. iput(lower_inode);
  184. if (S_ISLNK(lower_inode->i_mode))
  185. inode->i_op = &ecryptfs_symlink_iops;
  186. else if (S_ISDIR(lower_inode->i_mode))
  187. inode->i_op = &ecryptfs_dir_iops;
  188. if (S_ISDIR(lower_inode->i_mode))
  189. inode->i_fop = &ecryptfs_dir_fops;
  190. if (special_file(lower_inode->i_mode))
  191. init_special_inode(inode, lower_inode->i_mode,
  192. lower_inode->i_rdev);
  193. dentry->d_op = &ecryptfs_dops;
  194. if (flag)
  195. d_add(dentry, inode);
  196. else
  197. d_instantiate(dentry, inode);
  198. fsstack_copy_attr_all(inode, lower_inode, NULL);
  199. /* This size will be overwritten for real files w/ headers and
  200. * other metadata */
  201. fsstack_copy_inode_size(inode, lower_inode);
  202. rc = ecryptfs_init_persistent_file(dentry);
  203. if (rc) {
  204. printk(KERN_ERR "%s: Error attempting to initialize the "
  205. "persistent file for the dentry with name [%s]; "
  206. "rc = [%d]\n", __FUNCTION__, dentry->d_name.name, rc);
  207. goto out;
  208. }
  209. out:
  210. return rc;
  211. }
  212. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
  213. ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
  214. ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
  215. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  216. ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
  217. static match_table_t tokens = {
  218. {ecryptfs_opt_sig, "sig=%s"},
  219. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  220. {ecryptfs_opt_debug, "debug=%u"},
  221. {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
  222. {ecryptfs_opt_cipher, "cipher=%s"},
  223. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  224. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  225. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  226. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  227. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  228. {ecryptfs_opt_err, NULL}
  229. };
  230. static int ecryptfs_init_global_auth_toks(
  231. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  232. {
  233. struct ecryptfs_global_auth_tok *global_auth_tok;
  234. int rc = 0;
  235. list_for_each_entry(global_auth_tok,
  236. &mount_crypt_stat->global_auth_tok_list,
  237. mount_crypt_stat_list) {
  238. rc = ecryptfs_keyring_auth_tok_for_sig(
  239. &global_auth_tok->global_auth_tok_key,
  240. &global_auth_tok->global_auth_tok,
  241. global_auth_tok->sig);
  242. if (rc) {
  243. printk(KERN_ERR "Could not find valid key in user "
  244. "session keyring for sig specified in mount "
  245. "option: [%s]\n", global_auth_tok->sig);
  246. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  247. rc = 0;
  248. } else
  249. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  250. }
  251. return rc;
  252. }
  253. static void ecryptfs_init_mount_crypt_stat(
  254. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  255. {
  256. memset((void *)mount_crypt_stat, 0,
  257. sizeof(struct ecryptfs_mount_crypt_stat));
  258. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  259. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  260. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  261. }
  262. /**
  263. * ecryptfs_parse_options
  264. * @sb: The ecryptfs super block
  265. * @options: The options pased to the kernel
  266. *
  267. * Parse mount options:
  268. * debug=N - ecryptfs_verbosity level for debug output
  269. * sig=XXX - description(signature) of the key to use
  270. *
  271. * Returns the dentry object of the lower-level (lower/interposed)
  272. * directory; We want to mount our stackable file system on top of
  273. * that lower directory.
  274. *
  275. * The signature of the key to use must be the description of a key
  276. * already in the keyring. Mounting will fail if the key can not be
  277. * found.
  278. *
  279. * Returns zero on success; non-zero on error
  280. */
  281. static int ecryptfs_parse_options(struct super_block *sb, char *options)
  282. {
  283. char *p;
  284. int rc = 0;
  285. int sig_set = 0;
  286. int cipher_name_set = 0;
  287. int cipher_key_bytes;
  288. int cipher_key_bytes_set = 0;
  289. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  290. &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
  291. substring_t args[MAX_OPT_ARGS];
  292. int token;
  293. char *sig_src;
  294. char *debug_src;
  295. char *cipher_name_dst;
  296. char *cipher_name_src;
  297. char *cipher_key_bytes_src;
  298. int cipher_name_len;
  299. if (!options) {
  300. rc = -EINVAL;
  301. goto out;
  302. }
  303. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  304. while ((p = strsep(&options, ",")) != NULL) {
  305. if (!*p)
  306. continue;
  307. token = match_token(p, tokens, args);
  308. switch (token) {
  309. case ecryptfs_opt_sig:
  310. case ecryptfs_opt_ecryptfs_sig:
  311. sig_src = args[0].from;
  312. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  313. sig_src);
  314. if (rc) {
  315. printk(KERN_ERR "Error attempting to register "
  316. "global sig; rc = [%d]\n", rc);
  317. goto out;
  318. }
  319. sig_set = 1;
  320. break;
  321. case ecryptfs_opt_debug:
  322. case ecryptfs_opt_ecryptfs_debug:
  323. debug_src = args[0].from;
  324. ecryptfs_verbosity =
  325. (int)simple_strtol(debug_src, &debug_src,
  326. 0);
  327. ecryptfs_printk(KERN_DEBUG,
  328. "Verbosity set to [%d]" "\n",
  329. ecryptfs_verbosity);
  330. break;
  331. case ecryptfs_opt_cipher:
  332. case ecryptfs_opt_ecryptfs_cipher:
  333. cipher_name_src = args[0].from;
  334. cipher_name_dst =
  335. mount_crypt_stat->
  336. global_default_cipher_name;
  337. strncpy(cipher_name_dst, cipher_name_src,
  338. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  339. ecryptfs_printk(KERN_DEBUG,
  340. "The mount_crypt_stat "
  341. "global_default_cipher_name set to: "
  342. "[%s]\n", cipher_name_dst);
  343. cipher_name_set = 1;
  344. break;
  345. case ecryptfs_opt_ecryptfs_key_bytes:
  346. cipher_key_bytes_src = args[0].from;
  347. cipher_key_bytes =
  348. (int)simple_strtol(cipher_key_bytes_src,
  349. &cipher_key_bytes_src, 0);
  350. mount_crypt_stat->global_default_cipher_key_size =
  351. cipher_key_bytes;
  352. ecryptfs_printk(KERN_DEBUG,
  353. "The mount_crypt_stat "
  354. "global_default_cipher_key_size "
  355. "set to: [%d]\n", mount_crypt_stat->
  356. global_default_cipher_key_size);
  357. cipher_key_bytes_set = 1;
  358. break;
  359. case ecryptfs_opt_passthrough:
  360. mount_crypt_stat->flags |=
  361. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  362. break;
  363. case ecryptfs_opt_xattr_metadata:
  364. mount_crypt_stat->flags |=
  365. ECRYPTFS_XATTR_METADATA_ENABLED;
  366. break;
  367. case ecryptfs_opt_encrypted_view:
  368. mount_crypt_stat->flags |=
  369. ECRYPTFS_XATTR_METADATA_ENABLED;
  370. mount_crypt_stat->flags |=
  371. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  372. break;
  373. case ecryptfs_opt_err:
  374. default:
  375. ecryptfs_printk(KERN_WARNING,
  376. "eCryptfs: unrecognized option '%s'\n",
  377. p);
  378. }
  379. }
  380. if (!sig_set) {
  381. rc = -EINVAL;
  382. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  383. "auth tok signature as a mount "
  384. "parameter; see the eCryptfs README\n");
  385. goto out;
  386. }
  387. if (!cipher_name_set) {
  388. cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  389. if (unlikely(cipher_name_len
  390. >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
  391. rc = -EINVAL;
  392. BUG();
  393. goto out;
  394. }
  395. memcpy(mount_crypt_stat->global_default_cipher_name,
  396. ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
  397. mount_crypt_stat->global_default_cipher_name[cipher_name_len]
  398. = '\0';
  399. }
  400. if (!cipher_key_bytes_set) {
  401. mount_crypt_stat->global_default_cipher_key_size = 0;
  402. }
  403. rc = ecryptfs_add_new_key_tfm(
  404. NULL, mount_crypt_stat->global_default_cipher_name,
  405. mount_crypt_stat->global_default_cipher_key_size);
  406. if (rc) {
  407. printk(KERN_ERR "Error attempting to initialize cipher with "
  408. "name = [%s] and key size = [%td]; rc = [%d]\n",
  409. mount_crypt_stat->global_default_cipher_name,
  410. mount_crypt_stat->global_default_cipher_key_size, rc);
  411. rc = -EINVAL;
  412. goto out;
  413. }
  414. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  415. if (rc) {
  416. printk(KERN_WARNING "One or more global auth toks could not "
  417. "properly register; rc = [%d]\n", rc);
  418. }
  419. rc = 0;
  420. out:
  421. return rc;
  422. }
  423. struct kmem_cache *ecryptfs_sb_info_cache;
  424. /**
  425. * ecryptfs_fill_super
  426. * @sb: The ecryptfs super block
  427. * @raw_data: The options passed to mount
  428. * @silent: Not used but required by function prototype
  429. *
  430. * Sets up what we can of the sb, rest is done in ecryptfs_read_super
  431. *
  432. * Returns zero on success; non-zero otherwise
  433. */
  434. static int
  435. ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
  436. {
  437. int rc = 0;
  438. /* Released in ecryptfs_put_super() */
  439. ecryptfs_set_superblock_private(sb,
  440. kmem_cache_zalloc(ecryptfs_sb_info_cache,
  441. GFP_KERNEL));
  442. if (!ecryptfs_superblock_to_private(sb)) {
  443. ecryptfs_printk(KERN_WARNING, "Out of memory\n");
  444. rc = -ENOMEM;
  445. goto out;
  446. }
  447. sb->s_op = &ecryptfs_sops;
  448. /* Released through deactivate_super(sb) from get_sb_nodev */
  449. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  450. .hash = 0,.name = "/",.len = 1});
  451. if (!sb->s_root) {
  452. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  453. rc = -ENOMEM;
  454. goto out;
  455. }
  456. sb->s_root->d_op = &ecryptfs_dops;
  457. sb->s_root->d_sb = sb;
  458. sb->s_root->d_parent = sb->s_root;
  459. /* Released in d_release when dput(sb->s_root) is called */
  460. /* through deactivate_super(sb) from get_sb_nodev() */
  461. ecryptfs_set_dentry_private(sb->s_root,
  462. kmem_cache_zalloc(ecryptfs_dentry_info_cache,
  463. GFP_KERNEL));
  464. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  465. ecryptfs_printk(KERN_ERR,
  466. "dentry_info_cache alloc failed\n");
  467. rc = -ENOMEM;
  468. goto out;
  469. }
  470. rc = 0;
  471. out:
  472. /* Should be able to rely on deactivate_super called from
  473. * get_sb_nodev */
  474. return rc;
  475. }
  476. /**
  477. * ecryptfs_read_super
  478. * @sb: The ecryptfs super block
  479. * @dev_name: The path to mount over
  480. *
  481. * Read the super block of the lower filesystem, and use
  482. * ecryptfs_interpose to create our initial inode and super block
  483. * struct.
  484. */
  485. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  486. {
  487. int rc;
  488. struct nameidata nd;
  489. struct dentry *lower_root;
  490. struct vfsmount *lower_mnt;
  491. memset(&nd, 0, sizeof(struct nameidata));
  492. rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
  493. if (rc) {
  494. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  495. goto out;
  496. }
  497. lower_root = nd.dentry;
  498. lower_mnt = nd.mnt;
  499. ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
  500. sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
  501. sb->s_blocksize = lower_root->d_sb->s_blocksize;
  502. ecryptfs_set_dentry_lower(sb->s_root, lower_root);
  503. ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
  504. rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
  505. if (rc)
  506. goto out_free;
  507. rc = 0;
  508. goto out;
  509. out_free:
  510. path_release(&nd);
  511. out:
  512. return rc;
  513. }
  514. /**
  515. * ecryptfs_get_sb
  516. * @fs_type
  517. * @flags
  518. * @dev_name: The path to mount over
  519. * @raw_data: The options passed into the kernel
  520. *
  521. * The whole ecryptfs_get_sb process is broken into 4 functions:
  522. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  523. * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
  524. * with as much information as it can before needing
  525. * the lower filesystem.
  526. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  527. * ecryptfs_interpolate to perform most of the linking
  528. * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
  529. */
  530. static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
  531. const char *dev_name, void *raw_data,
  532. struct vfsmount *mnt)
  533. {
  534. int rc;
  535. struct super_block *sb;
  536. rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
  537. if (rc < 0) {
  538. printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
  539. goto out;
  540. }
  541. sb = mnt->mnt_sb;
  542. rc = ecryptfs_parse_options(sb, raw_data);
  543. if (rc) {
  544. printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
  545. goto out_abort;
  546. }
  547. rc = ecryptfs_read_super(sb, dev_name);
  548. if (rc) {
  549. printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
  550. goto out_abort;
  551. }
  552. goto out;
  553. out_abort:
  554. dput(sb->s_root);
  555. up_write(&sb->s_umount);
  556. deactivate_super(sb);
  557. out:
  558. return rc;
  559. }
  560. /**
  561. * ecryptfs_kill_block_super
  562. * @sb: The ecryptfs super block
  563. *
  564. * Used to bring the superblock down and free the private data.
  565. * Private data is free'd in ecryptfs_put_super()
  566. */
  567. static void ecryptfs_kill_block_super(struct super_block *sb)
  568. {
  569. generic_shutdown_super(sb);
  570. }
  571. static struct file_system_type ecryptfs_fs_type = {
  572. .owner = THIS_MODULE,
  573. .name = "ecryptfs",
  574. .get_sb = ecryptfs_get_sb,
  575. .kill_sb = ecryptfs_kill_block_super,
  576. .fs_flags = 0
  577. };
  578. /**
  579. * inode_info_init_once
  580. *
  581. * Initializes the ecryptfs_inode_info_cache when it is created
  582. */
  583. static void
  584. inode_info_init_once(struct kmem_cache *cachep, void *vptr)
  585. {
  586. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  587. inode_init_once(&ei->vfs_inode);
  588. }
  589. static struct ecryptfs_cache_info {
  590. struct kmem_cache **cache;
  591. const char *name;
  592. size_t size;
  593. void (*ctor)(struct kmem_cache *cache, void *obj);
  594. } ecryptfs_cache_infos[] = {
  595. {
  596. .cache = &ecryptfs_auth_tok_list_item_cache,
  597. .name = "ecryptfs_auth_tok_list_item",
  598. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  599. },
  600. {
  601. .cache = &ecryptfs_file_info_cache,
  602. .name = "ecryptfs_file_cache",
  603. .size = sizeof(struct ecryptfs_file_info),
  604. },
  605. {
  606. .cache = &ecryptfs_dentry_info_cache,
  607. .name = "ecryptfs_dentry_info_cache",
  608. .size = sizeof(struct ecryptfs_dentry_info),
  609. },
  610. {
  611. .cache = &ecryptfs_inode_info_cache,
  612. .name = "ecryptfs_inode_cache",
  613. .size = sizeof(struct ecryptfs_inode_info),
  614. .ctor = inode_info_init_once,
  615. },
  616. {
  617. .cache = &ecryptfs_sb_info_cache,
  618. .name = "ecryptfs_sb_cache",
  619. .size = sizeof(struct ecryptfs_sb_info),
  620. },
  621. {
  622. .cache = &ecryptfs_header_cache_0,
  623. .name = "ecryptfs_headers_0",
  624. .size = PAGE_CACHE_SIZE,
  625. },
  626. {
  627. .cache = &ecryptfs_header_cache_1,
  628. .name = "ecryptfs_headers_1",
  629. .size = PAGE_CACHE_SIZE,
  630. },
  631. {
  632. .cache = &ecryptfs_header_cache_2,
  633. .name = "ecryptfs_headers_2",
  634. .size = PAGE_CACHE_SIZE,
  635. },
  636. {
  637. .cache = &ecryptfs_xattr_cache,
  638. .name = "ecryptfs_xattr_cache",
  639. .size = PAGE_CACHE_SIZE,
  640. },
  641. {
  642. .cache = &ecryptfs_key_record_cache,
  643. .name = "ecryptfs_key_record_cache",
  644. .size = sizeof(struct ecryptfs_key_record),
  645. },
  646. {
  647. .cache = &ecryptfs_key_sig_cache,
  648. .name = "ecryptfs_key_sig_cache",
  649. .size = sizeof(struct ecryptfs_key_sig),
  650. },
  651. {
  652. .cache = &ecryptfs_global_auth_tok_cache,
  653. .name = "ecryptfs_global_auth_tok_cache",
  654. .size = sizeof(struct ecryptfs_global_auth_tok),
  655. },
  656. {
  657. .cache = &ecryptfs_key_tfm_cache,
  658. .name = "ecryptfs_key_tfm_cache",
  659. .size = sizeof(struct ecryptfs_key_tfm),
  660. },
  661. };
  662. static void ecryptfs_free_kmem_caches(void)
  663. {
  664. int i;
  665. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  666. struct ecryptfs_cache_info *info;
  667. info = &ecryptfs_cache_infos[i];
  668. if (*(info->cache))
  669. kmem_cache_destroy(*(info->cache));
  670. }
  671. }
  672. /**
  673. * ecryptfs_init_kmem_caches
  674. *
  675. * Returns zero on success; non-zero otherwise
  676. */
  677. static int ecryptfs_init_kmem_caches(void)
  678. {
  679. int i;
  680. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  681. struct ecryptfs_cache_info *info;
  682. info = &ecryptfs_cache_infos[i];
  683. *(info->cache) = kmem_cache_create(info->name, info->size,
  684. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  685. if (!*(info->cache)) {
  686. ecryptfs_free_kmem_caches();
  687. ecryptfs_printk(KERN_WARNING, "%s: "
  688. "kmem_cache_create failed\n",
  689. info->name);
  690. return -ENOMEM;
  691. }
  692. }
  693. return 0;
  694. }
  695. static struct kobject *ecryptfs_kobj;
  696. static ssize_t version_show(struct kobject *kobj,
  697. struct kobj_attribute *attr, char *buff)
  698. {
  699. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  700. }
  701. static struct kobj_attribute version_attr = __ATTR_RO(version);
  702. static struct attribute *attributes[] = {
  703. &version_attr.attr,
  704. NULL,
  705. };
  706. static struct attribute_group attr_group = {
  707. .attrs = attributes,
  708. };
  709. static int do_sysfs_registration(void)
  710. {
  711. int rc;
  712. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  713. if (!ecryptfs_kobj) {
  714. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  715. rc = -ENOMEM;
  716. goto out;
  717. }
  718. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  719. if (rc) {
  720. printk(KERN_ERR
  721. "Unable to create ecryptfs version attributes\n");
  722. kobject_put(ecryptfs_kobj);
  723. }
  724. out:
  725. return rc;
  726. }
  727. static void do_sysfs_unregistration(void)
  728. {
  729. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  730. kobject_put(ecryptfs_kobj);
  731. }
  732. static int __init ecryptfs_init(void)
  733. {
  734. int rc;
  735. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  736. rc = -EINVAL;
  737. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  738. "larger than the host's page size, and so "
  739. "eCryptfs cannot run on this system. The "
  740. "default eCryptfs extent size is [%d] bytes; "
  741. "the page size is [%d] bytes.\n",
  742. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  743. goto out;
  744. }
  745. rc = ecryptfs_init_kmem_caches();
  746. if (rc) {
  747. printk(KERN_ERR
  748. "Failed to allocate one or more kmem_cache objects\n");
  749. goto out;
  750. }
  751. rc = register_filesystem(&ecryptfs_fs_type);
  752. if (rc) {
  753. printk(KERN_ERR "Failed to register filesystem\n");
  754. goto out_free_kmem_caches;
  755. }
  756. rc = do_sysfs_registration();
  757. if (rc) {
  758. printk(KERN_ERR "sysfs registration failed\n");
  759. goto out_unregister_filesystem;
  760. }
  761. rc = ecryptfs_init_messaging(ecryptfs_transport);
  762. if (rc) {
  763. ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
  764. "initialize the eCryptfs netlink socket\n");
  765. goto out_do_sysfs_unregistration;
  766. }
  767. rc = ecryptfs_init_crypto();
  768. if (rc) {
  769. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  770. "rc = [%d]\n", rc);
  771. goto out_release_messaging;
  772. }
  773. goto out;
  774. out_release_messaging:
  775. ecryptfs_release_messaging(ecryptfs_transport);
  776. out_do_sysfs_unregistration:
  777. do_sysfs_unregistration();
  778. out_unregister_filesystem:
  779. unregister_filesystem(&ecryptfs_fs_type);
  780. out_free_kmem_caches:
  781. ecryptfs_free_kmem_caches();
  782. out:
  783. return rc;
  784. }
  785. static void __exit ecryptfs_exit(void)
  786. {
  787. int rc;
  788. rc = ecryptfs_destroy_crypto();
  789. if (rc)
  790. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  791. "rc = [%d]\n", rc);
  792. ecryptfs_release_messaging(ecryptfs_transport);
  793. do_sysfs_unregistration();
  794. unregister_filesystem(&ecryptfs_fs_type);
  795. ecryptfs_free_kmem_caches();
  796. }
  797. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  798. MODULE_DESCRIPTION("eCryptfs");
  799. MODULE_LICENSE("GPL");
  800. module_init(ecryptfs_init)
  801. module_exit(ecryptfs_exit)