xref: /openbmc/linux/fs/verity/signature.c (revision 6dc6657d)
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 
23 /*
24  * /proc/sys/fs/verity/require_signatures
25  * If 1, all verity files must have a valid builtin signature.
26  */
27 static int fsverity_require_signatures;
28 
29 /*
30  * Keyring that contains the trusted X.509 certificates.
31  *
32  * Only root (kuid=0) can modify this.  Also, root may use
33  * keyctl_restrict_keyring() to prevent any more additions.
34  */
35 static struct key *fsverity_keyring;
36 
37 /**
38  * fsverity_verify_signature() - check a verity file's signature
39  * @vi: the file's fsverity_info
40  * @signature: the file's built-in signature
41  * @sig_size: size of signature in bytes, or 0 if no signature
42  *
43  * If the file includes a signature of its fs-verity file digest, verify it
44  * against the certificates in the fs-verity keyring.
45  *
46  * Return: 0 on success (signature valid or not required); -errno on failure
47  */
48 int fsverity_verify_signature(const struct fsverity_info *vi,
49 			      const u8 *signature, size_t sig_size)
50 {
51 	const struct inode *inode = vi->inode;
52 	const struct fsverity_hash_alg *hash_alg = vi->tree_params.hash_alg;
53 	struct fsverity_formatted_digest *d;
54 	int err;
55 
56 	if (sig_size == 0) {
57 		if (fsverity_require_signatures) {
58 			fsverity_err(inode,
59 				     "require_signatures=1, rejecting unsigned file!");
60 			return -EPERM;
61 		}
62 		return 0;
63 	}
64 
65 	d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
66 	if (!d)
67 		return -ENOMEM;
68 	memcpy(d->magic, "FSVerity", 8);
69 	d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
70 	d->digest_size = cpu_to_le16(hash_alg->digest_size);
71 	memcpy(d->digest, vi->file_digest, hash_alg->digest_size);
72 
73 	err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
74 				     signature, sig_size, fsverity_keyring,
75 				     VERIFYING_UNSPECIFIED_SIGNATURE,
76 				     NULL, NULL);
77 	kfree(d);
78 
79 	if (err) {
80 		if (err == -ENOKEY)
81 			fsverity_err(inode,
82 				     "File's signing cert isn't in the fs-verity keyring");
83 		else if (err == -EKEYREJECTED)
84 			fsverity_err(inode, "Incorrect file signature");
85 		else if (err == -EBADMSG)
86 			fsverity_err(inode, "Malformed file signature");
87 		else
88 			fsverity_err(inode, "Error %d verifying file signature",
89 				     err);
90 		return err;
91 	}
92 
93 	return 0;
94 }
95 
96 #ifdef CONFIG_SYSCTL
97 static struct ctl_table_header *fsverity_sysctl_header;
98 
99 static struct ctl_table fsverity_sysctl_table[] = {
100 	{
101 		.procname       = "require_signatures",
102 		.data           = &fsverity_require_signatures,
103 		.maxlen         = sizeof(int),
104 		.mode           = 0644,
105 		.proc_handler   = proc_dointvec_minmax,
106 		.extra1         = SYSCTL_ZERO,
107 		.extra2         = SYSCTL_ONE,
108 	},
109 	{ }
110 };
111 
112 static int __init fsverity_sysctl_init(void)
113 {
114 	fsverity_sysctl_header = register_sysctl("fs/verity", fsverity_sysctl_table);
115 	if (!fsverity_sysctl_header) {
116 		pr_err("sysctl registration failed!\n");
117 		return -ENOMEM;
118 	}
119 	return 0;
120 }
121 #else /* !CONFIG_SYSCTL */
122 static inline int __init fsverity_sysctl_init(void)
123 {
124 	return 0;
125 }
126 #endif /* !CONFIG_SYSCTL */
127 
128 int __init fsverity_init_signature(void)
129 {
130 	struct key *ring;
131 	int err;
132 
133 	ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
134 			     current_cred(), KEY_POS_SEARCH |
135 				KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
136 				KEY_USR_SEARCH | KEY_USR_SETATTR,
137 			     KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
138 	if (IS_ERR(ring))
139 		return PTR_ERR(ring);
140 
141 	err = fsverity_sysctl_init();
142 	if (err)
143 		goto err_put_ring;
144 
145 	fsverity_keyring = ring;
146 	return 0;
147 
148 err_put_ring:
149 	key_put(ring);
150 	return err;
151 }
152