main.c 23 KB

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