xref: /OK3568_Linux_fs/kernel/fs/crypto/policy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Encryption policy functions for per-file encryption support.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2015, Google, Inc.
6*4882a593Smuzhiyun  * Copyright (C) 2015, Motorola Mobility.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Originally written by Michael Halcrow, 2015.
9*4882a593Smuzhiyun  * Modified by Jaegeuk Kim, 2015.
10*4882a593Smuzhiyun  * Modified by Eric Biggers, 2019 for v2 policy support.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/random.h>
14*4882a593Smuzhiyun #include <linux/seq_file.h>
15*4882a593Smuzhiyun #include <linux/string.h>
16*4882a593Smuzhiyun #include <linux/mount.h>
17*4882a593Smuzhiyun #include "fscrypt_private.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /**
20*4882a593Smuzhiyun  * fscrypt_policies_equal() - check whether two encryption policies are the same
21*4882a593Smuzhiyun  * @policy1: the first policy
22*4882a593Smuzhiyun  * @policy2: the second policy
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * Return: %true if equal, else %false
25*4882a593Smuzhiyun  */
fscrypt_policies_equal(const union fscrypt_policy * policy1,const union fscrypt_policy * policy2)26*4882a593Smuzhiyun bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
27*4882a593Smuzhiyun 			    const union fscrypt_policy *policy2)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	if (policy1->version != policy2->version)
30*4882a593Smuzhiyun 		return false;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static const union fscrypt_policy *
fscrypt_get_dummy_policy(struct super_block * sb)36*4882a593Smuzhiyun fscrypt_get_dummy_policy(struct super_block *sb)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	if (!sb->s_cop->get_dummy_policy)
39*4882a593Smuzhiyun 		return NULL;
40*4882a593Smuzhiyun 	return sb->s_cop->get_dummy_policy(sb);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
fscrypt_valid_enc_modes(u32 contents_mode,u32 filenames_mode)43*4882a593Smuzhiyun static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
46*4882a593Smuzhiyun 	    filenames_mode == FSCRYPT_MODE_AES_256_CTS)
47*4882a593Smuzhiyun 		return true;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
50*4882a593Smuzhiyun 	    filenames_mode == FSCRYPT_MODE_AES_128_CTS)
51*4882a593Smuzhiyun 		return true;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
54*4882a593Smuzhiyun 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
55*4882a593Smuzhiyun 		return true;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	return false;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
supported_direct_key_modes(const struct inode * inode,u32 contents_mode,u32 filenames_mode)60*4882a593Smuzhiyun static bool supported_direct_key_modes(const struct inode *inode,
61*4882a593Smuzhiyun 				       u32 contents_mode, u32 filenames_mode)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	const struct fscrypt_mode *mode;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	if (contents_mode != filenames_mode) {
66*4882a593Smuzhiyun 		fscrypt_warn(inode,
67*4882a593Smuzhiyun 			     "Direct key flag not allowed with different contents and filenames modes");
68*4882a593Smuzhiyun 		return false;
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 	mode = &fscrypt_modes[contents_mode];
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
73*4882a593Smuzhiyun 		fscrypt_warn(inode, "Direct key flag not allowed with %s",
74*4882a593Smuzhiyun 			     mode->friendly_name);
75*4882a593Smuzhiyun 		return false;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 	return true;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 * policy,const struct inode * inode,const char * type,int max_ino_bits,int max_lblk_bits)80*4882a593Smuzhiyun static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
81*4882a593Smuzhiyun 					 const struct inode *inode,
82*4882a593Smuzhiyun 					 const char *type,
83*4882a593Smuzhiyun 					 int max_ino_bits, int max_lblk_bits)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
86*4882a593Smuzhiyun 	int ino_bits = 64, lblk_bits = 64;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/*
89*4882a593Smuzhiyun 	 * IV_INO_LBLK_* exist only because of hardware limitations, and
90*4882a593Smuzhiyun 	 * currently the only known use case for them involves AES-256-XTS.
91*4882a593Smuzhiyun 	 * That's also all we test currently.  For these reasons, for now only
92*4882a593Smuzhiyun 	 * allow AES-256-XTS here.  This can be relaxed later if a use case for
93*4882a593Smuzhiyun 	 * IV_INO_LBLK_* with other encryption modes arises.
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
96*4882a593Smuzhiyun 		fscrypt_warn(inode,
97*4882a593Smuzhiyun 			     "Can't use %s policy with contents mode other than AES-256-XTS",
98*4882a593Smuzhiyun 			     type);
99*4882a593Smuzhiyun 		return false;
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/*
103*4882a593Smuzhiyun 	 * It's unsafe to include inode numbers in the IVs if the filesystem can
104*4882a593Smuzhiyun 	 * potentially renumber inodes, e.g. via filesystem shrinking.
105*4882a593Smuzhiyun 	 */
106*4882a593Smuzhiyun 	if (!sb->s_cop->has_stable_inodes ||
107*4882a593Smuzhiyun 	    !sb->s_cop->has_stable_inodes(sb)) {
108*4882a593Smuzhiyun 		fscrypt_warn(inode,
109*4882a593Smuzhiyun 			     "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
110*4882a593Smuzhiyun 			     type, sb->s_id);
111*4882a593Smuzhiyun 		return false;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 	if (sb->s_cop->get_ino_and_lblk_bits)
114*4882a593Smuzhiyun 		sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
115*4882a593Smuzhiyun 	if (ino_bits > max_ino_bits) {
116*4882a593Smuzhiyun 		fscrypt_warn(inode,
117*4882a593Smuzhiyun 			     "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
118*4882a593Smuzhiyun 			     type, sb->s_id);
119*4882a593Smuzhiyun 		return false;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 	if (lblk_bits > max_lblk_bits) {
122*4882a593Smuzhiyun 		fscrypt_warn(inode,
123*4882a593Smuzhiyun 			     "Can't use %s policy on filesystem '%s' because its block numbers are too long",
124*4882a593Smuzhiyun 			     type, sb->s_id);
125*4882a593Smuzhiyun 		return false;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 	return true;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 * policy,const struct inode * inode)130*4882a593Smuzhiyun static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
131*4882a593Smuzhiyun 					const struct inode *inode)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
134*4882a593Smuzhiyun 				     policy->filenames_encryption_mode)) {
135*4882a593Smuzhiyun 		fscrypt_warn(inode,
136*4882a593Smuzhiyun 			     "Unsupported encryption modes (contents %d, filenames %d)",
137*4882a593Smuzhiyun 			     policy->contents_encryption_mode,
138*4882a593Smuzhiyun 			     policy->filenames_encryption_mode);
139*4882a593Smuzhiyun 		return false;
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
143*4882a593Smuzhiyun 			      FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
144*4882a593Smuzhiyun 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
145*4882a593Smuzhiyun 			     policy->flags);
146*4882a593Smuzhiyun 		return false;
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
150*4882a593Smuzhiyun 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
151*4882a593Smuzhiyun 					policy->filenames_encryption_mode))
152*4882a593Smuzhiyun 		return false;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (IS_CASEFOLDED(inode)) {
155*4882a593Smuzhiyun 		/* With v1, there's no way to derive dirhash keys. */
156*4882a593Smuzhiyun 		fscrypt_warn(inode,
157*4882a593Smuzhiyun 			     "v1 policies can't be used on casefolded directories");
158*4882a593Smuzhiyun 		return false;
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	return true;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 * policy,const struct inode * inode)164*4882a593Smuzhiyun static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
165*4882a593Smuzhiyun 					const struct inode *inode)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	int count = 0;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
170*4882a593Smuzhiyun 				     policy->filenames_encryption_mode)) {
171*4882a593Smuzhiyun 		fscrypt_warn(inode,
172*4882a593Smuzhiyun 			     "Unsupported encryption modes (contents %d, filenames %d)",
173*4882a593Smuzhiyun 			     policy->contents_encryption_mode,
174*4882a593Smuzhiyun 			     policy->filenames_encryption_mode);
175*4882a593Smuzhiyun 		return false;
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
179*4882a593Smuzhiyun 			      FSCRYPT_POLICY_FLAG_DIRECT_KEY |
180*4882a593Smuzhiyun 			      FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
181*4882a593Smuzhiyun 			      FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
182*4882a593Smuzhiyun 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
183*4882a593Smuzhiyun 			     policy->flags);
184*4882a593Smuzhiyun 		return false;
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
188*4882a593Smuzhiyun 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
189*4882a593Smuzhiyun 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
190*4882a593Smuzhiyun 	if (count > 1) {
191*4882a593Smuzhiyun 		fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
192*4882a593Smuzhiyun 			     policy->flags);
193*4882a593Smuzhiyun 		return false;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
197*4882a593Smuzhiyun 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
198*4882a593Smuzhiyun 					policy->filenames_encryption_mode))
199*4882a593Smuzhiyun 		return false;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
202*4882a593Smuzhiyun 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
203*4882a593Smuzhiyun 					  32, 32))
204*4882a593Smuzhiyun 		return false;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/*
207*4882a593Smuzhiyun 	 * IV_INO_LBLK_32 hashes the inode number, so in principle it can
208*4882a593Smuzhiyun 	 * support any ino_bits.  However, currently the inode number is gotten
209*4882a593Smuzhiyun 	 * from inode::i_ino which is 'unsigned long'.  So for now the
210*4882a593Smuzhiyun 	 * implementation limit is 32 bits.
211*4882a593Smuzhiyun 	 */
212*4882a593Smuzhiyun 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
213*4882a593Smuzhiyun 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
214*4882a593Smuzhiyun 					  32, 32))
215*4882a593Smuzhiyun 		return false;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
218*4882a593Smuzhiyun 		fscrypt_warn(inode, "Reserved bits set in encryption policy");
219*4882a593Smuzhiyun 		return false;
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	return true;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun /**
226*4882a593Smuzhiyun  * fscrypt_supported_policy() - check whether an encryption policy is supported
227*4882a593Smuzhiyun  * @policy_u: the encryption policy
228*4882a593Smuzhiyun  * @inode: the inode on which the policy will be used
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * Given an encryption policy, check whether all its encryption modes and other
231*4882a593Smuzhiyun  * settings are supported by this kernel on the given inode.  (But we don't
232*4882a593Smuzhiyun  * currently don't check for crypto API support here, so attempting to use an
233*4882a593Smuzhiyun  * algorithm not configured into the crypto API will still fail later.)
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * Return: %true if supported, else %false
236*4882a593Smuzhiyun  */
fscrypt_supported_policy(const union fscrypt_policy * policy_u,const struct inode * inode)237*4882a593Smuzhiyun bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
238*4882a593Smuzhiyun 			      const struct inode *inode)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	switch (policy_u->version) {
241*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V1:
242*4882a593Smuzhiyun 		return fscrypt_supported_v1_policy(&policy_u->v1, inode);
243*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V2:
244*4882a593Smuzhiyun 		return fscrypt_supported_v2_policy(&policy_u->v2, inode);
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 	return false;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun /**
250*4882a593Smuzhiyun  * fscrypt_new_context() - create a new fscrypt_context
251*4882a593Smuzhiyun  * @ctx_u: output context
252*4882a593Smuzhiyun  * @policy_u: input policy
253*4882a593Smuzhiyun  * @nonce: nonce to use
254*4882a593Smuzhiyun  *
255*4882a593Smuzhiyun  * Create an fscrypt_context for an inode that is being assigned the given
256*4882a593Smuzhiyun  * encryption policy.  @nonce must be a new random nonce.
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Return: the size of the new context in bytes.
259*4882a593Smuzhiyun  */
fscrypt_new_context(union fscrypt_context * ctx_u,const union fscrypt_policy * policy_u,const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])260*4882a593Smuzhiyun static int fscrypt_new_context(union fscrypt_context *ctx_u,
261*4882a593Smuzhiyun 			       const union fscrypt_policy *policy_u,
262*4882a593Smuzhiyun 			       const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	memset(ctx_u, 0, sizeof(*ctx_u));
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	switch (policy_u->version) {
267*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V1: {
268*4882a593Smuzhiyun 		const struct fscrypt_policy_v1 *policy = &policy_u->v1;
269*4882a593Smuzhiyun 		struct fscrypt_context_v1 *ctx = &ctx_u->v1;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		ctx->version = FSCRYPT_CONTEXT_V1;
272*4882a593Smuzhiyun 		ctx->contents_encryption_mode =
273*4882a593Smuzhiyun 			policy->contents_encryption_mode;
274*4882a593Smuzhiyun 		ctx->filenames_encryption_mode =
275*4882a593Smuzhiyun 			policy->filenames_encryption_mode;
276*4882a593Smuzhiyun 		ctx->flags = policy->flags;
277*4882a593Smuzhiyun 		memcpy(ctx->master_key_descriptor,
278*4882a593Smuzhiyun 		       policy->master_key_descriptor,
279*4882a593Smuzhiyun 		       sizeof(ctx->master_key_descriptor));
280*4882a593Smuzhiyun 		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
281*4882a593Smuzhiyun 		return sizeof(*ctx);
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V2: {
284*4882a593Smuzhiyun 		const struct fscrypt_policy_v2 *policy = &policy_u->v2;
285*4882a593Smuzhiyun 		struct fscrypt_context_v2 *ctx = &ctx_u->v2;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 		ctx->version = FSCRYPT_CONTEXT_V2;
288*4882a593Smuzhiyun 		ctx->contents_encryption_mode =
289*4882a593Smuzhiyun 			policy->contents_encryption_mode;
290*4882a593Smuzhiyun 		ctx->filenames_encryption_mode =
291*4882a593Smuzhiyun 			policy->filenames_encryption_mode;
292*4882a593Smuzhiyun 		ctx->flags = policy->flags;
293*4882a593Smuzhiyun 		memcpy(ctx->master_key_identifier,
294*4882a593Smuzhiyun 		       policy->master_key_identifier,
295*4882a593Smuzhiyun 		       sizeof(ctx->master_key_identifier));
296*4882a593Smuzhiyun 		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
297*4882a593Smuzhiyun 		return sizeof(*ctx);
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 	BUG();
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /**
304*4882a593Smuzhiyun  * fscrypt_policy_from_context() - convert an fscrypt_context to
305*4882a593Smuzhiyun  *				   an fscrypt_policy
306*4882a593Smuzhiyun  * @policy_u: output policy
307*4882a593Smuzhiyun  * @ctx_u: input context
308*4882a593Smuzhiyun  * @ctx_size: size of input context in bytes
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * Given an fscrypt_context, build the corresponding fscrypt_policy.
311*4882a593Smuzhiyun  *
312*4882a593Smuzhiyun  * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
313*4882a593Smuzhiyun  * version number or size.
314*4882a593Smuzhiyun  *
315*4882a593Smuzhiyun  * This does *not* validate the settings within the policy itself, e.g. the
316*4882a593Smuzhiyun  * modes, flags, and reserved bits.  Use fscrypt_supported_policy() for that.
317*4882a593Smuzhiyun  */
fscrypt_policy_from_context(union fscrypt_policy * policy_u,const union fscrypt_context * ctx_u,int ctx_size)318*4882a593Smuzhiyun int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
319*4882a593Smuzhiyun 				const union fscrypt_context *ctx_u,
320*4882a593Smuzhiyun 				int ctx_size)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	memset(policy_u, 0, sizeof(*policy_u));
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (!fscrypt_context_is_valid(ctx_u, ctx_size))
325*4882a593Smuzhiyun 		return -EINVAL;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	switch (ctx_u->version) {
328*4882a593Smuzhiyun 	case FSCRYPT_CONTEXT_V1: {
329*4882a593Smuzhiyun 		const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
330*4882a593Smuzhiyun 		struct fscrypt_policy_v1 *policy = &policy_u->v1;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 		policy->version = FSCRYPT_POLICY_V1;
333*4882a593Smuzhiyun 		policy->contents_encryption_mode =
334*4882a593Smuzhiyun 			ctx->contents_encryption_mode;
335*4882a593Smuzhiyun 		policy->filenames_encryption_mode =
336*4882a593Smuzhiyun 			ctx->filenames_encryption_mode;
337*4882a593Smuzhiyun 		policy->flags = ctx->flags;
338*4882a593Smuzhiyun 		memcpy(policy->master_key_descriptor,
339*4882a593Smuzhiyun 		       ctx->master_key_descriptor,
340*4882a593Smuzhiyun 		       sizeof(policy->master_key_descriptor));
341*4882a593Smuzhiyun 		return 0;
342*4882a593Smuzhiyun 	}
343*4882a593Smuzhiyun 	case FSCRYPT_CONTEXT_V2: {
344*4882a593Smuzhiyun 		const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
345*4882a593Smuzhiyun 		struct fscrypt_policy_v2 *policy = &policy_u->v2;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 		policy->version = FSCRYPT_POLICY_V2;
348*4882a593Smuzhiyun 		policy->contents_encryption_mode =
349*4882a593Smuzhiyun 			ctx->contents_encryption_mode;
350*4882a593Smuzhiyun 		policy->filenames_encryption_mode =
351*4882a593Smuzhiyun 			ctx->filenames_encryption_mode;
352*4882a593Smuzhiyun 		policy->flags = ctx->flags;
353*4882a593Smuzhiyun 		memcpy(policy->__reserved, ctx->__reserved,
354*4882a593Smuzhiyun 		       sizeof(policy->__reserved));
355*4882a593Smuzhiyun 		memcpy(policy->master_key_identifier,
356*4882a593Smuzhiyun 		       ctx->master_key_identifier,
357*4882a593Smuzhiyun 		       sizeof(policy->master_key_identifier));
358*4882a593Smuzhiyun 		return 0;
359*4882a593Smuzhiyun 	}
360*4882a593Smuzhiyun 	}
361*4882a593Smuzhiyun 	/* unreachable */
362*4882a593Smuzhiyun 	return -EINVAL;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun /* Retrieve an inode's encryption policy */
fscrypt_get_policy(struct inode * inode,union fscrypt_policy * policy)366*4882a593Smuzhiyun static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	const struct fscrypt_info *ci;
369*4882a593Smuzhiyun 	union fscrypt_context ctx;
370*4882a593Smuzhiyun 	int ret;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	ci = fscrypt_get_info(inode);
373*4882a593Smuzhiyun 	if (ci) {
374*4882a593Smuzhiyun 		/* key available, use the cached policy */
375*4882a593Smuzhiyun 		*policy = ci->ci_policy;
376*4882a593Smuzhiyun 		return 0;
377*4882a593Smuzhiyun 	}
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	if (!IS_ENCRYPTED(inode))
380*4882a593Smuzhiyun 		return -ENODATA;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
383*4882a593Smuzhiyun 	if (ret < 0)
384*4882a593Smuzhiyun 		return (ret == -ERANGE) ? -EINVAL : ret;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	return fscrypt_policy_from_context(policy, &ctx, ret);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
set_encryption_policy(struct inode * inode,const union fscrypt_policy * policy)389*4882a593Smuzhiyun static int set_encryption_policy(struct inode *inode,
390*4882a593Smuzhiyun 				 const union fscrypt_policy *policy)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
393*4882a593Smuzhiyun 	union fscrypt_context ctx;
394*4882a593Smuzhiyun 	int ctxsize;
395*4882a593Smuzhiyun 	int err;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (!fscrypt_supported_policy(policy, inode))
398*4882a593Smuzhiyun 		return -EINVAL;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	switch (policy->version) {
401*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V1:
402*4882a593Smuzhiyun 		/*
403*4882a593Smuzhiyun 		 * The original encryption policy version provided no way of
404*4882a593Smuzhiyun 		 * verifying that the correct master key was supplied, which was
405*4882a593Smuzhiyun 		 * insecure in scenarios where multiple users have access to the
406*4882a593Smuzhiyun 		 * same encrypted files (even just read-only access).  The new
407*4882a593Smuzhiyun 		 * encryption policy version fixes this and also implies use of
408*4882a593Smuzhiyun 		 * an improved key derivation function and allows non-root users
409*4882a593Smuzhiyun 		 * to securely remove keys.  So as long as compatibility with
410*4882a593Smuzhiyun 		 * old kernels isn't required, it is recommended to use the new
411*4882a593Smuzhiyun 		 * policy version for all new encrypted directories.
412*4882a593Smuzhiyun 		 */
413*4882a593Smuzhiyun 		pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
414*4882a593Smuzhiyun 			     current->comm, current->pid);
415*4882a593Smuzhiyun 		break;
416*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V2:
417*4882a593Smuzhiyun 		err = fscrypt_verify_key_added(inode->i_sb,
418*4882a593Smuzhiyun 					       policy->v2.master_key_identifier);
419*4882a593Smuzhiyun 		if (err)
420*4882a593Smuzhiyun 			return err;
421*4882a593Smuzhiyun 		if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
422*4882a593Smuzhiyun 			pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy.  This should only be used if there are certain hardware limitations.\n",
423*4882a593Smuzhiyun 				     current->comm, current->pid);
424*4882a593Smuzhiyun 		break;
425*4882a593Smuzhiyun 	default:
426*4882a593Smuzhiyun 		WARN_ON(1);
427*4882a593Smuzhiyun 		return -EINVAL;
428*4882a593Smuzhiyun 	}
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
431*4882a593Smuzhiyun 	ctxsize = fscrypt_new_context(&ctx, policy, nonce);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
fscrypt_ioctl_set_policy(struct file * filp,const void __user * arg)436*4882a593Smuzhiyun int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	union fscrypt_policy policy;
439*4882a593Smuzhiyun 	union fscrypt_policy existing_policy;
440*4882a593Smuzhiyun 	struct inode *inode = file_inode(filp);
441*4882a593Smuzhiyun 	u8 version;
442*4882a593Smuzhiyun 	int size;
443*4882a593Smuzhiyun 	int ret;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (get_user(policy.version, (const u8 __user *)arg))
446*4882a593Smuzhiyun 		return -EFAULT;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	size = fscrypt_policy_size(&policy);
449*4882a593Smuzhiyun 	if (size <= 0)
450*4882a593Smuzhiyun 		return -EINVAL;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/*
453*4882a593Smuzhiyun 	 * We should just copy the remaining 'size - 1' bytes here, but a
454*4882a593Smuzhiyun 	 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
455*4882a593Smuzhiyun 	 * think that size can be 0 here (despite the check above!) *and* that
456*4882a593Smuzhiyun 	 * it's a compile-time constant.  Thus it would think copy_from_user()
457*4882a593Smuzhiyun 	 * is passed compile-time constant ULONG_MAX, causing the compile-time
458*4882a593Smuzhiyun 	 * buffer overflow check to fail, breaking the build. This only occurred
459*4882a593Smuzhiyun 	 * when building an i386 kernel with -Os and branch profiling enabled.
460*4882a593Smuzhiyun 	 *
461*4882a593Smuzhiyun 	 * Work around it by just copying the first byte again...
462*4882a593Smuzhiyun 	 */
463*4882a593Smuzhiyun 	version = policy.version;
464*4882a593Smuzhiyun 	if (copy_from_user(&policy, arg, size))
465*4882a593Smuzhiyun 		return -EFAULT;
466*4882a593Smuzhiyun 	policy.version = version;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	if (!inode_owner_or_capable(inode))
469*4882a593Smuzhiyun 		return -EACCES;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	ret = mnt_want_write_file(filp);
472*4882a593Smuzhiyun 	if (ret)
473*4882a593Smuzhiyun 		return ret;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	inode_lock(inode);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	ret = fscrypt_get_policy(inode, &existing_policy);
478*4882a593Smuzhiyun 	if (ret == -ENODATA) {
479*4882a593Smuzhiyun 		if (!S_ISDIR(inode->i_mode))
480*4882a593Smuzhiyun 			ret = -ENOTDIR;
481*4882a593Smuzhiyun 		else if (IS_DEADDIR(inode))
482*4882a593Smuzhiyun 			ret = -ENOENT;
483*4882a593Smuzhiyun 		else if (!inode->i_sb->s_cop->empty_dir(inode))
484*4882a593Smuzhiyun 			ret = -ENOTEMPTY;
485*4882a593Smuzhiyun 		else
486*4882a593Smuzhiyun 			ret = set_encryption_policy(inode, &policy);
487*4882a593Smuzhiyun 	} else if (ret == -EINVAL ||
488*4882a593Smuzhiyun 		   (ret == 0 && !fscrypt_policies_equal(&policy,
489*4882a593Smuzhiyun 							&existing_policy))) {
490*4882a593Smuzhiyun 		/* The file already uses a different encryption policy. */
491*4882a593Smuzhiyun 		ret = -EEXIST;
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	inode_unlock(inode);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	mnt_drop_write_file(filp);
497*4882a593Smuzhiyun 	return ret;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun /* Original ioctl version; can only get the original policy version */
fscrypt_ioctl_get_policy(struct file * filp,void __user * arg)502*4882a593Smuzhiyun int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	union fscrypt_policy policy;
505*4882a593Smuzhiyun 	int err;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	err = fscrypt_get_policy(file_inode(filp), &policy);
508*4882a593Smuzhiyun 	if (err)
509*4882a593Smuzhiyun 		return err;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if (policy.version != FSCRYPT_POLICY_V1)
512*4882a593Smuzhiyun 		return -EINVAL;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	if (copy_to_user(arg, &policy, sizeof(policy.v1)))
515*4882a593Smuzhiyun 		return -EFAULT;
516*4882a593Smuzhiyun 	return 0;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun /* Extended ioctl version; can get policies of any version */
fscrypt_ioctl_get_policy_ex(struct file * filp,void __user * uarg)521*4882a593Smuzhiyun int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	struct fscrypt_get_policy_ex_arg arg;
524*4882a593Smuzhiyun 	union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
525*4882a593Smuzhiyun 	size_t policy_size;
526*4882a593Smuzhiyun 	int err;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	/* arg is policy_size, then policy */
529*4882a593Smuzhiyun 	BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
530*4882a593Smuzhiyun 	BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
531*4882a593Smuzhiyun 		     offsetof(typeof(arg), policy));
532*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	err = fscrypt_get_policy(file_inode(filp), policy);
535*4882a593Smuzhiyun 	if (err)
536*4882a593Smuzhiyun 		return err;
537*4882a593Smuzhiyun 	policy_size = fscrypt_policy_size(policy);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
540*4882a593Smuzhiyun 		return -EFAULT;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	if (policy_size > arg.policy_size)
543*4882a593Smuzhiyun 		return -EOVERFLOW;
544*4882a593Smuzhiyun 	arg.policy_size = policy_size;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
547*4882a593Smuzhiyun 		return -EFAULT;
548*4882a593Smuzhiyun 	return 0;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
fscrypt_ioctl_get_nonce(struct file * filp,void __user * arg)553*4882a593Smuzhiyun int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	struct inode *inode = file_inode(filp);
556*4882a593Smuzhiyun 	union fscrypt_context ctx;
557*4882a593Smuzhiyun 	int ret;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
560*4882a593Smuzhiyun 	if (ret < 0)
561*4882a593Smuzhiyun 		return ret;
562*4882a593Smuzhiyun 	if (!fscrypt_context_is_valid(&ctx, ret))
563*4882a593Smuzhiyun 		return -EINVAL;
564*4882a593Smuzhiyun 	if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
565*4882a593Smuzhiyun 			 FSCRYPT_FILE_NONCE_SIZE))
566*4882a593Smuzhiyun 		return -EFAULT;
567*4882a593Smuzhiyun 	return 0;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun  * fscrypt_has_permitted_context() - is a file's encryption policy permitted
573*4882a593Smuzhiyun  *				     within its directory?
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * @parent: inode for parent directory
576*4882a593Smuzhiyun  * @child: inode for file being looked up, opened, or linked into @parent
577*4882a593Smuzhiyun  *
578*4882a593Smuzhiyun  * Filesystems must call this before permitting access to an inode in a
579*4882a593Smuzhiyun  * situation where the parent directory is encrypted (either before allowing
580*4882a593Smuzhiyun  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
581*4882a593Smuzhiyun  * and before any operation that involves linking an inode into an encrypted
582*4882a593Smuzhiyun  * directory, including link, rename, and cross rename.  It enforces the
583*4882a593Smuzhiyun  * constraint that within a given encrypted directory tree, all files use the
584*4882a593Smuzhiyun  * same encryption policy.  The pre-access check is needed to detect potentially
585*4882a593Smuzhiyun  * malicious offline violations of this constraint, while the link and rename
586*4882a593Smuzhiyun  * checks are needed to prevent online violations of this constraint.
587*4882a593Smuzhiyun  *
588*4882a593Smuzhiyun  * Return: 1 if permitted, 0 if forbidden.
589*4882a593Smuzhiyun  */
fscrypt_has_permitted_context(struct inode * parent,struct inode * child)590*4882a593Smuzhiyun int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun 	union fscrypt_policy parent_policy, child_policy;
593*4882a593Smuzhiyun 	int err, err1, err2;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	/* No restrictions on file types which are never encrypted */
596*4882a593Smuzhiyun 	if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
597*4882a593Smuzhiyun 	    !S_ISLNK(child->i_mode))
598*4882a593Smuzhiyun 		return 1;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	/* No restrictions if the parent directory is unencrypted */
601*4882a593Smuzhiyun 	if (!IS_ENCRYPTED(parent))
602*4882a593Smuzhiyun 		return 1;
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	/* Encrypted directories must not contain unencrypted files */
605*4882a593Smuzhiyun 	if (!IS_ENCRYPTED(child))
606*4882a593Smuzhiyun 		return 0;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	/*
609*4882a593Smuzhiyun 	 * Both parent and child are encrypted, so verify they use the same
610*4882a593Smuzhiyun 	 * encryption policy.  Compare the fscrypt_info structs if the keys are
611*4882a593Smuzhiyun 	 * available, otherwise retrieve and compare the fscrypt_contexts.
612*4882a593Smuzhiyun 	 *
613*4882a593Smuzhiyun 	 * Note that the fscrypt_context retrieval will be required frequently
614*4882a593Smuzhiyun 	 * when accessing an encrypted directory tree without the key.
615*4882a593Smuzhiyun 	 * Performance-wise this is not a big deal because we already don't
616*4882a593Smuzhiyun 	 * really optimize for file access without the key (to the extent that
617*4882a593Smuzhiyun 	 * such access is even possible), given that any attempted access
618*4882a593Smuzhiyun 	 * already causes a fscrypt_context retrieval and keyring search.
619*4882a593Smuzhiyun 	 *
620*4882a593Smuzhiyun 	 * In any case, if an unexpected error occurs, fall back to "forbidden".
621*4882a593Smuzhiyun 	 */
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	err = fscrypt_get_encryption_info(parent, true);
624*4882a593Smuzhiyun 	if (err)
625*4882a593Smuzhiyun 		return 0;
626*4882a593Smuzhiyun 	err = fscrypt_get_encryption_info(child, true);
627*4882a593Smuzhiyun 	if (err)
628*4882a593Smuzhiyun 		return 0;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	err1 = fscrypt_get_policy(parent, &parent_policy);
631*4882a593Smuzhiyun 	err2 = fscrypt_get_policy(child, &child_policy);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	/*
634*4882a593Smuzhiyun 	 * Allow the case where the parent and child both have an unrecognized
635*4882a593Smuzhiyun 	 * encryption policy, so that files with an unrecognized encryption
636*4882a593Smuzhiyun 	 * policy can be deleted.
637*4882a593Smuzhiyun 	 */
638*4882a593Smuzhiyun 	if (err1 == -EINVAL && err2 == -EINVAL)
639*4882a593Smuzhiyun 		return 1;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	if (err1 || err2)
642*4882a593Smuzhiyun 		return 0;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	return fscrypt_policies_equal(&parent_policy, &child_policy);
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun EXPORT_SYMBOL(fscrypt_has_permitted_context);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun /*
649*4882a593Smuzhiyun  * Return the encryption policy that new files in the directory will inherit, or
650*4882a593Smuzhiyun  * NULL if none, or an ERR_PTR() on error.  If the directory is encrypted, also
651*4882a593Smuzhiyun  * ensure that its key is set up, so that the new filename can be encrypted.
652*4882a593Smuzhiyun  */
fscrypt_policy_to_inherit(struct inode * dir)653*4882a593Smuzhiyun const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	int err;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	if (IS_ENCRYPTED(dir)) {
658*4882a593Smuzhiyun 		err = fscrypt_require_key(dir);
659*4882a593Smuzhiyun 		if (err)
660*4882a593Smuzhiyun 			return ERR_PTR(err);
661*4882a593Smuzhiyun 		return &dir->i_crypt_info->ci_policy;
662*4882a593Smuzhiyun 	}
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	return fscrypt_get_dummy_policy(dir->i_sb);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun /**
668*4882a593Smuzhiyun  * fscrypt_set_context() - Set the fscrypt context of a new inode
669*4882a593Smuzhiyun  * @inode: a new inode
670*4882a593Smuzhiyun  * @fs_data: private data given by FS and passed to ->set_context()
671*4882a593Smuzhiyun  *
672*4882a593Smuzhiyun  * This should be called after fscrypt_prepare_new_inode(), generally during a
673*4882a593Smuzhiyun  * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
676*4882a593Smuzhiyun  */
fscrypt_set_context(struct inode * inode,void * fs_data)677*4882a593Smuzhiyun int fscrypt_set_context(struct inode *inode, void *fs_data)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	struct fscrypt_info *ci = inode->i_crypt_info;
680*4882a593Smuzhiyun 	union fscrypt_context ctx;
681*4882a593Smuzhiyun 	int ctxsize;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	/* fscrypt_prepare_new_inode() should have set up the key already. */
684*4882a593Smuzhiyun 	if (WARN_ON_ONCE(!ci))
685*4882a593Smuzhiyun 		return -ENOKEY;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
688*4882a593Smuzhiyun 	ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	/*
691*4882a593Smuzhiyun 	 * This may be the first time the inode number is available, so do any
692*4882a593Smuzhiyun 	 * delayed key setup that requires the inode number.
693*4882a593Smuzhiyun 	 */
694*4882a593Smuzhiyun 	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
695*4882a593Smuzhiyun 	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
696*4882a593Smuzhiyun 		fscrypt_hash_inode_number(ci, ci->ci_master_key);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_set_context);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun /**
703*4882a593Smuzhiyun  * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
704*4882a593Smuzhiyun  * @sb: the filesystem on which test_dummy_encryption is being specified
705*4882a593Smuzhiyun  * @arg: the argument to the test_dummy_encryption option.  May be NULL.
706*4882a593Smuzhiyun  * @dummy_policy: the filesystem's current dummy policy (input/output, see
707*4882a593Smuzhiyun  *		  below)
708*4882a593Smuzhiyun  *
709*4882a593Smuzhiyun  * Handle the test_dummy_encryption mount option by creating a dummy encryption
710*4882a593Smuzhiyun  * policy, saving it in @dummy_policy, and adding the corresponding dummy
711*4882a593Smuzhiyun  * encryption key to the filesystem.  If the @dummy_policy is already set, then
712*4882a593Smuzhiyun  * instead validate that it matches @arg.  Don't support changing it via
713*4882a593Smuzhiyun  * remount, as that is difficult to do safely.
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * Return: 0 on success (dummy policy set, or the same policy is already set);
716*4882a593Smuzhiyun  *         -EEXIST if a different dummy policy is already set;
717*4882a593Smuzhiyun  *         or another -errno value.
718*4882a593Smuzhiyun  */
fscrypt_set_test_dummy_encryption(struct super_block * sb,const char * arg,struct fscrypt_dummy_policy * dummy_policy)719*4882a593Smuzhiyun int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
720*4882a593Smuzhiyun 				      struct fscrypt_dummy_policy *dummy_policy)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	struct fscrypt_key_specifier key_spec = { 0 };
723*4882a593Smuzhiyun 	int version;
724*4882a593Smuzhiyun 	union fscrypt_policy *policy = NULL;
725*4882a593Smuzhiyun 	int err;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	if (!arg)
728*4882a593Smuzhiyun 		arg = "v2";
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	if (!strcmp(arg, "v1")) {
731*4882a593Smuzhiyun 		version = FSCRYPT_POLICY_V1;
732*4882a593Smuzhiyun 		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
733*4882a593Smuzhiyun 		memset(key_spec.u.descriptor, 0x42,
734*4882a593Smuzhiyun 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
735*4882a593Smuzhiyun 	} else if (!strcmp(arg, "v2")) {
736*4882a593Smuzhiyun 		version = FSCRYPT_POLICY_V2;
737*4882a593Smuzhiyun 		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
738*4882a593Smuzhiyun 		/* key_spec.u.identifier gets filled in when adding the key */
739*4882a593Smuzhiyun 	} else {
740*4882a593Smuzhiyun 		err = -EINVAL;
741*4882a593Smuzhiyun 		goto out;
742*4882a593Smuzhiyun 	}
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
745*4882a593Smuzhiyun 	if (!policy) {
746*4882a593Smuzhiyun 		err = -ENOMEM;
747*4882a593Smuzhiyun 		goto out;
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	err = fscrypt_add_test_dummy_key(sb, &key_spec);
751*4882a593Smuzhiyun 	if (err)
752*4882a593Smuzhiyun 		goto out;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	policy->version = version;
755*4882a593Smuzhiyun 	switch (policy->version) {
756*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V1:
757*4882a593Smuzhiyun 		policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
758*4882a593Smuzhiyun 		policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
759*4882a593Smuzhiyun 		memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
760*4882a593Smuzhiyun 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
761*4882a593Smuzhiyun 		break;
762*4882a593Smuzhiyun 	case FSCRYPT_POLICY_V2:
763*4882a593Smuzhiyun 		policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
764*4882a593Smuzhiyun 		policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
765*4882a593Smuzhiyun 		memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
766*4882a593Smuzhiyun 		       FSCRYPT_KEY_IDENTIFIER_SIZE);
767*4882a593Smuzhiyun 		break;
768*4882a593Smuzhiyun 	default:
769*4882a593Smuzhiyun 		WARN_ON(1);
770*4882a593Smuzhiyun 		err = -EINVAL;
771*4882a593Smuzhiyun 		goto out;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	if (dummy_policy->policy) {
775*4882a593Smuzhiyun 		if (fscrypt_policies_equal(policy, dummy_policy->policy))
776*4882a593Smuzhiyun 			err = 0;
777*4882a593Smuzhiyun 		else
778*4882a593Smuzhiyun 			err = -EEXIST;
779*4882a593Smuzhiyun 		goto out;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 	dummy_policy->policy = policy;
782*4882a593Smuzhiyun 	policy = NULL;
783*4882a593Smuzhiyun 	err = 0;
784*4882a593Smuzhiyun out:
785*4882a593Smuzhiyun 	kfree(policy);
786*4882a593Smuzhiyun 	return err;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun /**
791*4882a593Smuzhiyun  * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
792*4882a593Smuzhiyun  * @seq: the seq_file to print the option to
793*4882a593Smuzhiyun  * @sep: the separator character to use
794*4882a593Smuzhiyun  * @sb: the filesystem whose options are being shown
795*4882a593Smuzhiyun  *
796*4882a593Smuzhiyun  * Show the test_dummy_encryption mount option, if it was specified.
797*4882a593Smuzhiyun  * This is mainly used for /proc/mounts.
798*4882a593Smuzhiyun  */
fscrypt_show_test_dummy_encryption(struct seq_file * seq,char sep,struct super_block * sb)799*4882a593Smuzhiyun void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
800*4882a593Smuzhiyun 					struct super_block *sb)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
803*4882a593Smuzhiyun 	int vers;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	if (!policy)
806*4882a593Smuzhiyun 		return;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	vers = policy->version;
809*4882a593Smuzhiyun 	if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
810*4882a593Smuzhiyun 		vers = 1;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);
815