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