main.c 23 KB

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