main.c 23 KB

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