GNU Linux-libre 6.1.86-gnu
[releases.git] / fs / verity / signature.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Verification of builtin signatures
4  *
5  * Copyright 2019 Google LLC
6  */
7
8 #include "fsverity_private.h"
9
10 #include <linux/cred.h>
11 #include <linux/key.h>
12 #include <linux/slab.h>
13 #include <linux/verification.h>
14
15 /*
16  * /proc/sys/fs/verity/require_signatures
17  * If 1, all verity files must have a valid builtin signature.
18  */
19 static int fsverity_require_signatures;
20
21 /*
22  * Keyring that contains the trusted X.509 certificates.
23  *
24  * Only root (kuid=0) can modify this.  Also, root may use
25  * keyctl_restrict_keyring() to prevent any more additions.
26  */
27 static struct key *fsverity_keyring;
28
29 /**
30  * fsverity_verify_signature() - check a verity file's signature
31  * @vi: the file's fsverity_info
32  * @signature: the file's built-in signature
33  * @sig_size: size of signature in bytes, or 0 if no signature
34  *
35  * If the file includes a signature of its fs-verity file digest, verify it
36  * against the certificates in the fs-verity keyring.
37  *
38  * Return: 0 on success (signature valid or not required); -errno on failure
39  */
40 int fsverity_verify_signature(const struct fsverity_info *vi,
41                               const u8 *signature, size_t sig_size)
42 {
43         const struct inode *inode = vi->inode;
44         const struct fsverity_hash_alg *hash_alg = vi->tree_params.hash_alg;
45         struct fsverity_formatted_digest *d;
46         int err;
47
48         if (sig_size == 0) {
49                 if (fsverity_require_signatures) {
50                         fsverity_err(inode,
51                                      "require_signatures=1, rejecting unsigned file!");
52                         return -EPERM;
53                 }
54                 return 0;
55         }
56
57         if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
58                 /*
59                  * The ".fs-verity" keyring is empty, due to builtin signatures
60                  * being supported by the kernel but not actually being used.
61                  * In this case, verify_pkcs7_signature() would always return an
62                  * error, usually ENOKEY.  It could also be EBADMSG if the
63                  * PKCS#7 is malformed, but that isn't very important to
64                  * distinguish.  So, just skip to ENOKEY to avoid the attack
65                  * surface of the PKCS#7 parser, which would otherwise be
66                  * reachable by any task able to execute FS_IOC_ENABLE_VERITY.
67                  */
68                 fsverity_err(inode,
69                              "fs-verity keyring is empty, rejecting signed file!");
70                 return -ENOKEY;
71         }
72
73         d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
74         if (!d)
75                 return -ENOMEM;
76         memcpy(d->magic, "FSVerity", 8);
77         d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
78         d->digest_size = cpu_to_le16(hash_alg->digest_size);
79         memcpy(d->digest, vi->file_digest, hash_alg->digest_size);
80
81         err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
82                                      signature, sig_size, fsverity_keyring,
83                                      VERIFYING_UNSPECIFIED_SIGNATURE,
84                                      NULL, NULL);
85         kfree(d);
86
87         if (err) {
88                 if (err == -ENOKEY)
89                         fsverity_err(inode,
90                                      "File's signing cert isn't in the fs-verity keyring");
91                 else if (err == -EKEYREJECTED)
92                         fsverity_err(inode, "Incorrect file signature");
93                 else if (err == -EBADMSG)
94                         fsverity_err(inode, "Malformed file signature");
95                 else
96                         fsverity_err(inode, "Error %d verifying file signature",
97                                      err);
98                 return err;
99         }
100
101         pr_debug("Valid signature for file digest %s:%*phN\n",
102                  hash_alg->name, hash_alg->digest_size, vi->file_digest);
103         return 0;
104 }
105
106 #ifdef CONFIG_SYSCTL
107 static struct ctl_table_header *fsverity_sysctl_header;
108
109 static const struct ctl_path fsverity_sysctl_path[] = {
110         { .procname = "fs", },
111         { .procname = "verity", },
112         { }
113 };
114
115 static struct ctl_table fsverity_sysctl_table[] = {
116         {
117                 .procname       = "require_signatures",
118                 .data           = &fsverity_require_signatures,
119                 .maxlen         = sizeof(int),
120                 .mode           = 0644,
121                 .proc_handler   = proc_dointvec_minmax,
122                 .extra1         = SYSCTL_ZERO,
123                 .extra2         = SYSCTL_ONE,
124         },
125         { }
126 };
127
128 static int __init fsverity_sysctl_init(void)
129 {
130         fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
131                                                        fsverity_sysctl_table);
132         if (!fsverity_sysctl_header) {
133                 pr_err("sysctl registration failed!\n");
134                 return -ENOMEM;
135         }
136         return 0;
137 }
138 #else /* !CONFIG_SYSCTL */
139 static inline int __init fsverity_sysctl_init(void)
140 {
141         return 0;
142 }
143 #endif /* !CONFIG_SYSCTL */
144
145 int __init fsverity_init_signature(void)
146 {
147         struct key *ring;
148         int err;
149
150         ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
151                              current_cred(), KEY_POS_SEARCH |
152                                 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
153                                 KEY_USR_SEARCH | KEY_USR_SETATTR,
154                              KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
155         if (IS_ERR(ring))
156                 return PTR_ERR(ring);
157
158         err = fsverity_sysctl_init();
159         if (err)
160                 goto err_put_ring;
161
162         fsverity_keyring = ring;
163         return 0;
164
165 err_put_ring:
166         key_put(ring);
167         return err;
168 }