main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. /* TODO: Is there a better way to identify if the inode is
  102. * special? */
  103. if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
  104. S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
  105. init_special_inode(inode, lower_inode->i_mode,
  106. lower_inode->i_rdev);
  107. dentry->d_op = &ecryptfs_dops;
  108. if (flag)
  109. d_add(dentry, inode);
  110. else
  111. d_instantiate(dentry, inode);
  112. ecryptfs_copy_attr_all(inode, lower_inode);
  113. /* This size will be overwritten for real files w/ headers and
  114. * other metadata */
  115. ecryptfs_copy_inode_size(inode, lower_inode);
  116. out:
  117. return rc;
  118. }
  119. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
  120. ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
  121. ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
  122. ecryptfs_opt_passthrough, ecryptfs_opt_err };
  123. static match_table_t tokens = {
  124. {ecryptfs_opt_sig, "sig=%s"},
  125. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  126. {ecryptfs_opt_debug, "debug=%u"},
  127. {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
  128. {ecryptfs_opt_cipher, "cipher=%s"},
  129. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  130. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  131. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  132. {ecryptfs_opt_err, NULL}
  133. };
  134. /**
  135. * ecryptfs_verify_version
  136. * @version: The version number to confirm
  137. *
  138. * Returns zero on good version; non-zero otherwise
  139. */
  140. static int ecryptfs_verify_version(u16 version)
  141. {
  142. int rc = 0;
  143. unsigned char major;
  144. unsigned char minor;
  145. major = ((version >> 8) & 0xFF);
  146. minor = (version & 0xFF);
  147. if (major != ECRYPTFS_VERSION_MAJOR) {
  148. ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
  149. "Expected [%d]; got [%d]\n",
  150. ECRYPTFS_VERSION_MAJOR, major);
  151. rc = -EINVAL;
  152. goto out;
  153. }
  154. if (minor != ECRYPTFS_VERSION_MINOR) {
  155. ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
  156. "Expected [%d]; got [%d]\n",
  157. ECRYPTFS_VERSION_MINOR, minor);
  158. rc = -EINVAL;
  159. goto out;
  160. }
  161. out:
  162. return rc;
  163. }
  164. /**
  165. * ecryptfs_parse_options
  166. * @sb: The ecryptfs super block
  167. * @options: The options pased to the kernel
  168. *
  169. * Parse mount options:
  170. * debug=N - ecryptfs_verbosity level for debug output
  171. * sig=XXX - description(signature) of the key to use
  172. *
  173. * Returns the dentry object of the lower-level (lower/interposed)
  174. * directory; We want to mount our stackable file system on top of
  175. * that lower directory.
  176. *
  177. * The signature of the key to use must be the description of a key
  178. * already in the keyring. Mounting will fail if the key can not be
  179. * found.
  180. *
  181. * Returns zero on success; non-zero on error
  182. */
  183. static int ecryptfs_parse_options(struct super_block *sb, char *options)
  184. {
  185. char *p;
  186. int rc = 0;
  187. int sig_set = 0;
  188. int cipher_name_set = 0;
  189. int cipher_key_bytes;
  190. int cipher_key_bytes_set = 0;
  191. struct key *auth_tok_key = NULL;
  192. struct ecryptfs_auth_tok *auth_tok = NULL;
  193. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  194. &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
  195. substring_t args[MAX_OPT_ARGS];
  196. int token;
  197. char *sig_src;
  198. char *sig_dst;
  199. char *debug_src;
  200. char *cipher_name_dst;
  201. char *cipher_name_src;
  202. char *cipher_key_bytes_src;
  203. struct crypto_tfm *tmp_tfm;
  204. int cipher_name_len;
  205. if (!options) {
  206. rc = -EINVAL;
  207. goto out;
  208. }
  209. while ((p = strsep(&options, ",")) != NULL) {
  210. if (!*p)
  211. continue;
  212. token = match_token(p, tokens, args);
  213. switch (token) {
  214. case ecryptfs_opt_sig:
  215. case ecryptfs_opt_ecryptfs_sig:
  216. sig_src = args[0].from;
  217. sig_dst =
  218. mount_crypt_stat->global_auth_tok_sig;
  219. memcpy(sig_dst, sig_src, ECRYPTFS_SIG_SIZE_HEX);
  220. sig_dst[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  221. ecryptfs_printk(KERN_DEBUG,
  222. "The mount_crypt_stat "
  223. "global_auth_tok_sig set to: "
  224. "[%s]\n", sig_dst);
  225. sig_set = 1;
  226. break;
  227. case ecryptfs_opt_debug:
  228. case ecryptfs_opt_ecryptfs_debug:
  229. debug_src = args[0].from;
  230. ecryptfs_verbosity =
  231. (int)simple_strtol(debug_src, &debug_src,
  232. 0);
  233. ecryptfs_printk(KERN_DEBUG,
  234. "Verbosity set to [%d]" "\n",
  235. ecryptfs_verbosity);
  236. break;
  237. case ecryptfs_opt_cipher:
  238. case ecryptfs_opt_ecryptfs_cipher:
  239. cipher_name_src = args[0].from;
  240. cipher_name_dst =
  241. mount_crypt_stat->
  242. global_default_cipher_name;
  243. strncpy(cipher_name_dst, cipher_name_src,
  244. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  245. ecryptfs_printk(KERN_DEBUG,
  246. "The mount_crypt_stat "
  247. "global_default_cipher_name set to: "
  248. "[%s]\n", cipher_name_dst);
  249. cipher_name_set = 1;
  250. break;
  251. case ecryptfs_opt_ecryptfs_key_bytes:
  252. cipher_key_bytes_src = args[0].from;
  253. cipher_key_bytes =
  254. (int)simple_strtol(cipher_key_bytes_src,
  255. &cipher_key_bytes_src, 0);
  256. mount_crypt_stat->global_default_cipher_key_size =
  257. cipher_key_bytes;
  258. ecryptfs_printk(KERN_DEBUG,
  259. "The mount_crypt_stat "
  260. "global_default_cipher_key_size "
  261. "set to: [%d]\n", mount_crypt_stat->
  262. global_default_cipher_key_size);
  263. cipher_key_bytes_set = 1;
  264. break;
  265. case ecryptfs_opt_passthrough:
  266. mount_crypt_stat->flags |=
  267. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  268. break;
  269. case ecryptfs_opt_err:
  270. default:
  271. ecryptfs_printk(KERN_WARNING,
  272. "eCryptfs: unrecognized option '%s'\n",
  273. p);
  274. }
  275. }
  276. /* Do not support lack of mount-wide signature in 0.1
  277. * release */
  278. if (!sig_set) {
  279. rc = -EINVAL;
  280. ecryptfs_printk(KERN_ERR, "You must supply a valid "
  281. "passphrase auth tok signature as a mount "
  282. "parameter; see the eCryptfs README\n");
  283. goto out;
  284. }
  285. if (!cipher_name_set) {
  286. cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  287. if (unlikely(cipher_name_len
  288. >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
  289. rc = -EINVAL;
  290. BUG();
  291. goto out;
  292. }
  293. memcpy(mount_crypt_stat->global_default_cipher_name,
  294. ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
  295. mount_crypt_stat->global_default_cipher_name[cipher_name_len]
  296. = '\0';
  297. }
  298. if (!cipher_key_bytes_set) {
  299. mount_crypt_stat->global_default_cipher_key_size =
  300. ECRYPTFS_DEFAULT_KEY_BYTES;
  301. ecryptfs_printk(KERN_DEBUG, "Cipher key size was not "
  302. "specified. Defaulting to [%d]\n",
  303. mount_crypt_stat->
  304. global_default_cipher_key_size);
  305. }
  306. rc = ecryptfs_process_cipher(
  307. &tmp_tfm,
  308. &mount_crypt_stat->global_key_tfm,
  309. mount_crypt_stat->global_default_cipher_name,
  310. mount_crypt_stat->global_default_cipher_key_size);
  311. if (tmp_tfm)
  312. crypto_free_tfm(tmp_tfm);
  313. if (rc) {
  314. printk(KERN_ERR "Error attempting to initialize cipher [%s] "
  315. "with key size [%Zd] bytes; rc = [%d]\n",
  316. mount_crypt_stat->global_default_cipher_name,
  317. mount_crypt_stat->global_default_cipher_key_size, rc);
  318. rc = -EINVAL;
  319. goto out;
  320. }
  321. mutex_init(&mount_crypt_stat->global_key_tfm_mutex);
  322. ecryptfs_printk(KERN_DEBUG, "Requesting the key with description: "
  323. "[%s]\n", mount_crypt_stat->global_auth_tok_sig);
  324. /* The reference to this key is held until umount is done The
  325. * call to key_put is done in ecryptfs_put_super() */
  326. auth_tok_key = request_key(&key_type_user,
  327. mount_crypt_stat->global_auth_tok_sig,
  328. NULL);
  329. if (!auth_tok_key || IS_ERR(auth_tok_key)) {
  330. ecryptfs_printk(KERN_ERR, "Could not find key with "
  331. "description: [%s]\n",
  332. mount_crypt_stat->global_auth_tok_sig);
  333. process_request_key_err(PTR_ERR(auth_tok_key));
  334. rc = -EINVAL;
  335. goto out;
  336. }
  337. auth_tok = ecryptfs_get_key_payload_data(auth_tok_key);
  338. if (ecryptfs_verify_version(auth_tok->version)) {
  339. ecryptfs_printk(KERN_ERR, "Data structure version mismatch. "
  340. "Userspace tools must match eCryptfs kernel "
  341. "module with major version [%d] and minor "
  342. "version [%d]\n", ECRYPTFS_VERSION_MAJOR,
  343. ECRYPTFS_VERSION_MINOR);
  344. rc = -EINVAL;
  345. goto out;
  346. }
  347. if (auth_tok->token_type != ECRYPTFS_PASSWORD) {
  348. ecryptfs_printk(KERN_ERR, "Invalid auth_tok structure "
  349. "returned from key\n");
  350. rc = -EINVAL;
  351. goto out;
  352. }
  353. mount_crypt_stat->global_auth_tok_key = auth_tok_key;
  354. mount_crypt_stat->global_auth_tok = auth_tok;
  355. out:
  356. return rc;
  357. }
  358. struct kmem_cache *ecryptfs_sb_info_cache;
  359. /**
  360. * ecryptfs_fill_super
  361. * @sb: The ecryptfs super block
  362. * @raw_data: The options passed to mount
  363. * @silent: Not used but required by function prototype
  364. *
  365. * Sets up what we can of the sb, rest is done in ecryptfs_read_super
  366. *
  367. * Returns zero on success; non-zero otherwise
  368. */
  369. static int
  370. ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
  371. {
  372. int rc = 0;
  373. /* Released in ecryptfs_put_super() */
  374. ecryptfs_set_superblock_private(sb,
  375. kmem_cache_alloc(ecryptfs_sb_info_cache,
  376. SLAB_KERNEL));
  377. if (!ecryptfs_superblock_to_private(sb)) {
  378. ecryptfs_printk(KERN_WARNING, "Out of memory\n");
  379. rc = -ENOMEM;
  380. goto out;
  381. }
  382. memset(ecryptfs_superblock_to_private(sb), 0,
  383. sizeof(struct ecryptfs_sb_info));
  384. sb->s_op = &ecryptfs_sops;
  385. /* Released through deactivate_super(sb) from get_sb_nodev */
  386. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  387. .hash = 0,.name = "/",.len = 1});
  388. if (!sb->s_root) {
  389. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  390. rc = -ENOMEM;
  391. goto out;
  392. }
  393. sb->s_root->d_op = &ecryptfs_dops;
  394. sb->s_root->d_sb = sb;
  395. sb->s_root->d_parent = sb->s_root;
  396. /* Released in d_release when dput(sb->s_root) is called */
  397. /* through deactivate_super(sb) from get_sb_nodev() */
  398. ecryptfs_set_dentry_private(sb->s_root,
  399. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  400. SLAB_KERNEL));
  401. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  402. ecryptfs_printk(KERN_ERR,
  403. "dentry_info_cache alloc failed\n");
  404. rc = -ENOMEM;
  405. goto out;
  406. }
  407. memset(ecryptfs_dentry_to_private(sb->s_root), 0,
  408. sizeof(struct ecryptfs_dentry_info));
  409. rc = 0;
  410. out:
  411. /* Should be able to rely on deactivate_super called from
  412. * get_sb_nodev */
  413. return rc;
  414. }
  415. /**
  416. * ecryptfs_read_super
  417. * @sb: The ecryptfs super block
  418. * @dev_name: The path to mount over
  419. *
  420. * Read the super block of the lower filesystem, and use
  421. * ecryptfs_interpose to create our initial inode and super block
  422. * struct.
  423. */
  424. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  425. {
  426. int rc;
  427. struct nameidata nd;
  428. struct dentry *lower_root;
  429. struct vfsmount *lower_mnt;
  430. memset(&nd, 0, sizeof(struct nameidata));
  431. rc = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  432. if (rc) {
  433. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  434. goto out_free;
  435. }
  436. lower_root = nd.dentry;
  437. if (!lower_root->d_inode) {
  438. ecryptfs_printk(KERN_WARNING,
  439. "No directory to interpose on\n");
  440. rc = -ENOENT;
  441. goto out_free;
  442. }
  443. lower_mnt = nd.mnt;
  444. ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
  445. sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
  446. ecryptfs_set_dentry_lower(sb->s_root, lower_root);
  447. ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
  448. if ((rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0)))
  449. goto out_free;
  450. rc = 0;
  451. goto out;
  452. out_free:
  453. path_release(&nd);
  454. out:
  455. return rc;
  456. }
  457. /**
  458. * ecryptfs_get_sb
  459. * @fs_type
  460. * @flags
  461. * @dev_name: The path to mount over
  462. * @raw_data: The options passed into the kernel
  463. *
  464. * The whole ecryptfs_get_sb process is broken into 4 functions:
  465. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  466. * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
  467. * with as much information as it can before needing
  468. * the lower filesystem.
  469. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  470. * ecryptfs_interpolate to perform most of the linking
  471. * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
  472. */
  473. static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
  474. const char *dev_name, void *raw_data,
  475. struct vfsmount *mnt)
  476. {
  477. int rc;
  478. struct super_block *sb;
  479. rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
  480. if (rc < 0) {
  481. printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
  482. goto out;
  483. }
  484. sb = mnt->mnt_sb;
  485. rc = ecryptfs_parse_options(sb, raw_data);
  486. if (rc) {
  487. printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
  488. goto out_abort;
  489. }
  490. rc = ecryptfs_read_super(sb, dev_name);
  491. if (rc) {
  492. printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
  493. goto out_abort;
  494. }
  495. goto out;
  496. out_abort:
  497. dput(sb->s_root);
  498. up_write(&sb->s_umount);
  499. deactivate_super(sb);
  500. out:
  501. return rc;
  502. }
  503. /**
  504. * ecryptfs_kill_block_super
  505. * @sb: The ecryptfs super block
  506. *
  507. * Used to bring the superblock down and free the private data.
  508. * Private data is free'd in ecryptfs_put_super()
  509. */
  510. static void ecryptfs_kill_block_super(struct super_block *sb)
  511. {
  512. generic_shutdown_super(sb);
  513. }
  514. static struct file_system_type ecryptfs_fs_type = {
  515. .owner = THIS_MODULE,
  516. .name = "ecryptfs",
  517. .get_sb = ecryptfs_get_sb,
  518. .kill_sb = ecryptfs_kill_block_super,
  519. .fs_flags = 0
  520. };
  521. /**
  522. * inode_info_init_once
  523. *
  524. * Initializes the ecryptfs_inode_info_cache when it is created
  525. */
  526. static void
  527. inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
  528. {
  529. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  530. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  531. SLAB_CTOR_CONSTRUCTOR)
  532. inode_init_once(&ei->vfs_inode);
  533. }
  534. static struct ecryptfs_cache_info {
  535. kmem_cache_t **cache;
  536. const char *name;
  537. size_t size;
  538. void (*ctor)(void*, struct kmem_cache *, unsigned long);
  539. } ecryptfs_cache_infos[] = {
  540. {
  541. .cache = &ecryptfs_auth_tok_list_item_cache,
  542. .name = "ecryptfs_auth_tok_list_item",
  543. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  544. },
  545. {
  546. .cache = &ecryptfs_file_info_cache,
  547. .name = "ecryptfs_file_cache",
  548. .size = sizeof(struct ecryptfs_file_info),
  549. },
  550. {
  551. .cache = &ecryptfs_dentry_info_cache,
  552. .name = "ecryptfs_dentry_info_cache",
  553. .size = sizeof(struct ecryptfs_dentry_info),
  554. },
  555. {
  556. .cache = &ecryptfs_inode_info_cache,
  557. .name = "ecryptfs_inode_cache",
  558. .size = sizeof(struct ecryptfs_inode_info),
  559. .ctor = inode_info_init_once,
  560. },
  561. {
  562. .cache = &ecryptfs_sb_info_cache,
  563. .name = "ecryptfs_sb_cache",
  564. .size = sizeof(struct ecryptfs_sb_info),
  565. },
  566. {
  567. .cache = &ecryptfs_header_cache_0,
  568. .name = "ecryptfs_headers_0",
  569. .size = PAGE_CACHE_SIZE,
  570. },
  571. {
  572. .cache = &ecryptfs_header_cache_1,
  573. .name = "ecryptfs_headers_1",
  574. .size = PAGE_CACHE_SIZE,
  575. },
  576. {
  577. .cache = &ecryptfs_header_cache_2,
  578. .name = "ecryptfs_headers_2",
  579. .size = PAGE_CACHE_SIZE,
  580. },
  581. {
  582. .cache = &ecryptfs_lower_page_cache,
  583. .name = "ecryptfs_lower_page_cache",
  584. .size = PAGE_CACHE_SIZE,
  585. },
  586. };
  587. static void ecryptfs_free_kmem_caches(void)
  588. {
  589. int i;
  590. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  591. struct ecryptfs_cache_info *info;
  592. info = &ecryptfs_cache_infos[i];
  593. if (*(info->cache))
  594. kmem_cache_destroy(*(info->cache));
  595. }
  596. }
  597. /**
  598. * ecryptfs_init_kmem_caches
  599. *
  600. * Returns zero on success; non-zero otherwise
  601. */
  602. static int ecryptfs_init_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. *(info->cache) = kmem_cache_create(info->name, info->size,
  609. 0, SLAB_HWCACHE_ALIGN, info->ctor, NULL);
  610. if (!*(info->cache)) {
  611. ecryptfs_free_kmem_caches();
  612. ecryptfs_printk(KERN_WARNING, "%s: "
  613. "kmem_cache_create failed\n",
  614. info->name);
  615. return -ENOMEM;
  616. }
  617. }
  618. return 0;
  619. }
  620. struct ecryptfs_obj {
  621. char *name;
  622. struct list_head slot_list;
  623. struct kobject kobj;
  624. };
  625. struct ecryptfs_attribute {
  626. struct attribute attr;
  627. ssize_t(*show) (struct ecryptfs_obj *, char *);
  628. ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
  629. };
  630. static ssize_t
  631. ecryptfs_attr_store(struct kobject *kobj,
  632. struct attribute *attr, const char *buf, size_t len)
  633. {
  634. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  635. kobj);
  636. struct ecryptfs_attribute *attribute =
  637. container_of(attr, struct ecryptfs_attribute, attr);
  638. return (attribute->store ? attribute->store(obj, buf, len) : 0);
  639. }
  640. static ssize_t
  641. ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  642. {
  643. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  644. kobj);
  645. struct ecryptfs_attribute *attribute =
  646. container_of(attr, struct ecryptfs_attribute, attr);
  647. return (attribute->show ? attribute->show(obj, buf) : 0);
  648. }
  649. static struct sysfs_ops ecryptfs_sysfs_ops = {
  650. .show = ecryptfs_attr_show,
  651. .store = ecryptfs_attr_store
  652. };
  653. static struct kobj_type ecryptfs_ktype = {
  654. .sysfs_ops = &ecryptfs_sysfs_ops
  655. };
  656. static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
  657. static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
  658. {
  659. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  660. }
  661. static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
  662. struct ecryptfs_version_str_map_elem {
  663. u32 flag;
  664. char *str;
  665. } ecryptfs_version_str_map[] = {
  666. {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
  667. {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
  668. {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
  669. {ECRYPTFS_VERSIONING_POLICY, "policy"}
  670. };
  671. static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
  672. {
  673. int i;
  674. int remaining = PAGE_SIZE;
  675. int total_written = 0;
  676. buff[0] = '\0';
  677. for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
  678. int entry_size;
  679. if (!(ECRYPTFS_VERSIONING_MASK
  680. & ecryptfs_version_str_map[i].flag))
  681. continue;
  682. entry_size = strlen(ecryptfs_version_str_map[i].str);
  683. if ((entry_size + 2) > remaining)
  684. goto out;
  685. memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
  686. buff[entry_size++] = '\n';
  687. buff[entry_size] = '\0';
  688. buff += entry_size;
  689. total_written += entry_size;
  690. remaining -= entry_size;
  691. }
  692. out:
  693. return total_written;
  694. }
  695. static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
  696. static int do_sysfs_registration(void)
  697. {
  698. int rc;
  699. if ((rc = subsystem_register(&ecryptfs_subsys))) {
  700. printk(KERN_ERR
  701. "Unable to register ecryptfs sysfs subsystem\n");
  702. goto out;
  703. }
  704. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  705. &sysfs_attr_version.attr);
  706. if (rc) {
  707. printk(KERN_ERR
  708. "Unable to create ecryptfs version attribute\n");
  709. subsystem_unregister(&ecryptfs_subsys);
  710. goto out;
  711. }
  712. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  713. &sysfs_attr_version_str.attr);
  714. if (rc) {
  715. printk(KERN_ERR
  716. "Unable to create ecryptfs version_str attribute\n");
  717. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  718. &sysfs_attr_version.attr);
  719. subsystem_unregister(&ecryptfs_subsys);
  720. goto out;
  721. }
  722. out:
  723. return rc;
  724. }
  725. static int __init ecryptfs_init(void)
  726. {
  727. int rc;
  728. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  729. rc = -EINVAL;
  730. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  731. "larger than the host's page size, and so "
  732. "eCryptfs cannot run on this system. The "
  733. "default eCryptfs extent size is [%d] bytes; "
  734. "the page size is [%d] bytes.\n",
  735. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  736. goto out;
  737. }
  738. rc = ecryptfs_init_kmem_caches();
  739. if (rc) {
  740. printk(KERN_ERR
  741. "Failed to allocate one or more kmem_cache objects\n");
  742. goto out;
  743. }
  744. rc = register_filesystem(&ecryptfs_fs_type);
  745. if (rc) {
  746. printk(KERN_ERR "Failed to register filesystem\n");
  747. ecryptfs_free_kmem_caches();
  748. goto out;
  749. }
  750. kset_set_kset_s(&ecryptfs_subsys, fs_subsys);
  751. sysfs_attr_version.attr.owner = THIS_MODULE;
  752. sysfs_attr_version_str.attr.owner = THIS_MODULE;
  753. rc = do_sysfs_registration();
  754. if (rc) {
  755. printk(KERN_ERR "sysfs registration failed\n");
  756. unregister_filesystem(&ecryptfs_fs_type);
  757. ecryptfs_free_kmem_caches();
  758. goto out;
  759. }
  760. out:
  761. return rc;
  762. }
  763. static void __exit ecryptfs_exit(void)
  764. {
  765. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  766. &sysfs_attr_version.attr);
  767. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  768. &sysfs_attr_version_str.attr);
  769. subsystem_unregister(&ecryptfs_subsys);
  770. unregister_filesystem(&ecryptfs_fs_type);
  771. ecryptfs_free_kmem_caches();
  772. }
  773. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  774. MODULE_DESCRIPTION("eCryptfs");
  775. MODULE_LICENSE("GPL");
  776. module_init(ecryptfs_init)
  777. module_exit(ecryptfs_exit)