main.c 24 KB

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