main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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_zalloc(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. sb->s_op = &ecryptfs_sops;
  374. /* Released through deactivate_super(sb) from get_sb_nodev */
  375. sb->s_root = d_alloc(NULL, &(const struct qstr) {
  376. .hash = 0,.name = "/",.len = 1});
  377. if (!sb->s_root) {
  378. ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
  379. rc = -ENOMEM;
  380. goto out;
  381. }
  382. sb->s_root->d_op = &ecryptfs_dops;
  383. sb->s_root->d_sb = sb;
  384. sb->s_root->d_parent = sb->s_root;
  385. /* Released in d_release when dput(sb->s_root) is called */
  386. /* through deactivate_super(sb) from get_sb_nodev() */
  387. ecryptfs_set_dentry_private(sb->s_root,
  388. kmem_cache_zalloc(ecryptfs_dentry_info_cache,
  389. GFP_KERNEL));
  390. if (!ecryptfs_dentry_to_private(sb->s_root)) {
  391. ecryptfs_printk(KERN_ERR,
  392. "dentry_info_cache alloc failed\n");
  393. rc = -ENOMEM;
  394. goto out;
  395. }
  396. rc = 0;
  397. out:
  398. /* Should be able to rely on deactivate_super called from
  399. * get_sb_nodev */
  400. return rc;
  401. }
  402. /**
  403. * ecryptfs_read_super
  404. * @sb: The ecryptfs super block
  405. * @dev_name: The path to mount over
  406. *
  407. * Read the super block of the lower filesystem, and use
  408. * ecryptfs_interpose to create our initial inode and super block
  409. * struct.
  410. */
  411. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  412. {
  413. int rc;
  414. struct nameidata nd;
  415. struct dentry *lower_root;
  416. struct vfsmount *lower_mnt;
  417. memset(&nd, 0, sizeof(struct nameidata));
  418. rc = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  419. if (rc) {
  420. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  421. goto out_free;
  422. }
  423. lower_root = nd.dentry;
  424. if (!lower_root->d_inode) {
  425. ecryptfs_printk(KERN_WARNING,
  426. "No directory to interpose on\n");
  427. rc = -ENOENT;
  428. goto out_free;
  429. }
  430. lower_mnt = nd.mnt;
  431. ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
  432. sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
  433. ecryptfs_set_dentry_lower(sb->s_root, lower_root);
  434. ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
  435. if ((rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0)))
  436. goto out_free;
  437. rc = 0;
  438. goto out;
  439. out_free:
  440. path_release(&nd);
  441. out:
  442. return rc;
  443. }
  444. /**
  445. * ecryptfs_get_sb
  446. * @fs_type
  447. * @flags
  448. * @dev_name: The path to mount over
  449. * @raw_data: The options passed into the kernel
  450. *
  451. * The whole ecryptfs_get_sb process is broken into 4 functions:
  452. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  453. * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
  454. * with as much information as it can before needing
  455. * the lower filesystem.
  456. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  457. * ecryptfs_interpolate to perform most of the linking
  458. * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
  459. */
  460. static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
  461. const char *dev_name, void *raw_data,
  462. struct vfsmount *mnt)
  463. {
  464. int rc;
  465. struct super_block *sb;
  466. rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
  467. if (rc < 0) {
  468. printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
  469. goto out;
  470. }
  471. sb = mnt->mnt_sb;
  472. rc = ecryptfs_parse_options(sb, raw_data);
  473. if (rc) {
  474. printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
  475. goto out_abort;
  476. }
  477. rc = ecryptfs_read_super(sb, dev_name);
  478. if (rc) {
  479. printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
  480. goto out_abort;
  481. }
  482. goto out;
  483. out_abort:
  484. dput(sb->s_root);
  485. up_write(&sb->s_umount);
  486. deactivate_super(sb);
  487. out:
  488. return rc;
  489. }
  490. /**
  491. * ecryptfs_kill_block_super
  492. * @sb: The ecryptfs super block
  493. *
  494. * Used to bring the superblock down and free the private data.
  495. * Private data is free'd in ecryptfs_put_super()
  496. */
  497. static void ecryptfs_kill_block_super(struct super_block *sb)
  498. {
  499. generic_shutdown_super(sb);
  500. }
  501. static struct file_system_type ecryptfs_fs_type = {
  502. .owner = THIS_MODULE,
  503. .name = "ecryptfs",
  504. .get_sb = ecryptfs_get_sb,
  505. .kill_sb = ecryptfs_kill_block_super,
  506. .fs_flags = 0
  507. };
  508. /**
  509. * inode_info_init_once
  510. *
  511. * Initializes the ecryptfs_inode_info_cache when it is created
  512. */
  513. static void
  514. inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
  515. {
  516. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  517. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  518. SLAB_CTOR_CONSTRUCTOR)
  519. inode_init_once(&ei->vfs_inode);
  520. }
  521. static struct ecryptfs_cache_info {
  522. struct kmem_cache **cache;
  523. const char *name;
  524. size_t size;
  525. void (*ctor)(void*, struct kmem_cache *, unsigned long);
  526. } ecryptfs_cache_infos[] = {
  527. {
  528. .cache = &ecryptfs_auth_tok_list_item_cache,
  529. .name = "ecryptfs_auth_tok_list_item",
  530. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  531. },
  532. {
  533. .cache = &ecryptfs_file_info_cache,
  534. .name = "ecryptfs_file_cache",
  535. .size = sizeof(struct ecryptfs_file_info),
  536. },
  537. {
  538. .cache = &ecryptfs_dentry_info_cache,
  539. .name = "ecryptfs_dentry_info_cache",
  540. .size = sizeof(struct ecryptfs_dentry_info),
  541. },
  542. {
  543. .cache = &ecryptfs_inode_info_cache,
  544. .name = "ecryptfs_inode_cache",
  545. .size = sizeof(struct ecryptfs_inode_info),
  546. .ctor = inode_info_init_once,
  547. },
  548. {
  549. .cache = &ecryptfs_sb_info_cache,
  550. .name = "ecryptfs_sb_cache",
  551. .size = sizeof(struct ecryptfs_sb_info),
  552. },
  553. {
  554. .cache = &ecryptfs_header_cache_0,
  555. .name = "ecryptfs_headers_0",
  556. .size = PAGE_CACHE_SIZE,
  557. },
  558. {
  559. .cache = &ecryptfs_header_cache_1,
  560. .name = "ecryptfs_headers_1",
  561. .size = PAGE_CACHE_SIZE,
  562. },
  563. {
  564. .cache = &ecryptfs_header_cache_2,
  565. .name = "ecryptfs_headers_2",
  566. .size = PAGE_CACHE_SIZE,
  567. },
  568. {
  569. .cache = &ecryptfs_lower_page_cache,
  570. .name = "ecryptfs_lower_page_cache",
  571. .size = PAGE_CACHE_SIZE,
  572. },
  573. };
  574. static void ecryptfs_free_kmem_caches(void)
  575. {
  576. int i;
  577. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  578. struct ecryptfs_cache_info *info;
  579. info = &ecryptfs_cache_infos[i];
  580. if (*(info->cache))
  581. kmem_cache_destroy(*(info->cache));
  582. }
  583. }
  584. /**
  585. * ecryptfs_init_kmem_caches
  586. *
  587. * Returns zero on success; non-zero otherwise
  588. */
  589. static int ecryptfs_init_kmem_caches(void)
  590. {
  591. int i;
  592. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  593. struct ecryptfs_cache_info *info;
  594. info = &ecryptfs_cache_infos[i];
  595. *(info->cache) = kmem_cache_create(info->name, info->size,
  596. 0, SLAB_HWCACHE_ALIGN, info->ctor, NULL);
  597. if (!*(info->cache)) {
  598. ecryptfs_free_kmem_caches();
  599. ecryptfs_printk(KERN_WARNING, "%s: "
  600. "kmem_cache_create failed\n",
  601. info->name);
  602. return -ENOMEM;
  603. }
  604. }
  605. return 0;
  606. }
  607. struct ecryptfs_obj {
  608. char *name;
  609. struct list_head slot_list;
  610. struct kobject kobj;
  611. };
  612. struct ecryptfs_attribute {
  613. struct attribute attr;
  614. ssize_t(*show) (struct ecryptfs_obj *, char *);
  615. ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
  616. };
  617. static ssize_t
  618. ecryptfs_attr_store(struct kobject *kobj,
  619. struct attribute *attr, const char *buf, size_t len)
  620. {
  621. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  622. kobj);
  623. struct ecryptfs_attribute *attribute =
  624. container_of(attr, struct ecryptfs_attribute, attr);
  625. return (attribute->store ? attribute->store(obj, buf, len) : 0);
  626. }
  627. static ssize_t
  628. ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  629. {
  630. struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
  631. kobj);
  632. struct ecryptfs_attribute *attribute =
  633. container_of(attr, struct ecryptfs_attribute, attr);
  634. return (attribute->show ? attribute->show(obj, buf) : 0);
  635. }
  636. static struct sysfs_ops ecryptfs_sysfs_ops = {
  637. .show = ecryptfs_attr_show,
  638. .store = ecryptfs_attr_store
  639. };
  640. static struct kobj_type ecryptfs_ktype = {
  641. .sysfs_ops = &ecryptfs_sysfs_ops
  642. };
  643. static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
  644. static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
  645. {
  646. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  647. }
  648. static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
  649. static struct ecryptfs_version_str_map_elem {
  650. u32 flag;
  651. char *str;
  652. } ecryptfs_version_str_map[] = {
  653. {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
  654. {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
  655. {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
  656. {ECRYPTFS_VERSIONING_POLICY, "policy"}
  657. };
  658. static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
  659. {
  660. int i;
  661. int remaining = PAGE_SIZE;
  662. int total_written = 0;
  663. buff[0] = '\0';
  664. for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
  665. int entry_size;
  666. if (!(ECRYPTFS_VERSIONING_MASK
  667. & ecryptfs_version_str_map[i].flag))
  668. continue;
  669. entry_size = strlen(ecryptfs_version_str_map[i].str);
  670. if ((entry_size + 2) > remaining)
  671. goto out;
  672. memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
  673. buff[entry_size++] = '\n';
  674. buff[entry_size] = '\0';
  675. buff += entry_size;
  676. total_written += entry_size;
  677. remaining -= entry_size;
  678. }
  679. out:
  680. return total_written;
  681. }
  682. static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
  683. static int do_sysfs_registration(void)
  684. {
  685. int rc;
  686. if ((rc = subsystem_register(&ecryptfs_subsys))) {
  687. printk(KERN_ERR
  688. "Unable to register ecryptfs sysfs subsystem\n");
  689. goto out;
  690. }
  691. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  692. &sysfs_attr_version.attr);
  693. if (rc) {
  694. printk(KERN_ERR
  695. "Unable to create ecryptfs version attribute\n");
  696. subsystem_unregister(&ecryptfs_subsys);
  697. goto out;
  698. }
  699. rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
  700. &sysfs_attr_version_str.attr);
  701. if (rc) {
  702. printk(KERN_ERR
  703. "Unable to create ecryptfs version_str attribute\n");
  704. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  705. &sysfs_attr_version.attr);
  706. subsystem_unregister(&ecryptfs_subsys);
  707. goto out;
  708. }
  709. out:
  710. return rc;
  711. }
  712. static int __init ecryptfs_init(void)
  713. {
  714. int rc;
  715. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  716. rc = -EINVAL;
  717. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  718. "larger than the host's page size, and so "
  719. "eCryptfs cannot run on this system. The "
  720. "default eCryptfs extent size is [%d] bytes; "
  721. "the page size is [%d] bytes.\n",
  722. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  723. goto out;
  724. }
  725. rc = ecryptfs_init_kmem_caches();
  726. if (rc) {
  727. printk(KERN_ERR
  728. "Failed to allocate one or more kmem_cache objects\n");
  729. goto out;
  730. }
  731. rc = register_filesystem(&ecryptfs_fs_type);
  732. if (rc) {
  733. printk(KERN_ERR "Failed to register filesystem\n");
  734. ecryptfs_free_kmem_caches();
  735. goto out;
  736. }
  737. kset_set_kset_s(&ecryptfs_subsys, fs_subsys);
  738. sysfs_attr_version.attr.owner = THIS_MODULE;
  739. sysfs_attr_version_str.attr.owner = THIS_MODULE;
  740. rc = do_sysfs_registration();
  741. if (rc) {
  742. printk(KERN_ERR "sysfs registration failed\n");
  743. unregister_filesystem(&ecryptfs_fs_type);
  744. ecryptfs_free_kmem_caches();
  745. goto out;
  746. }
  747. out:
  748. return rc;
  749. }
  750. static void __exit ecryptfs_exit(void)
  751. {
  752. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  753. &sysfs_attr_version.attr);
  754. sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
  755. &sysfs_attr_version_str.attr);
  756. subsystem_unregister(&ecryptfs_subsys);
  757. unregister_filesystem(&ecryptfs_fs_type);
  758. ecryptfs_free_kmem_caches();
  759. }
  760. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  761. MODULE_DESCRIPTION("eCryptfs");
  762. MODULE_LICENSE("GPL");
  763. module_init(ecryptfs_init)
  764. module_exit(ecryptfs_exit)