GNU Linux-libre 4.9.333-gnu1
[releases.git] / fs / crypto / policy.c
1 /*
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>
13 #include <linux/fscrypto.h>
14 #include <linux/mount.h>
15
16 static int inode_has_encryption_context(struct inode *inode)
17 {
18         if (!inode->i_sb->s_cop->get_context)
19                 return 0;
20         return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0);
21 }
22
23 /*
24  * check whether the policy is consistent with the encryption context
25  * for the inode
26  */
27 static int is_encryption_context_consistent_with_policy(struct inode *inode,
28                                 const struct fscrypt_policy *policy)
29 {
30         struct fscrypt_context ctx;
31         int res;
32
33         if (!inode->i_sb->s_cop->get_context)
34                 return 0;
35
36         res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
37         if (res != sizeof(ctx))
38                 return 0;
39
40         return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
41                         FS_KEY_DESCRIPTOR_SIZE) == 0 &&
42                         (ctx.flags == policy->flags) &&
43                         (ctx.contents_encryption_mode ==
44                          policy->contents_encryption_mode) &&
45                         (ctx.filenames_encryption_mode ==
46                          policy->filenames_encryption_mode));
47 }
48
49 static int create_encryption_context_from_policy(struct inode *inode,
50                                 const struct fscrypt_policy *policy)
51 {
52         struct fscrypt_context ctx;
53         int res;
54
55         if (!inode->i_sb->s_cop->set_context)
56                 return -EOPNOTSUPP;
57
58         if (inode->i_sb->s_cop->prepare_context) {
59                 res = inode->i_sb->s_cop->prepare_context(inode);
60                 if (res)
61                         return res;
62         }
63
64         ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
65         memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
66                                         FS_KEY_DESCRIPTOR_SIZE);
67
68         if (!fscrypt_valid_contents_enc_mode(
69                                 policy->contents_encryption_mode)) {
70                 printk(KERN_WARNING
71                        "%s: Invalid contents encryption mode %d\n", __func__,
72                         policy->contents_encryption_mode);
73                 return -EINVAL;
74         }
75
76         if (!fscrypt_valid_filenames_enc_mode(
77                                 policy->filenames_encryption_mode)) {
78                 printk(KERN_WARNING
79                         "%s: Invalid filenames encryption mode %d\n", __func__,
80                         policy->filenames_encryption_mode);
81                 return -EINVAL;
82         }
83
84         if (policy->flags & ~FS_POLICY_FLAGS_VALID)
85                 return -EINVAL;
86
87         ctx.contents_encryption_mode = policy->contents_encryption_mode;
88         ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
89         ctx.flags = policy->flags;
90         BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
91         get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
92
93         return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
94 }
95
96 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
97 {
98         struct fscrypt_policy policy;
99         struct inode *inode = file_inode(filp);
100         int ret;
101
102         if (copy_from_user(&policy, arg, sizeof(policy)))
103                 return -EFAULT;
104
105         if (!inode_owner_or_capable(inode))
106                 return -EACCES;
107
108         if (policy.version != 0)
109                 return -EINVAL;
110
111         ret = mnt_want_write_file(filp);
112         if (ret)
113                 return ret;
114
115         inode_lock(inode);
116
117         if (!inode_has_encryption_context(inode)) {
118                 if (!S_ISDIR(inode->i_mode))
119                         ret = -ENOTDIR;
120                 else if (IS_DEADDIR(inode))
121                         ret = -ENOENT;
122                 else if (!inode->i_sb->s_cop->empty_dir)
123                         ret = -EOPNOTSUPP;
124                 else if (!inode->i_sb->s_cop->empty_dir(inode))
125                         ret = -ENOTEMPTY;
126                 else
127                         ret = create_encryption_context_from_policy(inode,
128                                                                     &policy);
129         } else if (!is_encryption_context_consistent_with_policy(inode,
130                                                                  &policy)) {
131                 printk(KERN_WARNING
132                        "%s: Policy inconsistent with encryption context\n",
133                        __func__);
134                 ret = -EEXIST;
135         }
136
137         inode_unlock(inode);
138
139         mnt_drop_write_file(filp);
140         return ret;
141 }
142 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
143
144 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
145 {
146         struct inode *inode = file_inode(filp);
147         struct fscrypt_context ctx;
148         struct fscrypt_policy policy;
149         int res;
150
151         if (!inode->i_sb->s_cop->get_context ||
152                         !inode->i_sb->s_cop->is_encrypted(inode))
153                 return -ENODATA;
154
155         res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
156         if (res != sizeof(ctx))
157                 return -ENODATA;
158         if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
159                 return -EINVAL;
160
161         policy.version = 0;
162         policy.contents_encryption_mode = ctx.contents_encryption_mode;
163         policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
164         policy.flags = ctx.flags;
165         memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
166                                 FS_KEY_DESCRIPTOR_SIZE);
167
168         if (copy_to_user(arg, &policy, sizeof(policy)))
169                 return -EFAULT;
170         return 0;
171 }
172 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
173
174 /**
175  * fscrypt_has_permitted_context() - is a file's encryption policy permitted
176  *                                   within its directory?
177  *
178  * @parent: inode for parent directory
179  * @child: inode for file being looked up, opened, or linked into @parent
180  *
181  * Filesystems must call this before permitting access to an inode in a
182  * situation where the parent directory is encrypted (either before allowing
183  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
184  * and before any operation that involves linking an inode into an encrypted
185  * directory, including link, rename, and cross rename.  It enforces the
186  * constraint that within a given encrypted directory tree, all files use the
187  * same encryption policy.  The pre-access check is needed to detect potentially
188  * malicious offline violations of this constraint, while the link and rename
189  * checks are needed to prevent online violations of this constraint.
190  *
191  * Return: 1 if permitted, 0 if forbidden.
192  */
193 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
194 {
195         const struct fscrypt_operations *cops = parent->i_sb->s_cop;
196         const struct fscrypt_info *parent_ci, *child_ci;
197         struct fscrypt_context parent_ctx, child_ctx;
198         int res;
199
200         /* No restrictions on file types which are never encrypted */
201         if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
202             !S_ISLNK(child->i_mode))
203                 return 1;
204
205         /* No restrictions if the parent directory is unencrypted */
206         if (!cops->is_encrypted(parent))
207                 return 1;
208
209         /* Encrypted directories must not contain unencrypted files */
210         if (!cops->is_encrypted(child))
211                 return 0;
212
213         /*
214          * Both parent and child are encrypted, so verify they use the same
215          * encryption policy.  Compare the fscrypt_info structs if the keys are
216          * available, otherwise retrieve and compare the fscrypt_contexts.
217          *
218          * Note that the fscrypt_context retrieval will be required frequently
219          * when accessing an encrypted directory tree without the key.
220          * Performance-wise this is not a big deal because we already don't
221          * really optimize for file access without the key (to the extent that
222          * such access is even possible), given that any attempted access
223          * already causes a fscrypt_context retrieval and keyring search.
224          *
225          * In any case, if an unexpected error occurs, fall back to "forbidden".
226          */
227
228         res = fscrypt_get_encryption_info(parent);
229         if (res)
230                 return 0;
231         res = fscrypt_get_encryption_info(child);
232         if (res)
233                 return 0;
234         parent_ci = parent->i_crypt_info;
235         child_ci = child->i_crypt_info;
236
237         if (parent_ci && child_ci) {
238                 return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key,
239                               FS_KEY_DESCRIPTOR_SIZE) == 0 &&
240                         (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
241                         (parent_ci->ci_filename_mode ==
242                          child_ci->ci_filename_mode) &&
243                         (parent_ci->ci_flags == child_ci->ci_flags);
244         }
245
246         res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx));
247         if (res != sizeof(parent_ctx))
248                 return 0;
249
250         res = cops->get_context(child, &child_ctx, sizeof(child_ctx));
251         if (res != sizeof(child_ctx))
252                 return 0;
253
254         return memcmp(parent_ctx.master_key_descriptor,
255                       child_ctx.master_key_descriptor,
256                       FS_KEY_DESCRIPTOR_SIZE) == 0 &&
257                 (parent_ctx.contents_encryption_mode ==
258                  child_ctx.contents_encryption_mode) &&
259                 (parent_ctx.filenames_encryption_mode ==
260                  child_ctx.filenames_encryption_mode) &&
261                 (parent_ctx.flags == child_ctx.flags);
262 }
263 EXPORT_SYMBOL(fscrypt_has_permitted_context);
264
265 /**
266  * fscrypt_inherit_context() - Sets a child context from its parent
267  * @parent: Parent inode from which the context is inherited.
268  * @child:  Child inode that inherits the context from @parent.
269  * @fs_data:  private data given by FS.
270  * @preload:  preload child i_crypt_info
271  *
272  * Return: Zero on success, non-zero otherwise
273  */
274 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
275                                                 void *fs_data, bool preload)
276 {
277         struct fscrypt_context ctx;
278         struct fscrypt_info *ci;
279         int res;
280
281         if (!parent->i_sb->s_cop->set_context)
282                 return -EOPNOTSUPP;
283
284         res = fscrypt_get_encryption_info(parent);
285         if (res < 0)
286                 return res;
287
288         ci = parent->i_crypt_info;
289         if (ci == NULL)
290                 return -ENOKEY;
291
292         ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
293         if (fscrypt_dummy_context_enabled(parent)) {
294                 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
295                 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
296                 ctx.flags = 0;
297                 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
298                 res = 0;
299         } else {
300                 ctx.contents_encryption_mode = ci->ci_data_mode;
301                 ctx.filenames_encryption_mode = ci->ci_filename_mode;
302                 ctx.flags = ci->ci_flags;
303                 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
304                                 FS_KEY_DESCRIPTOR_SIZE);
305         }
306         get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
307         res = parent->i_sb->s_cop->set_context(child, &ctx,
308                                                 sizeof(ctx), fs_data);
309         if (res)
310                 return res;
311         return preload ? fscrypt_get_encryption_info(child): 0;
312 }
313 EXPORT_SYMBOL(fscrypt_inherit_context);