blob: 4c99972899c79c5a5728cc1592ce80abcd74121c [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>
Theodore Ts'occ4e0df2016-11-26 22:05:18 -050014#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070015
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070016/*
Eric Biggersefee590e2016-12-05 11:12:48 -080017 * check whether an encryption policy is consistent with an encryption context
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070018 */
Eric Biggersefee590e2016-12-05 11:12:48 -080019static 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{
Eric Biggersefee590e2016-12-05 11:12:48 -080023 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;
36 int res;
37
38 if (!inode->i_sb->s_cop->set_context)
39 return -EOPNOTSUPP;
40
41 if (inode->i_sb->s_cop->prepare_context) {
42 res = inode->i_sb->s_cop->prepare_context(inode);
43 if (res)
44 return res;
45 }
46
47 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
48 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
49 FS_KEY_DESCRIPTOR_SIZE);
50
51 if (!fscrypt_valid_contents_enc_mode(
Eric Biggers868e1bc2016-12-05 11:12:47 -080052 policy->contents_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070053 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070054
55 if (!fscrypt_valid_filenames_enc_mode(
Eric Biggers868e1bc2016-12-05 11:12:47 -080056 policy->filenames_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070057 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070058
59 if (policy->flags & ~FS_POLICY_FLAGS_VALID)
60 return -EINVAL;
61
62 ctx.contents_encryption_mode = policy->contents_encryption_mode;
63 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
64 ctx.flags = policy->flags;
65 BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
66 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
67
68 return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
69}
70
Eric Biggersdb717d82016-11-26 19:07:49 -050071int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070072{
Eric Biggersdb717d82016-11-26 19:07:49 -050073 struct fscrypt_policy policy;
Eric Biggersba63f232016-09-08 14:20:38 -070074 struct inode *inode = file_inode(filp);
75 int ret;
Eric Biggersefee590e2016-12-05 11:12:48 -080076 struct fscrypt_context ctx;
Eric Biggersba63f232016-09-08 14:20:38 -070077
Eric Biggersdb717d82016-11-26 19:07:49 -050078 if (copy_from_user(&policy, arg, sizeof(policy)))
79 return -EFAULT;
80
Eric Biggers163ae1c2016-09-08 10:57:08 -070081 if (!inode_owner_or_capable(inode))
82 return -EACCES;
83
Eric Biggersdb717d82016-11-26 19:07:49 -050084 if (policy.version != 0)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070085 return -EINVAL;
86
Eric Biggersba63f232016-09-08 14:20:38 -070087 ret = mnt_want_write_file(filp);
88 if (ret)
89 return ret;
90
Eric Biggers8906a822016-10-15 09:48:50 -040091 inode_lock(inode);
92
Eric Biggersefee590e2016-12-05 11:12:48 -080093 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
94 if (ret == -ENODATA) {
Eric Biggers002ced42016-09-08 11:36:39 -070095 if (!S_ISDIR(inode->i_mode))
Eric Biggersdffd0cf2016-12-05 11:12:45 -080096 ret = -ENOTDIR;
Eric Biggersba63f232016-09-08 14:20:38 -070097 else if (!inode->i_sb->s_cop->empty_dir)
98 ret = -EOPNOTSUPP;
99 else if (!inode->i_sb->s_cop->empty_dir(inode))
100 ret = -ENOTEMPTY;
101 else
102 ret = create_encryption_context_from_policy(inode,
Eric Biggersdb717d82016-11-26 19:07:49 -0500103 &policy);
Eric Biggersefee590e2016-12-05 11:12:48 -0800104 } else if (ret == sizeof(ctx) &&
105 is_encryption_context_consistent_with_policy(&ctx,
106 &policy)) {
107 /* The file already uses the same encryption policy. */
108 ret = 0;
109 } else if (ret >= 0 || ret == -ERANGE) {
110 /* The file already uses a different encryption policy. */
Eric Biggers8488cd92016-12-05 11:12:46 -0800111 ret = -EEXIST;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700112 }
113
Eric Biggers8906a822016-10-15 09:48:50 -0400114 inode_unlock(inode);
115
Eric Biggersba63f232016-09-08 14:20:38 -0700116 mnt_drop_write_file(filp);
117 return ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700118}
Eric Biggersdb717d82016-11-26 19:07:49 -0500119EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700120
Eric Biggersdb717d82016-11-26 19:07:49 -0500121int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700122{
Eric Biggersdb717d82016-11-26 19:07:49 -0500123 struct inode *inode = file_inode(filp);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700124 struct fscrypt_context ctx;
Eric Biggersdb717d82016-11-26 19:07:49 -0500125 struct fscrypt_policy policy;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700126 int res;
127
128 if (!inode->i_sb->s_cop->get_context ||
129 !inode->i_sb->s_cop->is_encrypted(inode))
130 return -ENODATA;
131
132 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
Eric Biggersefee590e2016-12-05 11:12:48 -0800133 if (res < 0 && res != -ERANGE)
134 return res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700135 if (res != sizeof(ctx))
Eric Biggersefee590e2016-12-05 11:12:48 -0800136 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700137 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
138 return -EINVAL;
139
Eric Biggersdb717d82016-11-26 19:07:49 -0500140 policy.version = 0;
141 policy.contents_encryption_mode = ctx.contents_encryption_mode;
142 policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
143 policy.flags = ctx.flags;
144 memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700145 FS_KEY_DESCRIPTOR_SIZE);
Eric Biggersdb717d82016-11-26 19:07:49 -0500146
147 if (copy_to_user(arg, &policy, sizeof(policy)))
148 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700149 return 0;
150}
Eric Biggersdb717d82016-11-26 19:07:49 -0500151EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700152
153int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
154{
155 struct fscrypt_info *parent_ci, *child_ci;
156 int res;
157
158 if ((parent == NULL) || (child == NULL)) {
159 printk(KERN_ERR "parent %p child %p\n", parent, child);
160 BUG_ON(1);
161 }
162
Eric Biggers42d97eb2016-12-19 14:20:13 -0800163 /* No restrictions on file types which are never encrypted */
164 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
165 !S_ISLNK(child->i_mode))
166 return 1;
167
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700168 /* no restrictions if the parent directory is not encrypted */
169 if (!parent->i_sb->s_cop->is_encrypted(parent))
170 return 1;
171 /* if the child directory is not encrypted, this is always a problem */
172 if (!parent->i_sb->s_cop->is_encrypted(child))
173 return 0;
174 res = fscrypt_get_encryption_info(parent);
175 if (res)
176 return 0;
177 res = fscrypt_get_encryption_info(child);
178 if (res)
179 return 0;
180 parent_ci = parent->i_crypt_info;
181 child_ci = child->i_crypt_info;
182 if (!parent_ci && !child_ci)
183 return 1;
184 if (!parent_ci || !child_ci)
185 return 0;
186
187 return (memcmp(parent_ci->ci_master_key,
188 child_ci->ci_master_key,
189 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
190 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
191 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
192 (parent_ci->ci_flags == child_ci->ci_flags));
193}
194EXPORT_SYMBOL(fscrypt_has_permitted_context);
195
196/**
197 * fscrypt_inherit_context() - Sets a child context from its parent
198 * @parent: Parent inode from which the context is inherited.
199 * @child: Child inode that inherits the context from @parent.
200 * @fs_data: private data given by FS.
201 * @preload: preload child i_crypt_info
202 *
203 * Return: Zero on success, non-zero otherwise
204 */
205int fscrypt_inherit_context(struct inode *parent, struct inode *child,
206 void *fs_data, bool preload)
207{
208 struct fscrypt_context ctx;
209 struct fscrypt_info *ci;
210 int res;
211
212 if (!parent->i_sb->s_cop->set_context)
213 return -EOPNOTSUPP;
214
215 res = fscrypt_get_encryption_info(parent);
216 if (res < 0)
217 return res;
218
219 ci = parent->i_crypt_info;
220 if (ci == NULL)
221 return -ENOKEY;
222
223 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
224 if (fscrypt_dummy_context_enabled(parent)) {
225 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
226 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
227 ctx.flags = 0;
228 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
229 res = 0;
230 } else {
231 ctx.contents_encryption_mode = ci->ci_data_mode;
232 ctx.filenames_encryption_mode = ci->ci_filename_mode;
233 ctx.flags = ci->ci_flags;
234 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
235 FS_KEY_DESCRIPTOR_SIZE);
236 }
237 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
238 res = parent->i_sb->s_cop->set_context(child, &ctx,
239 sizeof(ctx), fs_data);
240 if (res)
241 return res;
242 return preload ? fscrypt_get_encryption_info(child): 0;
243}
244EXPORT_SYMBOL(fscrypt_inherit_context);