blob: e258a354b3e544bfe438dea66a0775148a450e06 [file] [log] [blame]
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001/*
2 * Encryption policy functions for per-file encryption support.
3 *
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility.
6 *
7 * Written by Michael Halcrow, 2015.
8 * Modified by Jaegeuk Kim, 2015.
9 */
10
11#include <linux/random.h>
12#include <linux/string.h>
Eric Biggersba63f232016-09-08 14:20:38 -070013#include <linux/mount.h>
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070014#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070015
16/*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070017 * check whether an encryption policy is consistent with an encryption context
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070018 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070019static bool is_encryption_context_consistent_with_policy(
20 const struct fscrypt_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070021 const struct fscrypt_policy *policy)
22{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070023 return memcmp(ctx->master_key_descriptor, policy->master_key_descriptor,
24 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
25 (ctx->flags == policy->flags) &&
26 (ctx->contents_encryption_mode ==
27 policy->contents_encryption_mode) &&
28 (ctx->filenames_encryption_mode ==
29 policy->filenames_encryption_mode);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070030}
31
32static int create_encryption_context_from_policy(struct inode *inode,
33 const struct fscrypt_policy *policy)
34{
35 struct fscrypt_context ctx;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070036
37 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
38 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
39 FS_KEY_DESCRIPTOR_SIZE);
40
Daniel Walter8e989de2017-06-19 09:27:58 +020041 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
42 policy->filenames_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070043 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070044
45 if (policy->flags & ~FS_POLICY_FLAGS_VALID)
46 return -EINVAL;
47
48 ctx.contents_encryption_mode = policy->contents_encryption_mode;
49 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
50 ctx.flags = policy->flags;
51 BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
52 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
53
54 return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
55}
56
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070057int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070058{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070059 struct fscrypt_policy policy;
Eric Biggersba63f232016-09-08 14:20:38 -070060 struct inode *inode = file_inode(filp);
61 int ret;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070062 struct fscrypt_context ctx;
63
64 if (copy_from_user(&policy, arg, sizeof(policy)))
65 return -EFAULT;
Eric Biggersba63f232016-09-08 14:20:38 -070066
Eric Biggers163ae1c2016-09-08 10:57:08 -070067 if (!inode_owner_or_capable(inode))
68 return -EACCES;
69
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070070 if (policy.version != 0)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070071 return -EINVAL;
72
Eric Biggersba63f232016-09-08 14:20:38 -070073 ret = mnt_want_write_file(filp);
74 if (ret)
75 return ret;
76
Eric Biggers8906a822016-10-15 09:48:50 -040077 inode_lock(inode);
78
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070079 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
80 if (ret == -ENODATA) {
Eric Biggers002ced42016-09-08 11:36:39 -070081 if (!S_ISDIR(inode->i_mode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070082 ret = -ENOTDIR;
Hongjie Fange722fb72019-05-22 10:02:53 +080083 else if (IS_DEADDIR(inode))
84 ret = -ENOENT;
Eric Biggersba63f232016-09-08 14:20:38 -070085 else if (!inode->i_sb->s_cop->empty_dir)
86 ret = -EOPNOTSUPP;
87 else if (!inode->i_sb->s_cop->empty_dir(inode))
88 ret = -ENOTEMPTY;
89 else
90 ret = create_encryption_context_from_policy(inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070091 &policy);
92 } else if (ret == sizeof(ctx) &&
93 is_encryption_context_consistent_with_policy(&ctx,
94 &policy)) {
95 /* The file already uses the same encryption policy. */
96 ret = 0;
97 } else if (ret >= 0 || ret == -ERANGE) {
98 /* The file already uses a different encryption policy. */
99 ret = -EEXIST;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700100 }
101
Eric Biggers8906a822016-10-15 09:48:50 -0400102 inode_unlock(inode);
103
Eric Biggersba63f232016-09-08 14:20:38 -0700104 mnt_drop_write_file(filp);
105 return ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700106}
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700107EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700108
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700109int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700111 struct inode *inode = file_inode(filp);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700112 struct fscrypt_context ctx;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700113 struct fscrypt_policy policy;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700114 int res;
115
Eric Biggersd750ec72017-10-09 12:15:36 -0700116 if (!IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700117 return -ENODATA;
118
119 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700120 if (res < 0 && res != -ERANGE)
121 return res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700122 if (res != sizeof(ctx))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700123 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700124 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
125 return -EINVAL;
126
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700127 policy.version = 0;
128 policy.contents_encryption_mode = ctx.contents_encryption_mode;
129 policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
130 policy.flags = ctx.flags;
131 memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700132 FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700133
134 if (copy_to_user(arg, &policy, sizeof(policy)))
135 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700136 return 0;
137}
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700138EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700139
Eric Biggers64a599a2017-04-07 10:58:37 -0700140/**
141 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
142 * within its directory?
143 *
144 * @parent: inode for parent directory
145 * @child: inode for file being looked up, opened, or linked into @parent
146 *
147 * Filesystems must call this before permitting access to an inode in a
148 * situation where the parent directory is encrypted (either before allowing
149 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
150 * and before any operation that involves linking an inode into an encrypted
151 * directory, including link, rename, and cross rename. It enforces the
152 * constraint that within a given encrypted directory tree, all files use the
153 * same encryption policy. The pre-access check is needed to detect potentially
154 * malicious offline violations of this constraint, while the link and rename
155 * checks are needed to prevent online violations of this constraint.
156 *
Eric Biggers15e483d2019-01-22 16:20:21 -0800157 * Return: 1 if permitted, 0 if forbidden.
Eric Biggers64a599a2017-04-07 10:58:37 -0700158 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700159int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
160{
Eric Biggers64a599a2017-04-07 10:58:37 -0700161 const struct fscrypt_operations *cops = parent->i_sb->s_cop;
162 const struct fscrypt_info *parent_ci, *child_ci;
163 struct fscrypt_context parent_ctx, child_ctx;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700164 int res;
165
Eric Biggersd259b682016-12-19 14:20:13 -0800166 /* No restrictions on file types which are never encrypted */
167 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
168 !S_ISLNK(child->i_mode))
169 return 1;
170
Eric Biggers64a599a2017-04-07 10:58:37 -0700171 /* No restrictions if the parent directory is unencrypted */
Eric Biggersd750ec72017-10-09 12:15:36 -0700172 if (!IS_ENCRYPTED(parent))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700173 return 1;
Eric Biggers64a599a2017-04-07 10:58:37 -0700174
175 /* Encrypted directories must not contain unencrypted files */
Eric Biggersd750ec72017-10-09 12:15:36 -0700176 if (!IS_ENCRYPTED(child))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700177 return 0;
Eric Biggers64a599a2017-04-07 10:58:37 -0700178
179 /*
180 * Both parent and child are encrypted, so verify they use the same
181 * encryption policy. Compare the fscrypt_info structs if the keys are
182 * available, otherwise retrieve and compare the fscrypt_contexts.
183 *
184 * Note that the fscrypt_context retrieval will be required frequently
185 * when accessing an encrypted directory tree without the key.
186 * Performance-wise this is not a big deal because we already don't
187 * really optimize for file access without the key (to the extent that
188 * such access is even possible), given that any attempted access
189 * already causes a fscrypt_context retrieval and keyring search.
190 *
191 * In any case, if an unexpected error occurs, fall back to "forbidden".
192 */
193
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700194 res = fscrypt_get_encryption_info(parent);
195 if (res)
196 return 0;
197 res = fscrypt_get_encryption_info(child);
198 if (res)
199 return 0;
200 parent_ci = parent->i_crypt_info;
201 child_ci = child->i_crypt_info;
Eric Biggers64a599a2017-04-07 10:58:37 -0700202
203 if (parent_ci && child_ci) {
Eric Biggers6296c892018-08-07 08:22:25 +0200204 return memcmp(parent_ci->ci_master_key_descriptor,
205 child_ci->ci_master_key_descriptor,
Eric Biggers64a599a2017-04-07 10:58:37 -0700206 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
207 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
208 (parent_ci->ci_filename_mode ==
209 child_ci->ci_filename_mode) &&
210 (parent_ci->ci_flags == child_ci->ci_flags);
211 }
212
213 res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx));
214 if (res != sizeof(parent_ctx))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700215 return 0;
216
Eric Biggers64a599a2017-04-07 10:58:37 -0700217 res = cops->get_context(child, &child_ctx, sizeof(child_ctx));
218 if (res != sizeof(child_ctx))
219 return 0;
220
221 return memcmp(parent_ctx.master_key_descriptor,
222 child_ctx.master_key_descriptor,
223 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
224 (parent_ctx.contents_encryption_mode ==
225 child_ctx.contents_encryption_mode) &&
226 (parent_ctx.filenames_encryption_mode ==
227 child_ctx.filenames_encryption_mode) &&
228 (parent_ctx.flags == child_ctx.flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700229}
230EXPORT_SYMBOL(fscrypt_has_permitted_context);
231
232/**
233 * fscrypt_inherit_context() - Sets a child context from its parent
234 * @parent: Parent inode from which the context is inherited.
235 * @child: Child inode that inherits the context from @parent.
236 * @fs_data: private data given by FS.
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700237 * @preload: preload child i_crypt_info if true
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700238 *
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700239 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700240 */
241int fscrypt_inherit_context(struct inode *parent, struct inode *child,
242 void *fs_data, bool preload)
243{
244 struct fscrypt_context ctx;
245 struct fscrypt_info *ci;
246 int res;
247
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700248 res = fscrypt_get_encryption_info(parent);
249 if (res < 0)
250 return res;
251
252 ci = parent->i_crypt_info;
253 if (ci == NULL)
254 return -ENOKEY;
255
256 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700257 ctx.contents_encryption_mode = ci->ci_data_mode;
258 ctx.filenames_encryption_mode = ci->ci_filename_mode;
259 ctx.flags = ci->ci_flags;
Eric Biggers6296c892018-08-07 08:22:25 +0200260 memcpy(ctx.master_key_descriptor, ci->ci_master_key_descriptor,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700261 FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700262 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
263 res = parent->i_sb->s_cop->set_context(child, &ctx,
264 sizeof(ctx), fs_data);
265 if (res)
266 return res;
267 return preload ? fscrypt_get_encryption_info(child): 0;
268}
269EXPORT_SYMBOL(fscrypt_inherit_context);