xref: /OK3568_Linux_fs/kernel/fs/verity/signature.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 	d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
85 	if (!d)
86 		return -ENOMEM;
87 	memcpy(d->magic, "FSVerity", 8);
88 	d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
89 	d->digest_size = cpu_to_le16(hash_alg->digest_size);
90 	memcpy(d->digest, file_digest, hash_alg->digest_size);
91 
92 	err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
93 				     signature, sig_size, fsverity_keyring,
94 				     VERIFYING_UNSPECIFIED_SIGNATURE,
95 				     NULL, NULL);
96 	kfree(d);
97 
98 	if (err) {
99 		if (err == -ENOKEY)
100 			fsverity_err(inode,
101 				     "File's signing cert isn't in the fs-verity keyring");
102 		else if (err == -EKEYREJECTED)
103 			fsverity_err(inode, "Incorrect file signature");
104 		else if (err == -EBADMSG)
105 			fsverity_err(inode, "Malformed file signature");
106 		else
107 			fsverity_err(inode, "Error %d verifying file signature",
108 				     err);
109 		return err;
110 	}
111 
112 	pr_debug("Valid signature for file digest %s:%*phN\n",
113 		 hash_alg->name, hash_alg->digest_size, file_digest);
114 	return 0;
115 }
116 EXPORT_SYMBOL_GPL(__fsverity_verify_signature);
117 
118 #ifdef CONFIG_SYSCTL
119 static struct ctl_table_header *fsverity_sysctl_header;
120 
121 static const struct ctl_path fsverity_sysctl_path[] = {
122 	{ .procname = "fs", },
123 	{ .procname = "verity", },
124 	{ }
125 };
126 
127 static struct ctl_table fsverity_sysctl_table[] = {
128 	{
129 		.procname       = "require_signatures",
130 		.data           = &fsverity_require_signatures,
131 		.maxlen         = sizeof(int),
132 		.mode           = 0644,
133 		.proc_handler   = proc_dointvec_minmax,
134 		.extra1         = SYSCTL_ZERO,
135 		.extra2         = SYSCTL_ONE,
136 	},
137 	{ }
138 };
139 
fsverity_sysctl_init(void)140 static int __init fsverity_sysctl_init(void)
141 {
142 	fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
143 						       fsverity_sysctl_table);
144 	if (!fsverity_sysctl_header) {
145 		pr_err("sysctl registration failed!\n");
146 		return -ENOMEM;
147 	}
148 	return 0;
149 }
150 #else /* !CONFIG_SYSCTL */
fsverity_sysctl_init(void)151 static inline int __init fsverity_sysctl_init(void)
152 {
153 	return 0;
154 }
155 #endif /* !CONFIG_SYSCTL */
156 
fsverity_init_signature(void)157 int __init fsverity_init_signature(void)
158 {
159 	struct key *ring;
160 	int err;
161 
162 	ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
163 			     current_cred(), KEY_POS_SEARCH |
164 				KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
165 				KEY_USR_SEARCH | KEY_USR_SETATTR,
166 			     KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
167 	if (IS_ERR(ring))
168 		return PTR_ERR(ring);
169 
170 	err = fsverity_sysctl_init();
171 	if (err)
172 		goto err_put_ring;
173 
174 	fsverity_keyring = ring;
175 	return 0;
176 
177 err_put_ring:
178 	key_put(ring);
179 	return err;
180 }
181