main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. mount_crypt_stat->global_key_tfm = NULL;
  307. mount_crypt_stat->global_auth_tok_key = NULL;
  308. rc = -EINVAL;
  309. goto out;
  310. }
  311. mutex_init(&mount_crypt_stat->global_key_tfm_mutex);
  312. ecryptfs_printk(KERN_DEBUG, "Requesting the key with description: "
  313. "[%s]\n", mount_crypt_stat->global_auth_tok_sig);
  314. /* The reference to this key is held until umount is done The
  315. * call to key_put is done in ecryptfs_put_super() */
  316. auth_tok_key = request_key(&key_type_user,
  317. mount_crypt_stat->global_auth_tok_sig,
  318. NULL);
  319. if (!auth_tok_key || IS_ERR(auth_tok_key)) {
  320. ecryptfs_printk(KERN_ERR, "Could not find key with "
  321. "description: [%s]\n",
  322. mount_crypt_stat->global_auth_tok_sig);
  323. process_request_key_err(PTR_ERR(auth_tok_key));
  324. rc = -EINVAL;
  325. goto out;
  326. }
  327. auth_tok = ecryptfs_get_key_payload_data(auth_tok_key);
  328. if (ecryptfs_verify_version(auth_tok->version)) {
  329. ecryptfs_printk(KERN_ERR, "Data structure version mismatch. "
  330. "Userspace tools must match eCryptfs kernel "
  331. "module with major version [%d] and minor "
  332. "version [%d]\n", ECRYPTFS_VERSION_MAJOR,
  333. ECRYPTFS_VERSION_MINOR);
  334. rc = -EINVAL;
  335. goto out;
  336. }
  337. if (auth_tok->token_type != ECRYPTFS_PASSWORD) {
  338. ecryptfs_printk(KERN_ERR, "Invalid auth_tok structure "
  339. "returned from key\n");
  340. rc = -EINVAL;
  341. goto out;
  342. }
  343. mount_crypt_stat->global_auth_tok_key = auth_tok_key;
  344. mount_crypt_stat->global_auth_tok = auth_tok;
  345. out:
  346. return rc;
  347. }
  348. struct kmem_cache *ecryptfs_sb_info_cache;
  349. /**
  350. * ecryptfs_fill_super
  351. * @sb: The ecryptfs super block
  352. * @raw_data: The options passed to mount
  353. * @silent: Not used but required by function prototype
  354. *
  355. * Sets up what we can of the sb, rest is done in ecryptfs_read_super
  356. *
  357. * Returns zero on success; non-zero otherwise
  358. */
  359. static int
  360. ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
  361. {
  362. int rc = 0;
  363. /* Released in ecryptfs_put_super() */
  364. ecryptfs_set_superblock_private(sb,
  365. kmem_cache_alloc(ecryptfs_sb_info_cache,
  366. GFP_KERNEL));
  367. if (!ecryptfs_superblock_to_private(sb)) {
  368. ecryptfs_printk(KERN_WARNING, "Out of memory\n");
  369. rc = -ENOMEM;
  370. goto out;
  371. }
  372. memset(ecryptfs_superblock_to_private(sb), 0,
  373. sizeof(struct ecryptfs_sb_info));
  374. sb->s_op = &ecryptfs_sops;
  375. /* Released through deactivate_super(sb) from get_sb_nodev */
  376. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  377. .hash = 0,.name = "/",.len = 1});
  378. if (!sb->s_root) {
  379. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  380. rc = -ENOMEM;
  381. goto out;
  382. }
  383. sb->s_root->d_op = &ecryptfs_dops;
  384. sb->s_root->d_sb = sb;
  385. sb->s_root->d_parent = sb->s_root;
  386. /* Released in d_release when dput(sb->s_root) is called */
  387. /* through deactivate_super(sb) from get_sb_nodev() */
  388. ecryptfs_set_dentry_private(sb->s_root,
  389. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  390. GFP_KERNEL));
  391. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  392. ecryptfs_printk(KERN_ERR,
  393. "dentry_info_cache alloc failed\n");
  394. rc = -ENOMEM;
  395. goto out;
  396. }
  397. memset(ecryptfs_dentry_to_private(sb->s_root), 0,
  398. sizeof(struct ecryptfs_dentry_info));
  399. rc = 0;
  400. out:
  401. /* Should be able to rely on deactivate_super called from
  402. * get_sb_nodev */
  403. return rc;
  404. }
  405. /**
  406. * ecryptfs_read_super
  407. * @sb: The ecryptfs super block
  408. * @dev_name: The path to mount over
  409. *
  410. * Read the super block of the lower filesystem, and use
  411. * ecryptfs_interpose to create our initial inode and super block
  412. * struct.
  413. */
  414. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  415. {
  416. int rc;
  417. struct nameidata nd;
  418. struct dentry *lower_root;
  419. struct vfsmount *lower_mnt;
  420. memset(&nd, 0, sizeof(struct nameidata));
  421. rc = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  422. if (rc) {
  423. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  424. goto out_free;
  425. }
  426. lower_root = nd.dentry;
  427. if (!lower_root->d_inode) {
  428. ecryptfs_printk(KERN_WARNING,
  429. "No directory to interpose on\n");
  430. rc = -ENOENT;
  431. goto out_free;
  432. }
  433. lower_mnt = nd.mnt;
  434. ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
  435. sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
  436. ecryptfs_set_dentry_lower(sb->s_root, lower_root);
  437. ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
  438. if ((rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0)))
  439. goto out_free;
  440. rc = 0;
  441. goto out;
  442. out_free:
  443. path_release(&nd);
  444. out:
  445. return rc;
  446. }
  447. /**
  448. * ecryptfs_get_sb
  449. * @fs_type
  450. * @flags
  451. * @dev_name: The path to mount over
  452. * @raw_data: The options passed into the kernel
  453. *
  454. * The whole ecryptfs_get_sb process is broken into 4 functions:
  455. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  456. * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
  457. * with as much information as it can before needing
  458. * the lower filesystem.
  459. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  460. * ecryptfs_interpolate to perform most of the linking
  461. * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
  462. */
  463. static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
  464. const char *dev_name, void *raw_data,
  465. struct vfsmount *mnt)
  466. {
  467. int rc;
  468. struct super_block *sb;
  469. rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
  470. if (rc < 0) {
  471. printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
  472. goto out;
  473. }
  474. sb = mnt->mnt_sb;
  475. rc = ecryptfs_parse_options(sb, raw_data);
  476. if (rc) {
  477. printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
  478. goto out_abort;
  479. }
  480. rc = ecryptfs_read_super(sb, dev_name);
  481. if (rc) {
  482. printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
  483. goto out_abort;
  484. }
  485. goto out;
  486. out_abort:
  487. dput(sb->s_root);
  488. up_write(&sb->s_umount);
  489. deactivate_super(sb);
  490. out:
  491. return rc;
  492. }
  493. /**
  494. * ecryptfs_kill_block_super
  495. * @sb: The ecryptfs super block
  496. *
  497. * Used to bring the superblock down and free the private data.
  498. * Private data is free'd in ecryptfs_put_super()
  499. */
  500. static void ecryptfs_kill_block_super(struct super_block *sb)
  501. {
  502. generic_shutdown_super(sb);
  503. }
  504. static struct file_system_type ecryptfs_fs_type = {
  505. .owner = THIS_MODULE,
  506. .name = "ecryptfs",
  507. .get_sb = ecryptfs_get_sb,
  508. .kill_sb = ecryptfs_kill_block_super,
  509. .fs_flags = 0
  510. };
  511. /**
  512. * inode_info_init_once
  513. *
  514. * Initializes the ecryptfs_inode_info_cache when it is created
  515. */
  516. static void
  517. inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
  518. {
  519. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  520. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  521. SLAB_CTOR_CONSTRUCTOR)
  522. inode_init_once(&ei->vfs_inode);
  523. }
  524. static struct ecryptfs_cache_info {
  525. kmem_cache_t **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_lower_page_cache,
  573. .name = "ecryptfs_lower_page_cache",
  574. .size = PAGE_CACHE_SIZE,
  575. },
  576. };
  577. static void ecryptfs_free_kmem_caches(void)
  578. {
  579. int i;
  580. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  581. struct ecryptfs_cache_info *info;
  582. info = &ecryptfs_cache_infos[i];
  583. if (*(info->cache))
  584. kmem_cache_destroy(*(info->cache));
  585. }
  586. }
  587. /**
  588. * ecryptfs_init_kmem_caches
  589. *
  590. * Returns zero on success; non-zero otherwise
  591. */
  592. static int ecryptfs_init_kmem_caches(void)
  593. {
  594. int i;
  595. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  596. struct ecryptfs_cache_info *info;
  597. info = &ecryptfs_cache_infos[i];
  598. *(info->cache) = kmem_cache_create(info->name, info->size,
  599. 0, SLAB_HWCACHE_ALIGN, info->ctor, NULL);
  600. if (!*(info->cache)) {
  601. ecryptfs_free_kmem_caches();
  602. ecryptfs_printk(KERN_WARNING, "%s: "
  603. "kmem_cache_create failed\n",
  604. info->name);
  605. return -ENOMEM;
  606. }
  607. }
  608. return 0;
  609. }
  610. struct ecryptfs_obj {
  611. char *name;
  612. struct list_head slot_list;
  613. struct kobject kobj;
  614. };
  615. struct ecryptfs_attribute {
  616. struct attribute attr;
  617. ssize_t(*show) (struct ecryptfs_obj *, char *);
  618. ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
  619. };
  620. static ssize_t
  621. ecryptfs_attr_store(struct kobject *kobj,
  622. struct attribute *attr, const char *buf, size_t len)
  623. {
  624. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  625. kobj);
  626. struct ecryptfs_attribute *attribute =
  627. container_of(attr, struct ecryptfs_attribute, attr);
  628. return (attribute->store ? attribute->store(obj, buf, len) : 0);
  629. }
  630. static ssize_t
  631. ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  632. {
  633. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  634. kobj);
  635. struct ecryptfs_attribute *attribute =
  636. container_of(attr, struct ecryptfs_attribute, attr);
  637. return (attribute->show ? attribute->show(obj, buf) : 0);
  638. }
  639. static struct sysfs_ops ecryptfs_sysfs_ops = {
  640. .show = ecryptfs_attr_show,
  641. .store = ecryptfs_attr_store
  642. };
  643. static struct kobj_type ecryptfs_ktype = {
  644. .sysfs_ops = &ecryptfs_sysfs_ops
  645. };
  646. static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
  647. static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
  648. {
  649. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  650. }
  651. static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
  652. struct ecryptfs_version_str_map_elem {
  653. u32 flag;
  654. char *str;
  655. } ecryptfs_version_str_map[] = {
  656. {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
  657. {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
  658. {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
  659. {ECRYPTFS_VERSIONING_POLICY, "policy"}
  660. };
  661. static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
  662. {
  663. int i;
  664. int remaining = PAGE_SIZE;
  665. int total_written = 0;
  666. buff[0] = '\0';
  667. for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
  668. int entry_size;
  669. if (!(ECRYPTFS_VERSIONING_MASK
  670. & ecryptfs_version_str_map[i].flag))
  671. continue;
  672. entry_size = strlen(ecryptfs_version_str_map[i].str);
  673. if ((entry_size + 2) > remaining)
  674. goto out;
  675. memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
  676. buff[entry_size++] = '\n';
  677. buff[entry_size] = '\0';
  678. buff += entry_size;
  679. total_written += entry_size;
  680. remaining -= entry_size;
  681. }
  682. out:
  683. return total_written;
  684. }
  685. static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
  686. static int do_sysfs_registration(void)
  687. {
  688. int rc;
  689. if ((rc = subsystem_register(&ecryptfs_subsys))) {
  690. printk(KERN_ERR
  691. "Unable to register ecryptfs sysfs subsystem\n");
  692. goto out;
  693. }
  694. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  695. &sysfs_attr_version.attr);
  696. if (rc) {
  697. printk(KERN_ERR
  698. "Unable to create ecryptfs version attribute\n");
  699. subsystem_unregister(&ecryptfs_subsys);
  700. goto out;
  701. }
  702. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  703. &sysfs_attr_version_str.attr);
  704. if (rc) {
  705. printk(KERN_ERR
  706. "Unable to create ecryptfs version_str attribute\n");
  707. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  708. &sysfs_attr_version.attr);
  709. subsystem_unregister(&ecryptfs_subsys);
  710. goto out;
  711. }
  712. out:
  713. return rc;
  714. }
  715. static int __init ecryptfs_init(void)
  716. {
  717. int rc;
  718. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  719. rc = -EINVAL;
  720. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  721. "larger than the host's page size, and so "
  722. "eCryptfs cannot run on this system. The "
  723. "default eCryptfs extent size is [%d] bytes; "
  724. "the page size is [%d] bytes.\n",
  725. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  726. goto out;
  727. }
  728. rc = ecryptfs_init_kmem_caches();
  729. if (rc) {
  730. printk(KERN_ERR
  731. "Failed to allocate one or more kmem_cache objects\n");
  732. goto out;
  733. }
  734. rc = register_filesystem(&ecryptfs_fs_type);
  735. if (rc) {
  736. printk(KERN_ERR "Failed to register filesystem\n");
  737. ecryptfs_free_kmem_caches();
  738. goto out;
  739. }
  740. kset_set_kset_s(&ecryptfs_subsys, fs_subsys);
  741. sysfs_attr_version.attr.owner = THIS_MODULE;
  742. sysfs_attr_version_str.attr.owner = THIS_MODULE;
  743. rc = do_sysfs_registration();
  744. if (rc) {
  745. printk(KERN_ERR "sysfs registration failed\n");
  746. unregister_filesystem(&ecryptfs_fs_type);
  747. ecryptfs_free_kmem_caches();
  748. goto out;
  749. }
  750. out:
  751. return rc;
  752. }
  753. static void __exit ecryptfs_exit(void)
  754. {
  755. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  756. &sysfs_attr_version.attr);
  757. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  758. &sysfs_attr_version_str.attr);
  759. subsystem_unregister(&ecryptfs_subsys);
  760. unregister_filesystem(&ecryptfs_fs_type);
  761. ecryptfs_free_kmem_caches();
  762. }
  763. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  764. MODULE_DESCRIPTION("eCryptfs");
  765. MODULE_LICENSE("GPL");
  766. module_init(ecryptfs_init)
  767. module_exit(ecryptfs_exit)