main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. static 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,
  213. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  214. 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_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 *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_cipher:
  319. case ecryptfs_opt_ecryptfs_cipher:
  320. cipher_name_src = args[0].from;
  321. cipher_name_dst =
  322. mount_crypt_stat->
  323. global_default_cipher_name;
  324. strncpy(cipher_name_dst, cipher_name_src,
  325. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  326. ecryptfs_printk(KERN_DEBUG,
  327. "The mount_crypt_stat "
  328. "global_default_cipher_name set to: "
  329. "[%s]\n", cipher_name_dst);
  330. cipher_name_set = 1;
  331. break;
  332. case ecryptfs_opt_ecryptfs_key_bytes:
  333. cipher_key_bytes_src = args[0].from;
  334. cipher_key_bytes =
  335. (int)simple_strtol(cipher_key_bytes_src,
  336. &cipher_key_bytes_src, 0);
  337. mount_crypt_stat->global_default_cipher_key_size =
  338. cipher_key_bytes;
  339. ecryptfs_printk(KERN_DEBUG,
  340. "The mount_crypt_stat "
  341. "global_default_cipher_key_size "
  342. "set to: [%d]\n", mount_crypt_stat->
  343. global_default_cipher_key_size);
  344. cipher_key_bytes_set = 1;
  345. break;
  346. case ecryptfs_opt_passthrough:
  347. mount_crypt_stat->flags |=
  348. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  349. break;
  350. case ecryptfs_opt_xattr_metadata:
  351. mount_crypt_stat->flags |=
  352. ECRYPTFS_XATTR_METADATA_ENABLED;
  353. break;
  354. case ecryptfs_opt_encrypted_view:
  355. mount_crypt_stat->flags |=
  356. ECRYPTFS_XATTR_METADATA_ENABLED;
  357. mount_crypt_stat->flags |=
  358. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  359. break;
  360. case ecryptfs_opt_err:
  361. default:
  362. ecryptfs_printk(KERN_WARNING,
  363. "eCryptfs: unrecognized option '%s'\n",
  364. p);
  365. }
  366. }
  367. if (!sig_set) {
  368. rc = -EINVAL;
  369. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  370. "auth tok signature as a mount "
  371. "parameter; see the eCryptfs README\n");
  372. goto out;
  373. }
  374. if (!cipher_name_set) {
  375. cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  376. if (unlikely(cipher_name_len
  377. >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
  378. rc = -EINVAL;
  379. BUG();
  380. goto out;
  381. }
  382. memcpy(mount_crypt_stat->global_default_cipher_name,
  383. ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
  384. mount_crypt_stat->global_default_cipher_name[cipher_name_len]
  385. = '\0';
  386. }
  387. if (!cipher_key_bytes_set) {
  388. mount_crypt_stat->global_default_cipher_key_size = 0;
  389. }
  390. mutex_lock(&key_tfm_list_mutex);
  391. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  392. NULL))
  393. rc = ecryptfs_add_new_key_tfm(
  394. NULL, mount_crypt_stat->global_default_cipher_name,
  395. mount_crypt_stat->global_default_cipher_key_size);
  396. mutex_unlock(&key_tfm_list_mutex);
  397. if (rc) {
  398. printk(KERN_ERR "Error attempting to initialize cipher with "
  399. "name = [%s] and key size = [%td]; rc = [%d]\n",
  400. mount_crypt_stat->global_default_cipher_name,
  401. mount_crypt_stat->global_default_cipher_key_size, rc);
  402. rc = -EINVAL;
  403. goto out;
  404. }
  405. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  406. if (rc) {
  407. printk(KERN_WARNING "One or more global auth toks could not "
  408. "properly register; rc = [%d]\n", rc);
  409. }
  410. rc = 0;
  411. out:
  412. return rc;
  413. }
  414. struct kmem_cache *ecryptfs_sb_info_cache;
  415. /**
  416. * ecryptfs_fill_super
  417. * @sb: The ecryptfs super block
  418. * @raw_data: The options passed to mount
  419. * @silent: Not used but required by function prototype
  420. *
  421. * Sets up what we can of the sb, rest is done in ecryptfs_read_super
  422. *
  423. * Returns zero on success; non-zero otherwise
  424. */
  425. static int
  426. ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
  427. {
  428. int rc = 0;
  429. /* Released in ecryptfs_put_super() */
  430. ecryptfs_set_superblock_private(sb,
  431. kmem_cache_zalloc(ecryptfs_sb_info_cache,
  432. GFP_KERNEL));
  433. if (!ecryptfs_superblock_to_private(sb)) {
  434. ecryptfs_printk(KERN_WARNING, "Out of memory\n");
  435. rc = -ENOMEM;
  436. goto out;
  437. }
  438. sb->s_op = &ecryptfs_sops;
  439. /* Released through deactivate_super(sb) from get_sb_nodev */
  440. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  441. .hash = 0,.name = "/",.len = 1});
  442. if (!sb->s_root) {
  443. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  444. rc = -ENOMEM;
  445. goto out;
  446. }
  447. sb->s_root->d_op = &ecryptfs_dops;
  448. sb->s_root->d_sb = sb;
  449. sb->s_root->d_parent = sb->s_root;
  450. /* Released in d_release when dput(sb->s_root) is called */
  451. /* through deactivate_super(sb) from get_sb_nodev() */
  452. ecryptfs_set_dentry_private(sb->s_root,
  453. kmem_cache_zalloc(ecryptfs_dentry_info_cache,
  454. GFP_KERNEL));
  455. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  456. ecryptfs_printk(KERN_ERR,
  457. "dentry_info_cache alloc failed\n");
  458. rc = -ENOMEM;
  459. goto out;
  460. }
  461. rc = 0;
  462. out:
  463. /* Should be able to rely on deactivate_super called from
  464. * get_sb_nodev */
  465. return rc;
  466. }
  467. /**
  468. * ecryptfs_read_super
  469. * @sb: The ecryptfs super block
  470. * @dev_name: The path to mount over
  471. *
  472. * Read the super block of the lower filesystem, and use
  473. * ecryptfs_interpose to create our initial inode and super block
  474. * struct.
  475. */
  476. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  477. {
  478. int rc;
  479. struct nameidata nd;
  480. struct dentry *lower_root;
  481. struct vfsmount *lower_mnt;
  482. memset(&nd, 0, sizeof(struct nameidata));
  483. rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
  484. if (rc) {
  485. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  486. goto out;
  487. }
  488. lower_root = nd.path.dentry;
  489. lower_mnt = nd.path.mnt;
  490. ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
  491. sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
  492. sb->s_blocksize = lower_root->d_sb->s_blocksize;
  493. ecryptfs_set_dentry_lower(sb->s_root, lower_root);
  494. ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
  495. rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
  496. if (rc)
  497. goto out_free;
  498. rc = 0;
  499. goto out;
  500. out_free:
  501. path_release(&nd);
  502. out:
  503. return rc;
  504. }
  505. /**
  506. * ecryptfs_get_sb
  507. * @fs_type
  508. * @flags
  509. * @dev_name: The path to mount over
  510. * @raw_data: The options passed into the kernel
  511. *
  512. * The whole ecryptfs_get_sb process is broken into 4 functions:
  513. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  514. * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
  515. * with as much information as it can before needing
  516. * the lower filesystem.
  517. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  518. * ecryptfs_interpolate to perform most of the linking
  519. * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
  520. */
  521. static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
  522. const char *dev_name, void *raw_data,
  523. struct vfsmount *mnt)
  524. {
  525. int rc;
  526. struct super_block *sb;
  527. rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
  528. if (rc < 0) {
  529. printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
  530. goto out;
  531. }
  532. sb = mnt->mnt_sb;
  533. rc = ecryptfs_parse_options(sb, raw_data);
  534. if (rc) {
  535. printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
  536. goto out_abort;
  537. }
  538. rc = ecryptfs_read_super(sb, dev_name);
  539. if (rc) {
  540. printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
  541. goto out_abort;
  542. }
  543. goto out;
  544. out_abort:
  545. dput(sb->s_root);
  546. up_write(&sb->s_umount);
  547. deactivate_super(sb);
  548. out:
  549. return rc;
  550. }
  551. /**
  552. * ecryptfs_kill_block_super
  553. * @sb: The ecryptfs super block
  554. *
  555. * Used to bring the superblock down and free the private data.
  556. * Private data is free'd in ecryptfs_put_super()
  557. */
  558. static void ecryptfs_kill_block_super(struct super_block *sb)
  559. {
  560. generic_shutdown_super(sb);
  561. }
  562. static struct file_system_type ecryptfs_fs_type = {
  563. .owner = THIS_MODULE,
  564. .name = "ecryptfs",
  565. .get_sb = ecryptfs_get_sb,
  566. .kill_sb = ecryptfs_kill_block_super,
  567. .fs_flags = 0
  568. };
  569. /**
  570. * inode_info_init_once
  571. *
  572. * Initializes the ecryptfs_inode_info_cache when it is created
  573. */
  574. static void
  575. inode_info_init_once(struct kmem_cache *cachep, void *vptr)
  576. {
  577. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  578. inode_init_once(&ei->vfs_inode);
  579. }
  580. static struct ecryptfs_cache_info {
  581. struct kmem_cache **cache;
  582. const char *name;
  583. size_t size;
  584. void (*ctor)(struct kmem_cache *cache, void *obj);
  585. } ecryptfs_cache_infos[] = {
  586. {
  587. .cache = &ecryptfs_auth_tok_list_item_cache,
  588. .name = "ecryptfs_auth_tok_list_item",
  589. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  590. },
  591. {
  592. .cache = &ecryptfs_file_info_cache,
  593. .name = "ecryptfs_file_cache",
  594. .size = sizeof(struct ecryptfs_file_info),
  595. },
  596. {
  597. .cache = &ecryptfs_dentry_info_cache,
  598. .name = "ecryptfs_dentry_info_cache",
  599. .size = sizeof(struct ecryptfs_dentry_info),
  600. },
  601. {
  602. .cache = &ecryptfs_inode_info_cache,
  603. .name = "ecryptfs_inode_cache",
  604. .size = sizeof(struct ecryptfs_inode_info),
  605. .ctor = inode_info_init_once,
  606. },
  607. {
  608. .cache = &ecryptfs_sb_info_cache,
  609. .name = "ecryptfs_sb_cache",
  610. .size = sizeof(struct ecryptfs_sb_info),
  611. },
  612. {
  613. .cache = &ecryptfs_header_cache_1,
  614. .name = "ecryptfs_headers_1",
  615. .size = PAGE_CACHE_SIZE,
  616. },
  617. {
  618. .cache = &ecryptfs_header_cache_2,
  619. .name = "ecryptfs_headers_2",
  620. .size = PAGE_CACHE_SIZE,
  621. },
  622. {
  623. .cache = &ecryptfs_xattr_cache,
  624. .name = "ecryptfs_xattr_cache",
  625. .size = PAGE_CACHE_SIZE,
  626. },
  627. {
  628. .cache = &ecryptfs_key_record_cache,
  629. .name = "ecryptfs_key_record_cache",
  630. .size = sizeof(struct ecryptfs_key_record),
  631. },
  632. {
  633. .cache = &ecryptfs_key_sig_cache,
  634. .name = "ecryptfs_key_sig_cache",
  635. .size = sizeof(struct ecryptfs_key_sig),
  636. },
  637. {
  638. .cache = &ecryptfs_global_auth_tok_cache,
  639. .name = "ecryptfs_global_auth_tok_cache",
  640. .size = sizeof(struct ecryptfs_global_auth_tok),
  641. },
  642. {
  643. .cache = &ecryptfs_key_tfm_cache,
  644. .name = "ecryptfs_key_tfm_cache",
  645. .size = sizeof(struct ecryptfs_key_tfm),
  646. },
  647. };
  648. static void ecryptfs_free_kmem_caches(void)
  649. {
  650. int i;
  651. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  652. struct ecryptfs_cache_info *info;
  653. info = &ecryptfs_cache_infos[i];
  654. if (*(info->cache))
  655. kmem_cache_destroy(*(info->cache));
  656. }
  657. }
  658. /**
  659. * ecryptfs_init_kmem_caches
  660. *
  661. * Returns zero on success; non-zero otherwise
  662. */
  663. static int ecryptfs_init_kmem_caches(void)
  664. {
  665. int i;
  666. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  667. struct ecryptfs_cache_info *info;
  668. info = &ecryptfs_cache_infos[i];
  669. *(info->cache) = kmem_cache_create(info->name, info->size,
  670. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  671. if (!*(info->cache)) {
  672. ecryptfs_free_kmem_caches();
  673. ecryptfs_printk(KERN_WARNING, "%s: "
  674. "kmem_cache_create failed\n",
  675. info->name);
  676. return -ENOMEM;
  677. }
  678. }
  679. return 0;
  680. }
  681. static struct kobject *ecryptfs_kobj;
  682. static ssize_t version_show(struct kobject *kobj,
  683. struct kobj_attribute *attr, char *buff)
  684. {
  685. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  686. }
  687. static struct kobj_attribute version_attr = __ATTR_RO(version);
  688. static struct attribute *attributes[] = {
  689. &version_attr.attr,
  690. NULL,
  691. };
  692. static struct attribute_group attr_group = {
  693. .attrs = attributes,
  694. };
  695. static int do_sysfs_registration(void)
  696. {
  697. int rc;
  698. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  699. if (!ecryptfs_kobj) {
  700. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  701. rc = -ENOMEM;
  702. goto out;
  703. }
  704. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  705. if (rc) {
  706. printk(KERN_ERR
  707. "Unable to create ecryptfs version attributes\n");
  708. kobject_put(ecryptfs_kobj);
  709. }
  710. out:
  711. return rc;
  712. }
  713. static void do_sysfs_unregistration(void)
  714. {
  715. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  716. kobject_put(ecryptfs_kobj);
  717. }
  718. static int __init ecryptfs_init(void)
  719. {
  720. int rc;
  721. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  722. rc = -EINVAL;
  723. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  724. "larger than the host's page size, and so "
  725. "eCryptfs cannot run on this system. The "
  726. "default eCryptfs extent size is [%d] bytes; "
  727. "the page size is [%d] bytes.\n",
  728. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  729. goto out;
  730. }
  731. rc = ecryptfs_init_kmem_caches();
  732. if (rc) {
  733. printk(KERN_ERR
  734. "Failed to allocate one or more kmem_cache objects\n");
  735. goto out;
  736. }
  737. rc = register_filesystem(&ecryptfs_fs_type);
  738. if (rc) {
  739. printk(KERN_ERR "Failed to register filesystem\n");
  740. goto out_free_kmem_caches;
  741. }
  742. rc = do_sysfs_registration();
  743. if (rc) {
  744. printk(KERN_ERR "sysfs registration failed\n");
  745. goto out_unregister_filesystem;
  746. }
  747. rc = ecryptfs_init_messaging(ecryptfs_transport);
  748. if (rc) {
  749. ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
  750. "initialize the eCryptfs netlink socket\n");
  751. goto out_do_sysfs_unregistration;
  752. }
  753. rc = ecryptfs_init_crypto();
  754. if (rc) {
  755. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  756. "rc = [%d]\n", rc);
  757. goto out_release_messaging;
  758. }
  759. if (ecryptfs_verbosity > 0)
  760. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  761. "will be written to the syslog!\n", ecryptfs_verbosity);
  762. goto out;
  763. out_release_messaging:
  764. ecryptfs_release_messaging(ecryptfs_transport);
  765. out_do_sysfs_unregistration:
  766. do_sysfs_unregistration();
  767. out_unregister_filesystem:
  768. unregister_filesystem(&ecryptfs_fs_type);
  769. out_free_kmem_caches:
  770. ecryptfs_free_kmem_caches();
  771. out:
  772. return rc;
  773. }
  774. static void __exit ecryptfs_exit(void)
  775. {
  776. int rc;
  777. rc = ecryptfs_destroy_crypto();
  778. if (rc)
  779. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  780. "rc = [%d]\n", rc);
  781. ecryptfs_release_messaging(ecryptfs_transport);
  782. do_sysfs_unregistration();
  783. unregister_filesystem(&ecryptfs_fs_type);
  784. ecryptfs_free_kmem_caches();
  785. }
  786. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  787. MODULE_DESCRIPTION("eCryptfs");
  788. MODULE_LICENSE("GPL");
  789. module_init(ecryptfs_init)
  790. module_exit(ecryptfs_exit)