main.c 26 KB

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