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