Whamcloud - gitweb
LU-16667 build: struct mnt_idmap, linux/filelock.h
[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         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
311         u8 version;
312         int size;
313         int ret;
314
315         if (get_user(policy.version, (const u8 __user *)arg))
316                 return -EFAULT;
317
318         size = llcrypt_policy_size(&policy);
319         if (size <= 0)
320                 return -EINVAL;
321
322         /*
323          * We should just copy the remaining 'size - 1' bytes here, but a
324          * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
325          * think that size can be 0 here (despite the check above!) *and* that
326          * it's a compile-time constant.  Thus it would think copy_from_user()
327          * is passed compile-time constant ULONG_MAX, causing the compile-time
328          * buffer overflow check to fail, breaking the build. This only occurred
329          * when building an i386 kernel with -Os and branch profiling enabled.
330          *
331          * Work around it by just copying the first byte again...
332          */
333         version = policy.version;
334         if (copy_from_user(&policy, arg, size))
335                 return -EFAULT;
336         policy.version = version;
337
338         /* Force file/directory name encryption policy to null if
339          * LSI_FILENAME_ENC flag is not set on sb.
340          * This allows enabling filename encryption separately from data
341          * encryption, and can be useful for interoperability with
342          * encryption-unaware clients.
343          */
344         if (!(lsi->lsi_flags & LSI_FILENAME_ENC)) {
345                 CWARN("inode %lu: forcing policy filenames_encryption_mode to null\n",
346                       inode->i_ino);
347                 cfs_tty_write_msg("\n\nForcing policy filenames_encryption_mode to null.\n\n");
348                 switch (policy.version) {
349                 case LLCRYPT_POLICY_V1:
350                         policy.v1.filenames_encryption_mode = LLCRYPT_MODE_NULL;
351                         break;
352                 case LLCRYPT_POLICY_V2:
353                         policy.v2.filenames_encryption_mode = LLCRYPT_MODE_NULL;
354                         break;
355                 }
356         }
357
358         if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
359                 return -EACCES;
360
361         ret = mnt_want_write_file(filp);
362         if (ret)
363                 return ret;
364
365         inode_lock(inode);
366
367         ret = llcrypt_get_policy(inode, &existing_policy);
368         if (ret == -ENODATA) {
369                 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
370
371                 if (!S_ISDIR(inode->i_mode))
372                         ret = -ENOTDIR;
373                 else if (IS_DEADDIR(inode))
374                         ret = -ENOENT;
375                 else if (lsi && !lsi->lsi_cop->empty_dir(inode))
376                         ret = -ENOTEMPTY;
377                 else
378                         ret = set_encryption_policy(inode, &policy);
379         } else if (ret == -EINVAL ||
380                    (ret == 0 && !llcrypt_policies_equal(&policy,
381                                                         &existing_policy))) {
382                 /* The file already uses a different encryption policy. */
383                 ret = -EEXIST;
384         }
385
386         inode_unlock(inode);
387
388         mnt_drop_write_file(filp);
389         return ret;
390 }
391 EXPORT_SYMBOL(llcrypt_ioctl_set_policy);
392
393 /* Original ioctl version; can only get the original policy version */
394 int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
395 {
396         union llcrypt_policy policy;
397         int err;
398
399         err = llcrypt_get_policy(file_inode(filp), &policy);
400         if (err)
401                 return err;
402
403         if (policy.version != LLCRYPT_POLICY_V1)
404                 return -EINVAL;
405
406         if (copy_to_user(arg, &policy, sizeof(policy.v1)))
407                 return -EFAULT;
408         return 0;
409 }
410 EXPORT_SYMBOL(llcrypt_ioctl_get_policy);
411
412 /* Valid filenames_encryption_mode associated with contents_encryption_mode,
413  * as imposed by llcrypt_valid_enc_modes()
414  */
415 static inline u8 contents2filenames_encmode(u8 contents_encryption_mode)
416 {
417         if (contents_encryption_mode == LLCRYPT_MODE_AES_128_CBC)
418                 return LLCRYPT_MODE_AES_128_CTS;
419         if (contents_encryption_mode == LLCRYPT_MODE_AES_256_XTS)
420                 return LLCRYPT_MODE_AES_256_CTS;
421         if (contents_encryption_mode == LLCRYPT_MODE_ADIANTUM)
422                 return LLCRYPT_MODE_ADIANTUM;
423         return LLCRYPT_MODE_NULL;
424 }
425
426 /* Extended ioctl version; can get policies of any version */
427 int llcrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
428 {
429         struct llcrypt_get_policy_ex_arg arg;
430         union llcrypt_policy *policy = (union llcrypt_policy *)&arg.policy;
431         size_t policy_size;
432         struct inode *inode = file_inode(filp);
433         int err;
434
435         /* arg is policy_size, then policy */
436         BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
437         BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
438                      offsetof(typeof(arg), policy));
439         BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
440
441         if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
442                 return -EFAULT;
443
444         err = llcrypt_get_policy(file_inode(filp), policy);
445         if (err)
446                 return err;
447         policy_size = llcrypt_policy_size(policy);
448
449         if (policy_size > arg.policy_size)
450                 return -EOVERFLOW;
451         arg.policy_size = policy_size;
452
453         /* Do not return null filenames_encryption_mode to userspace, as it is
454          * unknown. Instead, return valid mode associated with
455          * contents_encryption_mode, as imposed by llcrypt_valid_enc_modes().
456          */
457         switch (policy->version) {
458         case LLCRYPT_POLICY_V1:
459                 if (policy->v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
460                         policy->v1.filenames_encryption_mode =
461                                 contents2filenames_encmode(
462                                         policy->v1.contents_encryption_mode);
463                         CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
464                               inode->i_ino,
465                               policy->v1.filenames_encryption_mode);
466                 }
467                 break;
468         case LLCRYPT_POLICY_V2:
469                 if (policy->v2.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
470                         policy->v2.filenames_encryption_mode =
471                                 contents2filenames_encmode(
472                                         policy->v2.contents_encryption_mode);
473                         CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
474                               inode->i_ino,
475                               policy->v2.filenames_encryption_mode);
476                 }
477                 break;
478         }
479
480         if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
481                 return -EFAULT;
482         return 0;
483 }
484 EXPORT_SYMBOL_GPL(llcrypt_ioctl_get_policy_ex);
485
486 /**
487  * llcrypt_has_permitted_context() - is a file's encryption policy permitted
488  *                                   within its directory?
489  *
490  * @parent: inode for parent directory
491  * @child: inode for file being looked up, opened, or linked into @parent
492  *
493  * Filesystems must call this before permitting access to an inode in a
494  * situation where the parent directory is encrypted (either before allowing
495  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
496  * and before any operation that involves linking an inode into an encrypted
497  * directory, including link, rename, and cross rename.  It enforces the
498  * constraint that within a given encrypted directory tree, all files use the
499  * same encryption policy.  The pre-access check is needed to detect potentially
500  * malicious offline violations of this constraint, while the link and rename
501  * checks are needed to prevent online violations of this constraint.
502  *
503  * Return: 1 if permitted, 0 if forbidden.
504  */
505 int llcrypt_has_permitted_context(struct inode *parent, struct inode *child)
506 {
507         union llcrypt_policy parent_policy, child_policy;
508         int err;
509
510         /* No restrictions on file types which are never encrypted */
511         if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
512             !S_ISLNK(child->i_mode))
513                 return 1;
514
515         /* No restrictions if the parent directory is unencrypted */
516         if (!IS_ENCRYPTED(parent))
517                 return 1;
518
519         /* Encrypted directories must not contain unencrypted files */
520         if (!IS_ENCRYPTED(child))
521                 return 0;
522
523         /*
524          * Both parent and child are encrypted, so verify they use the same
525          * encryption policy.  Compare the llcrypt_info structs if the keys are
526          * available, otherwise retrieve and compare the llcrypt_contexts.
527          *
528          * Note that the llcrypt_context retrieval will be required frequently
529          * when accessing an encrypted directory tree without the key.
530          * Performance-wise this is not a big deal because we already don't
531          * really optimize for file access without the key (to the extent that
532          * such access is even possible), given that any attempted access
533          * already causes a llcrypt_context retrieval and keyring search.
534          *
535          * In any case, if an unexpected error occurs, fall back to "forbidden".
536          */
537
538         err = llcrypt_get_encryption_info(parent);
539         if (err)
540                 return 0;
541         err = llcrypt_get_encryption_info(child);
542         if (err)
543                 return 0;
544
545         err = llcrypt_get_policy(parent, &parent_policy);
546         if (err)
547                 return 0;
548
549         err = llcrypt_get_policy(child, &child_policy);
550         if (err)
551                 return 0;
552
553         return llcrypt_policies_equal(&parent_policy, &child_policy);
554 }
555 EXPORT_SYMBOL(llcrypt_has_permitted_context);
556
557 /**
558  * llcrypt_inherit_context() - Sets a child context from its parent
559  * @parent: Parent inode from which the context is inherited.
560  * @child:  Child inode that inherits the context from @parent.
561  * @fs_data:  private data given by FS.
562  * @preload:  preload child crypt info if true
563  *
564  * Return: 0 on success, -errno on failure
565  */
566 int llcrypt_inherit_context(struct inode *parent, struct inode *child,
567                                                 void *fs_data, bool preload)
568 {
569         union llcrypt_context ctx;
570         int ctxsize;
571         struct llcrypt_info *ci;
572         struct lustre_sb_info *lsi = s2lsi(parent->i_sb);
573         int res;
574
575         res = llcrypt_get_encryption_info(parent);
576         if (res < 0)
577                 return res;
578
579         ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(parent));
580         if (ci == NULL)
581                 return -ENOKEY;
582
583         if (!lsi)
584                 return -ENOKEY;
585
586         ctxsize = llcrypt_new_context_from_policy(&ctx, &ci->ci_policy);
587
588         BUILD_BUG_ON(sizeof(ctx) != LLCRYPT_SET_CONTEXT_MAX_SIZE);
589         res = lsi->lsi_cop->set_context(child, &ctx, ctxsize, fs_data);
590         if (res)
591                 return res;
592         return preload ? llcrypt_get_encryption_info(child): 0;
593 }
594 EXPORT_SYMBOL(llcrypt_inherit_context);