main.c 26 KB

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