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