main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  113. struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  114. int rc;
  115. rc = ecryptfs_privileged_open(lower_file, lower_dentry, lower_mnt,
  116. cred);
  117. if (rc) {
  118. printk(KERN_ERR "Error opening lower file "
  119. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  120. "rc = [%d]\n", lower_dentry, lower_mnt, rc);
  121. (*lower_file) = NULL;
  122. }
  123. return rc;
  124. }
  125. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
  126. {
  127. struct ecryptfs_inode_info *inode_info;
  128. int count, rc = 0;
  129. inode_info = ecryptfs_inode_to_private(inode);
  130. mutex_lock(&inode_info->lower_file_mutex);
  131. count = atomic_inc_return(&inode_info->lower_file_count);
  132. if (WARN_ON_ONCE(count < 1))
  133. rc = -EINVAL;
  134. else if (count == 1) {
  135. rc = ecryptfs_init_lower_file(dentry,
  136. &inode_info->lower_file);
  137. if (rc)
  138. atomic_set(&inode_info->lower_file_count, 0);
  139. }
  140. mutex_unlock(&inode_info->lower_file_mutex);
  141. return rc;
  142. }
  143. void ecryptfs_put_lower_file(struct inode *inode)
  144. {
  145. struct ecryptfs_inode_info *inode_info;
  146. inode_info = ecryptfs_inode_to_private(inode);
  147. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  148. &inode_info->lower_file_mutex)) {
  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_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. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  484. if (IS_ERR(s)) {
  485. rc = PTR_ERR(s);
  486. goto out;
  487. }
  488. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
  489. if (rc)
  490. goto out1;
  491. ecryptfs_set_superblock_private(s, sbi);
  492. s->s_bdi = &sbi->bdi;
  493. /* ->kill_sb() will take care of sbi after that point */
  494. sbi = NULL;
  495. s->s_op = &ecryptfs_sops;
  496. s->s_d_op = &ecryptfs_dops;
  497. err = "Reading sb failed";
  498. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  499. if (rc) {
  500. ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
  501. goto out1;
  502. }
  503. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  504. rc = -EINVAL;
  505. printk(KERN_ERR "Mount on filesystem of type "
  506. "eCryptfs explicitly disallowed due to "
  507. "known incompatibilities\n");
  508. goto out_free;
  509. }
  510. if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
  511. rc = -EPERM;
  512. printk(KERN_ERR "Mount of device (uid: %d) not owned by "
  513. "requested user (uid: %d)\n",
  514. path.dentry->d_inode->i_uid, current_uid());
  515. goto out_free;
  516. }
  517. ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
  518. /**
  519. * Set the POSIX ACL flag based on whether they're enabled in the lower
  520. * mount. Force a read-only eCryptfs mount if the lower mount is ro.
  521. * Allow a ro eCryptfs mount even when the lower mount is rw.
  522. */
  523. s->s_flags = flags & ~MS_POSIXACL;
  524. s->s_flags |= path.dentry->d_sb->s_flags & (MS_RDONLY | MS_POSIXACL);
  525. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  526. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  527. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  528. inode = ecryptfs_get_inode(path.dentry->d_inode, s);
  529. rc = PTR_ERR(inode);
  530. if (IS_ERR(inode))
  531. goto out_free;
  532. s->s_root = d_make_root(inode);
  533. if (!s->s_root) {
  534. rc = -ENOMEM;
  535. goto out_free;
  536. }
  537. rc = -ENOMEM;
  538. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  539. if (!root_info)
  540. goto out_free;
  541. /* ->kill_sb() will take care of root_info */
  542. ecryptfs_set_dentry_private(s->s_root, root_info);
  543. ecryptfs_set_dentry_lower(s->s_root, path.dentry);
  544. ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
  545. s->s_flags |= MS_ACTIVE;
  546. return dget(s->s_root);
  547. out_free:
  548. path_put(&path);
  549. out1:
  550. deactivate_locked_super(s);
  551. out:
  552. if (sbi) {
  553. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  554. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  555. }
  556. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  557. return ERR_PTR(rc);
  558. }
  559. /**
  560. * ecryptfs_kill_block_super
  561. * @sb: The ecryptfs super block
  562. *
  563. * Used to bring the superblock down and free the private data.
  564. */
  565. static void ecryptfs_kill_block_super(struct super_block *sb)
  566. {
  567. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  568. kill_anon_super(sb);
  569. if (!sb_info)
  570. return;
  571. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  572. bdi_destroy(&sb_info->bdi);
  573. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  574. }
  575. static struct file_system_type ecryptfs_fs_type = {
  576. .owner = THIS_MODULE,
  577. .name = "ecryptfs",
  578. .mount = ecryptfs_mount,
  579. .kill_sb = ecryptfs_kill_block_super,
  580. .fs_flags = 0
  581. };
  582. /**
  583. * inode_info_init_once
  584. *
  585. * Initializes the ecryptfs_inode_info_cache when it is created
  586. */
  587. static void
  588. inode_info_init_once(void *vptr)
  589. {
  590. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  591. inode_init_once(&ei->vfs_inode);
  592. }
  593. static struct ecryptfs_cache_info {
  594. struct kmem_cache **cache;
  595. const char *name;
  596. size_t size;
  597. void (*ctor)(void *obj);
  598. } ecryptfs_cache_infos[] = {
  599. {
  600. .cache = &ecryptfs_auth_tok_list_item_cache,
  601. .name = "ecryptfs_auth_tok_list_item",
  602. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  603. },
  604. {
  605. .cache = &ecryptfs_file_info_cache,
  606. .name = "ecryptfs_file_cache",
  607. .size = sizeof(struct ecryptfs_file_info),
  608. },
  609. {
  610. .cache = &ecryptfs_dentry_info_cache,
  611. .name = "ecryptfs_dentry_info_cache",
  612. .size = sizeof(struct ecryptfs_dentry_info),
  613. },
  614. {
  615. .cache = &ecryptfs_inode_info_cache,
  616. .name = "ecryptfs_inode_cache",
  617. .size = sizeof(struct ecryptfs_inode_info),
  618. .ctor = inode_info_init_once,
  619. },
  620. {
  621. .cache = &ecryptfs_sb_info_cache,
  622. .name = "ecryptfs_sb_cache",
  623. .size = sizeof(struct ecryptfs_sb_info),
  624. },
  625. {
  626. .cache = &ecryptfs_header_cache,
  627. .name = "ecryptfs_headers",
  628. .size = PAGE_CACHE_SIZE,
  629. },
  630. {
  631. .cache = &ecryptfs_xattr_cache,
  632. .name = "ecryptfs_xattr_cache",
  633. .size = PAGE_CACHE_SIZE,
  634. },
  635. {
  636. .cache = &ecryptfs_key_record_cache,
  637. .name = "ecryptfs_key_record_cache",
  638. .size = sizeof(struct ecryptfs_key_record),
  639. },
  640. {
  641. .cache = &ecryptfs_key_sig_cache,
  642. .name = "ecryptfs_key_sig_cache",
  643. .size = sizeof(struct ecryptfs_key_sig),
  644. },
  645. {
  646. .cache = &ecryptfs_global_auth_tok_cache,
  647. .name = "ecryptfs_global_auth_tok_cache",
  648. .size = sizeof(struct ecryptfs_global_auth_tok),
  649. },
  650. {
  651. .cache = &ecryptfs_key_tfm_cache,
  652. .name = "ecryptfs_key_tfm_cache",
  653. .size = sizeof(struct ecryptfs_key_tfm),
  654. },
  655. };
  656. static void ecryptfs_free_kmem_caches(void)
  657. {
  658. int i;
  659. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  660. struct ecryptfs_cache_info *info;
  661. info = &ecryptfs_cache_infos[i];
  662. if (*(info->cache))
  663. kmem_cache_destroy(*(info->cache));
  664. }
  665. }
  666. /**
  667. * ecryptfs_init_kmem_caches
  668. *
  669. * Returns zero on success; non-zero otherwise
  670. */
  671. static int ecryptfs_init_kmem_caches(void)
  672. {
  673. int i;
  674. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  675. struct ecryptfs_cache_info *info;
  676. info = &ecryptfs_cache_infos[i];
  677. *(info->cache) = kmem_cache_create(info->name, info->size,
  678. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  679. if (!*(info->cache)) {
  680. ecryptfs_free_kmem_caches();
  681. ecryptfs_printk(KERN_WARNING, "%s: "
  682. "kmem_cache_create failed\n",
  683. info->name);
  684. return -ENOMEM;
  685. }
  686. }
  687. return 0;
  688. }
  689. static struct kobject *ecryptfs_kobj;
  690. static ssize_t version_show(struct kobject *kobj,
  691. struct kobj_attribute *attr, char *buff)
  692. {
  693. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  694. }
  695. static struct kobj_attribute version_attr = __ATTR_RO(version);
  696. static struct attribute *attributes[] = {
  697. &version_attr.attr,
  698. NULL,
  699. };
  700. static struct attribute_group attr_group = {
  701. .attrs = attributes,
  702. };
  703. static int do_sysfs_registration(void)
  704. {
  705. int rc;
  706. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  707. if (!ecryptfs_kobj) {
  708. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  709. rc = -ENOMEM;
  710. goto out;
  711. }
  712. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  713. if (rc) {
  714. printk(KERN_ERR
  715. "Unable to create ecryptfs version attributes\n");
  716. kobject_put(ecryptfs_kobj);
  717. }
  718. out:
  719. return rc;
  720. }
  721. static void do_sysfs_unregistration(void)
  722. {
  723. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  724. kobject_put(ecryptfs_kobj);
  725. }
  726. static int __init ecryptfs_init(void)
  727. {
  728. int rc;
  729. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  730. rc = -EINVAL;
  731. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  732. "larger than the host's page size, and so "
  733. "eCryptfs cannot run on this system. The "
  734. "default eCryptfs extent size is [%u] bytes; "
  735. "the page size is [%lu] bytes.\n",
  736. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  737. (unsigned long)PAGE_CACHE_SIZE);
  738. goto out;
  739. }
  740. rc = ecryptfs_init_kmem_caches();
  741. if (rc) {
  742. printk(KERN_ERR
  743. "Failed to allocate one or more kmem_cache objects\n");
  744. goto out;
  745. }
  746. rc = do_sysfs_registration();
  747. if (rc) {
  748. printk(KERN_ERR "sysfs registration failed\n");
  749. goto out_free_kmem_caches;
  750. }
  751. rc = ecryptfs_init_kthread();
  752. if (rc) {
  753. printk(KERN_ERR "%s: kthread initialization failed; "
  754. "rc = [%d]\n", __func__, rc);
  755. goto out_do_sysfs_unregistration;
  756. }
  757. rc = ecryptfs_init_messaging();
  758. if (rc) {
  759. printk(KERN_ERR "Failure occurred while attempting to "
  760. "initialize the communications channel to "
  761. "ecryptfsd\n");
  762. goto out_destroy_kthread;
  763. }
  764. rc = ecryptfs_init_crypto();
  765. if (rc) {
  766. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  767. "rc = [%d]\n", rc);
  768. goto out_release_messaging;
  769. }
  770. rc = register_filesystem(&ecryptfs_fs_type);
  771. if (rc) {
  772. printk(KERN_ERR "Failed to register filesystem\n");
  773. goto out_destroy_crypto;
  774. }
  775. if (ecryptfs_verbosity > 0)
  776. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  777. "will be written to the syslog!\n", ecryptfs_verbosity);
  778. goto out;
  779. out_destroy_crypto:
  780. ecryptfs_destroy_crypto();
  781. out_release_messaging:
  782. ecryptfs_release_messaging();
  783. out_destroy_kthread:
  784. ecryptfs_destroy_kthread();
  785. out_do_sysfs_unregistration:
  786. do_sysfs_unregistration();
  787. out_free_kmem_caches:
  788. ecryptfs_free_kmem_caches();
  789. out:
  790. return rc;
  791. }
  792. static void __exit ecryptfs_exit(void)
  793. {
  794. int rc;
  795. rc = ecryptfs_destroy_crypto();
  796. if (rc)
  797. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  798. "rc = [%d]\n", rc);
  799. ecryptfs_release_messaging();
  800. ecryptfs_destroy_kthread();
  801. do_sysfs_unregistration();
  802. unregister_filesystem(&ecryptfs_fs_type);
  803. ecryptfs_free_kmem_caches();
  804. }
  805. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  806. MODULE_DESCRIPTION("eCryptfs");
  807. MODULE_LICENSE("GPL");
  808. module_init(ecryptfs_init)
  809. module_exit(ecryptfs_exit)