main.c 24 KB

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