GNU Linux-libre 5.4.257-gnu1
[releases.git] / fs / verity / signature.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/verity/signature.c: 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  *
32  * If the file's fs-verity descriptor includes a signature of the file
33  * measurement, verify it against the certificates in the fs-verity keyring.
34  *
35  * Return: 0 on success (signature valid or not required); -errno on failure
36  */
37 int fsverity_verify_signature(const struct fsverity_info *vi,
38                               const struct fsverity_descriptor *desc,
39                               size_t desc_size)
40 {
41         const struct inode *inode = vi->inode;
42         const struct fsverity_hash_alg *hash_alg = vi->tree_params.hash_alg;
43         const u32 sig_size = le32_to_cpu(desc->sig_size);
44         struct fsverity_signed_digest *d;
45         int err;
46
47         if (sig_size == 0) {
48                 if (fsverity_require_signatures) {
49                         fsverity_err(inode,
50                                      "require_signatures=1, rejecting unsigned file!");
51                         return -EPERM;
52                 }
53                 return 0;
54         }
55
56         if (sig_size > desc_size - sizeof(*desc)) {
57                 fsverity_err(inode, "Signature overflows verity descriptor");
58                 return -EBADMSG;
59         }
60
61         if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
62                 /*
63                  * The ".fs-verity" keyring is empty, due to builtin signatures
64                  * being supported by the kernel but not actually being used.
65                  * In this case, verify_pkcs7_signature() would always return an
66                  * error, usually ENOKEY.  It could also be EBADMSG if the
67                  * PKCS#7 is malformed, but that isn't very important to
68                  * distinguish.  So, just skip to ENOKEY to avoid the attack
69                  * surface of the PKCS#7 parser, which would otherwise be
70                  * reachable by any task able to execute FS_IOC_ENABLE_VERITY.
71                  */
72                 fsverity_err(inode,
73                              "fs-verity keyring is empty, rejecting signed file!");
74                 return -ENOKEY;
75         }
76
77         d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
78         if (!d)
79                 return -ENOMEM;
80         memcpy(d->magic, "FSVerity", 8);
81         d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
82         d->digest_size = cpu_to_le16(hash_alg->digest_size);
83         memcpy(d->digest, vi->measurement, hash_alg->digest_size);
84
85         err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
86                                      desc->signature, sig_size,
87                                      fsverity_keyring,
88                                      VERIFYING_UNSPECIFIED_SIGNATURE,
89                                      NULL, NULL);
90         kfree(d);
91
92         if (err) {
93                 if (err == -ENOKEY)
94                         fsverity_err(inode,
95                                      "File's signing cert isn't in the fs-verity keyring");
96                 else if (err == -EKEYREJECTED)
97                         fsverity_err(inode, "Incorrect file signature");
98                 else if (err == -EBADMSG)
99                         fsverity_err(inode, "Malformed file signature");
100                 else
101                         fsverity_err(inode, "Error %d verifying file signature",
102                                      err);
103                 return err;
104         }
105
106         pr_debug("Valid signature for file measurement %s:%*phN\n",
107                  hash_alg->name, hash_alg->digest_size, vi->measurement);
108         return 0;
109 }
110
111 #ifdef CONFIG_SYSCTL
112 static struct ctl_table_header *fsverity_sysctl_header;
113
114 static const struct ctl_path fsverity_sysctl_path[] = {
115         { .procname = "fs", },
116         { .procname = "verity", },
117         { }
118 };
119
120 static struct ctl_table fsverity_sysctl_table[] = {
121         {
122                 .procname       = "require_signatures",
123                 .data           = &fsverity_require_signatures,
124                 .maxlen         = sizeof(int),
125                 .mode           = 0644,
126                 .proc_handler   = proc_dointvec_minmax,
127                 .extra1         = SYSCTL_ZERO,
128                 .extra2         = SYSCTL_ONE,
129         },
130         { }
131 };
132
133 static int __init fsverity_sysctl_init(void)
134 {
135         fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
136                                                        fsverity_sysctl_table);
137         if (!fsverity_sysctl_header) {
138                 pr_err("sysctl registration failed!\n");
139                 return -ENOMEM;
140         }
141         return 0;
142 }
143 #else /* !CONFIG_SYSCTL */
144 static inline int __init fsverity_sysctl_init(void)
145 {
146         return 0;
147 }
148 #endif /* !CONFIG_SYSCTL */
149
150 int __init fsverity_init_signature(void)
151 {
152         struct key *ring;
153         int err;
154
155         ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
156                              current_cred(), KEY_POS_SEARCH |
157                                 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
158                                 KEY_USR_SEARCH | KEY_USR_SETATTR,
159                              KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
160         if (IS_ERR(ring))
161                 return PTR_ERR(ring);
162
163         err = fsverity_sysctl_init();
164         if (err)
165                 goto err_put_ring;
166
167         fsverity_keyring = ring;
168         return 0;
169
170 err_put_ring:
171         key_put(ring);
172         return err;
173 }