main.c 24 KB

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