main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. 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. if (rc) {
  179. printk(KERN_ERR "Could not find valid key in user "
  180. "session keyring for sig specified in mount "
  181. "option: [%s]\n", global_auth_tok->sig);
  182. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  183. rc = 0;
  184. } else
  185. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  186. }
  187. return rc;
  188. }
  189. static void ecryptfs_init_mount_crypt_stat(
  190. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  191. {
  192. memset((void *)mount_crypt_stat, 0,
  193. sizeof(struct ecryptfs_mount_crypt_stat));
  194. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  195. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  196. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  197. }
  198. /**
  199. * ecryptfs_parse_options
  200. * @sb: The ecryptfs super block
  201. * @options: The options pased to the kernel
  202. *
  203. * Parse mount options:
  204. * debug=N - ecryptfs_verbosity level for debug output
  205. * sig=XXX - description(signature) of the key to use
  206. *
  207. * Returns the dentry object of the lower-level (lower/interposed)
  208. * directory; We want to mount our stackable file system on top of
  209. * that lower directory.
  210. *
  211. * The signature of the key to use must be the description of a key
  212. * already in the keyring. Mounting will fail if the key can not be
  213. * found.
  214. *
  215. * Returns zero on success; non-zero on error
  216. */
  217. static int ecryptfs_parse_options(struct super_block *sb, char *options)
  218. {
  219. char *p;
  220. int rc = 0;
  221. int sig_set = 0;
  222. int cipher_name_set = 0;
  223. int cipher_key_bytes;
  224. int cipher_key_bytes_set = 0;
  225. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  226. &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
  227. substring_t args[MAX_OPT_ARGS];
  228. int token;
  229. char *sig_src;
  230. char *debug_src;
  231. char *cipher_name_dst;
  232. char *cipher_name_src;
  233. char *cipher_key_bytes_src;
  234. int cipher_name_len;
  235. if (!options) {
  236. rc = -EINVAL;
  237. goto out;
  238. }
  239. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  240. while ((p = strsep(&options, ",")) != NULL) {
  241. if (!*p)
  242. continue;
  243. token = match_token(p, tokens, args);
  244. switch (token) {
  245. case ecryptfs_opt_sig:
  246. case ecryptfs_opt_ecryptfs_sig:
  247. sig_src = args[0].from;
  248. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  249. sig_src);
  250. if (rc) {
  251. printk(KERN_ERR "Error attempting to register "
  252. "global sig; rc = [%d]\n", rc);
  253. goto out;
  254. }
  255. sig_set = 1;
  256. break;
  257. case ecryptfs_opt_debug:
  258. case ecryptfs_opt_ecryptfs_debug:
  259. debug_src = args[0].from;
  260. ecryptfs_verbosity =
  261. (int)simple_strtol(debug_src, &debug_src,
  262. 0);
  263. ecryptfs_printk(KERN_DEBUG,
  264. "Verbosity set to [%d]" "\n",
  265. ecryptfs_verbosity);
  266. break;
  267. case ecryptfs_opt_cipher:
  268. case ecryptfs_opt_ecryptfs_cipher:
  269. cipher_name_src = args[0].from;
  270. cipher_name_dst =
  271. mount_crypt_stat->
  272. global_default_cipher_name;
  273. strncpy(cipher_name_dst, cipher_name_src,
  274. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  275. ecryptfs_printk(KERN_DEBUG,
  276. "The mount_crypt_stat "
  277. "global_default_cipher_name set to: "
  278. "[%s]\n", cipher_name_dst);
  279. cipher_name_set = 1;
  280. break;
  281. case ecryptfs_opt_ecryptfs_key_bytes:
  282. cipher_key_bytes_src = args[0].from;
  283. cipher_key_bytes =
  284. (int)simple_strtol(cipher_key_bytes_src,
  285. &cipher_key_bytes_src, 0);
  286. mount_crypt_stat->global_default_cipher_key_size =
  287. cipher_key_bytes;
  288. ecryptfs_printk(KERN_DEBUG,
  289. "The mount_crypt_stat "
  290. "global_default_cipher_key_size "
  291. "set to: [%d]\n", mount_crypt_stat->
  292. global_default_cipher_key_size);
  293. cipher_key_bytes_set = 1;
  294. break;
  295. case ecryptfs_opt_passthrough:
  296. mount_crypt_stat->flags |=
  297. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  298. break;
  299. case ecryptfs_opt_xattr_metadata:
  300. mount_crypt_stat->flags |=
  301. ECRYPTFS_XATTR_METADATA_ENABLED;
  302. break;
  303. case ecryptfs_opt_encrypted_view:
  304. mount_crypt_stat->flags |=
  305. ECRYPTFS_XATTR_METADATA_ENABLED;
  306. mount_crypt_stat->flags |=
  307. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  308. break;
  309. case ecryptfs_opt_err:
  310. default:
  311. ecryptfs_printk(KERN_WARNING,
  312. "eCryptfs: unrecognized option '%s'\n",
  313. p);
  314. }
  315. }
  316. if (!sig_set) {
  317. rc = -EINVAL;
  318. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  319. "auth tok signature as a mount "
  320. "parameter; see the eCryptfs README\n");
  321. goto out;
  322. }
  323. if (!cipher_name_set) {
  324. cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  325. if (unlikely(cipher_name_len
  326. >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
  327. rc = -EINVAL;
  328. BUG();
  329. goto out;
  330. }
  331. memcpy(mount_crypt_stat->global_default_cipher_name,
  332. ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
  333. mount_crypt_stat->global_default_cipher_name[cipher_name_len]
  334. = '\0';
  335. }
  336. if (!cipher_key_bytes_set) {
  337. mount_crypt_stat->global_default_cipher_key_size = 0;
  338. }
  339. rc = ecryptfs_add_new_key_tfm(
  340. NULL, mount_crypt_stat->global_default_cipher_name,
  341. mount_crypt_stat->global_default_cipher_key_size);
  342. if (rc) {
  343. printk(KERN_ERR "Error attempting to initialize cipher with "
  344. "name = [%s] and key size = [%td]; rc = [%d]\n",
  345. mount_crypt_stat->global_default_cipher_name,
  346. mount_crypt_stat->global_default_cipher_key_size, rc);
  347. rc = -EINVAL;
  348. goto out;
  349. }
  350. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  351. if (rc) {
  352. printk(KERN_WARNING "One or more global auth toks could not "
  353. "properly register; rc = [%d]\n", rc);
  354. }
  355. rc = 0;
  356. out:
  357. return rc;
  358. }
  359. struct kmem_cache *ecryptfs_sb_info_cache;
  360. /**
  361. * ecryptfs_fill_super
  362. * @sb: The ecryptfs super block
  363. * @raw_data: The options passed to mount
  364. * @silent: Not used but required by function prototype
  365. *
  366. * Sets up what we can of the sb, rest is done in ecryptfs_read_super
  367. *
  368. * Returns zero on success; non-zero otherwise
  369. */
  370. static int
  371. ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
  372. {
  373. int rc = 0;
  374. /* Released in ecryptfs_put_super() */
  375. ecryptfs_set_superblock_private(sb,
  376. kmem_cache_zalloc(ecryptfs_sb_info_cache,
  377. GFP_KERNEL));
  378. if (!ecryptfs_superblock_to_private(sb)) {
  379. ecryptfs_printk(KERN_WARNING, "Out of memory\n");
  380. rc = -ENOMEM;
  381. goto out;
  382. }
  383. sb->s_op = &ecryptfs_sops;
  384. /* Released through deactivate_super(sb) from get_sb_nodev */
  385. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  386. .hash = 0,.name = "/",.len = 1});
  387. if (!sb->s_root) {
  388. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  389. rc = -ENOMEM;
  390. goto out;
  391. }
  392. sb->s_root->d_op = &ecryptfs_dops;
  393. sb->s_root->d_sb = sb;
  394. sb->s_root->d_parent = sb->s_root;
  395. /* Released in d_release when dput(sb->s_root) is called */
  396. /* through deactivate_super(sb) from get_sb_nodev() */
  397. ecryptfs_set_dentry_private(sb->s_root,
  398. kmem_cache_zalloc(ecryptfs_dentry_info_cache,
  399. GFP_KERNEL));
  400. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  401. ecryptfs_printk(KERN_ERR,
  402. "dentry_info_cache alloc failed\n");
  403. rc = -ENOMEM;
  404. goto out;
  405. }
  406. rc = 0;
  407. out:
  408. /* Should be able to rely on deactivate_super called from
  409. * get_sb_nodev */
  410. return rc;
  411. }
  412. /**
  413. * ecryptfs_read_super
  414. * @sb: The ecryptfs super block
  415. * @dev_name: The path to mount over
  416. *
  417. * Read the super block of the lower filesystem, and use
  418. * ecryptfs_interpose to create our initial inode and super block
  419. * struct.
  420. */
  421. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  422. {
  423. int rc;
  424. struct nameidata nd;
  425. struct dentry *lower_root;
  426. struct vfsmount *lower_mnt;
  427. memset(&nd, 0, sizeof(struct nameidata));
  428. rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
  429. if (rc) {
  430. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  431. goto out;
  432. }
  433. lower_root = nd.dentry;
  434. lower_mnt = nd.mnt;
  435. ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
  436. sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
  437. ecryptfs_set_dentry_lower(sb->s_root, lower_root);
  438. ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
  439. rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
  440. if (rc)
  441. goto out_free;
  442. rc = 0;
  443. goto out;
  444. out_free:
  445. path_release(&nd);
  446. out:
  447. return rc;
  448. }
  449. /**
  450. * ecryptfs_get_sb
  451. * @fs_type
  452. * @flags
  453. * @dev_name: The path to mount over
  454. * @raw_data: The options passed into the kernel
  455. *
  456. * The whole ecryptfs_get_sb process is broken into 4 functions:
  457. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  458. * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
  459. * with as much information as it can before needing
  460. * the lower filesystem.
  461. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  462. * ecryptfs_interpolate to perform most of the linking
  463. * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
  464. */
  465. static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
  466. const char *dev_name, void *raw_data,
  467. struct vfsmount *mnt)
  468. {
  469. int rc;
  470. struct super_block *sb;
  471. rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
  472. if (rc < 0) {
  473. printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
  474. goto out;
  475. }
  476. sb = mnt->mnt_sb;
  477. rc = ecryptfs_parse_options(sb, raw_data);
  478. if (rc) {
  479. printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
  480. goto out_abort;
  481. }
  482. rc = ecryptfs_read_super(sb, dev_name);
  483. if (rc) {
  484. printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
  485. goto out_abort;
  486. }
  487. goto out;
  488. out_abort:
  489. dput(sb->s_root);
  490. up_write(&sb->s_umount);
  491. deactivate_super(sb);
  492. out:
  493. return rc;
  494. }
  495. /**
  496. * ecryptfs_kill_block_super
  497. * @sb: The ecryptfs super block
  498. *
  499. * Used to bring the superblock down and free the private data.
  500. * Private data is free'd in ecryptfs_put_super()
  501. */
  502. static void ecryptfs_kill_block_super(struct super_block *sb)
  503. {
  504. generic_shutdown_super(sb);
  505. }
  506. static struct file_system_type ecryptfs_fs_type = {
  507. .owner = THIS_MODULE,
  508. .name = "ecryptfs",
  509. .get_sb = ecryptfs_get_sb,
  510. .kill_sb = ecryptfs_kill_block_super,
  511. .fs_flags = 0
  512. };
  513. /**
  514. * inode_info_init_once
  515. *
  516. * Initializes the ecryptfs_inode_info_cache when it is created
  517. */
  518. static void
  519. inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
  520. {
  521. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  522. inode_init_once(&ei->vfs_inode);
  523. }
  524. static struct ecryptfs_cache_info {
  525. struct kmem_cache **cache;
  526. const char *name;
  527. size_t size;
  528. void (*ctor)(void*, struct kmem_cache *, unsigned long);
  529. } ecryptfs_cache_infos[] = {
  530. {
  531. .cache = &ecryptfs_auth_tok_list_item_cache,
  532. .name = "ecryptfs_auth_tok_list_item",
  533. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  534. },
  535. {
  536. .cache = &ecryptfs_file_info_cache,
  537. .name = "ecryptfs_file_cache",
  538. .size = sizeof(struct ecryptfs_file_info),
  539. },
  540. {
  541. .cache = &ecryptfs_dentry_info_cache,
  542. .name = "ecryptfs_dentry_info_cache",
  543. .size = sizeof(struct ecryptfs_dentry_info),
  544. },
  545. {
  546. .cache = &ecryptfs_inode_info_cache,
  547. .name = "ecryptfs_inode_cache",
  548. .size = sizeof(struct ecryptfs_inode_info),
  549. .ctor = inode_info_init_once,
  550. },
  551. {
  552. .cache = &ecryptfs_sb_info_cache,
  553. .name = "ecryptfs_sb_cache",
  554. .size = sizeof(struct ecryptfs_sb_info),
  555. },
  556. {
  557. .cache = &ecryptfs_header_cache_0,
  558. .name = "ecryptfs_headers_0",
  559. .size = PAGE_CACHE_SIZE,
  560. },
  561. {
  562. .cache = &ecryptfs_header_cache_1,
  563. .name = "ecryptfs_headers_1",
  564. .size = PAGE_CACHE_SIZE,
  565. },
  566. {
  567. .cache = &ecryptfs_header_cache_2,
  568. .name = "ecryptfs_headers_2",
  569. .size = PAGE_CACHE_SIZE,
  570. },
  571. {
  572. .cache = &ecryptfs_xattr_cache,
  573. .name = "ecryptfs_xattr_cache",
  574. .size = PAGE_CACHE_SIZE,
  575. },
  576. {
  577. .cache = &ecryptfs_lower_page_cache,
  578. .name = "ecryptfs_lower_page_cache",
  579. .size = PAGE_CACHE_SIZE,
  580. },
  581. {
  582. .cache = &ecryptfs_key_record_cache,
  583. .name = "ecryptfs_key_record_cache",
  584. .size = sizeof(struct ecryptfs_key_record),
  585. },
  586. {
  587. .cache = &ecryptfs_key_sig_cache,
  588. .name = "ecryptfs_key_sig_cache",
  589. .size = sizeof(struct ecryptfs_key_sig),
  590. },
  591. {
  592. .cache = &ecryptfs_global_auth_tok_cache,
  593. .name = "ecryptfs_global_auth_tok_cache",
  594. .size = sizeof(struct ecryptfs_global_auth_tok),
  595. },
  596. {
  597. .cache = &ecryptfs_key_tfm_cache,
  598. .name = "ecryptfs_key_tfm_cache",
  599. .size = sizeof(struct ecryptfs_key_tfm),
  600. },
  601. };
  602. static void ecryptfs_free_kmem_caches(void)
  603. {
  604. int i;
  605. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  606. struct ecryptfs_cache_info *info;
  607. info = &ecryptfs_cache_infos[i];
  608. if (*(info->cache))
  609. kmem_cache_destroy(*(info->cache));
  610. }
  611. }
  612. /**
  613. * ecryptfs_init_kmem_caches
  614. *
  615. * Returns zero on success; non-zero otherwise
  616. */
  617. static int ecryptfs_init_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. *(info->cache) = kmem_cache_create(info->name, info->size,
  624. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  625. if (!*(info->cache)) {
  626. ecryptfs_free_kmem_caches();
  627. ecryptfs_printk(KERN_WARNING, "%s: "
  628. "kmem_cache_create failed\n",
  629. info->name);
  630. return -ENOMEM;
  631. }
  632. }
  633. return 0;
  634. }
  635. struct ecryptfs_obj {
  636. char *name;
  637. struct list_head slot_list;
  638. struct kobject kobj;
  639. };
  640. struct ecryptfs_attribute {
  641. struct attribute attr;
  642. ssize_t(*show) (struct ecryptfs_obj *, char *);
  643. ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
  644. };
  645. static ssize_t
  646. ecryptfs_attr_store(struct kobject *kobj,
  647. struct attribute *attr, const char *buf, size_t len)
  648. {
  649. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  650. kobj);
  651. struct ecryptfs_attribute *attribute =
  652. container_of(attr, struct ecryptfs_attribute, attr);
  653. return (attribute->store ? attribute->store(obj, buf, len) : 0);
  654. }
  655. static ssize_t
  656. ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  657. {
  658. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  659. kobj);
  660. struct ecryptfs_attribute *attribute =
  661. container_of(attr, struct ecryptfs_attribute, attr);
  662. return (attribute->show ? attribute->show(obj, buf) : 0);
  663. }
  664. static struct sysfs_ops ecryptfs_sysfs_ops = {
  665. .show = ecryptfs_attr_show,
  666. .store = ecryptfs_attr_store
  667. };
  668. static struct kobj_type ecryptfs_ktype = {
  669. .sysfs_ops = &ecryptfs_sysfs_ops
  670. };
  671. static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
  672. static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
  673. {
  674. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  675. }
  676. static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
  677. static struct ecryptfs_version_str_map_elem {
  678. u32 flag;
  679. char *str;
  680. } ecryptfs_version_str_map[] = {
  681. {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
  682. {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
  683. {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
  684. {ECRYPTFS_VERSIONING_POLICY, "policy"},
  685. {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"},
  686. {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"}
  687. };
  688. static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
  689. {
  690. int i;
  691. int remaining = PAGE_SIZE;
  692. int total_written = 0;
  693. buff[0] = '\0';
  694. for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
  695. int entry_size;
  696. if (!(ECRYPTFS_VERSIONING_MASK
  697. & ecryptfs_version_str_map[i].flag))
  698. continue;
  699. entry_size = strlen(ecryptfs_version_str_map[i].str);
  700. if ((entry_size + 2) > remaining)
  701. goto out;
  702. memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
  703. buff[entry_size++] = '\n';
  704. buff[entry_size] = '\0';
  705. buff += entry_size;
  706. total_written += entry_size;
  707. remaining -= entry_size;
  708. }
  709. out:
  710. return total_written;
  711. }
  712. static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
  713. static int do_sysfs_registration(void)
  714. {
  715. int rc;
  716. rc = subsystem_register(&ecryptfs_subsys);
  717. if (rc) {
  718. printk(KERN_ERR
  719. "Unable to register ecryptfs sysfs subsystem\n");
  720. goto out;
  721. }
  722. rc = sysfs_create_file(&ecryptfs_subsys.kobj,
  723. &sysfs_attr_version.attr);
  724. if (rc) {
  725. printk(KERN_ERR
  726. "Unable to create ecryptfs version attribute\n");
  727. subsystem_unregister(&ecryptfs_subsys);
  728. goto out;
  729. }
  730. rc = sysfs_create_file(&ecryptfs_subsys.kobj,
  731. &sysfs_attr_version_str.attr);
  732. if (rc) {
  733. printk(KERN_ERR
  734. "Unable to create ecryptfs version_str attribute\n");
  735. sysfs_remove_file(&ecryptfs_subsys.kobj,
  736. &sysfs_attr_version.attr);
  737. subsystem_unregister(&ecryptfs_subsys);
  738. goto out;
  739. }
  740. out:
  741. return rc;
  742. }
  743. static void do_sysfs_unregistration(void)
  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. goto out_free_kmem_caches;
  774. }
  775. kobj_set_kset_s(&ecryptfs_subsys, fs_subsys);
  776. rc = do_sysfs_registration();
  777. if (rc) {
  778. printk(KERN_ERR "sysfs registration failed\n");
  779. goto out_unregister_filesystem;
  780. }
  781. rc = ecryptfs_init_messaging(ecryptfs_transport);
  782. if (rc) {
  783. ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
  784. "initialize the eCryptfs netlink socket\n");
  785. goto out_do_sysfs_unregistration;
  786. }
  787. rc = ecryptfs_init_crypto();
  788. if (rc) {
  789. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  790. "rc = [%d]\n", rc);
  791. goto out_release_messaging;
  792. }
  793. goto out;
  794. out_release_messaging:
  795. ecryptfs_release_messaging(ecryptfs_transport);
  796. out_do_sysfs_unregistration:
  797. do_sysfs_unregistration();
  798. out_unregister_filesystem:
  799. unregister_filesystem(&ecryptfs_fs_type);
  800. out_free_kmem_caches:
  801. ecryptfs_free_kmem_caches();
  802. out:
  803. return rc;
  804. }
  805. static void __exit ecryptfs_exit(void)
  806. {
  807. int rc;
  808. rc = ecryptfs_destroy_crypto();
  809. if (rc)
  810. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  811. "rc = [%d]\n", rc);
  812. ecryptfs_release_messaging(ecryptfs_transport);
  813. do_sysfs_unregistration();
  814. unregister_filesystem(&ecryptfs_fs_type);
  815. ecryptfs_free_kmem_caches();
  816. }
  817. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  818. MODULE_DESCRIPTION("eCryptfs");
  819. MODULE_LICENSE("GPL");
  820. module_init(ecryptfs_init)
  821. module_exit(ecryptfs_exit)