main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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_lower_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. * lower file struct and associates it with the eCryptfs
  97. * inode. When all eCryptfs files associated with the inode are released, the
  98. * file is closed.
  99. *
  100. * The lower file will be opened with read/write permissions, if
  101. * possible. Otherwise, it is opened read-only.
  102. *
  103. * This function does nothing if a lower file is already
  104. * associated with the eCryptfs inode.
  105. *
  106. * Returns zero on success; non-zero otherwise
  107. */
  108. static int ecryptfs_init_lower_file(struct dentry *dentry,
  109. struct file **lower_file)
  110. {
  111. const struct cred *cred = current_cred();
  112. struct path *path = ecryptfs_dentry_to_lower_path(dentry);
  113. int rc;
  114. rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
  115. cred);
  116. if (rc) {
  117. printk(KERN_ERR "Error opening lower file "
  118. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  119. "rc = [%d]\n", path->dentry, path->mnt, rc);
  120. (*lower_file) = NULL;
  121. }
  122. return rc;
  123. }
  124. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
  125. {
  126. struct ecryptfs_inode_info *inode_info;
  127. int count, rc = 0;
  128. inode_info = ecryptfs_inode_to_private(inode);
  129. mutex_lock(&inode_info->lower_file_mutex);
  130. count = atomic_inc_return(&inode_info->lower_file_count);
  131. if (WARN_ON_ONCE(count < 1))
  132. rc = -EINVAL;
  133. else if (count == 1) {
  134. rc = ecryptfs_init_lower_file(dentry,
  135. &inode_info->lower_file);
  136. if (rc)
  137. atomic_set(&inode_info->lower_file_count, 0);
  138. }
  139. mutex_unlock(&inode_info->lower_file_mutex);
  140. return rc;
  141. }
  142. void ecryptfs_put_lower_file(struct inode *inode)
  143. {
  144. struct ecryptfs_inode_info *inode_info;
  145. inode_info = ecryptfs_inode_to_private(inode);
  146. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  147. &inode_info->lower_file_mutex)) {
  148. filemap_write_and_wait(inode->i_mapping);
  149. fput(inode_info->lower_file);
  150. inode_info->lower_file = NULL;
  151. mutex_unlock(&inode_info->lower_file_mutex);
  152. }
  153. }
  154. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  155. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  156. ecryptfs_opt_ecryptfs_key_bytes,
  157. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  158. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  159. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  160. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  161. ecryptfs_opt_check_dev_ruid,
  162. ecryptfs_opt_err };
  163. static const match_table_t tokens = {
  164. {ecryptfs_opt_sig, "sig=%s"},
  165. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  166. {ecryptfs_opt_cipher, "cipher=%s"},
  167. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  168. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  169. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  170. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  171. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  172. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  173. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  174. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  175. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  176. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  177. {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
  178. {ecryptfs_opt_err, NULL}
  179. };
  180. static int ecryptfs_init_global_auth_toks(
  181. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  182. {
  183. struct ecryptfs_global_auth_tok *global_auth_tok;
  184. struct ecryptfs_auth_tok *auth_tok;
  185. int rc = 0;
  186. list_for_each_entry(global_auth_tok,
  187. &mount_crypt_stat->global_auth_tok_list,
  188. mount_crypt_stat_list) {
  189. rc = ecryptfs_keyring_auth_tok_for_sig(
  190. &global_auth_tok->global_auth_tok_key, &auth_tok,
  191. global_auth_tok->sig);
  192. if (rc) {
  193. printk(KERN_ERR "Could not find valid key in user "
  194. "session keyring for sig specified in mount "
  195. "option: [%s]\n", global_auth_tok->sig);
  196. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  197. goto out;
  198. } else {
  199. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  200. up_write(&(global_auth_tok->global_auth_tok_key)->sem);
  201. }
  202. }
  203. out:
  204. return rc;
  205. }
  206. static void ecryptfs_init_mount_crypt_stat(
  207. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  208. {
  209. memset((void *)mount_crypt_stat, 0,
  210. sizeof(struct ecryptfs_mount_crypt_stat));
  211. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  212. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  213. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  214. }
  215. /**
  216. * ecryptfs_parse_options
  217. * @sb: The ecryptfs super block
  218. * @options: The options passed to the kernel
  219. * @check_ruid: set to 1 if device uid should be checked against the ruid
  220. *
  221. * Parse mount options:
  222. * debug=N - ecryptfs_verbosity level for debug output
  223. * sig=XXX - description(signature) of the key to use
  224. *
  225. * Returns the dentry object of the lower-level (lower/interposed)
  226. * directory; We want to mount our stackable file system on top of
  227. * that lower directory.
  228. *
  229. * The signature of the key to use must be the description of a key
  230. * already in the keyring. Mounting will fail if the key can not be
  231. * found.
  232. *
  233. * Returns zero on success; non-zero on error
  234. */
  235. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
  236. uid_t *check_ruid)
  237. {
  238. char *p;
  239. int rc = 0;
  240. int sig_set = 0;
  241. int cipher_name_set = 0;
  242. int fn_cipher_name_set = 0;
  243. int cipher_key_bytes;
  244. int cipher_key_bytes_set = 0;
  245. int fn_cipher_key_bytes;
  246. int fn_cipher_key_bytes_set = 0;
  247. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  248. &sbi->mount_crypt_stat;
  249. substring_t args[MAX_OPT_ARGS];
  250. int token;
  251. char *sig_src;
  252. char *cipher_name_dst;
  253. char *cipher_name_src;
  254. char *fn_cipher_name_dst;
  255. char *fn_cipher_name_src;
  256. char *fnek_dst;
  257. char *fnek_src;
  258. char *cipher_key_bytes_src;
  259. char *fn_cipher_key_bytes_src;
  260. u8 cipher_code;
  261. *check_ruid = 0;
  262. if (!options) {
  263. rc = -EINVAL;
  264. goto out;
  265. }
  266. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  267. while ((p = strsep(&options, ",")) != NULL) {
  268. if (!*p)
  269. continue;
  270. token = match_token(p, tokens, args);
  271. switch (token) {
  272. case ecryptfs_opt_sig:
  273. case ecryptfs_opt_ecryptfs_sig:
  274. sig_src = args[0].from;
  275. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  276. sig_src, 0);
  277. if (rc) {
  278. printk(KERN_ERR "Error attempting to register "
  279. "global sig; rc = [%d]\n", rc);
  280. goto out;
  281. }
  282. sig_set = 1;
  283. break;
  284. case ecryptfs_opt_cipher:
  285. case ecryptfs_opt_ecryptfs_cipher:
  286. cipher_name_src = args[0].from;
  287. cipher_name_dst =
  288. mount_crypt_stat->
  289. global_default_cipher_name;
  290. strncpy(cipher_name_dst, cipher_name_src,
  291. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  292. cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  293. cipher_name_set = 1;
  294. break;
  295. case ecryptfs_opt_ecryptfs_key_bytes:
  296. cipher_key_bytes_src = args[0].from;
  297. cipher_key_bytes =
  298. (int)simple_strtol(cipher_key_bytes_src,
  299. &cipher_key_bytes_src, 0);
  300. mount_crypt_stat->global_default_cipher_key_size =
  301. cipher_key_bytes;
  302. cipher_key_bytes_set = 1;
  303. break;
  304. case ecryptfs_opt_passthrough:
  305. mount_crypt_stat->flags |=
  306. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  307. break;
  308. case ecryptfs_opt_xattr_metadata:
  309. mount_crypt_stat->flags |=
  310. ECRYPTFS_XATTR_METADATA_ENABLED;
  311. break;
  312. case ecryptfs_opt_encrypted_view:
  313. mount_crypt_stat->flags |=
  314. ECRYPTFS_XATTR_METADATA_ENABLED;
  315. mount_crypt_stat->flags |=
  316. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  317. break;
  318. case ecryptfs_opt_fnek_sig:
  319. fnek_src = args[0].from;
  320. fnek_dst =
  321. mount_crypt_stat->global_default_fnek_sig;
  322. strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
  323. mount_crypt_stat->global_default_fnek_sig[
  324. ECRYPTFS_SIG_SIZE_HEX] = '\0';
  325. rc = ecryptfs_add_global_auth_tok(
  326. mount_crypt_stat,
  327. mount_crypt_stat->global_default_fnek_sig,
  328. ECRYPTFS_AUTH_TOK_FNEK);
  329. if (rc) {
  330. printk(KERN_ERR "Error attempting to register "
  331. "global fnek sig [%s]; rc = [%d]\n",
  332. mount_crypt_stat->global_default_fnek_sig,
  333. rc);
  334. goto out;
  335. }
  336. mount_crypt_stat->flags |=
  337. (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
  338. | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
  339. break;
  340. case ecryptfs_opt_fn_cipher:
  341. fn_cipher_name_src = args[0].from;
  342. fn_cipher_name_dst =
  343. mount_crypt_stat->global_default_fn_cipher_name;
  344. strncpy(fn_cipher_name_dst, fn_cipher_name_src,
  345. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  346. mount_crypt_stat->global_default_fn_cipher_name[
  347. ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  348. fn_cipher_name_set = 1;
  349. break;
  350. case ecryptfs_opt_fn_cipher_key_bytes:
  351. fn_cipher_key_bytes_src = args[0].from;
  352. fn_cipher_key_bytes =
  353. (int)simple_strtol(fn_cipher_key_bytes_src,
  354. &fn_cipher_key_bytes_src, 0);
  355. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  356. fn_cipher_key_bytes;
  357. fn_cipher_key_bytes_set = 1;
  358. break;
  359. case ecryptfs_opt_unlink_sigs:
  360. mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
  361. break;
  362. case ecryptfs_opt_mount_auth_tok_only:
  363. mount_crypt_stat->flags |=
  364. ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
  365. break;
  366. case ecryptfs_opt_check_dev_ruid:
  367. *check_ruid = 1;
  368. break;
  369. case ecryptfs_opt_err:
  370. default:
  371. printk(KERN_WARNING
  372. "%s: eCryptfs: unrecognized option [%s]\n",
  373. __func__, p);
  374. }
  375. }
  376. if (!sig_set) {
  377. rc = -EINVAL;
  378. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  379. "auth tok signature as a mount "
  380. "parameter; see the eCryptfs README\n");
  381. goto out;
  382. }
  383. if (!cipher_name_set) {
  384. int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  385. BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  386. strcpy(mount_crypt_stat->global_default_cipher_name,
  387. ECRYPTFS_DEFAULT_CIPHER);
  388. }
  389. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  390. && !fn_cipher_name_set)
  391. strcpy(mount_crypt_stat->global_default_fn_cipher_name,
  392. mount_crypt_stat->global_default_cipher_name);
  393. if (!cipher_key_bytes_set)
  394. mount_crypt_stat->global_default_cipher_key_size = 0;
  395. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  396. && !fn_cipher_key_bytes_set)
  397. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  398. mount_crypt_stat->global_default_cipher_key_size;
  399. cipher_code = ecryptfs_code_for_cipher_string(
  400. mount_crypt_stat->global_default_cipher_name,
  401. mount_crypt_stat->global_default_cipher_key_size);
  402. if (!cipher_code) {
  403. ecryptfs_printk(KERN_ERR,
  404. "eCryptfs doesn't support cipher: %s",
  405. mount_crypt_stat->global_default_cipher_name);
  406. rc = -EINVAL;
  407. goto out;
  408. }
  409. mutex_lock(&key_tfm_list_mutex);
  410. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  411. NULL)) {
  412. rc = ecryptfs_add_new_key_tfm(
  413. NULL, mount_crypt_stat->global_default_cipher_name,
  414. mount_crypt_stat->global_default_cipher_key_size);
  415. if (rc) {
  416. printk(KERN_ERR "Error attempting to initialize "
  417. "cipher with name = [%s] and key size = [%td]; "
  418. "rc = [%d]\n",
  419. mount_crypt_stat->global_default_cipher_name,
  420. mount_crypt_stat->global_default_cipher_key_size,
  421. rc);
  422. rc = -EINVAL;
  423. mutex_unlock(&key_tfm_list_mutex);
  424. goto out;
  425. }
  426. }
  427. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  428. && !ecryptfs_tfm_exists(
  429. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  430. rc = ecryptfs_add_new_key_tfm(
  431. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  432. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  433. if (rc) {
  434. printk(KERN_ERR "Error attempting to initialize "
  435. "cipher with name = [%s] and key size = [%td]; "
  436. "rc = [%d]\n",
  437. mount_crypt_stat->global_default_fn_cipher_name,
  438. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  439. rc);
  440. rc = -EINVAL;
  441. mutex_unlock(&key_tfm_list_mutex);
  442. goto out;
  443. }
  444. }
  445. mutex_unlock(&key_tfm_list_mutex);
  446. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  447. if (rc)
  448. printk(KERN_WARNING "One or more global auth toks could not "
  449. "properly register; rc = [%d]\n", rc);
  450. out:
  451. return rc;
  452. }
  453. struct kmem_cache *ecryptfs_sb_info_cache;
  454. static struct file_system_type ecryptfs_fs_type;
  455. /**
  456. * ecryptfs_get_sb
  457. * @fs_type
  458. * @flags
  459. * @dev_name: The path to mount over
  460. * @raw_data: The options passed into the kernel
  461. */
  462. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  463. const char *dev_name, void *raw_data)
  464. {
  465. struct super_block *s;
  466. struct ecryptfs_sb_info *sbi;
  467. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  468. struct ecryptfs_dentry_info *root_info;
  469. const char *err = "Getting sb failed";
  470. struct inode *inode;
  471. struct path path;
  472. uid_t check_ruid;
  473. int rc;
  474. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  475. if (!sbi) {
  476. rc = -ENOMEM;
  477. goto out;
  478. }
  479. rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
  480. if (rc) {
  481. err = "Error parsing options";
  482. goto out;
  483. }
  484. mount_crypt_stat = &sbi->mount_crypt_stat;
  485. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  486. if (IS_ERR(s)) {
  487. rc = PTR_ERR(s);
  488. goto out;
  489. }
  490. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs");
  491. if (rc)
  492. goto out1;
  493. ecryptfs_set_superblock_private(s, sbi);
  494. s->s_bdi = &sbi->bdi;
  495. /* ->kill_sb() will take care of sbi after that point */
  496. sbi = NULL;
  497. s->s_op = &ecryptfs_sops;
  498. s->s_d_op = &ecryptfs_dops;
  499. err = "Reading sb failed";
  500. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  501. if (rc) {
  502. ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
  503. goto out1;
  504. }
  505. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  506. rc = -EINVAL;
  507. printk(KERN_ERR "Mount on filesystem of type "
  508. "eCryptfs explicitly disallowed due to "
  509. "known incompatibilities\n");
  510. goto out_free;
  511. }
  512. if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
  513. rc = -EPERM;
  514. printk(KERN_ERR "Mount of device (uid: %d) not owned by "
  515. "requested user (uid: %d)\n",
  516. i_uid_read(d_inode(path.dentry)),
  517. from_kuid(&init_user_ns, current_uid()));
  518. goto out_free;
  519. }
  520. ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
  521. /**
  522. * Set the POSIX ACL flag based on whether they're enabled in the lower
  523. * mount.
  524. */
  525. s->s_flags = flags & ~MS_POSIXACL;
  526. s->s_flags |= path.dentry->d_sb->s_flags & MS_POSIXACL;
  527. /**
  528. * Force a read-only eCryptfs mount when:
  529. * 1) The lower mount is ro
  530. * 2) The ecryptfs_encrypted_view mount option is specified
  531. */
  532. if (path.dentry->d_sb->s_flags & MS_RDONLY ||
  533. mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  534. s->s_flags |= MS_RDONLY;
  535. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  536. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  537. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  538. s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
  539. rc = -EINVAL;
  540. if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  541. pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
  542. goto out_free;
  543. }
  544. inode = ecryptfs_get_inode(d_inode(path.dentry), s);
  545. rc = PTR_ERR(inode);
  546. if (IS_ERR(inode))
  547. goto out_free;
  548. s->s_root = d_make_root(inode);
  549. if (!s->s_root) {
  550. rc = -ENOMEM;
  551. goto out_free;
  552. }
  553. rc = -ENOMEM;
  554. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  555. if (!root_info)
  556. goto out_free;
  557. /* ->kill_sb() will take care of root_info */
  558. ecryptfs_set_dentry_private(s->s_root, root_info);
  559. root_info->lower_path = path;
  560. s->s_flags |= MS_ACTIVE;
  561. return dget(s->s_root);
  562. out_free:
  563. path_put(&path);
  564. out1:
  565. deactivate_locked_super(s);
  566. out:
  567. if (sbi) {
  568. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  569. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  570. }
  571. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  572. return ERR_PTR(rc);
  573. }
  574. /**
  575. * ecryptfs_kill_block_super
  576. * @sb: The ecryptfs super block
  577. *
  578. * Used to bring the superblock down and free the private data.
  579. */
  580. static void ecryptfs_kill_block_super(struct super_block *sb)
  581. {
  582. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  583. kill_anon_super(sb);
  584. if (!sb_info)
  585. return;
  586. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  587. bdi_destroy(&sb_info->bdi);
  588. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  589. }
  590. static struct file_system_type ecryptfs_fs_type = {
  591. .owner = THIS_MODULE,
  592. .name = "ecryptfs",
  593. .mount = ecryptfs_mount,
  594. .kill_sb = ecryptfs_kill_block_super,
  595. .fs_flags = 0
  596. };
  597. MODULE_ALIAS_FS("ecryptfs");
  598. /**
  599. * inode_info_init_once
  600. *
  601. * Initializes the ecryptfs_inode_info_cache when it is created
  602. */
  603. static void
  604. inode_info_init_once(void *vptr)
  605. {
  606. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  607. inode_init_once(&ei->vfs_inode);
  608. }
  609. static struct ecryptfs_cache_info {
  610. struct kmem_cache **cache;
  611. const char *name;
  612. size_t size;
  613. void (*ctor)(void *obj);
  614. } ecryptfs_cache_infos[] = {
  615. {
  616. .cache = &ecryptfs_auth_tok_list_item_cache,
  617. .name = "ecryptfs_auth_tok_list_item",
  618. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  619. },
  620. {
  621. .cache = &ecryptfs_file_info_cache,
  622. .name = "ecryptfs_file_cache",
  623. .size = sizeof(struct ecryptfs_file_info),
  624. },
  625. {
  626. .cache = &ecryptfs_dentry_info_cache,
  627. .name = "ecryptfs_dentry_info_cache",
  628. .size = sizeof(struct ecryptfs_dentry_info),
  629. },
  630. {
  631. .cache = &ecryptfs_inode_info_cache,
  632. .name = "ecryptfs_inode_cache",
  633. .size = sizeof(struct ecryptfs_inode_info),
  634. .ctor = inode_info_init_once,
  635. },
  636. {
  637. .cache = &ecryptfs_sb_info_cache,
  638. .name = "ecryptfs_sb_cache",
  639. .size = sizeof(struct ecryptfs_sb_info),
  640. },
  641. {
  642. .cache = &ecryptfs_header_cache,
  643. .name = "ecryptfs_headers",
  644. .size = PAGE_CACHE_SIZE,
  645. },
  646. {
  647. .cache = &ecryptfs_xattr_cache,
  648. .name = "ecryptfs_xattr_cache",
  649. .size = PAGE_CACHE_SIZE,
  650. },
  651. {
  652. .cache = &ecryptfs_key_record_cache,
  653. .name = "ecryptfs_key_record_cache",
  654. .size = sizeof(struct ecryptfs_key_record),
  655. },
  656. {
  657. .cache = &ecryptfs_key_sig_cache,
  658. .name = "ecryptfs_key_sig_cache",
  659. .size = sizeof(struct ecryptfs_key_sig),
  660. },
  661. {
  662. .cache = &ecryptfs_global_auth_tok_cache,
  663. .name = "ecryptfs_global_auth_tok_cache",
  664. .size = sizeof(struct ecryptfs_global_auth_tok),
  665. },
  666. {
  667. .cache = &ecryptfs_key_tfm_cache,
  668. .name = "ecryptfs_key_tfm_cache",
  669. .size = sizeof(struct ecryptfs_key_tfm),
  670. },
  671. };
  672. static void ecryptfs_free_kmem_caches(void)
  673. {
  674. int i;
  675. /*
  676. * Make sure all delayed rcu free inodes are flushed before we
  677. * destroy cache.
  678. */
  679. rcu_barrier();
  680. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  681. struct ecryptfs_cache_info *info;
  682. info = &ecryptfs_cache_infos[i];
  683. if (*(info->cache))
  684. kmem_cache_destroy(*(info->cache));
  685. }
  686. }
  687. /**
  688. * ecryptfs_init_kmem_caches
  689. *
  690. * Returns zero on success; non-zero otherwise
  691. */
  692. static int ecryptfs_init_kmem_caches(void)
  693. {
  694. int i;
  695. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  696. struct ecryptfs_cache_info *info;
  697. info = &ecryptfs_cache_infos[i];
  698. *(info->cache) = kmem_cache_create(info->name, info->size,
  699. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  700. if (!*(info->cache)) {
  701. ecryptfs_free_kmem_caches();
  702. ecryptfs_printk(KERN_WARNING, "%s: "
  703. "kmem_cache_create failed\n",
  704. info->name);
  705. return -ENOMEM;
  706. }
  707. }
  708. return 0;
  709. }
  710. static struct kobject *ecryptfs_kobj;
  711. static ssize_t version_show(struct kobject *kobj,
  712. struct kobj_attribute *attr, char *buff)
  713. {
  714. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  715. }
  716. static struct kobj_attribute version_attr = __ATTR_RO(version);
  717. static struct attribute *attributes[] = {
  718. &version_attr.attr,
  719. NULL,
  720. };
  721. static struct attribute_group attr_group = {
  722. .attrs = attributes,
  723. };
  724. static int do_sysfs_registration(void)
  725. {
  726. int rc;
  727. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  728. if (!ecryptfs_kobj) {
  729. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  730. rc = -ENOMEM;
  731. goto out;
  732. }
  733. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  734. if (rc) {
  735. printk(KERN_ERR
  736. "Unable to create ecryptfs version attributes\n");
  737. kobject_put(ecryptfs_kobj);
  738. }
  739. out:
  740. return rc;
  741. }
  742. static void do_sysfs_unregistration(void)
  743. {
  744. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  745. kobject_put(ecryptfs_kobj);
  746. }
  747. static int __init ecryptfs_init(void)
  748. {
  749. int rc;
  750. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  751. rc = -EINVAL;
  752. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  753. "larger than the host's page size, and so "
  754. "eCryptfs cannot run on this system. The "
  755. "default eCryptfs extent size is [%u] bytes; "
  756. "the page size is [%lu] bytes.\n",
  757. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  758. (unsigned long)PAGE_CACHE_SIZE);
  759. goto out;
  760. }
  761. rc = ecryptfs_init_kmem_caches();
  762. if (rc) {
  763. printk(KERN_ERR
  764. "Failed to allocate one or more kmem_cache objects\n");
  765. goto out;
  766. }
  767. rc = do_sysfs_registration();
  768. if (rc) {
  769. printk(KERN_ERR "sysfs registration failed\n");
  770. goto out_free_kmem_caches;
  771. }
  772. rc = ecryptfs_init_kthread();
  773. if (rc) {
  774. printk(KERN_ERR "%s: kthread initialization failed; "
  775. "rc = [%d]\n", __func__, rc);
  776. goto out_do_sysfs_unregistration;
  777. }
  778. rc = ecryptfs_init_messaging();
  779. if (rc) {
  780. printk(KERN_ERR "Failure occurred while attempting to "
  781. "initialize the communications channel to "
  782. "ecryptfsd\n");
  783. goto out_destroy_kthread;
  784. }
  785. rc = ecryptfs_init_crypto();
  786. if (rc) {
  787. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  788. "rc = [%d]\n", rc);
  789. goto out_release_messaging;
  790. }
  791. rc = register_filesystem(&ecryptfs_fs_type);
  792. if (rc) {
  793. printk(KERN_ERR "Failed to register filesystem\n");
  794. goto out_destroy_crypto;
  795. }
  796. if (ecryptfs_verbosity > 0)
  797. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  798. "will be written to the syslog!\n", ecryptfs_verbosity);
  799. goto out;
  800. out_destroy_crypto:
  801. ecryptfs_destroy_crypto();
  802. out_release_messaging:
  803. ecryptfs_release_messaging();
  804. out_destroy_kthread:
  805. ecryptfs_destroy_kthread();
  806. out_do_sysfs_unregistration:
  807. do_sysfs_unregistration();
  808. out_free_kmem_caches:
  809. ecryptfs_free_kmem_caches();
  810. out:
  811. return rc;
  812. }
  813. static void __exit ecryptfs_exit(void)
  814. {
  815. int rc;
  816. rc = ecryptfs_destroy_crypto();
  817. if (rc)
  818. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  819. "rc = [%d]\n", rc);
  820. ecryptfs_release_messaging();
  821. ecryptfs_destroy_kthread();
  822. do_sysfs_unregistration();
  823. unregister_filesystem(&ecryptfs_fs_type);
  824. ecryptfs_free_kmem_caches();
  825. }
  826. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  827. MODULE_DESCRIPTION("eCryptfs");
  828. MODULE_LICENSE("GPL");
  829. module_init(ecryptfs_init)
  830. module_exit(ecryptfs_exit)