GNU Linux-libre 4.4.284-gnu1
[releases.git] / fs / f2fs / crypto_policy.c
1 /*
2  * copied from linux/fs/ext4/crypto_policy.c
3  *
4  * Copyright (C) 2015, Google, Inc.
5  * Copyright (C) 2015, Motorola Mobility.
6  *
7  * This contains encryption policy functions for f2fs with some modifications
8  * to support f2fs-specific xattr APIs.
9  *
10  * Written by Michael Halcrow, 2015.
11  * Modified by Jaegeuk Kim, 2015.
12  */
13 #include <linux/random.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/f2fs_fs.h>
17
18 #include "f2fs.h"
19 #include "xattr.h"
20
21 static int f2fs_inode_has_encryption_context(struct inode *inode)
22 {
23         int res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
24                         F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0, NULL);
25         return (res > 0);
26 }
27
28 /*
29  * check whether the policy is consistent with the encryption context
30  * for the inode
31  */
32 static int f2fs_is_encryption_context_consistent_with_policy(
33         struct inode *inode, const struct f2fs_encryption_policy *policy)
34 {
35         struct f2fs_encryption_context ctx;
36         int res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
37                                 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
38                                 sizeof(ctx), NULL);
39
40         if (res != sizeof(ctx))
41                 return 0;
42
43         return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
44                                 F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
45                         (ctx.flags == policy->flags) &&
46                         (ctx.contents_encryption_mode ==
47                          policy->contents_encryption_mode) &&
48                         (ctx.filenames_encryption_mode ==
49                          policy->filenames_encryption_mode));
50 }
51
52 static int f2fs_create_encryption_context_from_policy(
53         struct inode *inode, const struct f2fs_encryption_policy *policy)
54 {
55         struct f2fs_encryption_context ctx;
56
57         ctx.format = F2FS_ENCRYPTION_CONTEXT_FORMAT_V1;
58         memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
59                         F2FS_KEY_DESCRIPTOR_SIZE);
60
61         if (!f2fs_valid_contents_enc_mode(policy->contents_encryption_mode)) {
62                 printk(KERN_WARNING
63                        "%s: Invalid contents encryption mode %d\n", __func__,
64                         policy->contents_encryption_mode);
65                 return -EINVAL;
66         }
67
68         if (!f2fs_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
69                 printk(KERN_WARNING
70                        "%s: Invalid filenames encryption mode %d\n", __func__,
71                         policy->filenames_encryption_mode);
72                 return -EINVAL;
73         }
74
75         if (policy->flags & ~F2FS_POLICY_FLAGS_VALID)
76                 return -EINVAL;
77
78         ctx.contents_encryption_mode = policy->contents_encryption_mode;
79         ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
80         ctx.flags = policy->flags;
81         BUILD_BUG_ON(sizeof(ctx.nonce) != F2FS_KEY_DERIVATION_NONCE_SIZE);
82         get_random_bytes(ctx.nonce, F2FS_KEY_DERIVATION_NONCE_SIZE);
83
84         return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
85                         F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
86                         sizeof(ctx), NULL, XATTR_CREATE);
87 }
88
89 int f2fs_process_policy(const struct f2fs_encryption_policy *policy,
90                         struct inode *inode)
91 {
92         if (!inode_owner_or_capable(inode))
93                 return -EACCES;
94
95         if (policy->version != 0)
96                 return -EINVAL;
97
98         if (!S_ISDIR(inode->i_mode))
99                 return -EINVAL;
100
101         if (!f2fs_inode_has_encryption_context(inode)) {
102                 if (IS_DEADDIR(inode))
103                         return -ENOENT;
104                 if (!f2fs_empty_dir(inode))
105                         return -ENOTEMPTY;
106                 return f2fs_create_encryption_context_from_policy(inode,
107                                                                   policy);
108         }
109
110         if (f2fs_is_encryption_context_consistent_with_policy(inode, policy))
111                 return 0;
112
113         printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
114                __func__);
115         return -EINVAL;
116 }
117
118 int f2fs_get_policy(struct inode *inode, struct f2fs_encryption_policy *policy)
119 {
120         struct f2fs_encryption_context ctx;
121         int res;
122
123         if (!f2fs_encrypted_inode(inode))
124                 return -ENODATA;
125
126         res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
127                                 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
128                                 &ctx, sizeof(ctx), NULL);
129         if (res != sizeof(ctx))
130                 return -ENODATA;
131         if (ctx.format != F2FS_ENCRYPTION_CONTEXT_FORMAT_V1)
132                 return -EINVAL;
133
134         policy->version = 0;
135         policy->contents_encryption_mode = ctx.contents_encryption_mode;
136         policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
137         policy->flags = ctx.flags;
138         memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
139                         F2FS_KEY_DESCRIPTOR_SIZE);
140         return 0;
141 }
142
143 int f2fs_is_child_context_consistent_with_parent(struct inode *parent,
144                                                 struct inode *child)
145 {
146         const struct f2fs_crypt_info *parent_ci, *child_ci;
147         struct f2fs_encryption_context parent_ctx, child_ctx;
148         int res;
149
150         /* 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
155         /* No restrictions if the parent directory is unencrypted */
156         if (!f2fs_encrypted_inode(parent))
157                 return 1;
158
159         /* Encrypted directories must not contain unencrypted files */
160         if (!f2fs_encrypted_inode(child))
161                 return 0;
162
163         /*
164          * Both parent and child are encrypted, so verify they use the same
165          * encryption policy.  Compare the fscrypt_info structs if the keys are
166          * available, otherwise retrieve and compare the fscrypt_contexts.
167          *
168          * Note that the fscrypt_context retrieval will be required frequently
169          * when accessing an encrypted directory tree without the key.
170          * Performance-wise this is not a big deal because we already don't
171          * really optimize for file access without the key (to the extent that
172          * such access is even possible), given that any attempted access
173          * already causes a fscrypt_context retrieval and keyring search.
174          *
175          * In any case, if an unexpected error occurs, fall back to "forbidden".
176          */
177
178         res = f2fs_get_encryption_info(parent);
179         if (res)
180                 return 0;
181         res = f2fs_get_encryption_info(child);
182         if (res)
183                 return 0;
184         parent_ci = F2FS_I(parent)->i_crypt_info;
185         child_ci = F2FS_I(child)->i_crypt_info;
186         if (parent_ci && child_ci) {
187                 return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key,
188                               F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
189                         (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
190                         (parent_ci->ci_filename_mode ==
191                          child_ci->ci_filename_mode) &&
192                         (parent_ci->ci_flags == child_ci->ci_flags);
193         }
194
195         res = f2fs_getxattr(parent, F2FS_XATTR_INDEX_ENCRYPTION,
196                             F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
197                             &parent_ctx, sizeof(parent_ctx), NULL);
198         if (res != sizeof(parent_ctx))
199                 return 0;
200
201         res = f2fs_getxattr(child, F2FS_XATTR_INDEX_ENCRYPTION,
202                             F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
203                             &child_ctx, sizeof(child_ctx), NULL);
204         if (res != sizeof(child_ctx))
205                 return 0;
206
207         return memcmp(parent_ctx.master_key_descriptor,
208                       child_ctx.master_key_descriptor,
209                       F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
210                 (parent_ctx.contents_encryption_mode ==
211                  child_ctx.contents_encryption_mode) &&
212                 (parent_ctx.filenames_encryption_mode ==
213                  child_ctx.filenames_encryption_mode) &&
214                 (parent_ctx.flags == child_ctx.flags);
215 }
216
217 /**
218  * f2fs_inherit_context() - Sets a child context from its parent
219  * @parent: Parent inode from which the context is inherited.
220  * @child:  Child inode that inherits the context from @parent.
221  *
222  * Return: Zero on success, non-zero otherwise
223  */
224 int f2fs_inherit_context(struct inode *parent, struct inode *child,
225                                                 struct page *ipage)
226 {
227         struct f2fs_encryption_context ctx;
228         struct f2fs_crypt_info *ci;
229         int res;
230
231         res = f2fs_get_encryption_info(parent);
232         if (res < 0)
233                 return res;
234
235         ci = F2FS_I(parent)->i_crypt_info;
236         BUG_ON(ci == NULL);
237
238         ctx.format = F2FS_ENCRYPTION_CONTEXT_FORMAT_V1;
239
240         ctx.contents_encryption_mode = ci->ci_data_mode;
241         ctx.filenames_encryption_mode = ci->ci_filename_mode;
242         ctx.flags = ci->ci_flags;
243         memcpy(ctx.master_key_descriptor, ci->ci_master_key,
244                         F2FS_KEY_DESCRIPTOR_SIZE);
245
246         get_random_bytes(ctx.nonce, F2FS_KEY_DERIVATION_NONCE_SIZE);
247         return f2fs_setxattr(child, F2FS_XATTR_INDEX_ENCRYPTION,
248                                 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
249                                 sizeof(ctx), ipage, XATTR_CREATE);
250 }