main.c 22 KB

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