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