main.c 22 KB

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