main.c 25 KB

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