• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/fs/ext4/crypto_policy.c
3  *
4  * Copyright (C) 2015, Google, Inc.
5  *
6  * This contains encryption policy functions for ext4
7  *
8  * Written by Michael Halcrow, 2015.
9  */
10 
11 #include <linux/random.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
14 
15 #include "ext4_jbd2.h"
16 #include "ext4.h"
17 #include "xattr.h"
18 
ext4_inode_has_encryption_context(struct inode * inode)19 static int ext4_inode_has_encryption_context(struct inode *inode)
20 {
21 	int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
22 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
23 	return (res > 0);
24 }
25 
26 /*
27  * check whether the policy is consistent with the encryption context
28  * for the inode
29  */
ext4_is_encryption_context_consistent_with_policy(struct inode * inode,const struct ext4_encryption_policy * policy)30 static int ext4_is_encryption_context_consistent_with_policy(
31 	struct inode *inode, const struct ext4_encryption_policy *policy)
32 {
33 	struct ext4_encryption_context ctx;
34 	int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
35 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
36 				 sizeof(ctx));
37 	if (res != sizeof(ctx))
38 		return 0;
39 	return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
40 			EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
41 		(ctx.flags ==
42 		 policy->flags) &&
43 		(ctx.contents_encryption_mode ==
44 		 policy->contents_encryption_mode) &&
45 		(ctx.filenames_encryption_mode ==
46 		 policy->filenames_encryption_mode));
47 }
48 
ext4_create_encryption_context_from_policy(struct inode * inode,const struct ext4_encryption_policy * policy)49 static int ext4_create_encryption_context_from_policy(
50 	struct inode *inode, const struct ext4_encryption_policy *policy)
51 {
52 	struct ext4_encryption_context ctx;
53 	handle_t *handle;
54 	int res, res2;
55 
56 	res = ext4_convert_inline_data(inode);
57 	if (res)
58 		return res;
59 
60 	ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
61 	memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
62 	       EXT4_KEY_DESCRIPTOR_SIZE);
63 	if (!ext4_valid_enc_modes(policy->contents_encryption_mode,
64 				  policy->filenames_encryption_mode)) {
65 		printk(KERN_WARNING
66 		       "%s: Invalid encryption modes (contents %d, filenames %d)\n",
67 		       __func__, policy->contents_encryption_mode,
68 		       policy->filenames_encryption_mode);
69 		return -EINVAL;
70 	}
71 	if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
72 		return -EINVAL;
73 	ctx.contents_encryption_mode = policy->contents_encryption_mode;
74 	ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
75 	ctx.flags = policy->flags;
76 	BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
77 	get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
78 
79 	handle = ext4_journal_start(inode, EXT4_HT_MISC,
80 				    ext4_jbd2_credits_xattr(inode));
81 	if (IS_ERR(handle))
82 		return PTR_ERR(handle);
83 	res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
84 			     EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
85 			     sizeof(ctx), 0);
86 	if (!res) {
87 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
88 		res = ext4_mark_inode_dirty(handle, inode);
89 		if (res)
90 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
91 	}
92 	res2 = ext4_journal_stop(handle);
93 	if (!res)
94 		res = res2;
95 	return res;
96 }
97 
ext4_process_policy(const struct ext4_encryption_policy * policy,struct inode * inode)98 int ext4_process_policy(const struct ext4_encryption_policy *policy,
99 			struct inode *inode)
100 {
101 	if (!inode_owner_or_capable(inode))
102 		return -EACCES;
103 
104 	if (policy->version != 0)
105 		return -EINVAL;
106 
107 	if (!ext4_inode_has_encryption_context(inode)) {
108 		if (!S_ISDIR(inode->i_mode))
109 			return -EINVAL;
110 		if (IS_DEADDIR(inode))
111 			return -ENOENT;
112 		if (!ext4_empty_dir(inode))
113 			return -ENOTEMPTY;
114 		return ext4_create_encryption_context_from_policy(inode,
115 								  policy);
116 	}
117 
118 	if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
119 		return 0;
120 
121 	printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
122 	       __func__);
123 	return -EINVAL;
124 }
125 
ext4_get_policy(struct inode * inode,struct ext4_encryption_policy * policy)126 int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
127 {
128 	struct ext4_encryption_context ctx;
129 
130 	int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
131 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
132 				 &ctx, sizeof(ctx));
133 	if (res != sizeof(ctx))
134 		return -ENOENT;
135 	if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
136 		return -EINVAL;
137 	policy->version = 0;
138 	policy->contents_encryption_mode = ctx.contents_encryption_mode;
139 	policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
140 	policy->flags = ctx.flags;
141 	memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
142 	       EXT4_KEY_DESCRIPTOR_SIZE);
143 	return 0;
144 }
145 
ext4_is_child_context_consistent_with_parent(struct inode * parent,struct inode * child)146 int ext4_is_child_context_consistent_with_parent(struct inode *parent,
147 						 struct inode *child)
148 {
149 	const struct ext4_crypt_info *parent_ci, *child_ci;
150 	struct ext4_encryption_context parent_ctx, child_ctx;
151 	int res;
152 
153 	/* No restrictions on file types which are never encrypted */
154 	if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
155 	    !S_ISLNK(child->i_mode))
156 		return 1;
157 
158 	/* No restrictions if the parent directory is unencrypted */
159 	if (!ext4_encrypted_inode(parent))
160 		return 1;
161 
162 	/* Encrypted directories must not contain unencrypted files */
163 	if (!ext4_encrypted_inode(child))
164 		return 0;
165 
166 	/*
167 	 * Both parent and child are encrypted, so verify they use the same
168 	 * encryption policy.  Compare the fscrypt_info structs if the keys are
169 	 * available, otherwise retrieve and compare the fscrypt_contexts.
170 	 *
171 	 * Note that the fscrypt_context retrieval will be required frequently
172 	 * when accessing an encrypted directory tree without the key.
173 	 * Performance-wise this is not a big deal because we already don't
174 	 * really optimize for file access without the key (to the extent that
175 	 * such access is even possible), given that any attempted access
176 	 * already causes a fscrypt_context retrieval and keyring search.
177 	 *
178 	 * In any case, if an unexpected error occurs, fall back to "forbidden".
179 	 */
180 
181 	res = ext4_get_encryption_info(parent);
182 	if (res)
183 		return 0;
184 	res = ext4_get_encryption_info(child);
185 	if (res)
186 		return 0;
187 	parent_ci = EXT4_I(parent)->i_crypt_info;
188 	child_ci = EXT4_I(child)->i_crypt_info;
189 	if (parent_ci && child_ci) {
190 		return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key,
191 			      EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
192 			(parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
193 			(parent_ci->ci_filename_mode ==
194 			 child_ci->ci_filename_mode) &&
195 			(parent_ci->ci_flags == child_ci->ci_flags);
196 	}
197 
198 	res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
199 			     EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
200 			     &parent_ctx, sizeof(parent_ctx));
201 	if (res != sizeof(parent_ctx))
202 		return 0;
203 
204 	res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION,
205 			     EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
206 			     &child_ctx, sizeof(child_ctx));
207 	if (res != sizeof(child_ctx))
208 		return 0;
209 
210 	return memcmp(parent_ctx.master_key_descriptor,
211 		      child_ctx.master_key_descriptor,
212 		      EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
213 		(parent_ctx.contents_encryption_mode ==
214 		 child_ctx.contents_encryption_mode) &&
215 		(parent_ctx.filenames_encryption_mode ==
216 		 child_ctx.filenames_encryption_mode) &&
217 		(parent_ctx.flags == child_ctx.flags);
218 }
219 
220 /**
221  * ext4_inherit_context() - Sets a child context from its parent
222  * @parent: Parent inode from which the context is inherited.
223  * @child:  Child inode that inherits the context from @parent.
224  *
225  * Return: Zero on success, non-zero otherwise
226  */
ext4_inherit_context(struct inode * parent,struct inode * child)227 int ext4_inherit_context(struct inode *parent, struct inode *child)
228 {
229 	struct ext4_encryption_context ctx;
230 	struct ext4_crypt_info *ci;
231 	int res;
232 
233 	res = ext4_get_encryption_info(parent);
234 	if (res < 0)
235 		return res;
236 	ci = EXT4_I(parent)->i_crypt_info;
237 	if (ci == NULL)
238 		return -ENOKEY;
239 
240 	ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
241 	if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
242 		ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
243 		ctx.filenames_encryption_mode =
244 			EXT4_ENCRYPTION_MODE_AES_256_CTS;
245 		ctx.flags = 0;
246 		memset(ctx.master_key_descriptor, 0x42,
247 		       EXT4_KEY_DESCRIPTOR_SIZE);
248 		res = 0;
249 	} else {
250 		ctx.contents_encryption_mode = ci->ci_data_mode;
251 		ctx.filenames_encryption_mode = ci->ci_filename_mode;
252 		ctx.flags = ci->ci_flags;
253 		memcpy(ctx.master_key_descriptor, ci->ci_master_key,
254 		       EXT4_KEY_DESCRIPTOR_SIZE);
255 	}
256 	get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
257 	res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
258 			     EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
259 			     sizeof(ctx), 0);
260 	if (!res) {
261 		ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
262 		ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
263 		res = ext4_get_encryption_info(child);
264 	}
265 	return res;
266 }
267