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