main.c 25 KB

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