main.c 24 KB

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