main.c 25 KB

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