blob: ccab9ca588fe9e9065172c9041090565e24de2c3 [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(inode))
86 ret = -ENOTEMPTY;
87 else
88 ret = create_encryption_context_from_policy(inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070089 &policy);
90 } else if (ret == sizeof(ctx) &&
91 is_encryption_context_consistent_with_policy(&ctx,
92 &policy)) {
93 /* The file already uses the same encryption policy. */
94 ret = 0;
95 } else if (ret >= 0 || ret == -ERANGE) {
96 /* The file already uses a different encryption policy. */
97 ret = -EEXIST;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070098 }
99
Eric Biggers8906a822016-10-15 09:48:50 -0400100 inode_unlock(inode);
101
Eric Biggersba63f232016-09-08 14:20:38 -0700102 mnt_drop_write_file(filp);
103 return ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700104}
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700105EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700106
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700107int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700108{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700109 struct inode *inode = file_inode(filp);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110 struct fscrypt_context ctx;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700111 struct fscrypt_policy policy;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700112 int res;
113
Eric Biggersd750ec72017-10-09 12:15:36 -0700114 if (!IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700115 return -ENODATA;
116
117 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700118 if (res < 0 && res != -ERANGE)
119 return res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700120 if (res != sizeof(ctx))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700121 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700122 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
123 return -EINVAL;
124
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700125 policy.version = 0;
126 policy.contents_encryption_mode = ctx.contents_encryption_mode;
127 policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
128 policy.flags = ctx.flags;
129 memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700130 FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700131
132 if (copy_to_user(arg, &policy, sizeof(policy)))
133 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700134 return 0;
135}
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700136EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700137
Eric Biggers64a599a2017-04-07 10:58:37 -0700138/**
139 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
140 * within its directory?
141 *
142 * @parent: inode for parent directory
143 * @child: inode for file being looked up, opened, or linked into @parent
144 *
145 * Filesystems must call this before permitting access to an inode in a
146 * situation where the parent directory is encrypted (either before allowing
147 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
148 * and before any operation that involves linking an inode into an encrypted
149 * directory, including link, rename, and cross rename. It enforces the
150 * constraint that within a given encrypted directory tree, all files use the
151 * same encryption policy. The pre-access check is needed to detect potentially
152 * malicious offline violations of this constraint, while the link and rename
153 * checks are needed to prevent online violations of this constraint.
154 *
Eric Biggers15e483d2019-01-22 16:20:21 -0800155 * Return: 1 if permitted, 0 if forbidden.
Eric Biggers64a599a2017-04-07 10:58:37 -0700156 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700157int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
158{
Eric Biggers64a599a2017-04-07 10:58:37 -0700159 const struct fscrypt_operations *cops = parent->i_sb->s_cop;
160 const struct fscrypt_info *parent_ci, *child_ci;
161 struct fscrypt_context parent_ctx, child_ctx;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700162 int res;
163
Eric Biggersd259b682016-12-19 14:20:13 -0800164 /* No restrictions on file types which are never encrypted */
165 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
166 !S_ISLNK(child->i_mode))
167 return 1;
168
Eric Biggers64a599a2017-04-07 10:58:37 -0700169 /* No restrictions if the parent directory is unencrypted */
Eric Biggersd750ec72017-10-09 12:15:36 -0700170 if (!IS_ENCRYPTED(parent))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700171 return 1;
Eric Biggers64a599a2017-04-07 10:58:37 -0700172
173 /* Encrypted directories must not contain unencrypted files */
Eric Biggersd750ec72017-10-09 12:15:36 -0700174 if (!IS_ENCRYPTED(child))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700175 return 0;
Eric Biggers64a599a2017-04-07 10:58:37 -0700176
177 /*
178 * Both parent and child are encrypted, so verify they use the same
179 * encryption policy. Compare the fscrypt_info structs if the keys are
180 * available, otherwise retrieve and compare the fscrypt_contexts.
181 *
182 * Note that the fscrypt_context retrieval will be required frequently
183 * when accessing an encrypted directory tree without the key.
184 * Performance-wise this is not a big deal because we already don't
185 * really optimize for file access without the key (to the extent that
186 * such access is even possible), given that any attempted access
187 * already causes a fscrypt_context retrieval and keyring search.
188 *
189 * In any case, if an unexpected error occurs, fall back to "forbidden".
190 */
191
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700192 res = fscrypt_get_encryption_info(parent);
193 if (res)
194 return 0;
195 res = fscrypt_get_encryption_info(child);
196 if (res)
197 return 0;
Eric Biggers70e22212019-04-11 14:32:15 -0700198 parent_ci = READ_ONCE(parent->i_crypt_info);
199 child_ci = READ_ONCE(child->i_crypt_info);
Eric Biggers64a599a2017-04-07 10:58:37 -0700200
201 if (parent_ci && child_ci) {
Eric Biggers6296c892018-08-07 08:22:25 +0200202 return memcmp(parent_ci->ci_master_key_descriptor,
203 child_ci->ci_master_key_descriptor,
Eric Biggers64a599a2017-04-07 10:58:37 -0700204 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
205 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
206 (parent_ci->ci_filename_mode ==
207 child_ci->ci_filename_mode) &&
208 (parent_ci->ci_flags == child_ci->ci_flags);
209 }
210
211 res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx));
212 if (res != sizeof(parent_ctx))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700213 return 0;
214
Eric Biggers64a599a2017-04-07 10:58:37 -0700215 res = cops->get_context(child, &child_ctx, sizeof(child_ctx));
216 if (res != sizeof(child_ctx))
217 return 0;
218
219 return memcmp(parent_ctx.master_key_descriptor,
220 child_ctx.master_key_descriptor,
221 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
222 (parent_ctx.contents_encryption_mode ==
223 child_ctx.contents_encryption_mode) &&
224 (parent_ctx.filenames_encryption_mode ==
225 child_ctx.filenames_encryption_mode) &&
226 (parent_ctx.flags == child_ctx.flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700227}
228EXPORT_SYMBOL(fscrypt_has_permitted_context);
229
230/**
231 * fscrypt_inherit_context() - Sets a child context from its parent
232 * @parent: Parent inode from which the context is inherited.
233 * @child: Child inode that inherits the context from @parent.
234 * @fs_data: private data given by FS.
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700235 * @preload: preload child i_crypt_info if true
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700236 *
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700237 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700238 */
239int fscrypt_inherit_context(struct inode *parent, struct inode *child,
240 void *fs_data, bool preload)
241{
242 struct fscrypt_context ctx;
243 struct fscrypt_info *ci;
244 int res;
245
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700246 res = fscrypt_get_encryption_info(parent);
247 if (res < 0)
248 return res;
249
Eric Biggers70e22212019-04-11 14:32:15 -0700250 ci = READ_ONCE(parent->i_crypt_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700251 if (ci == NULL)
252 return -ENOKEY;
253
254 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700255 ctx.contents_encryption_mode = ci->ci_data_mode;
256 ctx.filenames_encryption_mode = ci->ci_filename_mode;
257 ctx.flags = ci->ci_flags;
Eric Biggers6296c892018-08-07 08:22:25 +0200258 memcpy(ctx.master_key_descriptor, ci->ci_master_key_descriptor,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700259 FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700260 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
261 res = parent->i_sb->s_cop->set_context(child, &ctx,
262 sizeof(ctx), fs_data);
263 if (res)
264 return res;
265 return preload ? fscrypt_get_encryption_info(child): 0;
266}
267EXPORT_SYMBOL(fscrypt_inherit_context);