main.c 25 KB

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