VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
1a59d1b8e05ea (Thomas Gleixner     2019-05-27 08:55:05 +0200   1) // SPDX-License-Identifier: GPL-2.0-or-later
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100   2) /*
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700   3)  * eCryptfs: Linux filesystem encryption layer
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700   4)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700   5)  * Copyright (C) 1997-2003 Erez Zadok
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700   6)  * Copyright (C) 2001-2003 Stony Brook University
dd2a3b7ad98f8 (Michael Halcrow     2007-02-12 00:53:46 -0800   7)  * Copyright (C) 2004-2007 International Business Machines Corp.
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700   8)  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700   9)  *              Michael C. Thompson <mcthomps@us.ibm.com>
f8e48a8408f5e (Tyler Hicks         2020-02-13 21:25:54 +0000  10)  *              Tyler Hicks <code@tyhicks.com>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  11)  */
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  12) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  13) #include <linux/dcache.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  14) #include <linux/file.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  15) #include <linux/module.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  16) #include <linux/namei.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  17) #include <linux/skbuff.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  18) #include <linux/mount.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  19) #include <linux/pagemap.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  20) #include <linux/key.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  21) #include <linux/parser.h>
0cc72dc7f0501 (Josef "Jeff" Sipek  2006-12-08 02:36:31 -0800  22) #include <linux/fs_stack.h>
5a0e3ad6af866 (Tejun Heo           2010-03-24 17:04:11 +0900  23) #include <linux/slab.h>
070baa51286e5 (Roberto Sassu       2010-11-03 11:11:22 +0100  24) #include <linux/magic.h>
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  25) #include "ecryptfs_kernel.h"
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  26) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100  27) /*
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  28)  * Module parameter that defines the ecryptfs_verbosity level.
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  29)  */
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  30) int ecryptfs_verbosity = 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  31) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  32) module_param(ecryptfs_verbosity, int, 0);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  33) MODULE_PARM_DESC(ecryptfs_verbosity,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  34) 		 "Initial verbosity level (0 or 1; defaults to "
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  35) 		 "0, which is Quiet)");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  36) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100  37) /*
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700  38)  * Module parameter that defines the number of message buffer elements
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  39)  */
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  40) unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  41) 
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  42) module_param(ecryptfs_message_buf_len, uint, 0);
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  43) MODULE_PARM_DESC(ecryptfs_message_buf_len,
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  44) 		 "Number of message buffer elements");
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  45) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100  46) /*
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  47)  * Module parameter that defines the maximum guaranteed amount of time to wait
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700  48)  * for a response from ecryptfsd.  The actual sleep time will be, more than
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  49)  * likely, a small amount greater than this specified value, but only less if
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700  50)  * the message successfully arrives.
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  51)  */
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  52) signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  53) 
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  54) module_param(ecryptfs_message_wait_timeout, long, 0);
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  55) MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  56) 		 "Maximum number of seconds that an operation will "
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  57) 		 "sleep while waiting for a message response from "
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  58) 		 "userspace");
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  59) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100  60) /*
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  61)  * Module parameter that is an estimate of the maximum number of users
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  62)  * that will be concurrently using eCryptfs. Set this to the right
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  63)  * value to balance performance and memory use.
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  64)  */
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  65) unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  66) 
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  67) module_param(ecryptfs_number_of_users, uint, 0);
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  68) MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  69) 		 "concurrent users of eCryptfs");
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800  70) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  71) void __ecryptfs_printk(const char *fmt, ...)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  72) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  73) 	va_list args;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  74) 	va_start(args, fmt);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  75) 	if (fmt[1] == '7') { /* KERN_DEBUG */
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  76) 		if (ecryptfs_verbosity >= 1)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  77) 			vprintk(fmt, args);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  78) 	} else
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  79) 		vprintk(fmt, args);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  80) 	va_end(args);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  81) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700  82) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100  83) /*
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500  84)  * ecryptfs_init_lower_file
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  85)  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  86)  *                   the lower dentry and the lower mount set
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  87)  *
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  88)  * eCryptfs only ever keeps a single open file for every lower
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  89)  * inode. All I/O operations to the lower inode occur through that
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  90)  * file. When the first eCryptfs dentry that interposes with the first
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  91)  * lower dentry for that inode is created, this function creates the
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500  92)  * lower file struct and associates it with the eCryptfs
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500  93)  * inode. When all eCryptfs files associated with the inode are released, the
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500  94)  * file is closed.
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  95)  *
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500  96)  * The lower file will be opened with read/write permissions, if
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  97)  * possible. Otherwise, it is opened read-only.
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700  98)  *
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500  99)  * This function does nothing if a lower file is already
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 100)  * associated with the eCryptfs inode.
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 101)  *
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 102)  * Returns zero on success; non-zero otherwise
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 103)  */
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 104) static int ecryptfs_init_lower_file(struct dentry *dentry,
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 105) 				    struct file **lower_file)
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 106) {
745ca2475a6ac (David Howells       2008-11-14 10:39:22 +1100 107) 	const struct cred *cred = current_cred();
cc18ec3c8f5dd (Matthew Wilcox      2013-06-15 07:55:59 -0400 108) 	struct path *path = ecryptfs_dentry_to_lower_path(dentry);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 109) 	int rc;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 110) 
cc18ec3c8f5dd (Matthew Wilcox      2013-06-15 07:55:59 -0400 111) 	rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 112) 				      cred);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 113) 	if (rc) {
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 114) 		printk(KERN_ERR "Error opening lower file "
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 115) 		       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
cc18ec3c8f5dd (Matthew Wilcox      2013-06-15 07:55:59 -0400 116) 		       "rc = [%d]\n", path->dentry, path->mnt, rc);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 117) 		(*lower_file) = NULL;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 118) 	}
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 119) 	return rc;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 120) }
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 121) 
3b06b3ebf4417 (Tyler Hicks         2011-05-24 03:49:02 -0500 122) int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 123) {
3b06b3ebf4417 (Tyler Hicks         2011-05-24 03:49:02 -0500 124) 	struct ecryptfs_inode_info *inode_info;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 125) 	int count, rc = 0;
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 126) 
3b06b3ebf4417 (Tyler Hicks         2011-05-24 03:49:02 -0500 127) 	inode_info = ecryptfs_inode_to_private(inode);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 128) 	mutex_lock(&inode_info->lower_file_mutex);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 129) 	count = atomic_inc_return(&inode_info->lower_file_count);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 130) 	if (WARN_ON_ONCE(count < 1))
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 131) 		rc = -EINVAL;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 132) 	else if (count == 1) {
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 133) 		rc = ecryptfs_init_lower_file(dentry,
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 134) 					      &inode_info->lower_file);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 135) 		if (rc)
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 136) 			atomic_set(&inode_info->lower_file_count, 0);
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 137) 	}
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 138) 	mutex_unlock(&inode_info->lower_file_mutex);
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 139) 	return rc;
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 140) }
4981e081cfe2c (Michael Halcrow     2007-10-16 01:28:09 -0700 141) 
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 142) void ecryptfs_put_lower_file(struct inode *inode)
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 143) {
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 144) 	struct ecryptfs_inode_info *inode_info;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 145) 
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 146) 	inode_info = ecryptfs_inode_to_private(inode);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 147) 	if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 148) 				      &inode_info->lower_file_mutex)) {
7149f2558d5b5 (Tyler Hicks         2012-09-12 18:02:46 -0700 149) 		filemap_write_and_wait(inode->i_mapping);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 150) 		fput(inode_info->lower_file);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 151) 		inode_info->lower_file = NULL;
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 152) 		mutex_unlock(&inode_info->lower_file_mutex);
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 153) 	}
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 154) }
332ab16f830f5 (Tyler Hicks         2011-04-14 15:35:11 -0500 155) 
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 156) enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 157)        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 158)        ecryptfs_opt_ecryptfs_key_bytes,
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 159)        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 160)        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 161)        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 162)        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 163)        ecryptfs_opt_check_dev_ruid,
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 164)        ecryptfs_opt_err };
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 165) 
a447c0932445f (Steven Whitehouse   2008-10-13 10:46:57 +0100 166) static const match_table_t tokens = {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 167) 	{ecryptfs_opt_sig, "sig=%s"},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 168) 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 169) 	{ecryptfs_opt_cipher, "cipher=%s"},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 170) 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 171) 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 172) 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 173) 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 174) 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 175) 	{ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 176) 	{ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 177) 	{ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
e77cc8d243f9f (Tyler Hicks         2009-04-22 04:08:46 -0500 178) 	{ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 179) 	{ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 180) 	{ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 181) 	{ecryptfs_opt_err, NULL}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 182) };
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 183) 
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 184) static int ecryptfs_init_global_auth_toks(
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 185) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 186) {
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 187) 	struct ecryptfs_global_auth_tok *global_auth_tok;
0e1fc5ef470cc (Roberto Sassu       2011-03-21 16:00:53 +0100 188) 	struct ecryptfs_auth_tok *auth_tok;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 189) 	int rc = 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 190) 
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 191) 	list_for_each_entry(global_auth_tok,
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 192) 			    &mount_crypt_stat->global_auth_tok_list,
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 193) 			    mount_crypt_stat_list) {
5dda6992a3138 (Michael Halcrow     2007-10-16 01:28:06 -0700 194) 		rc = ecryptfs_keyring_auth_tok_for_sig(
0e1fc5ef470cc (Roberto Sassu       2011-03-21 16:00:53 +0100 195) 			&global_auth_tok->global_auth_tok_key, &auth_tok,
5dda6992a3138 (Michael Halcrow     2007-10-16 01:28:06 -0700 196) 			global_auth_tok->sig);
5dda6992a3138 (Michael Halcrow     2007-10-16 01:28:06 -0700 197) 		if (rc) {
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 198) 			printk(KERN_ERR "Could not find valid key in user "
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 199) 			       "session keyring for sig specified in mount "
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 200) 			       "option: [%s]\n", global_auth_tok->sig);
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 201) 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
982363c97f8ca (Eric Sandeen        2008-07-23 21:30:04 -0700 202) 			goto out;
b5695d04634fa (Roberto Sassu       2011-03-21 16:00:55 +0100 203) 		} else {
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 204) 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
b5695d04634fa (Roberto Sassu       2011-03-21 16:00:55 +0100 205) 			up_write(&(global_auth_tok->global_auth_tok_key)->sem);
b5695d04634fa (Roberto Sassu       2011-03-21 16:00:55 +0100 206) 		}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 207) 	}
982363c97f8ca (Eric Sandeen        2008-07-23 21:30:04 -0700 208) out:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 209) 	return rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 210) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 211) 
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 212) static void ecryptfs_init_mount_crypt_stat(
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 213) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 214) {
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 215) 	memset((void *)mount_crypt_stat, 0,
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 216) 	       sizeof(struct ecryptfs_mount_crypt_stat));
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 217) 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 218) 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 219) 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 220) }
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 221) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 222) /**
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 223)  * ecryptfs_parse_options
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100 224)  * @sbi: The ecryptfs super block
25985edcedea6 (Lucas De Marchi     2011-03-30 22:57:33 -0300 225)  * @options: The options passed to the kernel
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 226)  * @check_ruid: set to 1 if device uid should be checked against the ruid
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 227)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 228)  * Parse mount options:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 229)  * debug=N 	   - ecryptfs_verbosity level for debug output
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 230)  * sig=XXX	   - description(signature) of the key to use
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 231)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 232)  * Returns the dentry object of the lower-level (lower/interposed)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 233)  * directory; We want to mount our stackable file system on top of
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 234)  * that lower directory.
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 235)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 236)  * The signature of the key to use must be the description of a key
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 237)  * already in the keyring. Mounting will fail if the key can not be
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 238)  * found.
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 239)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 240)  * Returns zero on success; non-zero on error
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 241)  */
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 242) static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 243) 				  uid_t *check_ruid)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 244) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 245) 	char *p;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 246) 	int rc = 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 247) 	int sig_set = 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 248) 	int cipher_name_set = 0;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 249) 	int fn_cipher_name_set = 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 250) 	int cipher_key_bytes;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 251) 	int cipher_key_bytes_set = 0;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 252) 	int fn_cipher_key_bytes;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 253) 	int fn_cipher_key_bytes_set = 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 254) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 255) 		&sbi->mount_crypt_stat;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 256) 	substring_t args[MAX_OPT_ARGS];
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 257) 	int token;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 258) 	char *sig_src;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 259) 	char *cipher_name_dst;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 260) 	char *cipher_name_src;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 261) 	char *fn_cipher_name_dst;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 262) 	char *fn_cipher_name_src;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 263) 	char *fnek_dst;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 264) 	char *fnek_src;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 265) 	char *cipher_key_bytes_src;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 266) 	char *fn_cipher_key_bytes_src;
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 267) 	u8 cipher_code;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 268) 
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 269) 	*check_ruid = 0;
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 270) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 271) 	if (!options) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 272) 		rc = -EINVAL;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 273) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 274) 	}
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 275) 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 276) 	while ((p = strsep(&options, ",")) != NULL) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 277) 		if (!*p)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 278) 			continue;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 279) 		token = match_token(p, tokens, args);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 280) 		switch (token) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 281) 		case ecryptfs_opt_sig:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 282) 		case ecryptfs_opt_ecryptfs_sig:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 283) 			sig_src = args[0].from;
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 284) 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
84814d642a4f1 (Tyler Hicks         2009-03-13 13:51:59 -0700 285) 							  sig_src, 0);
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 286) 			if (rc) {
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 287) 				printk(KERN_ERR "Error attempting to register "
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 288) 				       "global sig; rc = [%d]\n", rc);
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 289) 				goto out;
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 290) 			}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 291) 			sig_set = 1;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 292) 			break;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 293) 		case ecryptfs_opt_cipher:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 294) 		case ecryptfs_opt_ecryptfs_cipher:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 295) 			cipher_name_src = args[0].from;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 296) 			cipher_name_dst =
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 297) 				mount_crypt_stat->
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 298) 				global_default_cipher_name;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 299) 			strncpy(cipher_name_dst, cipher_name_src,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 300) 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 301) 			cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 302) 			cipher_name_set = 1;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 303) 			break;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 304) 		case ecryptfs_opt_ecryptfs_key_bytes:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 305) 			cipher_key_bytes_src = args[0].from;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 306) 			cipher_key_bytes =
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 307) 				(int)simple_strtol(cipher_key_bytes_src,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 308) 						   &cipher_key_bytes_src, 0);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 309) 			mount_crypt_stat->global_default_cipher_key_size =
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 310) 				cipher_key_bytes;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 311) 			cipher_key_bytes_set = 1;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 312) 			break;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 313) 		case ecryptfs_opt_passthrough:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 314) 			mount_crypt_stat->flags |=
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 315) 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 316) 			break;
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 317) 		case ecryptfs_opt_xattr_metadata:
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 318) 			mount_crypt_stat->flags |=
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 319) 				ECRYPTFS_XATTR_METADATA_ENABLED;
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 320) 			break;
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 321) 		case ecryptfs_opt_encrypted_view:
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 322) 			mount_crypt_stat->flags |=
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 323) 				ECRYPTFS_XATTR_METADATA_ENABLED;
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 324) 			mount_crypt_stat->flags |=
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 325) 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
17398957aa0a0 (Michael Halcrow     2007-02-12 00:53:45 -0800 326) 			break;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 327) 		case ecryptfs_opt_fnek_sig:
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 328) 			fnek_src = args[0].from;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 329) 			fnek_dst =
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 330) 				mount_crypt_stat->global_default_fnek_sig;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 331) 			strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 332) 			mount_crypt_stat->global_default_fnek_sig[
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 333) 				ECRYPTFS_SIG_SIZE_HEX] = '\0';
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 334) 			rc = ecryptfs_add_global_auth_tok(
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 335) 				mount_crypt_stat,
84814d642a4f1 (Tyler Hicks         2009-03-13 13:51:59 -0700 336) 				mount_crypt_stat->global_default_fnek_sig,
84814d642a4f1 (Tyler Hicks         2009-03-13 13:51:59 -0700 337) 				ECRYPTFS_AUTH_TOK_FNEK);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 338) 			if (rc) {
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 339) 				printk(KERN_ERR "Error attempting to register "
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 340) 				       "global fnek sig [%s]; rc = [%d]\n",
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 341) 				       mount_crypt_stat->global_default_fnek_sig,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 342) 				       rc);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 343) 				goto out;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 344) 			}
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 345) 			mount_crypt_stat->flags |=
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 346) 				(ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 347) 				 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 348) 			break;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 349) 		case ecryptfs_opt_fn_cipher:
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 350) 			fn_cipher_name_src = args[0].from;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 351) 			fn_cipher_name_dst =
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 352) 				mount_crypt_stat->global_default_fn_cipher_name;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 353) 			strncpy(fn_cipher_name_dst, fn_cipher_name_src,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 354) 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 355) 			mount_crypt_stat->global_default_fn_cipher_name[
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 356) 				ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 357) 			fn_cipher_name_set = 1;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 358) 			break;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 359) 		case ecryptfs_opt_fn_cipher_key_bytes:
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 360) 			fn_cipher_key_bytes_src = args[0].from;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 361) 			fn_cipher_key_bytes =
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 362) 				(int)simple_strtol(fn_cipher_key_bytes_src,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 363) 						   &fn_cipher_key_bytes_src, 0);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 364) 			mount_crypt_stat->global_default_fn_cipher_key_bytes =
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 365) 				fn_cipher_key_bytes;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 366) 			fn_cipher_key_bytes_set = 1;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 367) 			break;
e77cc8d243f9f (Tyler Hicks         2009-04-22 04:08:46 -0500 368) 		case ecryptfs_opt_unlink_sigs:
e77cc8d243f9f (Tyler Hicks         2009-04-22 04:08:46 -0500 369) 			mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
e77cc8d243f9f (Tyler Hicks         2009-04-22 04:08:46 -0500 370) 			break;
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 371) 		case ecryptfs_opt_mount_auth_tok_only:
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 372) 			mount_crypt_stat->flags |=
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 373) 				ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
f16feb5119a87 (Roberto Sassu       2010-10-06 18:31:32 +0200 374) 			break;
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 375) 		case ecryptfs_opt_check_dev_ruid:
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 376) 			*check_ruid = 1;
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 377) 			break;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 378) 		case ecryptfs_opt_err:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 379) 		default:
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 380) 			printk(KERN_WARNING
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 381) 			       "%s: eCryptfs: unrecognized option [%s]\n",
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 382) 			       __func__, p);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 383) 		}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 384) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 385) 	if (!sig_set) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 386) 		rc = -EINVAL;
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 387) 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 388) 				"auth tok signature as a mount "
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 389) 				"parameter; see the eCryptfs README\n");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 390) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 391) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 392) 	if (!cipher_name_set) {
8f2368095e250 (Miklos Szeredi      2008-07-23 21:30:05 -0700 393) 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
8f2368095e250 (Miklos Szeredi      2008-07-23 21:30:05 -0700 394) 
2a559a8bdeae8 (Colin Ian King      2015-02-23 11:34:10 +0000 395) 		BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
8f2368095e250 (Miklos Szeredi      2008-07-23 21:30:05 -0700 396) 		strcpy(mount_crypt_stat->global_default_cipher_name,
8f2368095e250 (Miklos Szeredi      2008-07-23 21:30:05 -0700 397) 		       ECRYPTFS_DEFAULT_CIPHER);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 398) 	}
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 399) 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 400) 	    && !fn_cipher_name_set)
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 401) 		strcpy(mount_crypt_stat->global_default_fn_cipher_name,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 402) 		       mount_crypt_stat->global_default_cipher_name);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 403) 	if (!cipher_key_bytes_set)
e5d9cbde6ce00 (Michael Halcrow     2006-10-30 22:07:16 -0800 404) 		mount_crypt_stat->global_default_cipher_key_size = 0;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 405) 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 406) 	    && !fn_cipher_key_bytes_set)
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 407) 		mount_crypt_stat->global_default_fn_cipher_key_bytes =
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 408) 			mount_crypt_stat->global_default_cipher_key_size;
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 409) 
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 410) 	cipher_code = ecryptfs_code_for_cipher_string(
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 411) 		mount_crypt_stat->global_default_cipher_name,
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 412) 		mount_crypt_stat->global_default_cipher_key_size);
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 413) 	if (!cipher_code) {
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 414) 		ecryptfs_printk(KERN_ERR,
0996b67df6c13 (Colin Ian King      2016-09-27 05:18:02 -0700 415) 				"eCryptfs doesn't support cipher: %s\n",
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 416) 				mount_crypt_stat->global_default_cipher_name);
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 417) 		rc = -EINVAL;
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 418) 		goto out;
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 419) 	}
5f5b331d5c212 (Tim Sally           2012-07-12 19:10:24 -0400 420) 
af440f52927e4 (Eric Sandeen        2008-02-06 01:38:37 -0800 421) 	mutex_lock(&key_tfm_list_mutex);
af440f52927e4 (Eric Sandeen        2008-02-06 01:38:37 -0800 422) 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 423) 				 NULL)) {
af440f52927e4 (Eric Sandeen        2008-02-06 01:38:37 -0800 424) 		rc = ecryptfs_add_new_key_tfm(
af440f52927e4 (Eric Sandeen        2008-02-06 01:38:37 -0800 425) 			NULL, mount_crypt_stat->global_default_cipher_name,
af440f52927e4 (Eric Sandeen        2008-02-06 01:38:37 -0800 426) 			mount_crypt_stat->global_default_cipher_key_size);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 427) 		if (rc) {
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 428) 			printk(KERN_ERR "Error attempting to initialize "
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 429) 			       "cipher with name = [%s] and key size = [%td]; "
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 430) 			       "rc = [%d]\n",
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 431) 			       mount_crypt_stat->global_default_cipher_name,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 432) 			       mount_crypt_stat->global_default_cipher_key_size,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 433) 			       rc);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 434) 			rc = -EINVAL;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 435) 			mutex_unlock(&key_tfm_list_mutex);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 436) 			goto out;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 437) 		}
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 438) 	}
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 439) 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 440) 	    && !ecryptfs_tfm_exists(
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 441) 		    mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 442) 		rc = ecryptfs_add_new_key_tfm(
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 443) 			NULL, mount_crypt_stat->global_default_fn_cipher_name,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 444) 			mount_crypt_stat->global_default_fn_cipher_key_bytes);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 445) 		if (rc) {
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 446) 			printk(KERN_ERR "Error attempting to initialize "
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 447) 			       "cipher with name = [%s] and key size = [%td]; "
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 448) 			       "rc = [%d]\n",
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 449) 			       mount_crypt_stat->global_default_fn_cipher_name,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 450) 			       mount_crypt_stat->global_default_fn_cipher_key_bytes,
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 451) 			       rc);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 452) 			rc = -EINVAL;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 453) 			mutex_unlock(&key_tfm_list_mutex);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 454) 			goto out;
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 455) 		}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 456) 	}
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 457) 	mutex_unlock(&key_tfm_list_mutex);
5dda6992a3138 (Michael Halcrow     2007-10-16 01:28:06 -0700 458) 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
87c94c4df0149 (Michael Halcrow     2009-01-06 14:42:01 -0800 459) 	if (rc)
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 460) 		printk(KERN_WARNING "One or more global auth toks could not "
f4aad16adfb8f (Michael Halcrow     2007-10-16 01:27:53 -0700 461) 		       "properly register; rc = [%d]\n", rc);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 462) out:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 463) 	return rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 464) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 465) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 466) struct kmem_cache *ecryptfs_sb_info_cache;
4403158ba295c (Al Viro             2010-05-17 00:59:46 -0400 467) static struct file_system_type ecryptfs_fs_type;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 468) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100 469) /*
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100 470)  * ecryptfs_mount
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100 471)  * @fs_type: The filesystem type that the superblock should belong to
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100 472)  * @flags: The flags associated with the mount
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 473)  * @dev_name: The path to mount over
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 474)  * @raw_data: The options passed into the kernel
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 475)  */
4d143beb0429e (Al Viro             2010-07-26 13:33:36 +0400 476) static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
4d143beb0429e (Al Viro             2010-07-26 13:33:36 +0400 477) 			const char *dev_name, void *raw_data)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 478) {
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 479) 	struct super_block *s;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 480) 	struct ecryptfs_sb_info *sbi;
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 481) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 482) 	struct ecryptfs_dentry_info *root_info;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 483) 	const char *err = "Getting sb failed";
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 484) 	struct inode *inode;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 485) 	struct path path;
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 486) 	uid_t check_ruid;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 487) 	int rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 488) 
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 489) 	sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 490) 	if (!sbi) {
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 491) 		rc = -ENOMEM;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 492) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 493) 	}
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 494) 
9046625511ad8 (Jeffrey Mitchell    2021-02-26 15:00:23 -0600 495) 	if (!dev_name) {
9046625511ad8 (Jeffrey Mitchell    2021-02-26 15:00:23 -0600 496) 		rc = -EINVAL;
9046625511ad8 (Jeffrey Mitchell    2021-02-26 15:00:23 -0600 497) 		err = "Device name cannot be null";
9046625511ad8 (Jeffrey Mitchell    2021-02-26 15:00:23 -0600 498) 		goto out;
9046625511ad8 (Jeffrey Mitchell    2021-02-26 15:00:23 -0600 499) 	}
9046625511ad8 (Jeffrey Mitchell    2021-02-26 15:00:23 -0600 500) 
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 501) 	rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 502) 	if (rc) {
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 503) 		err = "Error parsing options";
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 504) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 505) 	}
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 506) 	mount_crypt_stat = &sbi->mount_crypt_stat;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 507) 
9249e17fe094d (David Howells       2012-06-25 12:55:37 +0100 508) 	s = sget(fs_type, NULL, set_anon_super, flags, NULL);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 509) 	if (IS_ERR(s)) {
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 510) 		rc = PTR_ERR(s);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 511) 		goto out;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 512) 	}
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 513) 
e836818bd9ec5 (Jan Kara            2017-04-12 12:24:35 +0200 514) 	rc = super_setup_bdi(s);
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 515) 	if (rc)
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 516) 		goto out1;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 517) 
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 518) 	ecryptfs_set_superblock_private(s, sbi);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 519) 
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 520) 	/* ->kill_sb() will take care of sbi after that point */
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 521) 	sbi = NULL;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 522) 	s->s_op = &ecryptfs_sops;
4b899da50dcf1 (Andreas Gruenbacher 2016-09-29 17:48:36 +0200 523) 	s->s_xattr = ecryptfs_xattr_handlers;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 524) 	s->s_d_op = &ecryptfs_dops;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 525) 
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 526) 	err = "Reading sb failed";
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 527) 	rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 528) 	if (rc) {
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 529) 		ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 530) 		goto out1;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 531) 	}
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 532) 	if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 533) 		rc = -EINVAL;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 534) 		printk(KERN_ERR "Mount on filesystem of type "
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 535) 			"eCryptfs explicitly disallowed due to "
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 536) 			"known incompatibilities\n");
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 537) 		goto out_free;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 538) 	}
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 539) 
0f16ff0f545d5 (Christian Brauner   2021-01-21 14:19:46 +0100 540) 	if (mnt_user_ns(path.mnt) != &init_user_ns) {
0f16ff0f545d5 (Christian Brauner   2021-01-21 14:19:46 +0100 541) 		rc = -EINVAL;
0f16ff0f545d5 (Christian Brauner   2021-01-21 14:19:46 +0100 542) 		printk(KERN_ERR "Mounting on idmapped mounts currently disallowed\n");
0f16ff0f545d5 (Christian Brauner   2021-01-21 14:19:46 +0100 543) 		goto out_free;
0f16ff0f545d5 (Christian Brauner   2021-01-21 14:19:46 +0100 544) 	}
0f16ff0f545d5 (Christian Brauner   2021-01-21 14:19:46 +0100 545) 
2b0143b5c986b (David Howells       2015-03-17 22:25:59 +0000 546) 	if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 547) 		rc = -EPERM;
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 548) 		printk(KERN_ERR "Mount of device (uid: %d) not owned by "
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 549) 		       "requested user (uid: %d)\n",
2b0143b5c986b (David Howells       2015-03-17 22:25:59 +0000 550) 			i_uid_read(d_inode(path.dentry)),
cdf8c58a35464 (Eric W. Biederman   2012-02-07 16:24:33 -0800 551) 			from_kuid(&init_user_ns, current_uid()));
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 552) 		goto out_free;
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 553) 	}
764355487ea22 (John Johansen       2011-07-22 08:14:15 -0700 554) 
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 555) 	ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
069ddcda37b2c (Tyler Hicks         2012-06-11 15:42:32 -0700 556) 
069ddcda37b2c (Tyler Hicks         2012-06-11 15:42:32 -0700 557) 	/**
069ddcda37b2c (Tyler Hicks         2012-06-11 15:42:32 -0700 558) 	 * Set the POSIX ACL flag based on whether they're enabled in the lower
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 559) 	 * mount.
069ddcda37b2c (Tyler Hicks         2012-06-11 15:42:32 -0700 560) 	 */
1751e8a6cb935 (Linus Torvalds      2017-11-27 13:05:09 -0800 561) 	s->s_flags = flags & ~SB_POSIXACL;
1751e8a6cb935 (Linus Torvalds      2017-11-27 13:05:09 -0800 562) 	s->s_flags |= path.dentry->d_sb->s_flags & SB_POSIXACL;
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 563) 
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 564) 	/**
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 565) 	 * Force a read-only eCryptfs mount when:
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 566) 	 *   1) The lower mount is ro
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 567) 	 *   2) The ecryptfs_encrypted_view mount option is specified
332b122d39c9c (Tyler Hicks         2014-10-07 15:51:55 -0500 568) 	 */
bc98a42c1f7d0 (David Howells       2017-07-17 08:45:34 +0100 569) 	if (sb_rdonly(path.dentry->d_sb) || mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
1751e8a6cb935 (Linus Torvalds      2017-11-27 13:05:09 -0800 570) 		s->s_flags |= SB_RDONLY;
069ddcda37b2c (Tyler Hicks         2012-06-11 15:42:32 -0700 571) 
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 572) 	s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 573) 	s->s_blocksize = path.dentry->d_sb->s_blocksize;
070baa51286e5 (Roberto Sassu       2010-11-03 11:11:22 +0100 574) 	s->s_magic = ECRYPTFS_SUPER_MAGIC;
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 575) 	s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 576) 
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 577) 	rc = -EINVAL;
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 578) 	if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 579) 		pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 580) 		goto out_free;
69c433ed2ecd2 (Miklos Szeredi      2014-10-24 00:14:39 +0200 581) 	}
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 582) 
2b0143b5c986b (David Howells       2015-03-17 22:25:59 +0000 583) 	inode = ecryptfs_get_inode(d_inode(path.dentry), s);
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 584) 	rc = PTR_ERR(inode);
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 585) 	if (IS_ERR(inode))
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 586) 		goto out_free;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 587) 
48fde701aff66 (Al Viro             2012-01-08 22:15:13 -0500 588) 	s->s_root = d_make_root(inode);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 589) 	if (!s->s_root) {
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 590) 		rc = -ENOMEM;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 591) 		goto out_free;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 592) 	}
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 593) 
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 594) 	rc = -ENOMEM;
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 595) 	root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 596) 	if (!root_info)
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 597) 		goto out_free;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 598) 
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 599) 	/* ->kill_sb() will take care of root_info */
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 600) 	ecryptfs_set_dentry_private(s->s_root, root_info);
92dd123033d50 (Al Viro             2013-09-15 20:50:13 -0400 601) 	root_info->lower_path = path;
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 602) 
1751e8a6cb935 (Linus Torvalds      2017-11-27 13:05:09 -0800 603) 	s->s_flags |= SB_ACTIVE;
4d143beb0429e (Al Viro             2010-07-26 13:33:36 +0400 604) 	return dget(s->s_root);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 605) 
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 606) out_free:
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 607) 	path_put(&path);
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 608) out1:
66cb76666d695 (Al Viro             2011-01-12 20:04:37 -0500 609) 	deactivate_locked_super(s);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 610) out:
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 611) 	if (sbi) {
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 612) 		ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 613) 		kmem_cache_free(ecryptfs_sb_info_cache, sbi);
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 614) 	}
2ccde7c631f99 (Al Viro             2010-03-21 12:24:29 -0400 615) 	printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
4d143beb0429e (Al Viro             2010-07-26 13:33:36 +0400 616) 	return ERR_PTR(rc);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 617) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 618) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 619) /**
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 620)  * ecryptfs_kill_block_super
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 621)  * @sb: The ecryptfs super block
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 622)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 623)  * Used to bring the superblock down and free the private data.
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 624)  */
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 625) static void ecryptfs_kill_block_super(struct super_block *sb)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 626) {
decabd6650915 (Al Viro             2010-03-20 22:32:26 -0400 627) 	struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
decabd6650915 (Al Viro             2010-03-20 22:32:26 -0400 628) 	kill_anon_super(sb);
decabd6650915 (Al Viro             2010-03-20 22:32:26 -0400 629) 	if (!sb_info)
decabd6650915 (Al Viro             2010-03-20 22:32:26 -0400 630) 		return;
decabd6650915 (Al Viro             2010-03-20 22:32:26 -0400 631) 	ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
decabd6650915 (Al Viro             2010-03-20 22:32:26 -0400 632) 	kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 633) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 634) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 635) static struct file_system_type ecryptfs_fs_type = {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 636) 	.owner = THIS_MODULE,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 637) 	.name = "ecryptfs",
4d143beb0429e (Al Viro             2010-07-26 13:33:36 +0400 638) 	.mount = ecryptfs_mount,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 639) 	.kill_sb = ecryptfs_kill_block_super,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 640) 	.fs_flags = 0
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 641) };
7f78e03513940 (Eric W. Biederman   2013-03-02 19:39:14 -0800 642) MODULE_ALIAS_FS("ecryptfs");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 643) 
e24012062e3df (Lee Jones           2021-03-30 17:44:53 +0100 644) /*
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 645)  * inode_info_init_once
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 646)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 647)  * Initializes the ecryptfs_inode_info_cache when it is created
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 648)  */
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 649) static void
51cc50685a427 (Alexey Dobriyan     2008-07-25 19:45:34 -0700 650) inode_info_init_once(void *vptr)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 651) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 652) 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 653) 
a35afb830f8d7 (Christoph Lameter   2007-05-16 22:10:57 -0700 654) 	inode_init_once(&ei->vfs_inode);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 655) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 656) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 657) static struct ecryptfs_cache_info {
e18b890bb0881 (Christoph Lameter   2006-12-06 20:33:20 -0800 658) 	struct kmem_cache **cache;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 659) 	const char *name;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 660) 	size_t size;
d50112edde1d0 (Alexey Dobriyan     2017-11-15 17:32:18 -0800 661) 	slab_flags_t flags;
51cc50685a427 (Alexey Dobriyan     2008-07-25 19:45:34 -0700 662) 	void (*ctor)(void *obj);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 663) } ecryptfs_cache_infos[] = {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 664) 	{
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 665) 		.cache = &ecryptfs_auth_tok_list_item_cache,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 666) 		.name = "ecryptfs_auth_tok_list_item",
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 667) 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 668) 	},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 669) 	{
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 670) 		.cache = &ecryptfs_file_info_cache,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 671) 		.name = "ecryptfs_file_cache",
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 672) 		.size = sizeof(struct ecryptfs_file_info),
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 673) 	},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 674) 	{
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 675) 		.cache = &ecryptfs_dentry_info_cache,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 676) 		.name = "ecryptfs_dentry_info_cache",
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 677) 		.size = sizeof(struct ecryptfs_dentry_info),
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 678) 	},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 679) 	{
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 680) 		.cache = &ecryptfs_inode_info_cache,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 681) 		.name = "ecryptfs_inode_cache",
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 682) 		.size = sizeof(struct ecryptfs_inode_info),
5d097056c9a01 (Vladimir Davydov    2016-01-14 15:18:21 -0800 683) 		.flags = SLAB_ACCOUNT,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 684) 		.ctor = inode_info_init_once,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 685) 	},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 686) 	{
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 687) 		.cache = &ecryptfs_sb_info_cache,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 688) 		.name = "ecryptfs_sb_cache",
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 689) 		.size = sizeof(struct ecryptfs_sb_info),
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 690) 	},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 691) 	{
3063287053bca (Tyler Hicks         2011-05-24 05:11:12 -0500 692) 		.cache = &ecryptfs_header_cache,
3063287053bca (Tyler Hicks         2011-05-24 05:11:12 -0500 693) 		.name = "ecryptfs_headers",
09cbfeaf1a5a6 (Kirill A. Shutemov  2016-04-01 15:29:47 +0300 694) 		.size = PAGE_SIZE,
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 695) 	},
dd2a3b7ad98f8 (Michael Halcrow     2007-02-12 00:53:46 -0800 696) 	{
dd2a3b7ad98f8 (Michael Halcrow     2007-02-12 00:53:46 -0800 697) 		.cache = &ecryptfs_xattr_cache,
dd2a3b7ad98f8 (Michael Halcrow     2007-02-12 00:53:46 -0800 698) 		.name = "ecryptfs_xattr_cache",
09cbfeaf1a5a6 (Kirill A. Shutemov  2016-04-01 15:29:47 +0300 699) 		.size = PAGE_SIZE,
dd2a3b7ad98f8 (Michael Halcrow     2007-02-12 00:53:46 -0800 700) 	},
eb95e7ffa50fa (Michael Halcrow     2007-02-16 01:28:40 -0800 701) 	{
eb95e7ffa50fa (Michael Halcrow     2007-02-16 01:28:40 -0800 702) 		.cache = &ecryptfs_key_record_cache,
eb95e7ffa50fa (Michael Halcrow     2007-02-16 01:28:40 -0800 703) 		.name = "ecryptfs_key_record_cache",
eb95e7ffa50fa (Michael Halcrow     2007-02-16 01:28:40 -0800 704) 		.size = sizeof(struct ecryptfs_key_record),
eb95e7ffa50fa (Michael Halcrow     2007-02-16 01:28:40 -0800 705) 	},
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 706) 	{
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 707) 		.cache = &ecryptfs_key_sig_cache,
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 708) 		.name = "ecryptfs_key_sig_cache",
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 709) 		.size = sizeof(struct ecryptfs_key_sig),
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 710) 	},
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 711) 	{
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 712) 		.cache = &ecryptfs_global_auth_tok_cache,
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 713) 		.name = "ecryptfs_global_auth_tok_cache",
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 714) 		.size = sizeof(struct ecryptfs_global_auth_tok),
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 715) 	},
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 716) 	{
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 717) 		.cache = &ecryptfs_key_tfm_cache,
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 718) 		.name = "ecryptfs_key_tfm_cache",
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 719) 		.size = sizeof(struct ecryptfs_key_tfm),
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 720) 	},
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 721) };
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 722) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 723) static void ecryptfs_free_kmem_caches(void)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 724) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 725) 	int i;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 726) 
8c0a85377048b (Kirill A. Shutemov  2012-09-26 11:33:07 +1000 727) 	/*
8c0a85377048b (Kirill A. Shutemov  2012-09-26 11:33:07 +1000 728) 	 * Make sure all delayed rcu free inodes are flushed before we
8c0a85377048b (Kirill A. Shutemov  2012-09-26 11:33:07 +1000 729) 	 * destroy cache.
8c0a85377048b (Kirill A. Shutemov  2012-09-26 11:33:07 +1000 730) 	 */
8c0a85377048b (Kirill A. Shutemov  2012-09-26 11:33:07 +1000 731) 	rcu_barrier();
8c0a85377048b (Kirill A. Shutemov  2012-09-26 11:33:07 +1000 732) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 733) 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 734) 		struct ecryptfs_cache_info *info;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 735) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 736) 		info = &ecryptfs_cache_infos[i];
c39341cf0d083 (Julia Lawall        2015-09-13 14:15:21 +0200 737) 		kmem_cache_destroy(*(info->cache));
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 738) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 739) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 740) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 741) /**
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 742)  * ecryptfs_init_kmem_caches
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 743)  *
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 744)  * Returns zero on success; non-zero otherwise
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 745)  */
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 746) static int ecryptfs_init_kmem_caches(void)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 747) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 748) 	int i;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 749) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 750) 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 751) 		struct ecryptfs_cache_info *info;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 752) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 753) 		info = &ecryptfs_cache_infos[i];
5d097056c9a01 (Vladimir Davydov    2016-01-14 15:18:21 -0800 754) 		*(info->cache) = kmem_cache_create(info->name, info->size, 0,
5d097056c9a01 (Vladimir Davydov    2016-01-14 15:18:21 -0800 755) 				SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 756) 		if (!*(info->cache)) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 757) 			ecryptfs_free_kmem_caches();
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 758) 			ecryptfs_printk(KERN_WARNING, "%s: "
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 759) 					"kmem_cache_create failed\n",
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 760) 					info->name);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 761) 			return -ENOMEM;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 762) 		}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 763) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 764) 	return 0;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 765) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 766) 
6e90aa972dda8 (Greg Kroah-Hartman  2007-11-06 15:08:08 -0800 767) static struct kobject *ecryptfs_kobj;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 768) 
386f275f5d097 (Kay Sievers         2007-11-02 13:47:53 +0100 769) static ssize_t version_show(struct kobject *kobj,
386f275f5d097 (Kay Sievers         2007-11-02 13:47:53 +0100 770) 			    struct kobj_attribute *attr, char *buff)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 771) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 772) 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 773) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 774) 
386f275f5d097 (Kay Sievers         2007-11-02 13:47:53 +0100 775) static struct kobj_attribute version_attr = __ATTR_RO(version);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 776) 
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 777) static struct attribute *attributes[] = {
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 778) 	&version_attr.attr,
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 779) 	NULL,
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 780) };
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 781) 
4670269faba72 (Arvind Yadav        2017-06-30 15:03:23 +0530 782) static const struct attribute_group attr_group = {
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 783) 	.attrs = attributes,
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 784) };
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 785) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 786) static int do_sysfs_registration(void)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 787) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 788) 	int rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 789) 
6e90aa972dda8 (Greg Kroah-Hartman  2007-11-06 15:08:08 -0800 790) 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
6e90aa972dda8 (Greg Kroah-Hartman  2007-11-06 15:08:08 -0800 791) 	if (!ecryptfs_kobj) {
917e865df7eb0 (Greg Kroah-Hartman  2007-10-29 20:13:17 +0100 792) 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
917e865df7eb0 (Greg Kroah-Hartman  2007-10-29 20:13:17 +0100 793) 		rc = -ENOMEM;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 794) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 795) 	}
6e90aa972dda8 (Greg Kroah-Hartman  2007-11-06 15:08:08 -0800 796) 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 797) 	if (rc) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 798) 		printk(KERN_ERR
30a468b1c1b99 (Greg Kroah-Hartman  2007-10-15 15:01:24 -0700 799) 		       "Unable to create ecryptfs version attributes\n");
197b12d6796a3 (Greg Kroah-Hartman  2007-12-20 08:13:05 -0800 800) 		kobject_put(ecryptfs_kobj);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 801) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 802) out:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 803) 	return rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 804) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 805) 
a75de1b3799f8 (Ryusuke Konishi     2007-08-10 13:00:56 -0700 806) static void do_sysfs_unregistration(void)
a75de1b3799f8 (Ryusuke Konishi     2007-08-10 13:00:56 -0700 807) {
6e90aa972dda8 (Greg Kroah-Hartman  2007-11-06 15:08:08 -0800 808) 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
197b12d6796a3 (Greg Kroah-Hartman  2007-12-20 08:13:05 -0800 809) 	kobject_put(ecryptfs_kobj);
a75de1b3799f8 (Ryusuke Konishi     2007-08-10 13:00:56 -0700 810) }
a75de1b3799f8 (Ryusuke Konishi     2007-08-10 13:00:56 -0700 811) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 812) static int __init ecryptfs_init(void)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 813) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 814) 	int rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 815) 
09cbfeaf1a5a6 (Kirill A. Shutemov  2016-04-01 15:29:47 +0300 816) 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 817) 		rc = -EINVAL;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 818) 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 819) 				"larger than the host's page size, and so "
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 820) 				"eCryptfs cannot run on this system. The "
888d57bbc91eb (Joe Perches         2010-11-10 15:46:16 -0800 821) 				"default eCryptfs extent size is [%u] bytes; "
888d57bbc91eb (Joe Perches         2010-11-10 15:46:16 -0800 822) 				"the page size is [%lu] bytes.\n",
888d57bbc91eb (Joe Perches         2010-11-10 15:46:16 -0800 823) 				ECRYPTFS_DEFAULT_EXTENT_SIZE,
09cbfeaf1a5a6 (Kirill A. Shutemov  2016-04-01 15:29:47 +0300 824) 				(unsigned long)PAGE_SIZE);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 825) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 826) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 827) 	rc = ecryptfs_init_kmem_caches();
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 828) 	if (rc) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 829) 		printk(KERN_ERR
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 830) 		       "Failed to allocate one or more kmem_cache objects\n");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 831) 		goto out;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 832) 	}
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 833) 	rc = do_sysfs_registration();
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 834) 	if (rc) {
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 835) 		printk(KERN_ERR "sysfs registration failed\n");
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 836) 		goto out_free_kmem_caches;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 837) 	}
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 838) 	rc = ecryptfs_init_kthread();
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 839) 	if (rc) {
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 840) 		printk(KERN_ERR "%s: kthread initialization failed; "
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 841) 		       "rc = [%d]\n", __func__, rc);
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 842) 		goto out_do_sysfs_unregistration;
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 843) 	}
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700 844) 	rc = ecryptfs_init_messaging();
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800 845) 	if (rc) {
25985edcedea6 (Lucas De Marchi     2011-03-30 22:57:33 -0300 846) 		printk(KERN_ERR "Failure occurred while attempting to "
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700 847) 				"initialize the communications channel to "
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700 848) 				"ecryptfsd\n");
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 849) 		goto out_destroy_kthread;
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 850) 	}
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 851) 	rc = ecryptfs_init_crypto();
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 852) 	if (rc) {
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 853) 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
956159c3d6e7e (Michael Halcrow     2007-10-16 01:27:55 -0700 854) 		       "rc = [%d]\n", rc);
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 855) 		goto out_release_messaging;
dddfa461fc895 (Michael Halcrow     2007-02-12 00:53:44 -0800 856) 	}
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 857) 	rc = register_filesystem(&ecryptfs_fs_type);
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 858) 	if (rc) {
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 859) 		printk(KERN_ERR "Failed to register filesystem\n");
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 860) 		goto out_destroy_crypto;
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 861) 	}
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 862) 	if (ecryptfs_verbosity > 0)
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 863) 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 864) 			"will be written to the syslog!\n", ecryptfs_verbosity);
2830bfd6cf661 (Eric Sandeen        2008-02-06 01:38:34 -0800 865) 
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 866) 	goto out;
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 867) out_destroy_crypto:
0794f569ec307 (Al Viro             2012-03-17 21:29:13 -0400 868) 	ecryptfs_destroy_crypto();
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 869) out_release_messaging:
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700 870) 	ecryptfs_release_messaging();
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 871) out_destroy_kthread:
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 872) 	ecryptfs_destroy_kthread();
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 873) out_do_sysfs_unregistration:
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 874) 	do_sysfs_unregistration();
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 875) out_free_kmem_caches:
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 876) 	ecryptfs_free_kmem_caches();
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 877) out:
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 878) 	return rc;
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 879) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 880) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 881) static void __exit ecryptfs_exit(void)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 882) {
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 883) 	int rc;
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 884) 
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 885) 	rc = ecryptfs_destroy_crypto();
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 886) 	if (rc)
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 887) 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 888) 		       "rc = [%d]\n", rc);
624ae52845168 (Tyler Hicks         2008-10-15 22:02:51 -0700 889) 	ecryptfs_release_messaging();
746f1e558bc52 (Michael Halcrow     2008-07-23 21:30:02 -0700 890) 	ecryptfs_destroy_kthread();
cf81f89d9a85b (Michael Halcrow     2007-10-16 01:28:07 -0700 891) 	do_sysfs_unregistration();
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 892) 	unregister_filesystem(&ecryptfs_fs_type);
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 893) 	ecryptfs_free_kmem_caches();
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 894) }
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 895) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 896) MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 897) MODULE_DESCRIPTION("eCryptfs");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 898) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 899) MODULE_LICENSE("GPL");
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 900) 
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 901) module_init(ecryptfs_init)
237fead619984 (Michael Halcrow     2006-10-04 02:16:22 -0700 902) module_exit(ecryptfs_exit)