blob: 4908906d54d562263093cd5245fcb14e36d18b8e [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
37 if (!inode->i_sb->s_cop->set_context)
38 return -EOPNOTSUPP;
39
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070040 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
41 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
42 FS_KEY_DESCRIPTOR_SIZE);
43
44 if (!fscrypt_valid_contents_enc_mode(
Eric Biggers868e1bc2016-12-05 11:12:47 -080045 policy->contents_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070046 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070047
48 if (!fscrypt_valid_filenames_enc_mode(
Eric Biggers868e1bc2016-12-05 11:12:47 -080049 policy->filenames_encryption_mode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070050 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070051
52 if (policy->flags & ~FS_POLICY_FLAGS_VALID)
53 return -EINVAL;
54
55 ctx.contents_encryption_mode = policy->contents_encryption_mode;
56 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
57 ctx.flags = policy->flags;
58 BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
59 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
60
61 return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
62}
63
Eric Biggersdb717d82016-11-26 19:07:49 -050064int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070065{
Eric Biggersdb717d82016-11-26 19:07:49 -050066 struct fscrypt_policy policy;
Eric Biggersba63f232016-09-08 14:20:38 -070067 struct inode *inode = file_inode(filp);
68 int ret;
Eric Biggersefee590e2016-12-05 11:12:48 -080069 struct fscrypt_context ctx;
Eric Biggersba63f232016-09-08 14:20:38 -070070
Eric Biggersdb717d82016-11-26 19:07:49 -050071 if (copy_from_user(&policy, arg, sizeof(policy)))
72 return -EFAULT;
73
Eric Biggers163ae1c2016-09-08 10:57:08 -070074 if (!inode_owner_or_capable(inode))
75 return -EACCES;
76
Eric Biggersdb717d82016-11-26 19:07:49 -050077 if (policy.version != 0)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070078 return -EINVAL;
79
Eric Biggersba63f232016-09-08 14:20:38 -070080 ret = mnt_want_write_file(filp);
81 if (ret)
82 return ret;
83
Eric Biggers8906a822016-10-15 09:48:50 -040084 inode_lock(inode);
85
Eric Biggersefee590e2016-12-05 11:12:48 -080086 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
87 if (ret == -ENODATA) {
Eric Biggers002ced42016-09-08 11:36:39 -070088 if (!S_ISDIR(inode->i_mode))
Eric Biggersdffd0cf2016-12-05 11:12:45 -080089 ret = -ENOTDIR;
Eric Biggersba63f232016-09-08 14:20:38 -070090 else if (!inode->i_sb->s_cop->empty_dir)
91 ret = -EOPNOTSUPP;
92 else if (!inode->i_sb->s_cop->empty_dir(inode))
93 ret = -ENOTEMPTY;
94 else
95 ret = create_encryption_context_from_policy(inode,
Eric Biggersdb717d82016-11-26 19:07:49 -050096 &policy);
Eric Biggersefee590e2016-12-05 11:12:48 -080097 } else if (ret == sizeof(ctx) &&
98 is_encryption_context_consistent_with_policy(&ctx,
99 &policy)) {
100 /* The file already uses the same encryption policy. */
101 ret = 0;
102 } else if (ret >= 0 || ret == -ERANGE) {
103 /* The file already uses a different encryption policy. */
Eric Biggers8488cd92016-12-05 11:12:46 -0800104 ret = -EEXIST;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700105 }
106
Eric Biggers8906a822016-10-15 09:48:50 -0400107 inode_unlock(inode);
108
Eric Biggersba63f232016-09-08 14:20:38 -0700109 mnt_drop_write_file(filp);
110 return ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700111}
Eric Biggersdb717d82016-11-26 19:07:49 -0500112EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700113
Eric Biggersdb717d82016-11-26 19:07:49 -0500114int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700115{
Eric Biggersdb717d82016-11-26 19:07:49 -0500116 struct inode *inode = file_inode(filp);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700117 struct fscrypt_context ctx;
Eric Biggersdb717d82016-11-26 19:07:49 -0500118 struct fscrypt_policy policy;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700119 int res;
120
121 if (!inode->i_sb->s_cop->get_context ||
122 !inode->i_sb->s_cop->is_encrypted(inode))
123 return -ENODATA;
124
125 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
Eric Biggersefee590e2016-12-05 11:12:48 -0800126 if (res < 0 && res != -ERANGE)
127 return res;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700128 if (res != sizeof(ctx))
Eric Biggersefee590e2016-12-05 11:12:48 -0800129 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700130 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
131 return -EINVAL;
132
Eric Biggersdb717d82016-11-26 19:07:49 -0500133 policy.version = 0;
134 policy.contents_encryption_mode = ctx.contents_encryption_mode;
135 policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
136 policy.flags = ctx.flags;
137 memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700138 FS_KEY_DESCRIPTOR_SIZE);
Eric Biggersdb717d82016-11-26 19:07:49 -0500139
140 if (copy_to_user(arg, &policy, sizeof(policy)))
141 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700142 return 0;
143}
Eric Biggersdb717d82016-11-26 19:07:49 -0500144EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700145
146int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
147{
148 struct fscrypt_info *parent_ci, *child_ci;
149 int res;
150
151 if ((parent == NULL) || (child == NULL)) {
152 printk(KERN_ERR "parent %p child %p\n", parent, child);
153 BUG_ON(1);
154 }
155
Eric Biggers42d97eb2016-12-19 14:20:13 -0800156 /* No restrictions on file types which are never encrypted */
157 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
158 !S_ISLNK(child->i_mode))
159 return 1;
160
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700161 /* no restrictions if the parent directory is not encrypted */
162 if (!parent->i_sb->s_cop->is_encrypted(parent))
163 return 1;
164 /* if the child directory is not encrypted, this is always a problem */
165 if (!parent->i_sb->s_cop->is_encrypted(child))
166 return 0;
167 res = fscrypt_get_encryption_info(parent);
168 if (res)
169 return 0;
170 res = fscrypt_get_encryption_info(child);
171 if (res)
172 return 0;
173 parent_ci = parent->i_crypt_info;
174 child_ci = child->i_crypt_info;
175 if (!parent_ci && !child_ci)
176 return 1;
177 if (!parent_ci || !child_ci)
178 return 0;
179
180 return (memcmp(parent_ci->ci_master_key,
181 child_ci->ci_master_key,
182 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
183 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
184 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
185 (parent_ci->ci_flags == child_ci->ci_flags));
186}
187EXPORT_SYMBOL(fscrypt_has_permitted_context);
188
189/**
190 * fscrypt_inherit_context() - Sets a child context from its parent
191 * @parent: Parent inode from which the context is inherited.
192 * @child: Child inode that inherits the context from @parent.
193 * @fs_data: private data given by FS.
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500194 * @preload: preload child i_crypt_info if true
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700195 *
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500196 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700197 */
198int fscrypt_inherit_context(struct inode *parent, struct inode *child,
199 void *fs_data, bool preload)
200{
201 struct fscrypt_context ctx;
202 struct fscrypt_info *ci;
203 int res;
204
205 if (!parent->i_sb->s_cop->set_context)
206 return -EOPNOTSUPP;
207
208 res = fscrypt_get_encryption_info(parent);
209 if (res < 0)
210 return res;
211
212 ci = parent->i_crypt_info;
213 if (ci == NULL)
214 return -ENOKEY;
215
216 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500217 ctx.contents_encryption_mode = ci->ci_data_mode;
218 ctx.filenames_encryption_mode = ci->ci_filename_mode;
219 ctx.flags = ci->ci_flags;
220 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
221 FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700222 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
223 res = parent->i_sb->s_cop->set_context(child, &ctx,
224 sizeof(ctx), fs_data);
225 if (res)
226 return res;
227 return preload ? fscrypt_get_encryption_info(child): 0;
228}
229EXPORT_SYMBOL(fscrypt_inherit_context);