main.c 26 KB

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