blob: d71ec3780d0c454b8c9172ad87bbae407e52f93d [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;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070036
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
38 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
39 FS_KEY_DESCRIPTOR_SIZE);
40
41 if (!fscrypt_valid_contents_enc_mode(
Eric Biggers868e1bc2016-12-05 11:12:47 -080042 policy->contents_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070043 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070044
45 if (!fscrypt_valid_filenames_enc_mode(
Eric Biggers868e1bc2016-12-05 11:12:47 -080046 policy->filenames_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070047 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070048
49 if (policy->flags & ~FS_POLICY_FLAGS_VALID)
50 return -EINVAL;
51
52 ctx.contents_encryption_mode = policy->contents_encryption_mode;
53 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
54 ctx.flags = policy->flags;
55 BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
56 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
57
58 return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
59}
60
Eric Biggersdb717d82016-11-26 19:07:49 -050061int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070062{
Eric Biggersdb717d82016-11-26 19:07:49 -050063 struct fscrypt_policy policy;
Eric Biggersba63f232016-09-08 14:20:38 -070064 struct inode *inode = file_inode(filp);
65 int ret;
Eric Biggersefee590e2016-12-05 11:12:48 -080066 struct fscrypt_context ctx;
Eric Biggersba63f232016-09-08 14:20:38 -070067
Eric Biggersdb717d82016-11-26 19:07:49 -050068 if (copy_from_user(&policy, arg, sizeof(policy)))
69 return -EFAULT;
70
Eric Biggers163ae1c2016-09-08 10:57:08 -070071 if (!inode_owner_or_capable(inode))
72 return -EACCES;
73
Eric Biggersdb717d82016-11-26 19:07:49 -050074 if (policy.version != 0)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070075 return -EINVAL;
76
Eric Biggersba63f232016-09-08 14:20:38 -070077 ret = mnt_want_write_file(filp);
78 if (ret)
79 return ret;
80
Eric Biggers8906a822016-10-15 09:48:50 -040081 inode_lock(inode);
82
Eric Biggersefee590e2016-12-05 11:12:48 -080083 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
84 if (ret == -ENODATA) {
Eric Biggers002ced42016-09-08 11:36:39 -070085 if (!S_ISDIR(inode->i_mode))
Eric Biggersdffd0cf2016-12-05 11:12:45 -080086 ret = -ENOTDIR;
Eric Biggersba63f232016-09-08 14:20:38 -070087 else if (!inode->i_sb->s_cop->empty_dir(inode))
88 ret = -ENOTEMPTY;
89 else
90 ret = create_encryption_context_from_policy(inode,
Eric Biggersdb717d82016-11-26 19:07:49 -050091 &policy);
Eric Biggersefee590e2016-12-05 11:12:48 -080092 } 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. */
Eric Biggers8488cd92016-12-05 11:12:46 -080099 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}
Eric Biggersdb717d82016-11-26 19:07:49 -0500107EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700108
Eric Biggersdb717d82016-11-26 19:07:49 -0500109int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110{
Eric Biggersdb717d82016-11-26 19:07:49 -0500111 struct inode *inode = file_inode(filp);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700112 struct fscrypt_context ctx;
Eric Biggersdb717d82016-11-26 19:07:49 -0500113 struct fscrypt_policy policy;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700114 int res;
115
Eric Biggerscd39e4b2017-04-04 14:39:41 -0700116 if (!inode->i_sb->s_cop->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));
Eric Biggersefee590e2016-12-05 11:12:48 -0800120 if (res < 0 && res != -ERANGE)
121 return res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700122 if (res != sizeof(ctx))
Eric Biggersefee590e2016-12-05 11:12:48 -0800123 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700124 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
125 return -EINVAL;
126
Eric Biggersdb717d82016-11-26 19:07:49 -0500127 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);
Eric Biggersdb717d82016-11-26 19:07:49 -0500133
134 if (copy_to_user(arg, &policy, sizeof(policy)))
135 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700136 return 0;
137}
Eric Biggersdb717d82016-11-26 19:07:49 -0500138EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700139
140int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
141{
142 struct fscrypt_info *parent_ci, *child_ci;
143 int res;
144
145 if ((parent == NULL) || (child == NULL)) {
146 printk(KERN_ERR "parent %p child %p\n", parent, child);
147 BUG_ON(1);
148 }
149
Eric Biggers42d97eb2016-12-19 14:20:13 -0800150 /* No restrictions on file types which are never encrypted */
151 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
152 !S_ISLNK(child->i_mode))
153 return 1;
154
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700155 /* no restrictions if the parent directory is not encrypted */
156 if (!parent->i_sb->s_cop->is_encrypted(parent))
157 return 1;
158 /* if the child directory is not encrypted, this is always a problem */
159 if (!parent->i_sb->s_cop->is_encrypted(child))
160 return 0;
161 res = fscrypt_get_encryption_info(parent);
162 if (res)
163 return 0;
164 res = fscrypt_get_encryption_info(child);
165 if (res)
166 return 0;
167 parent_ci = parent->i_crypt_info;
168 child_ci = child->i_crypt_info;
169 if (!parent_ci && !child_ci)
170 return 1;
171 if (!parent_ci || !child_ci)
172 return 0;
173
174 return (memcmp(parent_ci->ci_master_key,
175 child_ci->ci_master_key,
176 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
177 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
178 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
179 (parent_ci->ci_flags == child_ci->ci_flags));
180}
181EXPORT_SYMBOL(fscrypt_has_permitted_context);
182
183/**
184 * fscrypt_inherit_context() - Sets a child context from its parent
185 * @parent: Parent inode from which the context is inherited.
186 * @child: Child inode that inherits the context from @parent.
187 * @fs_data: private data given by FS.
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500188 * @preload: preload child i_crypt_info if true
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700189 *
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500190 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700191 */
192int fscrypt_inherit_context(struct inode *parent, struct inode *child,
193 void *fs_data, bool preload)
194{
195 struct fscrypt_context ctx;
196 struct fscrypt_info *ci;
197 int res;
198
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700199 res = fscrypt_get_encryption_info(parent);
200 if (res < 0)
201 return res;
202
203 ci = parent->i_crypt_info;
204 if (ci == NULL)
205 return -ENOKEY;
206
207 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500208 ctx.contents_encryption_mode = ci->ci_data_mode;
209 ctx.filenames_encryption_mode = ci->ci_filename_mode;
210 ctx.flags = ci->ci_flags;
211 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
212 FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700213 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
214 res = parent->i_sb->s_cop->set_context(child, &ctx,
215 sizeof(ctx), fs_data);
216 if (res)
217 return res;
218 return preload ? fscrypt_get_encryption_info(child): 0;
219}
220EXPORT_SYMBOL(fscrypt_inherit_context);