main.c 26 KB

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