main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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/mount.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/key.h>
  35. #include <linux/parser.h>
  36. #include <linux/fs_stack.h>
  37. #include <linux/slab.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. /**
  48. * Module parameter that defines the number of message buffer elements
  49. */
  50. unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
  51. module_param(ecryptfs_message_buf_len, uint, 0);
  52. MODULE_PARM_DESC(ecryptfs_message_buf_len,
  53. "Number of message buffer elements");
  54. /**
  55. * Module parameter that defines the maximum guaranteed amount of time to wait
  56. * for a response from ecryptfsd. The actual sleep time will be, more than
  57. * likely, a small amount greater than this specified value, but only less if
  58. * the message successfully arrives.
  59. */
  60. signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
  61. module_param(ecryptfs_message_wait_timeout, long, 0);
  62. MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
  63. "Maximum number of seconds that an operation will "
  64. "sleep while waiting for a message response from "
  65. "userspace");
  66. /**
  67. * Module parameter that is an estimate of the maximum number of users
  68. * that will be concurrently using eCryptfs. Set this to the right
  69. * value to balance performance and memory use.
  70. */
  71. unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
  72. module_param(ecryptfs_number_of_users, uint, 0);
  73. MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
  74. "concurrent users of eCryptfs");
  75. void __ecryptfs_printk(const char *fmt, ...)
  76. {
  77. va_list args;
  78. va_start(args, fmt);
  79. if (fmt[1] == '7') { /* KERN_DEBUG */
  80. if (ecryptfs_verbosity >= 1)
  81. vprintk(fmt, args);
  82. } else
  83. vprintk(fmt, args);
  84. va_end(args);
  85. }
  86. /**
  87. * ecryptfs_init_persistent_file
  88. * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
  89. * the lower dentry and the lower mount set
  90. *
  91. * eCryptfs only ever keeps a single open file for every lower
  92. * inode. All I/O operations to the lower inode occur through that
  93. * file. When the first eCryptfs dentry that interposes with the first
  94. * lower dentry for that inode is created, this function creates the
  95. * persistent file struct and associates it with the eCryptfs
  96. * inode. When the eCryptfs inode is destroyed, the file is closed.
  97. *
  98. * The persistent file will be opened with read/write permissions, if
  99. * possible. Otherwise, it is opened read-only.
  100. *
  101. * This function does nothing if a lower persistent file is already
  102. * associated with the eCryptfs inode.
  103. *
  104. * Returns zero on success; non-zero otherwise
  105. */
  106. int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
  107. {
  108. const struct cred *cred = current_cred();
  109. struct ecryptfs_inode_info *inode_info =
  110. ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
  111. int rc = 0;
  112. mutex_lock(&inode_info->lower_file_mutex);
  113. if (!inode_info->lower_file) {
  114. struct dentry *lower_dentry;
  115. struct vfsmount *lower_mnt =
  116. ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  117. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  118. rc = ecryptfs_privileged_open(&inode_info->lower_file,
  119. lower_dentry, lower_mnt, cred);
  120. if (rc) {
  121. printk(KERN_ERR "Error opening lower persistent file "
  122. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  123. "rc = [%d]\n", lower_dentry, lower_mnt, rc);
  124. inode_info->lower_file = NULL;
  125. }
  126. }
  127. mutex_unlock(&inode_info->lower_file_mutex);
  128. return rc;
  129. }
  130. /**
  131. * ecryptfs_interpose
  132. * @lower_dentry: Existing dentry in the lower filesystem
  133. * @dentry: ecryptfs' dentry
  134. * @sb: ecryptfs's super_block
  135. * @flags: flags to govern behavior of interpose procedure
  136. *
  137. * Interposes upper and lower dentries.
  138. *
  139. * Returns zero on success; non-zero otherwise
  140. */
  141. int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
  142. struct super_block *sb, u32 flags)
  143. {
  144. struct inode *lower_inode;
  145. struct inode *inode;
  146. int rc = 0;
  147. lower_inode = lower_dentry->d_inode;
  148. if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
  149. rc = -EXDEV;
  150. goto out;
  151. }
  152. if (!igrab(lower_inode)) {
  153. rc = -ESTALE;
  154. goto out;
  155. }
  156. inode = iget5_locked(sb, (unsigned long)lower_inode,
  157. ecryptfs_inode_test, ecryptfs_inode_set,
  158. lower_inode);
  159. if (!inode) {
  160. rc = -EACCES;
  161. iput(lower_inode);
  162. goto out;
  163. }
  164. if (inode->i_state & I_NEW)
  165. unlock_new_inode(inode);
  166. else
  167. iput(lower_inode);
  168. if (S_ISLNK(lower_inode->i_mode))
  169. inode->i_op = &ecryptfs_symlink_iops;
  170. else if (S_ISDIR(lower_inode->i_mode))
  171. inode->i_op = &ecryptfs_dir_iops;
  172. if (S_ISDIR(lower_inode->i_mode))
  173. inode->i_fop = &ecryptfs_dir_fops;
  174. if (special_file(lower_inode->i_mode))
  175. init_special_inode(inode, lower_inode->i_mode,
  176. lower_inode->i_rdev);
  177. d_set_d_op(dentry, &ecryptfs_dops);
  178. fsstack_copy_attr_all(inode, lower_inode);
  179. /* This size will be overwritten for real files w/ headers and
  180. * other metadata */
  181. fsstack_copy_inode_size(inode, lower_inode);
  182. if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
  183. d_add(dentry, inode);
  184. else
  185. d_instantiate(dentry, inode);
  186. out:
  187. return rc;
  188. }
  189. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  190. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  191. ecryptfs_opt_ecryptfs_key_bytes,
  192. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  193. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  194. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  195. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  196. ecryptfs_opt_err };
  197. static const match_table_t tokens = {
  198. {ecryptfs_opt_sig, "sig=%s"},
  199. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  200. {ecryptfs_opt_cipher, "cipher=%s"},
  201. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  202. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  203. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  204. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  205. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  206. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  207. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  208. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  209. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  210. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  211. {ecryptfs_opt_err, NULL}
  212. };
  213. static int ecryptfs_init_global_auth_toks(
  214. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  215. {
  216. struct ecryptfs_global_auth_tok *global_auth_tok;
  217. int rc = 0;
  218. list_for_each_entry(global_auth_tok,
  219. &mount_crypt_stat->global_auth_tok_list,
  220. mount_crypt_stat_list) {
  221. rc = ecryptfs_keyring_auth_tok_for_sig(
  222. &global_auth_tok->global_auth_tok_key,
  223. &global_auth_tok->global_auth_tok,
  224. global_auth_tok->sig);
  225. if (rc) {
  226. printk(KERN_ERR "Could not find valid key in user "
  227. "session keyring for sig specified in mount "
  228. "option: [%s]\n", global_auth_tok->sig);
  229. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  230. goto out;
  231. } else
  232. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  233. }
  234. out:
  235. return rc;
  236. }
  237. static void ecryptfs_init_mount_crypt_stat(
  238. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  239. {
  240. memset((void *)mount_crypt_stat, 0,
  241. sizeof(struct ecryptfs_mount_crypt_stat));
  242. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  243. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  244. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  245. }
  246. /**
  247. * ecryptfs_parse_options
  248. * @sb: The ecryptfs super block
  249. * @options: The options pased to the kernel
  250. *
  251. * Parse mount options:
  252. * debug=N - ecryptfs_verbosity level for debug output
  253. * sig=XXX - description(signature) of the key to use
  254. *
  255. * Returns the dentry object of the lower-level (lower/interposed)
  256. * directory; We want to mount our stackable file system on top of
  257. * that lower directory.
  258. *
  259. * The signature of the key to use must be the description of a key
  260. * already in the keyring. Mounting will fail if the key can not be
  261. * found.
  262. *
  263. * Returns zero on success; non-zero on error
  264. */
  265. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options)
  266. {
  267. char *p;
  268. int rc = 0;
  269. int sig_set = 0;
  270. int cipher_name_set = 0;
  271. int fn_cipher_name_set = 0;
  272. int cipher_key_bytes;
  273. int cipher_key_bytes_set = 0;
  274. int fn_cipher_key_bytes;
  275. int fn_cipher_key_bytes_set = 0;
  276. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  277. &sbi->mount_crypt_stat;
  278. substring_t args[MAX_OPT_ARGS];
  279. int token;
  280. char *sig_src;
  281. char *cipher_name_dst;
  282. char *cipher_name_src;
  283. char *fn_cipher_name_dst;
  284. char *fn_cipher_name_src;
  285. char *fnek_dst;
  286. char *fnek_src;
  287. char *cipher_key_bytes_src;
  288. char *fn_cipher_key_bytes_src;
  289. if (!options) {
  290. rc = -EINVAL;
  291. goto out;
  292. }
  293. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  294. while ((p = strsep(&options, ",")) != NULL) {
  295. if (!*p)
  296. continue;
  297. token = match_token(p, tokens, args);
  298. switch (token) {
  299. case ecryptfs_opt_sig:
  300. case ecryptfs_opt_ecryptfs_sig:
  301. sig_src = args[0].from;
  302. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  303. sig_src, 0);
  304. if (rc) {
  305. printk(KERN_ERR "Error attempting to register "
  306. "global sig; rc = [%d]\n", rc);
  307. goto out;
  308. }
  309. sig_set = 1;
  310. break;
  311. case ecryptfs_opt_cipher:
  312. case ecryptfs_opt_ecryptfs_cipher:
  313. cipher_name_src = args[0].from;
  314. cipher_name_dst =
  315. mount_crypt_stat->
  316. global_default_cipher_name;
  317. strncpy(cipher_name_dst, cipher_name_src,
  318. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  319. cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  320. cipher_name_set = 1;
  321. break;
  322. case ecryptfs_opt_ecryptfs_key_bytes:
  323. cipher_key_bytes_src = args[0].from;
  324. cipher_key_bytes =
  325. (int)simple_strtol(cipher_key_bytes_src,
  326. &cipher_key_bytes_src, 0);
  327. mount_crypt_stat->global_default_cipher_key_size =
  328. cipher_key_bytes;
  329. cipher_key_bytes_set = 1;
  330. break;
  331. case ecryptfs_opt_passthrough:
  332. mount_crypt_stat->flags |=
  333. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  334. break;
  335. case ecryptfs_opt_xattr_metadata:
  336. mount_crypt_stat->flags |=
  337. ECRYPTFS_XATTR_METADATA_ENABLED;
  338. break;
  339. case ecryptfs_opt_encrypted_view:
  340. mount_crypt_stat->flags |=
  341. ECRYPTFS_XATTR_METADATA_ENABLED;
  342. mount_crypt_stat->flags |=
  343. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  344. break;
  345. case ecryptfs_opt_fnek_sig:
  346. fnek_src = args[0].from;
  347. fnek_dst =
  348. mount_crypt_stat->global_default_fnek_sig;
  349. strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
  350. mount_crypt_stat->global_default_fnek_sig[
  351. ECRYPTFS_SIG_SIZE_HEX] = '\0';
  352. rc = ecryptfs_add_global_auth_tok(
  353. mount_crypt_stat,
  354. mount_crypt_stat->global_default_fnek_sig,
  355. ECRYPTFS_AUTH_TOK_FNEK);
  356. if (rc) {
  357. printk(KERN_ERR "Error attempting to register "
  358. "global fnek sig [%s]; rc = [%d]\n",
  359. mount_crypt_stat->global_default_fnek_sig,
  360. rc);
  361. goto out;
  362. }
  363. mount_crypt_stat->flags |=
  364. (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
  365. | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
  366. break;
  367. case ecryptfs_opt_fn_cipher:
  368. fn_cipher_name_src = args[0].from;
  369. fn_cipher_name_dst =
  370. mount_crypt_stat->global_default_fn_cipher_name;
  371. strncpy(fn_cipher_name_dst, fn_cipher_name_src,
  372. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  373. mount_crypt_stat->global_default_fn_cipher_name[
  374. ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  375. fn_cipher_name_set = 1;
  376. break;
  377. case ecryptfs_opt_fn_cipher_key_bytes:
  378. fn_cipher_key_bytes_src = args[0].from;
  379. fn_cipher_key_bytes =
  380. (int)simple_strtol(fn_cipher_key_bytes_src,
  381. &fn_cipher_key_bytes_src, 0);
  382. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  383. fn_cipher_key_bytes;
  384. fn_cipher_key_bytes_set = 1;
  385. break;
  386. case ecryptfs_opt_unlink_sigs:
  387. mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
  388. break;
  389. case ecryptfs_opt_mount_auth_tok_only:
  390. mount_crypt_stat->flags |=
  391. ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
  392. break;
  393. case ecryptfs_opt_err:
  394. default:
  395. printk(KERN_WARNING
  396. "%s: eCryptfs: unrecognized option [%s]\n",
  397. __func__, p);
  398. }
  399. }
  400. if (!sig_set) {
  401. rc = -EINVAL;
  402. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  403. "auth tok signature as a mount "
  404. "parameter; see the eCryptfs README\n");
  405. goto out;
  406. }
  407. if (!cipher_name_set) {
  408. int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  409. BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  410. strcpy(mount_crypt_stat->global_default_cipher_name,
  411. ECRYPTFS_DEFAULT_CIPHER);
  412. }
  413. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  414. && !fn_cipher_name_set)
  415. strcpy(mount_crypt_stat->global_default_fn_cipher_name,
  416. mount_crypt_stat->global_default_cipher_name);
  417. if (!cipher_key_bytes_set)
  418. mount_crypt_stat->global_default_cipher_key_size = 0;
  419. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  420. && !fn_cipher_key_bytes_set)
  421. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  422. mount_crypt_stat->global_default_cipher_key_size;
  423. mutex_lock(&key_tfm_list_mutex);
  424. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  425. NULL)) {
  426. rc = ecryptfs_add_new_key_tfm(
  427. NULL, mount_crypt_stat->global_default_cipher_name,
  428. mount_crypt_stat->global_default_cipher_key_size);
  429. if (rc) {
  430. printk(KERN_ERR "Error attempting to initialize "
  431. "cipher with name = [%s] and key size = [%td]; "
  432. "rc = [%d]\n",
  433. mount_crypt_stat->global_default_cipher_name,
  434. mount_crypt_stat->global_default_cipher_key_size,
  435. rc);
  436. rc = -EINVAL;
  437. mutex_unlock(&key_tfm_list_mutex);
  438. goto out;
  439. }
  440. }
  441. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  442. && !ecryptfs_tfm_exists(
  443. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  444. rc = ecryptfs_add_new_key_tfm(
  445. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  446. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  447. if (rc) {
  448. printk(KERN_ERR "Error attempting to initialize "
  449. "cipher with name = [%s] and key size = [%td]; "
  450. "rc = [%d]\n",
  451. mount_crypt_stat->global_default_fn_cipher_name,
  452. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  453. rc);
  454. rc = -EINVAL;
  455. mutex_unlock(&key_tfm_list_mutex);
  456. goto out;
  457. }
  458. }
  459. mutex_unlock(&key_tfm_list_mutex);
  460. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  461. if (rc)
  462. printk(KERN_WARNING "One or more global auth toks could not "
  463. "properly register; rc = [%d]\n", rc);
  464. out:
  465. return rc;
  466. }
  467. struct kmem_cache *ecryptfs_sb_info_cache;
  468. static struct file_system_type ecryptfs_fs_type;
  469. /**
  470. * ecryptfs_read_super
  471. * @sb: The ecryptfs super block
  472. * @dev_name: The path to mount over
  473. *
  474. * Read the super block of the lower filesystem, and use
  475. * ecryptfs_interpose to create our initial inode and super block
  476. * struct.
  477. */
  478. static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
  479. {
  480. struct path path;
  481. int rc;
  482. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  483. if (rc) {
  484. ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
  485. goto out;
  486. }
  487. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  488. rc = -EINVAL;
  489. printk(KERN_ERR "Mount on filesystem of type "
  490. "eCryptfs explicitly disallowed due to "
  491. "known incompatibilities\n");
  492. goto out_free;
  493. }
  494. ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
  495. sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  496. sb->s_blocksize = path.dentry->d_sb->s_blocksize;
  497. ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
  498. ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
  499. rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
  500. if (rc)
  501. goto out_free;
  502. rc = 0;
  503. goto out;
  504. out_free:
  505. path_put(&path);
  506. out:
  507. return rc;
  508. }
  509. /**
  510. * ecryptfs_get_sb
  511. * @fs_type
  512. * @flags
  513. * @dev_name: The path to mount over
  514. * @raw_data: The options passed into the kernel
  515. *
  516. * The whole ecryptfs_get_sb process is broken into 3 functions:
  517. * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
  518. * ecryptfs_read_super(): this accesses the lower filesystem and uses
  519. * ecryptfs_interpose to perform most of the linking
  520. * ecryptfs_interpose(): links the lower filesystem into ecryptfs (inode.c)
  521. */
  522. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  523. const char *dev_name, void *raw_data)
  524. {
  525. struct super_block *s;
  526. struct ecryptfs_sb_info *sbi;
  527. struct ecryptfs_dentry_info *root_info;
  528. const char *err = "Getting sb failed";
  529. int rc;
  530. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  531. if (!sbi) {
  532. rc = -ENOMEM;
  533. goto out;
  534. }
  535. rc = ecryptfs_parse_options(sbi, raw_data);
  536. if (rc) {
  537. err = "Error parsing options";
  538. goto out;
  539. }
  540. s = sget(fs_type, NULL, set_anon_super, NULL);
  541. if (IS_ERR(s)) {
  542. rc = PTR_ERR(s);
  543. goto out;
  544. }
  545. s->s_flags = flags;
  546. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
  547. if (rc) {
  548. deactivate_locked_super(s);
  549. goto out;
  550. }
  551. ecryptfs_set_superblock_private(s, sbi);
  552. s->s_bdi = &sbi->bdi;
  553. /* ->kill_sb() will take care of sbi after that point */
  554. sbi = NULL;
  555. s->s_op = &ecryptfs_sops;
  556. rc = -ENOMEM;
  557. s->s_root = d_alloc(NULL, &(const struct qstr) {
  558. .hash = 0,.name = "/",.len = 1});
  559. if (!s->s_root) {
  560. deactivate_locked_super(s);
  561. goto out;
  562. }
  563. d_set_d_op(s->s_root, &ecryptfs_dops);
  564. s->s_root->d_sb = s;
  565. s->s_root->d_parent = s->s_root;
  566. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  567. if (!root_info) {
  568. deactivate_locked_super(s);
  569. goto out;
  570. }
  571. /* ->kill_sb() will take care of root_info */
  572. ecryptfs_set_dentry_private(s->s_root, root_info);
  573. s->s_flags |= MS_ACTIVE;
  574. rc = ecryptfs_read_super(s, dev_name);
  575. if (rc) {
  576. deactivate_locked_super(s);
  577. err = "Reading sb failed";
  578. goto out;
  579. }
  580. return dget(s->s_root);
  581. out:
  582. if (sbi) {
  583. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  584. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  585. }
  586. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  587. return ERR_PTR(rc);
  588. }
  589. /**
  590. * ecryptfs_kill_block_super
  591. * @sb: The ecryptfs super block
  592. *
  593. * Used to bring the superblock down and free the private data.
  594. */
  595. static void ecryptfs_kill_block_super(struct super_block *sb)
  596. {
  597. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  598. kill_anon_super(sb);
  599. if (!sb_info)
  600. return;
  601. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  602. bdi_destroy(&sb_info->bdi);
  603. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  604. }
  605. static struct file_system_type ecryptfs_fs_type = {
  606. .owner = THIS_MODULE,
  607. .name = "ecryptfs",
  608. .mount = ecryptfs_mount,
  609. .kill_sb = ecryptfs_kill_block_super,
  610. .fs_flags = 0
  611. };
  612. /**
  613. * inode_info_init_once
  614. *
  615. * Initializes the ecryptfs_inode_info_cache when it is created
  616. */
  617. static void
  618. inode_info_init_once(void *vptr)
  619. {
  620. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  621. inode_init_once(&ei->vfs_inode);
  622. }
  623. static struct ecryptfs_cache_info {
  624. struct kmem_cache **cache;
  625. const char *name;
  626. size_t size;
  627. void (*ctor)(void *obj);
  628. } ecryptfs_cache_infos[] = {
  629. {
  630. .cache = &ecryptfs_auth_tok_list_item_cache,
  631. .name = "ecryptfs_auth_tok_list_item",
  632. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  633. },
  634. {
  635. .cache = &ecryptfs_file_info_cache,
  636. .name = "ecryptfs_file_cache",
  637. .size = sizeof(struct ecryptfs_file_info),
  638. },
  639. {
  640. .cache = &ecryptfs_dentry_info_cache,
  641. .name = "ecryptfs_dentry_info_cache",
  642. .size = sizeof(struct ecryptfs_dentry_info),
  643. },
  644. {
  645. .cache = &ecryptfs_inode_info_cache,
  646. .name = "ecryptfs_inode_cache",
  647. .size = sizeof(struct ecryptfs_inode_info),
  648. .ctor = inode_info_init_once,
  649. },
  650. {
  651. .cache = &ecryptfs_sb_info_cache,
  652. .name = "ecryptfs_sb_cache",
  653. .size = sizeof(struct ecryptfs_sb_info),
  654. },
  655. {
  656. .cache = &ecryptfs_header_cache_1,
  657. .name = "ecryptfs_headers_1",
  658. .size = PAGE_CACHE_SIZE,
  659. },
  660. {
  661. .cache = &ecryptfs_header_cache_2,
  662. .name = "ecryptfs_headers_2",
  663. .size = PAGE_CACHE_SIZE,
  664. },
  665. {
  666. .cache = &ecryptfs_xattr_cache,
  667. .name = "ecryptfs_xattr_cache",
  668. .size = PAGE_CACHE_SIZE,
  669. },
  670. {
  671. .cache = &ecryptfs_key_record_cache,
  672. .name = "ecryptfs_key_record_cache",
  673. .size = sizeof(struct ecryptfs_key_record),
  674. },
  675. {
  676. .cache = &ecryptfs_key_sig_cache,
  677. .name = "ecryptfs_key_sig_cache",
  678. .size = sizeof(struct ecryptfs_key_sig),
  679. },
  680. {
  681. .cache = &ecryptfs_global_auth_tok_cache,
  682. .name = "ecryptfs_global_auth_tok_cache",
  683. .size = sizeof(struct ecryptfs_global_auth_tok),
  684. },
  685. {
  686. .cache = &ecryptfs_key_tfm_cache,
  687. .name = "ecryptfs_key_tfm_cache",
  688. .size = sizeof(struct ecryptfs_key_tfm),
  689. },
  690. {
  691. .cache = &ecryptfs_open_req_cache,
  692. .name = "ecryptfs_open_req_cache",
  693. .size = sizeof(struct ecryptfs_open_req),
  694. },
  695. };
  696. static void ecryptfs_free_kmem_caches(void)
  697. {
  698. int i;
  699. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  700. struct ecryptfs_cache_info *info;
  701. info = &ecryptfs_cache_infos[i];
  702. if (*(info->cache))
  703. kmem_cache_destroy(*(info->cache));
  704. }
  705. }
  706. /**
  707. * ecryptfs_init_kmem_caches
  708. *
  709. * Returns zero on success; non-zero otherwise
  710. */
  711. static int ecryptfs_init_kmem_caches(void)
  712. {
  713. int i;
  714. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  715. struct ecryptfs_cache_info *info;
  716. info = &ecryptfs_cache_infos[i];
  717. *(info->cache) = kmem_cache_create(info->name, info->size,
  718. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  719. if (!*(info->cache)) {
  720. ecryptfs_free_kmem_caches();
  721. ecryptfs_printk(KERN_WARNING, "%s: "
  722. "kmem_cache_create failed\n",
  723. info->name);
  724. return -ENOMEM;
  725. }
  726. }
  727. return 0;
  728. }
  729. static struct kobject *ecryptfs_kobj;
  730. static ssize_t version_show(struct kobject *kobj,
  731. struct kobj_attribute *attr, char *buff)
  732. {
  733. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  734. }
  735. static struct kobj_attribute version_attr = __ATTR_RO(version);
  736. static struct attribute *attributes[] = {
  737. &version_attr.attr,
  738. NULL,
  739. };
  740. static struct attribute_group attr_group = {
  741. .attrs = attributes,
  742. };
  743. static int do_sysfs_registration(void)
  744. {
  745. int rc;
  746. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  747. if (!ecryptfs_kobj) {
  748. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  749. rc = -ENOMEM;
  750. goto out;
  751. }
  752. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  753. if (rc) {
  754. printk(KERN_ERR
  755. "Unable to create ecryptfs version attributes\n");
  756. kobject_put(ecryptfs_kobj);
  757. }
  758. out:
  759. return rc;
  760. }
  761. static void do_sysfs_unregistration(void)
  762. {
  763. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  764. kobject_put(ecryptfs_kobj);
  765. }
  766. static int __init ecryptfs_init(void)
  767. {
  768. int rc;
  769. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  770. rc = -EINVAL;
  771. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  772. "larger than the host's page size, and so "
  773. "eCryptfs cannot run on this system. The "
  774. "default eCryptfs extent size is [%d] bytes; "
  775. "the page size is [%d] bytes.\n",
  776. ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
  777. goto out;
  778. }
  779. rc = ecryptfs_init_kmem_caches();
  780. if (rc) {
  781. printk(KERN_ERR
  782. "Failed to allocate one or more kmem_cache objects\n");
  783. goto out;
  784. }
  785. rc = register_filesystem(&ecryptfs_fs_type);
  786. if (rc) {
  787. printk(KERN_ERR "Failed to register filesystem\n");
  788. goto out_free_kmem_caches;
  789. }
  790. rc = do_sysfs_registration();
  791. if (rc) {
  792. printk(KERN_ERR "sysfs registration failed\n");
  793. goto out_unregister_filesystem;
  794. }
  795. rc = ecryptfs_init_kthread();
  796. if (rc) {
  797. printk(KERN_ERR "%s: kthread initialization failed; "
  798. "rc = [%d]\n", __func__, rc);
  799. goto out_do_sysfs_unregistration;
  800. }
  801. rc = ecryptfs_init_messaging();
  802. if (rc) {
  803. printk(KERN_ERR "Failure occured while attempting to "
  804. "initialize the communications channel to "
  805. "ecryptfsd\n");
  806. goto out_destroy_kthread;
  807. }
  808. rc = ecryptfs_init_crypto();
  809. if (rc) {
  810. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  811. "rc = [%d]\n", rc);
  812. goto out_release_messaging;
  813. }
  814. if (ecryptfs_verbosity > 0)
  815. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  816. "will be written to the syslog!\n", ecryptfs_verbosity);
  817. goto out;
  818. out_release_messaging:
  819. ecryptfs_release_messaging();
  820. out_destroy_kthread:
  821. ecryptfs_destroy_kthread();
  822. out_do_sysfs_unregistration:
  823. do_sysfs_unregistration();
  824. out_unregister_filesystem:
  825. unregister_filesystem(&ecryptfs_fs_type);
  826. out_free_kmem_caches:
  827. ecryptfs_free_kmem_caches();
  828. out:
  829. return rc;
  830. }
  831. static void __exit ecryptfs_exit(void)
  832. {
  833. int rc;
  834. rc = ecryptfs_destroy_crypto();
  835. if (rc)
  836. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  837. "rc = [%d]\n", rc);
  838. ecryptfs_release_messaging();
  839. ecryptfs_destroy_kthread();
  840. do_sysfs_unregistration();
  841. unregister_filesystem(&ecryptfs_fs_type);
  842. ecryptfs_free_kmem_caches();
  843. }
  844. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  845. MODULE_DESCRIPTION("eCryptfs");
  846. MODULE_LICENSE("GPL");
  847. module_init(ecryptfs_init)
  848. module_exit(ecryptfs_exit)