main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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/mount.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/key.h>
  34. #include <linux/parser.h>
  35. #include <linux/fs_stack.h>
  36. #include <linux/slab.h>
  37. #include <linux/magic.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_lower_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. * lower file struct and associates it with the eCryptfs
  96. * inode. When all eCryptfs files associated with the inode are released, the
  97. * file is closed.
  98. *
  99. * The lower 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 file is already
  103. * associated with the eCryptfs inode.
  104. *
  105. * Returns zero on success; non-zero otherwise
  106. */
  107. static int ecryptfs_init_lower_file(struct dentry *dentry,
  108. struct file **lower_file)
  109. {
  110. const struct cred *cred = current_cred();
  111. struct path *path = ecryptfs_dentry_to_lower_path(dentry);
  112. int rc;
  113. rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
  114. cred);
  115. if (rc) {
  116. printk(KERN_ERR "Error opening lower file "
  117. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  118. "rc = [%d]\n", path->dentry, path->mnt, rc);
  119. (*lower_file) = NULL;
  120. }
  121. return rc;
  122. }
  123. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
  124. {
  125. struct ecryptfs_inode_info *inode_info;
  126. int count, rc = 0;
  127. inode_info = ecryptfs_inode_to_private(inode);
  128. mutex_lock(&inode_info->lower_file_mutex);
  129. count = atomic_inc_return(&inode_info->lower_file_count);
  130. if (WARN_ON_ONCE(count < 1))
  131. rc = -EINVAL;
  132. else if (count == 1) {
  133. rc = ecryptfs_init_lower_file(dentry,
  134. &inode_info->lower_file);
  135. if (rc)
  136. atomic_set(&inode_info->lower_file_count, 0);
  137. }
  138. mutex_unlock(&inode_info->lower_file_mutex);
  139. return rc;
  140. }
  141. void ecryptfs_put_lower_file(struct inode *inode)
  142. {
  143. struct ecryptfs_inode_info *inode_info;
  144. inode_info = ecryptfs_inode_to_private(inode);
  145. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  146. &inode_info->lower_file_mutex)) {
  147. filemap_write_and_wait(inode->i_mapping);
  148. fput(inode_info->lower_file);
  149. inode_info->lower_file = NULL;
  150. mutex_unlock(&inode_info->lower_file_mutex);
  151. }
  152. }
  153. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  154. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  155. ecryptfs_opt_ecryptfs_key_bytes,
  156. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  157. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  158. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  159. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  160. ecryptfs_opt_check_dev_ruid,
  161. ecryptfs_opt_err };
  162. static const match_table_t tokens = {
  163. {ecryptfs_opt_sig, "sig=%s"},
  164. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  165. {ecryptfs_opt_cipher, "cipher=%s"},
  166. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  167. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  168. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  169. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  170. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  171. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  172. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  173. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  174. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  175. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  176. {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
  177. {ecryptfs_opt_err, NULL}
  178. };
  179. static int ecryptfs_init_global_auth_toks(
  180. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  181. {
  182. struct ecryptfs_global_auth_tok *global_auth_tok;
  183. struct ecryptfs_auth_tok *auth_tok;
  184. int rc = 0;
  185. list_for_each_entry(global_auth_tok,
  186. &mount_crypt_stat->global_auth_tok_list,
  187. mount_crypt_stat_list) {
  188. rc = ecryptfs_keyring_auth_tok_for_sig(
  189. &global_auth_tok->global_auth_tok_key, &auth_tok,
  190. global_auth_tok->sig);
  191. if (rc) {
  192. printk(KERN_ERR "Could not find valid key in user "
  193. "session keyring for sig specified in mount "
  194. "option: [%s]\n", global_auth_tok->sig);
  195. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  196. goto out;
  197. } else {
  198. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  199. up_write(&(global_auth_tok->global_auth_tok_key)->sem);
  200. }
  201. }
  202. out:
  203. return rc;
  204. }
  205. static void ecryptfs_init_mount_crypt_stat(
  206. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  207. {
  208. memset((void *)mount_crypt_stat, 0,
  209. sizeof(struct ecryptfs_mount_crypt_stat));
  210. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  211. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  212. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  213. }
  214. /**
  215. * ecryptfs_parse_options
  216. * @sb: The ecryptfs super block
  217. * @options: The options passed to the kernel
  218. * @check_ruid: set to 1 if device uid should be checked against the ruid
  219. *
  220. * Parse mount options:
  221. * debug=N - ecryptfs_verbosity level for debug output
  222. * sig=XXX - description(signature) of the key to use
  223. *
  224. * Returns the dentry object of the lower-level (lower/interposed)
  225. * directory; We want to mount our stackable file system on top of
  226. * that lower directory.
  227. *
  228. * The signature of the key to use must be the description of a key
  229. * already in the keyring. Mounting will fail if the key can not be
  230. * found.
  231. *
  232. * Returns zero on success; non-zero on error
  233. */
  234. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
  235. uid_t *check_ruid)
  236. {
  237. char *p;
  238. int rc = 0;
  239. int sig_set = 0;
  240. int cipher_name_set = 0;
  241. int fn_cipher_name_set = 0;
  242. int cipher_key_bytes;
  243. int cipher_key_bytes_set = 0;
  244. int fn_cipher_key_bytes;
  245. int fn_cipher_key_bytes_set = 0;
  246. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  247. &sbi->mount_crypt_stat;
  248. substring_t args[MAX_OPT_ARGS];
  249. int token;
  250. char *sig_src;
  251. char *cipher_name_dst;
  252. char *cipher_name_src;
  253. char *fn_cipher_name_dst;
  254. char *fn_cipher_name_src;
  255. char *fnek_dst;
  256. char *fnek_src;
  257. char *cipher_key_bytes_src;
  258. char *fn_cipher_key_bytes_src;
  259. u8 cipher_code;
  260. *check_ruid = 0;
  261. if (!options) {
  262. rc = -EINVAL;
  263. goto out;
  264. }
  265. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  266. while ((p = strsep(&options, ",")) != NULL) {
  267. if (!*p)
  268. continue;
  269. token = match_token(p, tokens, args);
  270. switch (token) {
  271. case ecryptfs_opt_sig:
  272. case ecryptfs_opt_ecryptfs_sig:
  273. sig_src = args[0].from;
  274. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  275. sig_src, 0);
  276. if (rc) {
  277. printk(KERN_ERR "Error attempting to register "
  278. "global sig; rc = [%d]\n", rc);
  279. goto out;
  280. }
  281. sig_set = 1;
  282. break;
  283. case ecryptfs_opt_cipher:
  284. case ecryptfs_opt_ecryptfs_cipher:
  285. cipher_name_src = args[0].from;
  286. cipher_name_dst =
  287. mount_crypt_stat->
  288. global_default_cipher_name;
  289. strncpy(cipher_name_dst, cipher_name_src,
  290. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  291. cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  292. cipher_name_set = 1;
  293. break;
  294. case ecryptfs_opt_ecryptfs_key_bytes:
  295. cipher_key_bytes_src = args[0].from;
  296. cipher_key_bytes =
  297. (int)simple_strtol(cipher_key_bytes_src,
  298. &cipher_key_bytes_src, 0);
  299. mount_crypt_stat->global_default_cipher_key_size =
  300. cipher_key_bytes;
  301. cipher_key_bytes_set = 1;
  302. break;
  303. case ecryptfs_opt_passthrough:
  304. mount_crypt_stat->flags |=
  305. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  306. break;
  307. case ecryptfs_opt_xattr_metadata:
  308. mount_crypt_stat->flags |=
  309. ECRYPTFS_XATTR_METADATA_ENABLED;
  310. break;
  311. case ecryptfs_opt_encrypted_view:
  312. mount_crypt_stat->flags |=
  313. ECRYPTFS_XATTR_METADATA_ENABLED;
  314. mount_crypt_stat->flags |=
  315. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  316. break;
  317. case ecryptfs_opt_fnek_sig:
  318. fnek_src = args[0].from;
  319. fnek_dst =
  320. mount_crypt_stat->global_default_fnek_sig;
  321. strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
  322. mount_crypt_stat->global_default_fnek_sig[
  323. ECRYPTFS_SIG_SIZE_HEX] = '\0';
  324. rc = ecryptfs_add_global_auth_tok(
  325. mount_crypt_stat,
  326. mount_crypt_stat->global_default_fnek_sig,
  327. ECRYPTFS_AUTH_TOK_FNEK);
  328. if (rc) {
  329. printk(KERN_ERR "Error attempting to register "
  330. "global fnek sig [%s]; rc = [%d]\n",
  331. mount_crypt_stat->global_default_fnek_sig,
  332. rc);
  333. goto out;
  334. }
  335. mount_crypt_stat->flags |=
  336. (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
  337. | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
  338. break;
  339. case ecryptfs_opt_fn_cipher:
  340. fn_cipher_name_src = args[0].from;
  341. fn_cipher_name_dst =
  342. mount_crypt_stat->global_default_fn_cipher_name;
  343. strncpy(fn_cipher_name_dst, fn_cipher_name_src,
  344. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  345. mount_crypt_stat->global_default_fn_cipher_name[
  346. ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  347. fn_cipher_name_set = 1;
  348. break;
  349. case ecryptfs_opt_fn_cipher_key_bytes:
  350. fn_cipher_key_bytes_src = args[0].from;
  351. fn_cipher_key_bytes =
  352. (int)simple_strtol(fn_cipher_key_bytes_src,
  353. &fn_cipher_key_bytes_src, 0);
  354. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  355. fn_cipher_key_bytes;
  356. fn_cipher_key_bytes_set = 1;
  357. break;
  358. case ecryptfs_opt_unlink_sigs:
  359. mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
  360. break;
  361. case ecryptfs_opt_mount_auth_tok_only:
  362. mount_crypt_stat->flags |=
  363. ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
  364. break;
  365. case ecryptfs_opt_check_dev_ruid:
  366. *check_ruid = 1;
  367. break;
  368. case ecryptfs_opt_err:
  369. default:
  370. printk(KERN_WARNING
  371. "%s: eCryptfs: unrecognized option [%s]\n",
  372. __func__, p);
  373. }
  374. }
  375. if (!sig_set) {
  376. rc = -EINVAL;
  377. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  378. "auth tok signature as a mount "
  379. "parameter; see the eCryptfs README\n");
  380. goto out;
  381. }
  382. if (!cipher_name_set) {
  383. int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  384. BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  385. strcpy(mount_crypt_stat->global_default_cipher_name,
  386. ECRYPTFS_DEFAULT_CIPHER);
  387. }
  388. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  389. && !fn_cipher_name_set)
  390. strcpy(mount_crypt_stat->global_default_fn_cipher_name,
  391. mount_crypt_stat->global_default_cipher_name);
  392. if (!cipher_key_bytes_set)
  393. mount_crypt_stat->global_default_cipher_key_size = 0;
  394. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  395. && !fn_cipher_key_bytes_set)
  396. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  397. mount_crypt_stat->global_default_cipher_key_size;
  398. cipher_code = ecryptfs_code_for_cipher_string(
  399. mount_crypt_stat->global_default_cipher_name,
  400. mount_crypt_stat->global_default_cipher_key_size);
  401. if (!cipher_code) {
  402. ecryptfs_printk(KERN_ERR,
  403. "eCryptfs doesn't support cipher: %s",
  404. mount_crypt_stat->global_default_cipher_name);
  405. rc = -EINVAL;
  406. goto out;
  407. }
  408. mutex_lock(&key_tfm_list_mutex);
  409. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  410. NULL)) {
  411. rc = ecryptfs_add_new_key_tfm(
  412. NULL, mount_crypt_stat->global_default_cipher_name,
  413. mount_crypt_stat->global_default_cipher_key_size);
  414. if (rc) {
  415. printk(KERN_ERR "Error attempting to initialize "
  416. "cipher with name = [%s] and key size = [%td]; "
  417. "rc = [%d]\n",
  418. mount_crypt_stat->global_default_cipher_name,
  419. mount_crypt_stat->global_default_cipher_key_size,
  420. rc);
  421. rc = -EINVAL;
  422. mutex_unlock(&key_tfm_list_mutex);
  423. goto out;
  424. }
  425. }
  426. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  427. && !ecryptfs_tfm_exists(
  428. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  429. rc = ecryptfs_add_new_key_tfm(
  430. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  431. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  432. if (rc) {
  433. printk(KERN_ERR "Error attempting to initialize "
  434. "cipher with name = [%s] and key size = [%td]; "
  435. "rc = [%d]\n",
  436. mount_crypt_stat->global_default_fn_cipher_name,
  437. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  438. rc);
  439. rc = -EINVAL;
  440. mutex_unlock(&key_tfm_list_mutex);
  441. goto out;
  442. }
  443. }
  444. mutex_unlock(&key_tfm_list_mutex);
  445. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  446. if (rc)
  447. printk(KERN_WARNING "One or more global auth toks could not "
  448. "properly register; rc = [%d]\n", rc);
  449. out:
  450. return rc;
  451. }
  452. struct kmem_cache *ecryptfs_sb_info_cache;
  453. static struct file_system_type ecryptfs_fs_type;
  454. /**
  455. * ecryptfs_get_sb
  456. * @fs_type
  457. * @flags
  458. * @dev_name: The path to mount over
  459. * @raw_data: The options passed into the kernel
  460. */
  461. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  462. const char *dev_name, void *raw_data)
  463. {
  464. struct super_block *s;
  465. struct ecryptfs_sb_info *sbi;
  466. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  467. struct ecryptfs_dentry_info *root_info;
  468. const char *err = "Getting sb failed";
  469. struct inode *inode;
  470. struct path path;
  471. uid_t check_ruid;
  472. int rc;
  473. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  474. if (!sbi) {
  475. rc = -ENOMEM;
  476. goto out;
  477. }
  478. rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
  479. if (rc) {
  480. err = "Error parsing options";
  481. goto out;
  482. }
  483. mount_crypt_stat = &sbi->mount_crypt_stat;
  484. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  485. if (IS_ERR(s)) {
  486. rc = PTR_ERR(s);
  487. goto out;
  488. }
  489. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs");
  490. if (rc)
  491. goto out1;
  492. ecryptfs_set_superblock_private(s, sbi);
  493. s->s_bdi = &sbi->bdi;
  494. /* ->kill_sb() will take care of sbi after that point */
  495. sbi = NULL;
  496. s->s_op = &ecryptfs_sops;
  497. s->s_xattr = ecryptfs_xattr_handlers;
  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. unsigned long flags;
  614. void (*ctor)(void *obj);
  615. } ecryptfs_cache_infos[] = {
  616. {
  617. .cache = &ecryptfs_auth_tok_list_item_cache,
  618. .name = "ecryptfs_auth_tok_list_item",
  619. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  620. },
  621. {
  622. .cache = &ecryptfs_file_info_cache,
  623. .name = "ecryptfs_file_cache",
  624. .size = sizeof(struct ecryptfs_file_info),
  625. },
  626. {
  627. .cache = &ecryptfs_dentry_info_cache,
  628. .name = "ecryptfs_dentry_info_cache",
  629. .size = sizeof(struct ecryptfs_dentry_info),
  630. },
  631. {
  632. .cache = &ecryptfs_inode_info_cache,
  633. .name = "ecryptfs_inode_cache",
  634. .size = sizeof(struct ecryptfs_inode_info),
  635. .flags = SLAB_ACCOUNT,
  636. .ctor = inode_info_init_once,
  637. },
  638. {
  639. .cache = &ecryptfs_sb_info_cache,
  640. .name = "ecryptfs_sb_cache",
  641. .size = sizeof(struct ecryptfs_sb_info),
  642. },
  643. {
  644. .cache = &ecryptfs_header_cache,
  645. .name = "ecryptfs_headers",
  646. .size = PAGE_SIZE,
  647. },
  648. {
  649. .cache = &ecryptfs_xattr_cache,
  650. .name = "ecryptfs_xattr_cache",
  651. .size = PAGE_SIZE,
  652. },
  653. {
  654. .cache = &ecryptfs_key_record_cache,
  655. .name = "ecryptfs_key_record_cache",
  656. .size = sizeof(struct ecryptfs_key_record),
  657. },
  658. {
  659. .cache = &ecryptfs_key_sig_cache,
  660. .name = "ecryptfs_key_sig_cache",
  661. .size = sizeof(struct ecryptfs_key_sig),
  662. },
  663. {
  664. .cache = &ecryptfs_global_auth_tok_cache,
  665. .name = "ecryptfs_global_auth_tok_cache",
  666. .size = sizeof(struct ecryptfs_global_auth_tok),
  667. },
  668. {
  669. .cache = &ecryptfs_key_tfm_cache,
  670. .name = "ecryptfs_key_tfm_cache",
  671. .size = sizeof(struct ecryptfs_key_tfm),
  672. },
  673. };
  674. static void ecryptfs_free_kmem_caches(void)
  675. {
  676. int i;
  677. /*
  678. * Make sure all delayed rcu free inodes are flushed before we
  679. * destroy cache.
  680. */
  681. rcu_barrier();
  682. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  683. struct ecryptfs_cache_info *info;
  684. info = &ecryptfs_cache_infos[i];
  685. kmem_cache_destroy(*(info->cache));
  686. }
  687. }
  688. /**
  689. * ecryptfs_init_kmem_caches
  690. *
  691. * Returns zero on success; non-zero otherwise
  692. */
  693. static int ecryptfs_init_kmem_caches(void)
  694. {
  695. int i;
  696. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  697. struct ecryptfs_cache_info *info;
  698. info = &ecryptfs_cache_infos[i];
  699. *(info->cache) = kmem_cache_create(info->name, info->size, 0,
  700. SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
  701. if (!*(info->cache)) {
  702. ecryptfs_free_kmem_caches();
  703. ecryptfs_printk(KERN_WARNING, "%s: "
  704. "kmem_cache_create failed\n",
  705. info->name);
  706. return -ENOMEM;
  707. }
  708. }
  709. return 0;
  710. }
  711. static struct kobject *ecryptfs_kobj;
  712. static ssize_t version_show(struct kobject *kobj,
  713. struct kobj_attribute *attr, char *buff)
  714. {
  715. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  716. }
  717. static struct kobj_attribute version_attr = __ATTR_RO(version);
  718. static struct attribute *attributes[] = {
  719. &version_attr.attr,
  720. NULL,
  721. };
  722. static struct attribute_group attr_group = {
  723. .attrs = attributes,
  724. };
  725. static int do_sysfs_registration(void)
  726. {
  727. int rc;
  728. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  729. if (!ecryptfs_kobj) {
  730. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  731. rc = -ENOMEM;
  732. goto out;
  733. }
  734. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  735. if (rc) {
  736. printk(KERN_ERR
  737. "Unable to create ecryptfs version attributes\n");
  738. kobject_put(ecryptfs_kobj);
  739. }
  740. out:
  741. return rc;
  742. }
  743. static void do_sysfs_unregistration(void)
  744. {
  745. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  746. kobject_put(ecryptfs_kobj);
  747. }
  748. static int __init ecryptfs_init(void)
  749. {
  750. int rc;
  751. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
  752. rc = -EINVAL;
  753. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  754. "larger than the host's page size, and so "
  755. "eCryptfs cannot run on this system. The "
  756. "default eCryptfs extent size is [%u] bytes; "
  757. "the page size is [%lu] bytes.\n",
  758. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  759. (unsigned long)PAGE_SIZE);
  760. goto out;
  761. }
  762. rc = ecryptfs_init_kmem_caches();
  763. if (rc) {
  764. printk(KERN_ERR
  765. "Failed to allocate one or more kmem_cache objects\n");
  766. goto out;
  767. }
  768. rc = do_sysfs_registration();
  769. if (rc) {
  770. printk(KERN_ERR "sysfs registration failed\n");
  771. goto out_free_kmem_caches;
  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 occurred 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. rc = register_filesystem(&ecryptfs_fs_type);
  793. if (rc) {
  794. printk(KERN_ERR "Failed to register filesystem\n");
  795. goto out_destroy_crypto;
  796. }
  797. if (ecryptfs_verbosity > 0)
  798. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  799. "will be written to the syslog!\n", ecryptfs_verbosity);
  800. goto out;
  801. out_destroy_crypto:
  802. ecryptfs_destroy_crypto();
  803. out_release_messaging:
  804. ecryptfs_release_messaging();
  805. out_destroy_kthread:
  806. ecryptfs_destroy_kthread();
  807. out_do_sysfs_unregistration:
  808. do_sysfs_unregistration();
  809. out_free_kmem_caches:
  810. ecryptfs_free_kmem_caches();
  811. out:
  812. return rc;
  813. }
  814. static void __exit ecryptfs_exit(void)
  815. {
  816. int rc;
  817. rc = ecryptfs_destroy_crypto();
  818. if (rc)
  819. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  820. "rc = [%d]\n", rc);
  821. ecryptfs_release_messaging();
  822. ecryptfs_destroy_kthread();
  823. do_sysfs_unregistration();
  824. unregister_filesystem(&ecryptfs_fs_type);
  825. ecryptfs_free_kmem_caches();
  826. }
  827. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  828. MODULE_DESCRIPTION("eCryptfs");
  829. MODULE_LICENSE("GPL");
  830. module_init(ecryptfs_init)
  831. module_exit(ecryptfs_exit)