Whamcloud - gitweb
LU-14651 llite: extend inode methods with user namespace arg
[fs/lustre-release.git] / libcfs / libcfs / crypto / policy.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Encryption policy functions for per-file encryption support.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  * Copyright (C) 2015, Motorola Mobility.
7  *
8  * Originally written by Michael Halcrow, 2015.
9  * Modified by Jaegeuk Kim, 2015.
10  * Modified by Eric Biggers, 2019 for v2 policy support.
11  */
12 /*
13  * Linux commit 219d54332a09
14  * tags/v5.4
15  */
16
17 #include <linux/random.h>
18 #include <linux/string.h>
19 #include <linux/mount.h>
20 #include <lustre_compat.h>
21 #include "llcrypt_private.h"
22
23 /**
24  * llcrypt_policies_equal - check whether two encryption policies are the same
25  *
26  * Return: %true if equal, else %false
27  */
28 bool llcrypt_policies_equal(const union llcrypt_policy *policy1,
29                             const union llcrypt_policy *policy2)
30 {
31         if (policy1->version != policy2->version)
32                 return false;
33
34         return !memcmp(policy1, policy2, llcrypt_policy_size(policy1));
35 }
36
37 /**
38  * llcrypt_supported_policy - check whether an encryption policy is supported
39  *
40  * Given an encryption policy, check whether all its encryption modes and other
41  * settings are supported by this kernel.  (But we don't currently don't check
42  * for crypto API support here, so attempting to use an algorithm not configured
43  * into the crypto API will still fail later.)
44  *
45  * Return: %true if supported, else %false
46  */
47 bool llcrypt_supported_policy(const union llcrypt_policy *policy_u,
48                               const struct inode *inode)
49 {
50         switch (policy_u->version) {
51         case LLCRYPT_POLICY_V1: {
52                 const struct llcrypt_policy_v1 *policy = &policy_u->v1;
53
54                 if (!llcrypt_valid_enc_modes(policy->contents_encryption_mode,
55                                              policy->filenames_encryption_mode)) {
56                         llcrypt_warn(inode,
57                                      "Unsupported encryption modes (contents %d, filenames %d)",
58                                      policy->contents_encryption_mode,
59                                      policy->filenames_encryption_mode);
60                         return false;
61                 }
62
63                 if (policy->flags & ~LLCRYPT_POLICY_FLAGS_VALID) {
64                         llcrypt_warn(inode,
65                                      "Unsupported encryption flags (0x%02x)",
66                                      policy->flags);
67                         return false;
68                 }
69
70                 return true;
71         }
72         case LLCRYPT_POLICY_V2: {
73                 const struct llcrypt_policy_v2 *policy = &policy_u->v2;
74
75                 if (!llcrypt_valid_enc_modes(policy->contents_encryption_mode,
76                                              policy->filenames_encryption_mode)) {
77                         llcrypt_warn(inode,
78                                      "Unsupported encryption modes (contents %d, filenames %d)",
79                                      policy->contents_encryption_mode,
80                                      policy->filenames_encryption_mode);
81                         return false;
82                 }
83
84                 if (policy->flags & ~LLCRYPT_POLICY_FLAGS_VALID) {
85                         llcrypt_warn(inode,
86                                      "Unsupported encryption flags (0x%02x)",
87                                      policy->flags);
88                         return false;
89                 }
90
91                 if (memchr_inv(policy->__reserved, 0,
92                                sizeof(policy->__reserved))) {
93                         llcrypt_warn(inode,
94                                      "Reserved bits set in encryption policy");
95                         return false;
96                 }
97
98                 return true;
99         }
100         }
101         return false;
102 }
103
104 /**
105  * llcrypt_new_context_from_policy - create a new llcrypt_context from a policy
106  *
107  * Create an llcrypt_context for an inode that is being assigned the given
108  * encryption policy.  A new nonce is randomly generated.
109  *
110  * Return: the size of the new context in bytes.
111  */
112 static int llcrypt_new_context_from_policy(union llcrypt_context *ctx_u,
113                                            const union llcrypt_policy *policy_u)
114 {
115         memset(ctx_u, 0, sizeof(*ctx_u));
116
117         switch (policy_u->version) {
118         case LLCRYPT_POLICY_V1: {
119                 const struct llcrypt_policy_v1 *policy = &policy_u->v1;
120                 struct llcrypt_context_v1 *ctx = &ctx_u->v1;
121
122                 ctx->version = LLCRYPT_CONTEXT_V1;
123                 ctx->contents_encryption_mode =
124                         policy->contents_encryption_mode;
125                 ctx->filenames_encryption_mode =
126                         policy->filenames_encryption_mode;
127                 ctx->flags = policy->flags;
128                 memcpy(ctx->master_key_descriptor,
129                        policy->master_key_descriptor,
130                        sizeof(ctx->master_key_descriptor));
131                 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
132                 return sizeof(*ctx);
133         }
134         case LLCRYPT_POLICY_V2: {
135                 const struct llcrypt_policy_v2 *policy = &policy_u->v2;
136                 struct llcrypt_context_v2 *ctx = &ctx_u->v2;
137
138                 ctx->version = LLCRYPT_CONTEXT_V2;
139                 ctx->contents_encryption_mode =
140                         policy->contents_encryption_mode;
141                 ctx->filenames_encryption_mode =
142                         policy->filenames_encryption_mode;
143                 ctx->flags = policy->flags;
144                 memcpy(ctx->master_key_identifier,
145                        policy->master_key_identifier,
146                        sizeof(ctx->master_key_identifier));
147                 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
148                 return sizeof(*ctx);
149         }
150         }
151         BUG();
152 }
153
154 /**
155  * llcrypt_policy_from_context - convert an llcrypt_context to an llcrypt_policy
156  *
157  * Given an llcrypt_context, build the corresponding llcrypt_policy.
158  *
159  * Return: 0 on success, or -EINVAL if the llcrypt_context has an unrecognized
160  * version number or size.
161  *
162  * This does *not* validate the settings within the policy itself, e.g. the
163  * modes, flags, and reserved bits.  Use llcrypt_supported_policy() for that.
164  */
165 int llcrypt_policy_from_context(union llcrypt_policy *policy_u,
166                                 const union llcrypt_context *ctx_u,
167                                 int ctx_size)
168 {
169         memset(policy_u, 0, sizeof(*policy_u));
170
171         if (ctx_size <= 0 || ctx_size != llcrypt_context_size(ctx_u))
172                 return -EINVAL;
173
174         switch (ctx_u->version) {
175         case LLCRYPT_CONTEXT_V1: {
176                 const struct llcrypt_context_v1 *ctx = &ctx_u->v1;
177                 struct llcrypt_policy_v1 *policy = &policy_u->v1;
178
179                 policy->version = LLCRYPT_POLICY_V1;
180                 policy->contents_encryption_mode =
181                         ctx->contents_encryption_mode;
182                 policy->filenames_encryption_mode =
183                         ctx->filenames_encryption_mode;
184                 policy->flags = ctx->flags;
185                 memcpy(policy->master_key_descriptor,
186                        ctx->master_key_descriptor,
187                        sizeof(policy->master_key_descriptor));
188                 return 0;
189         }
190         case LLCRYPT_CONTEXT_V2: {
191                 const struct llcrypt_context_v2 *ctx = &ctx_u->v2;
192                 struct llcrypt_policy_v2 *policy = &policy_u->v2;
193
194                 policy->version = LLCRYPT_POLICY_V2;
195                 policy->contents_encryption_mode =
196                         ctx->contents_encryption_mode;
197                 policy->filenames_encryption_mode =
198                         ctx->filenames_encryption_mode;
199                 policy->flags = ctx->flags;
200                 memcpy(policy->__reserved, ctx->__reserved,
201                        sizeof(policy->__reserved));
202                 memcpy(policy->master_key_identifier,
203                        ctx->master_key_identifier,
204                        sizeof(policy->master_key_identifier));
205                 return 0;
206         }
207         }
208         /* unreachable */
209         return -EINVAL;
210 }
211
212 /* Retrieve an inode's encryption policy */
213 static int llcrypt_get_policy(struct inode *inode, union llcrypt_policy *policy)
214 {
215         const struct llcrypt_info *ci;
216         union llcrypt_context ctx;
217         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
218         int ret;
219
220         ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
221         if (ci) {
222                 /* key available, use the cached policy */
223                 *policy = ci->ci_policy;
224                 return 0;
225         }
226
227         if (!IS_ENCRYPTED(inode))
228                 return -ENODATA;
229
230         if (!lsi)
231                 return -ENODATA;
232
233         ret = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
234         if (ret < 0)
235                 return (ret == -ERANGE) ? -EINVAL : ret;
236
237         return llcrypt_policy_from_context(policy, &ctx, ret);
238 }
239
240 static int set_encryption_policy(struct inode *inode,
241                                  const union llcrypt_policy *policy)
242 {
243         union llcrypt_context ctx;
244         int ctxsize;
245         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
246         int err;
247
248         if (!llcrypt_supported_policy(policy, inode))
249                 return -EINVAL;
250
251         switch (policy->version) {
252         case LLCRYPT_POLICY_V1:
253                 /*
254                  * The original encryption policy version provided no way of
255                  * verifying that the correct master key was supplied, which was
256                  * insecure in scenarios where multiple users have access to the
257                  * same encrypted files (even just read-only access).  The new
258                  * encryption policy version fixes this and also implies use of
259                  * an improved key derivation function and allows non-root users
260                  * to securely remove keys.  So as long as compatibility with
261                  * old kernels isn't required, it is recommended to use the new
262                  * policy version for all new encrypted directories.
263                  */
264                 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
265                              current->comm, current->pid);
266                 break;
267         case LLCRYPT_POLICY_V2:
268                 err = llcrypt_verify_key_added(inode->i_sb,
269                                                policy->v2.master_key_identifier);
270                 if (err)
271                         return err;
272                 break;
273         default:
274                 WARN_ON(1);
275                 return -EINVAL;
276         }
277
278         ctxsize = llcrypt_new_context_from_policy(&ctx, policy);
279
280         if (!lsi)
281                 return -EINVAL;
282
283         return lsi->lsi_cop->set_context(inode, &ctx, ctxsize, NULL);
284 }
285
286 /* Tell if an inode's encryption policy has filename encryption */
287 bool llcrypt_policy_has_filename_enc(struct inode *inode)
288 {
289         union llcrypt_policy policy;
290         int err;
291
292         err = llcrypt_get_policy(inode, &policy);
293         if (err)
294                 return true;
295
296         if ((policy.version == LLCRYPT_POLICY_V1 &&
297              policy.v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) ||
298             (policy.version == LLCRYPT_POLICY_V2 &&
299              policy.v2.filenames_encryption_mode == LLCRYPT_MODE_NULL))
300                 return false;
301         return true;
302 }
303 EXPORT_SYMBOL(llcrypt_policy_has_filename_enc);
304
305 int llcrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
306 {
307         union llcrypt_policy policy;
308         union llcrypt_policy existing_policy;
309         struct inode *inode = file_inode(filp);
310         u8 version;
311         int size;
312         int ret;
313
314         if (get_user(policy.version, (const u8 __user *)arg))
315                 return -EFAULT;
316
317         size = llcrypt_policy_size(&policy);
318         if (size <= 0)
319                 return -EINVAL;
320
321         /*
322          * We should just copy the remaining 'size - 1' bytes here, but a
323          * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
324          * think that size can be 0 here (despite the check above!) *and* that
325          * it's a compile-time constant.  Thus it would think copy_from_user()
326          * is passed compile-time constant ULONG_MAX, causing the compile-time
327          * buffer overflow check to fail, breaking the build. This only occurred
328          * when building an i386 kernel with -Os and branch profiling enabled.
329          *
330          * Work around it by just copying the first byte again...
331          */
332         version = policy.version;
333         if (copy_from_user(&policy, arg, size))
334                 return -EFAULT;
335         policy.version = version;
336
337         if (!inode_owner_or_capable(&init_user_ns, inode))
338                 return -EACCES;
339
340         ret = mnt_want_write_file(filp);
341         if (ret)
342                 return ret;
343
344         inode_lock(inode);
345
346         ret = llcrypt_get_policy(inode, &existing_policy);
347         if (ret == -ENODATA) {
348                 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
349
350                 if (!S_ISDIR(inode->i_mode))
351                         ret = -ENOTDIR;
352                 else if (IS_DEADDIR(inode))
353                         ret = -ENOENT;
354                 else if (lsi && !lsi->lsi_cop->empty_dir(inode))
355                         ret = -ENOTEMPTY;
356                 else
357                         ret = set_encryption_policy(inode, &policy);
358         } else if (ret == -EINVAL ||
359                    (ret == 0 && !llcrypt_policies_equal(&policy,
360                                                         &existing_policy))) {
361                 /* The file already uses a different encryption policy. */
362                 ret = -EEXIST;
363         }
364
365         inode_unlock(inode);
366
367         mnt_drop_write_file(filp);
368         return ret;
369 }
370 EXPORT_SYMBOL(llcrypt_ioctl_set_policy);
371
372 /* Original ioctl version; can only get the original policy version */
373 int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
374 {
375         union llcrypt_policy policy;
376         int err;
377
378         err = llcrypt_get_policy(file_inode(filp), &policy);
379         if (err)
380                 return err;
381
382         if (policy.version != LLCRYPT_POLICY_V1)
383                 return -EINVAL;
384
385         if (copy_to_user(arg, &policy, sizeof(policy.v1)))
386                 return -EFAULT;
387         return 0;
388 }
389 EXPORT_SYMBOL(llcrypt_ioctl_get_policy);
390
391 /* Valid filenames_encryption_mode associated with contents_encryption_mode,
392  * as imposed by llcrypt_valid_enc_modes()
393  */
394 static inline u8 contents2filenames_encmode(u8 contents_encryption_mode)
395 {
396         if (contents_encryption_mode == LLCRYPT_MODE_AES_128_CBC)
397                 return LLCRYPT_MODE_AES_128_CTS;
398         if (contents_encryption_mode == LLCRYPT_MODE_AES_256_XTS)
399                 return LLCRYPT_MODE_AES_256_CTS;
400         if (contents_encryption_mode == LLCRYPT_MODE_ADIANTUM)
401                 return LLCRYPT_MODE_ADIANTUM;
402         return LLCRYPT_MODE_NULL;
403 }
404
405 /* Extended ioctl version; can get policies of any version */
406 int llcrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
407 {
408         struct llcrypt_get_policy_ex_arg arg;
409         union llcrypt_policy *policy = (union llcrypt_policy *)&arg.policy;
410         size_t policy_size;
411         struct inode *inode = file_inode(filp);
412         int err;
413
414         /* arg is policy_size, then policy */
415         BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
416         BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
417                      offsetof(typeof(arg), policy));
418         BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
419
420         err = llcrypt_get_policy(file_inode(filp), policy);
421         if (err)
422                 return err;
423         policy_size = llcrypt_policy_size(policy);
424
425         if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
426                 return -EFAULT;
427
428         if (policy_size > arg.policy_size)
429                 return -EOVERFLOW;
430         arg.policy_size = policy_size;
431
432         /* Do not return null filenames_encryption_mode to userspace, as it is
433          * unknown. Instead, return valid mode associated with
434          * contents_encryption_mode, as imposed by llcrypt_valid_enc_modes().
435          */
436         switch (policy->version) {
437         case LLCRYPT_POLICY_V1:
438                 if (policy->v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
439                         policy->v1.filenames_encryption_mode =
440                                 contents2filenames_encmode(
441                                         policy->v1.contents_encryption_mode);
442                         CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
443                               inode->i_ino,
444                               policy->v1.filenames_encryption_mode);
445                 }
446                 break;
447         case LLCRYPT_POLICY_V2:
448                 if (policy->v2.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
449                         policy->v2.filenames_encryption_mode =
450                                 contents2filenames_encmode(
451                                         policy->v2.contents_encryption_mode);
452                         CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
453                               inode->i_ino,
454                               policy->v2.filenames_encryption_mode);
455                 }
456                 break;
457         }
458
459         if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
460                 return -EFAULT;
461         return 0;
462 }
463 EXPORT_SYMBOL_GPL(llcrypt_ioctl_get_policy_ex);
464
465 /**
466  * llcrypt_has_permitted_context() - is a file's encryption policy permitted
467  *                                   within its directory?
468  *
469  * @parent: inode for parent directory
470  * @child: inode for file being looked up, opened, or linked into @parent
471  *
472  * Filesystems must call this before permitting access to an inode in a
473  * situation where the parent directory is encrypted (either before allowing
474  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
475  * and before any operation that involves linking an inode into an encrypted
476  * directory, including link, rename, and cross rename.  It enforces the
477  * constraint that within a given encrypted directory tree, all files use the
478  * same encryption policy.  The pre-access check is needed to detect potentially
479  * malicious offline violations of this constraint, while the link and rename
480  * checks are needed to prevent online violations of this constraint.
481  *
482  * Return: 1 if permitted, 0 if forbidden.
483  */
484 int llcrypt_has_permitted_context(struct inode *parent, struct inode *child)
485 {
486         union llcrypt_policy parent_policy, child_policy;
487         int err;
488
489         /* No restrictions on file types which are never encrypted */
490         if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
491             !S_ISLNK(child->i_mode))
492                 return 1;
493
494         /* No restrictions if the parent directory is unencrypted */
495         if (!IS_ENCRYPTED(parent))
496                 return 1;
497
498         /* Encrypted directories must not contain unencrypted files */
499         if (!IS_ENCRYPTED(child))
500                 return 0;
501
502         /*
503          * Both parent and child are encrypted, so verify they use the same
504          * encryption policy.  Compare the llcrypt_info structs if the keys are
505          * available, otherwise retrieve and compare the llcrypt_contexts.
506          *
507          * Note that the llcrypt_context retrieval will be required frequently
508          * when accessing an encrypted directory tree without the key.
509          * Performance-wise this is not a big deal because we already don't
510          * really optimize for file access without the key (to the extent that
511          * such access is even possible), given that any attempted access
512          * already causes a llcrypt_context retrieval and keyring search.
513          *
514          * In any case, if an unexpected error occurs, fall back to "forbidden".
515          */
516
517         err = llcrypt_get_encryption_info(parent);
518         if (err)
519                 return 0;
520         err = llcrypt_get_encryption_info(child);
521         if (err)
522                 return 0;
523
524         err = llcrypt_get_policy(parent, &parent_policy);
525         if (err)
526                 return 0;
527
528         err = llcrypt_get_policy(child, &child_policy);
529         if (err)
530                 return 0;
531
532         return llcrypt_policies_equal(&parent_policy, &child_policy);
533 }
534 EXPORT_SYMBOL(llcrypt_has_permitted_context);
535
536 /**
537  * llcrypt_inherit_context() - Sets a child context from its parent
538  * @parent: Parent inode from which the context is inherited.
539  * @child:  Child inode that inherits the context from @parent.
540  * @fs_data:  private data given by FS.
541  * @preload:  preload child crypt info if true
542  *
543  * Return: 0 on success, -errno on failure
544  */
545 int llcrypt_inherit_context(struct inode *parent, struct inode *child,
546                                                 void *fs_data, bool preload)
547 {
548         union llcrypt_context ctx;
549         int ctxsize;
550         struct llcrypt_info *ci;
551         struct lustre_sb_info *lsi = s2lsi(parent->i_sb);
552         int res;
553
554         res = llcrypt_get_encryption_info(parent);
555         if (res < 0)
556                 return res;
557
558         ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(parent));
559         if (ci == NULL)
560                 return -ENOKEY;
561
562         if (!lsi)
563                 return -ENOKEY;
564
565         ctxsize = llcrypt_new_context_from_policy(&ctx, &ci->ci_policy);
566
567         BUILD_BUG_ON(sizeof(ctx) != LLCRYPT_SET_CONTEXT_MAX_SIZE);
568         res = lsi->lsi_cop->set_context(child, &ctx, ctxsize, fs_data);
569         if (res)
570                 return res;
571         return preload ? llcrypt_get_encryption_info(child): 0;
572 }
573 EXPORT_SYMBOL(llcrypt_inherit_context);