main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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-2006 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  8. * Michael C. Thompson <mcthomps@us.ibm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <linux/dcache.h>
  26. #include <linux/file.h>
  27. #include <linux/module.h>
  28. #include <linux/namei.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/crypto.h>
  31. #include <linux/netlink.h>
  32. #include <linux/mount.h>
  33. #include <linux/dcache.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/key.h>
  36. #include <linux/parser.h>
  37. #include "ecryptfs_kernel.h"
  38. /**
  39. * Module parameter that defines the ecryptfs_verbosity level.
  40. */
  41. int ecryptfs_verbosity = 0;
  42. module_param(ecryptfs_verbosity, int, 0);
  43. MODULE_PARM_DESC(ecryptfs_verbosity,
  44. "Initial verbosity level (0 or 1; defaults to "
  45. "0, which is Quiet)");
  46. void __ecryptfs_printk(const char *fmt, ...)
  47. {
  48. va_list args;
  49. va_start(args, fmt);
  50. if (fmt[1] == '7') { /* KERN_DEBUG */
  51. if (ecryptfs_verbosity >= 1)
  52. vprintk(fmt, args);
  53. } else
  54. vprintk(fmt, args);
  55. va_end(args);
  56. }
  57. /**
  58. * ecryptfs_interpose
  59. * @lower_dentry: Existing dentry in the lower filesystem
  60. * @dentry: ecryptfs' dentry
  61. * @sb: ecryptfs's super_block
  62. * @flag: If set to true, then d_add is called, else d_instantiate is called
  63. *
  64. * Interposes upper and lower dentries.
  65. *
  66. * Returns zero on success; non-zero otherwise
  67. */
  68. int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
  69. struct super_block *sb, int flag)
  70. {
  71. struct inode *lower_inode;
  72. struct inode *inode;
  73. int rc = 0;
  74. lower_inode = lower_dentry->d_inode;
  75. if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
  76. rc = -EXDEV;
  77. goto out;
  78. }
  79. if (!igrab(lower_inode)) {
  80. rc = -ESTALE;
  81. goto out;
  82. }
  83. inode = iget5_locked(sb, (unsigned long)lower_inode,
  84. ecryptfs_inode_test, ecryptfs_inode_set,
  85. lower_inode);
  86. if (!inode) {
  87. rc = -EACCES;
  88. iput(lower_inode);
  89. goto out;
  90. }
  91. if (inode->i_state & I_NEW)
  92. unlock_new_inode(inode);
  93. else
  94. iput(lower_inode);
  95. if (S_ISLNK(lower_inode->i_mode))
  96. inode->i_op = &ecryptfs_symlink_iops;
  97. else if (S_ISDIR(lower_inode->i_mode))
  98. inode->i_op = &ecryptfs_dir_iops;
  99. if (S_ISDIR(lower_inode->i_mode))
  100. inode->i_fop = &ecryptfs_dir_fops;
  101. if (special_file(lower_inode->i_mode))
  102. init_special_inode(inode, lower_inode->i_mode,
  103. lower_inode->i_rdev);
  104. dentry->d_op = &ecryptfs_dops;
  105. if (flag)
  106. d_add(dentry, inode);
  107. else
  108. d_instantiate(dentry, inode);
  109. ecryptfs_copy_attr_all(inode, lower_inode);
  110. /* This size will be overwritten for real files w/ headers and
  111. * other metadata */
  112. ecryptfs_copy_inode_size(inode, lower_inode);
  113. out:
  114. return rc;
  115. }
  116. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
  117. ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
  118. ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
  119. ecryptfs_opt_passthrough, ecryptfs_opt_err };
  120. static match_table_t tokens = {
  121. {ecryptfs_opt_sig, "sig=%s"},
  122. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  123. {ecryptfs_opt_debug, "debug=%u"},
  124. {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
  125. {ecryptfs_opt_cipher, "cipher=%s"},
  126. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  127. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  128. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  129. {ecryptfs_opt_err, NULL}
  130. };
  131. /**
  132. * ecryptfs_verify_version
  133. * @version: The version number to confirm
  134. *
  135. * Returns zero on good version; non-zero otherwise
  136. */
  137. static int ecryptfs_verify_version(u16 version)
  138. {
  139. int rc = 0;
  140. unsigned char major;
  141. unsigned char minor;
  142. major = ((version >> 8) & 0xFF);
  143. minor = (version & 0xFF);
  144. if (major != ECRYPTFS_VERSION_MAJOR) {
  145. ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
  146. "Expected [%d]; got [%d]\n",
  147. ECRYPTFS_VERSION_MAJOR, major);
  148. rc = -EINVAL;
  149. goto out;
  150. }
  151. if (minor != ECRYPTFS_VERSION_MINOR) {
  152. ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
  153. "Expected [%d]; got [%d]\n",
  154. ECRYPTFS_VERSION_MINOR, minor);
  155. rc = -EINVAL;
  156. goto out;
  157. }
  158. out:
  159. return rc;
  160. }
  161. /**
  162. * ecryptfs_parse_options
  163. * @sb: The ecryptfs super block
  164. * @options: The options pased to the kernel
  165. *
  166. * Parse mount options:
  167. * debug=N - ecryptfs_verbosity level for debug output
  168. * sig=XXX - description(signature) of the key to use
  169. *
  170. * Returns the dentry object of the lower-level (lower/interposed)
  171. * directory; We want to mount our stackable file system on top of
  172. * that lower directory.
  173. *
  174. * The signature of the key to use must be the description of a key
  175. * already in the keyring. Mounting will fail if the key can not be
  176. * found.
  177. *
  178. * Returns zero on success; non-zero on error
  179. */
  180. static int ecryptfs_parse_options(struct super_block *sb, char *options)
  181. {
  182. char *p;
  183. int rc = 0;
  184. int sig_set = 0;
  185. int cipher_name_set = 0;
  186. int cipher_key_bytes;
  187. int cipher_key_bytes_set = 0;
  188. struct key *auth_tok_key = NULL;
  189. struct ecryptfs_auth_tok *auth_tok = NULL;
  190. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  191. &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
  192. substring_t args[MAX_OPT_ARGS];
  193. int token;
  194. char *sig_src;
  195. char *sig_dst;
  196. char *debug_src;
  197. char *cipher_name_dst;
  198. char *cipher_name_src;
  199. char *cipher_key_bytes_src;
  200. int cipher_name_len;
  201. if (!options) {
  202. rc = -EINVAL;
  203. goto out;
  204. }
  205. while ((p = strsep(&options, ",")) != NULL) {
  206. if (!*p)
  207. continue;
  208. token = match_token(p, tokens, args);
  209. switch (token) {
  210. case ecryptfs_opt_sig:
  211. case ecryptfs_opt_ecryptfs_sig:
  212. sig_src = args[0].from;
  213. sig_dst =
  214. mount_crypt_stat->global_auth_tok_sig;
  215. memcpy(sig_dst, sig_src, ECRYPTFS_SIG_SIZE_HEX);
  216. sig_dst[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  217. ecryptfs_printk(KERN_DEBUG,
  218. "The mount_crypt_stat "
  219. "global_auth_tok_sig set to: "
  220. "[%s]\n", sig_dst);
  221. sig_set = 1;
  222. break;
  223. case ecryptfs_opt_debug:
  224. case ecryptfs_opt_ecryptfs_debug:
  225. debug_src = args[0].from;
  226. ecryptfs_verbosity =
  227. (int)simple_strtol(debug_src, &debug_src,
  228. 0);
  229. ecryptfs_printk(KERN_DEBUG,
  230. "Verbosity set to [%d]" "\n",
  231. ecryptfs_verbosity);
  232. break;
  233. case ecryptfs_opt_cipher:
  234. case ecryptfs_opt_ecryptfs_cipher:
  235. cipher_name_src = args[0].from;
  236. cipher_name_dst =
  237. mount_crypt_stat->
  238. global_default_cipher_name;
  239. strncpy(cipher_name_dst, cipher_name_src,
  240. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  241. ecryptfs_printk(KERN_DEBUG,
  242. "The mount_crypt_stat "
  243. "global_default_cipher_name set to: "
  244. "[%s]\n", cipher_name_dst);
  245. cipher_name_set = 1;
  246. break;
  247. case ecryptfs_opt_ecryptfs_key_bytes:
  248. cipher_key_bytes_src = args[0].from;
  249. cipher_key_bytes =
  250. (int)simple_strtol(cipher_key_bytes_src,
  251. &cipher_key_bytes_src, 0);
  252. mount_crypt_stat->global_default_cipher_key_size =
  253. cipher_key_bytes;
  254. ecryptfs_printk(KERN_DEBUG,
  255. "The mount_crypt_stat "
  256. "global_default_cipher_key_size "
  257. "set to: [%d]\n", mount_crypt_stat->
  258. global_default_cipher_key_size);
  259. cipher_key_bytes_set = 1;
  260. break;
  261. case ecryptfs_opt_passthrough:
  262. mount_crypt_stat->flags |=
  263. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  264. break;
  265. case ecryptfs_opt_err:
  266. default:
  267. ecryptfs_printk(KERN_WARNING,
  268. "eCryptfs: unrecognized option '%s'\n",
  269. p);
  270. }
  271. }
  272. /* Do not support lack of mount-wide signature in 0.1
  273. * release */
  274. if (!sig_set) {
  275. rc = -EINVAL;
  276. ecryptfs_printk(KERN_ERR, "You must supply a valid "
  277. "passphrase auth tok signature as a mount "
  278. "parameter; see the eCryptfs README\n");
  279. goto out;
  280. }
  281. if (!cipher_name_set) {
  282. cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  283. if (unlikely(cipher_name_len
  284. >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
  285. rc = -EINVAL;
  286. BUG();
  287. goto out;
  288. }
  289. memcpy(mount_crypt_stat->global_default_cipher_name,
  290. ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
  291. mount_crypt_stat->global_default_cipher_name[cipher_name_len]
  292. = '\0';
  293. }
  294. if (!cipher_key_bytes_set) {
  295. mount_crypt_stat->global_default_cipher_key_size = 0;
  296. }
  297. rc = ecryptfs_process_cipher(
  298. &mount_crypt_stat->global_key_tfm,
  299. mount_crypt_stat->global_default_cipher_name,
  300. &mount_crypt_stat->global_default_cipher_key_size);
  301. if (rc) {
  302. printk(KERN_ERR "Error attempting to initialize cipher [%s] "
  303. "with key size [%Zd] bytes; rc = [%d]\n",
  304. mount_crypt_stat->global_default_cipher_name,
  305. mount_crypt_stat->global_default_cipher_key_size, rc);
  306. rc = -EINVAL;
  307. goto out;
  308. }
  309. mutex_init(&mount_crypt_stat->global_key_tfm_mutex);
  310. ecryptfs_printk(KERN_DEBUG, "Requesting the key with description: "
  311. "[%s]\n", mount_crypt_stat->global_auth_tok_sig);
  312. /* The reference to this key is held until umount is done The
  313. * call to key_put is done in ecryptfs_put_super() */
  314. auth_tok_key = request_key(&key_type_user,
  315. mount_crypt_stat->global_auth_tok_sig,
  316. NULL);
  317. if (!auth_tok_key || IS_ERR(auth_tok_key)) {
  318. ecryptfs_printk(KERN_ERR, "Could not find key with "
  319. "description: [%s]\n",
  320. mount_crypt_stat->global_auth_tok_sig);
  321. process_request_key_err(PTR_ERR(auth_tok_key));
  322. rc = -EINVAL;
  323. goto out;
  324. }
  325. auth_tok = ecryptfs_get_key_payload_data(auth_tok_key);
  326. if (ecryptfs_verify_version(auth_tok->version)) {
  327. ecryptfs_printk(KERN_ERR, "Data structure version mismatch. "
  328. "Userspace tools must match eCryptfs kernel "
  329. "module with major version [%d] and minor "
  330. "version [%d]\n", ECRYPTFS_VERSION_MAJOR,
  331. ECRYPTFS_VERSION_MINOR);
  332. rc = -EINVAL;
  333. goto out;
  334. }
  335. if (auth_tok->token_type != ECRYPTFS_PASSWORD) {
  336. ecryptfs_printk(KERN_ERR, "Invalid auth_tok structure "
  337. "returned from key\n");
  338. rc = -EINVAL;
  339. goto out;
  340. }
  341. mount_crypt_stat->global_auth_tok_key = auth_tok_key;
  342. mount_crypt_stat->global_auth_tok = auth_tok;
  343. out:
  344. return rc;
  345. }
  346. struct kmem_cache *ecryptfs_sb_info_cache;
  347. /**
  348. * ecryptfs_fill_super
  349. * @sb: The ecryptfs super block
  350. * @raw_data: The options passed to mount
  351. * @silent: Not used but required by function prototype
  352. *
  353. * Sets up what we can of the sb, rest is done in ecryptfs_read_super
  354. *
  355. * Returns zero on success; non-zero otherwise
  356. */
  357. static int
  358. ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
  359. {
  360. int rc = 0;
  361. /* Released in ecryptfs_put_super() */
  362. ecryptfs_set_superblock_private(sb,
  363. kmem_cache_alloc(ecryptfs_sb_info_cache,
  364. SLAB_KERNEL));
  365. if (!ecryptfs_superblock_to_private(sb)) {
  366. ecryptfs_printk(KERN_WARNING, "Out of memory\n");
  367. rc = -ENOMEM;
  368. goto out;
  369. }
  370. memset(ecryptfs_superblock_to_private(sb), 0,
  371. sizeof(struct ecryptfs_sb_info));
  372. sb->s_op = &ecryptfs_sops;
  373. /* Released through deactivate_super(sb) from get_sb_nodev */
  374. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  375. .hash = 0,.name = "/",.len = 1});
  376. if (!sb->s_root) {
  377. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  378. rc = -ENOMEM;
  379. goto out;
  380. }
  381. sb->s_root->d_op = &ecryptfs_dops;
  382. sb->s_root->d_sb = sb;
  383. sb->s_root->d_parent = sb->s_root;
  384. /* Released in d_release when dput(sb->s_root) is called */
  385. /* through deactivate_super(sb) from get_sb_nodev() */
  386. ecryptfs_set_dentry_private(sb->s_root,
  387. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  388. SLAB_KERNEL));
  389. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  390. ecryptfs_printk(KERN_ERR,
  391. "dentry_info_cache alloc failed\n");
  392. rc = -ENOMEM;
  393. goto out;
  394. }
  395. memset(ecryptfs_dentry_to_private(sb->s_root), 0,
  396. sizeof(struct ecryptfs_dentry_info));
  397. rc = 0;
  398. out:
  399. /* Should be able to rely on deactivate_super called from
  400. * get_sb_nodev */
  401. return rc;
  402. }
  403. /**
  404. * ecryptfs_read_super
  405. * @sb: The ecryptfs super block
  406. * @dev_name: The path to mount over
  407. *
  408. * Read the super block of the lower filesystem, and use
  409. * ecryptfs_interpose to create our initial inode and super block
  410. * struct.
  411. */
  412. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  413. {
  414. int rc;
  415. struct nameidata nd;
  416. struct dentry *lower_root;
  417. struct vfsmount *lower_mnt;
  418. memset(&nd, 0, sizeof(struct nameidata));
  419. rc = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  420. if (rc) {
  421. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  422. goto out_free;
  423. }
  424. lower_root = nd.dentry;
  425. if (!lower_root->d_inode) {
  426. ecryptfs_printk(KERN_WARNING,
  427. "No directory to interpose on\n");
  428. rc = -ENOENT;
  429. goto out_free;
  430. }
  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. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  519. SLAB_CTOR_CONSTRUCTOR)
  520. inode_init_once(&ei->vfs_inode);
  521. }
  522. static struct ecryptfs_cache_info {
  523. kmem_cache_t **cache;
  524. const char *name;
  525. size_t size;
  526. void (*ctor)(void*, struct kmem_cache *, unsigned long);
  527. } ecryptfs_cache_infos[] = {
  528. {
  529. .cache = &ecryptfs_auth_tok_list_item_cache,
  530. .name = "ecryptfs_auth_tok_list_item",
  531. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  532. },
  533. {
  534. .cache = &ecryptfs_file_info_cache,
  535. .name = "ecryptfs_file_cache",
  536. .size = sizeof(struct ecryptfs_file_info),
  537. },
  538. {
  539. .cache = &ecryptfs_dentry_info_cache,
  540. .name = "ecryptfs_dentry_info_cache",
  541. .size = sizeof(struct ecryptfs_dentry_info),
  542. },
  543. {
  544. .cache = &ecryptfs_inode_info_cache,
  545. .name = "ecryptfs_inode_cache",
  546. .size = sizeof(struct ecryptfs_inode_info),
  547. .ctor = inode_info_init_once,
  548. },
  549. {
  550. .cache = &ecryptfs_sb_info_cache,
  551. .name = "ecryptfs_sb_cache",
  552. .size = sizeof(struct ecryptfs_sb_info),
  553. },
  554. {
  555. .cache = &ecryptfs_header_cache_0,
  556. .name = "ecryptfs_headers_0",
  557. .size = PAGE_CACHE_SIZE,
  558. },
  559. {
  560. .cache = &ecryptfs_header_cache_1,
  561. .name = "ecryptfs_headers_1",
  562. .size = PAGE_CACHE_SIZE,
  563. },
  564. {
  565. .cache = &ecryptfs_header_cache_2,
  566. .name = "ecryptfs_headers_2",
  567. .size = PAGE_CACHE_SIZE,
  568. },
  569. {
  570. .cache = &ecryptfs_lower_page_cache,
  571. .name = "ecryptfs_lower_page_cache",
  572. .size = PAGE_CACHE_SIZE,
  573. },
  574. };
  575. static void ecryptfs_free_kmem_caches(void)
  576. {
  577. int i;
  578. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  579. struct ecryptfs_cache_info *info;
  580. info = &ecryptfs_cache_infos[i];
  581. if (*(info->cache))
  582. kmem_cache_destroy(*(info->cache));
  583. }
  584. }
  585. /**
  586. * ecryptfs_init_kmem_caches
  587. *
  588. * Returns zero on success; non-zero otherwise
  589. */
  590. static int ecryptfs_init_kmem_caches(void)
  591. {
  592. int i;
  593. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  594. struct ecryptfs_cache_info *info;
  595. info = &ecryptfs_cache_infos[i];
  596. *(info->cache) = kmem_cache_create(info->name, info->size,
  597. 0, SLAB_HWCACHE_ALIGN, info->ctor, NULL);
  598. if (!*(info->cache)) {
  599. ecryptfs_free_kmem_caches();
  600. ecryptfs_printk(KERN_WARNING, "%s: "
  601. "kmem_cache_create failed\n",
  602. info->name);
  603. return -ENOMEM;
  604. }
  605. }
  606. return 0;
  607. }
  608. struct ecryptfs_obj {
  609. char *name;
  610. struct list_head slot_list;
  611. struct kobject kobj;
  612. };
  613. struct ecryptfs_attribute {
  614. struct attribute attr;
  615. ssize_t(*show) (struct ecryptfs_obj *, char *);
  616. ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
  617. };
  618. static ssize_t
  619. ecryptfs_attr_store(struct kobject *kobj,
  620. struct attribute *attr, const char *buf, size_t len)
  621. {
  622. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  623. kobj);
  624. struct ecryptfs_attribute *attribute =
  625. container_of(attr, struct ecryptfs_attribute, attr);
  626. return (attribute->store ? attribute->store(obj, buf, len) : 0);
  627. }
  628. static ssize_t
  629. ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  630. {
  631. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  632. kobj);
  633. struct ecryptfs_attribute *attribute =
  634. container_of(attr, struct ecryptfs_attribute, attr);
  635. return (attribute->show ? attribute->show(obj, buf) : 0);
  636. }
  637. static struct sysfs_ops ecryptfs_sysfs_ops = {
  638. .show = ecryptfs_attr_show,
  639. .store = ecryptfs_attr_store
  640. };
  641. static struct kobj_type ecryptfs_ktype = {
  642. .sysfs_ops = &ecryptfs_sysfs_ops
  643. };
  644. static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
  645. static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
  646. {
  647. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  648. }
  649. static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
  650. struct ecryptfs_version_str_map_elem {
  651. u32 flag;
  652. char *str;
  653. } ecryptfs_version_str_map[] = {
  654. {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
  655. {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
  656. {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
  657. {ECRYPTFS_VERSIONING_POLICY, "policy"}
  658. };
  659. static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
  660. {
  661. int i;
  662. int remaining = PAGE_SIZE;
  663. int total_written = 0;
  664. buff[0] = '\0';
  665. for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
  666. int entry_size;
  667. if (!(ECRYPTFS_VERSIONING_MASK
  668. & ecryptfs_version_str_map[i].flag))
  669. continue;
  670. entry_size = strlen(ecryptfs_version_str_map[i].str);
  671. if ((entry_size + 2) > remaining)
  672. goto out;
  673. memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
  674. buff[entry_size++] = '\n';
  675. buff[entry_size] = '\0';
  676. buff += entry_size;
  677. total_written += entry_size;
  678. remaining -= entry_size;
  679. }
  680. out:
  681. return total_written;
  682. }
  683. static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
  684. static int do_sysfs_registration(void)
  685. {
  686. int rc;
  687. if ((rc = subsystem_register(&ecryptfs_subsys))) {
  688. printk(KERN_ERR
  689. "Unable to register ecryptfs sysfs subsystem\n");
  690. goto out;
  691. }
  692. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  693. &sysfs_attr_version.attr);
  694. if (rc) {
  695. printk(KERN_ERR
  696. "Unable to create ecryptfs version attribute\n");
  697. subsystem_unregister(&ecryptfs_subsys);
  698. goto out;
  699. }
  700. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  701. &sysfs_attr_version_str.attr);
  702. if (rc) {
  703. printk(KERN_ERR
  704. "Unable to create ecryptfs version_str attribute\n");
  705. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  706. &sysfs_attr_version.attr);
  707. subsystem_unregister(&ecryptfs_subsys);
  708. goto out;
  709. }
  710. out:
  711. return rc;
  712. }
  713. static int __init ecryptfs_init(void)
  714. {
  715. int rc;
  716. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  717. rc = -EINVAL;
  718. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  719. "larger than the host's page size, and so "
  720. "eCryptfs cannot run on this system. The "
  721. "default eCryptfs extent size is [%d] bytes; "
  722. "the page size is [%d] bytes.\n",
  723. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  724. goto out;
  725. }
  726. rc = ecryptfs_init_kmem_caches();
  727. if (rc) {
  728. printk(KERN_ERR
  729. "Failed to allocate one or more kmem_cache objects\n");
  730. goto out;
  731. }
  732. rc = register_filesystem(&ecryptfs_fs_type);
  733. if (rc) {
  734. printk(KERN_ERR "Failed to register filesystem\n");
  735. ecryptfs_free_kmem_caches();
  736. goto out;
  737. }
  738. kset_set_kset_s(&ecryptfs_subsys, fs_subsys);
  739. sysfs_attr_version.attr.owner = THIS_MODULE;
  740. sysfs_attr_version_str.attr.owner = THIS_MODULE;
  741. rc = do_sysfs_registration();
  742. if (rc) {
  743. printk(KERN_ERR "sysfs registration failed\n");
  744. unregister_filesystem(&ecryptfs_fs_type);
  745. ecryptfs_free_kmem_caches();
  746. goto out;
  747. }
  748. out:
  749. return rc;
  750. }
  751. static void __exit ecryptfs_exit(void)
  752. {
  753. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  754. &sysfs_attr_version.attr);
  755. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  756. &sysfs_attr_version_str.attr);
  757. subsystem_unregister(&ecryptfs_subsys);
  758. unregister_filesystem(&ecryptfs_fs_type);
  759. ecryptfs_free_kmem_caches();
  760. }
  761. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  762. MODULE_DESCRIPTION("eCryptfs");
  763. MODULE_LICENSE("GPL");
  764. module_init(ecryptfs_init)
  765. module_exit(ecryptfs_exit)