Skip to content

Commit 6364d10

Browse files
l0kodjarkkojs
authored andcommitted
certs: Allow root user to append signed hashes to the blacklist keyring
Add a kernel option SYSTEM_BLACKLIST_AUTH_UPDATE to enable the root user to dynamically add new keys to the blacklist keyring. This enables to invalidate new certificates, either from being loaded in a keyring, or from being trusted in a PKCS#7 certificate chain. This also enables to add new file hashes to be denied by the integrity infrastructure. Being able to untrust a certificate which could have normaly been trusted is a sensitive operation. This is why adding new hashes to the blacklist keyring is only allowed when these hashes are signed and vouched by the builtin trusted keyring. A blacklist hash is stored as a key description. The PKCS#7 signature of this description must be provided as the key payload. Marking a certificate as untrusted should be enforced while the system is running. It is then forbiden to remove such blacklist keys. Update blacklist keyring, blacklist key and revoked certificate access rights: * allows the root user to search for a specific blacklisted hash, which make sense because the descriptions are already viewable; * forbids key update (blacklist and asymmetric ones); * restricts kernel rights on the blacklist keyring to align with the root user rights. See help in tools/certs/print-cert-tbs-hash.sh . Cc: David Howells <dhowells@redhat.com> Cc: David Woodhouse <dwmw2@infradead.org> Cc: Eric Snowberg <eric.snowberg@oracle.com> Cc: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Mickaël Salaün <mic@linux.microsoft.com> Link: https://lore.kernel.org/r/20210712170313.884724-6-mic@digikod.net Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Tested-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent addf466 commit 6364d10

File tree

2 files changed

+85
-21
lines changed

2 files changed

+85
-21
lines changed

certs/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,14 @@ config SYSTEM_REVOCATION_KEYS
127127
containing X.509 certificates to be included in the default blacklist
128128
keyring.
129129

130+
config SYSTEM_BLACKLIST_AUTH_UPDATE
131+
bool "Allow root to add signed blacklist keys"
132+
depends on SYSTEM_BLACKLIST_KEYRING
133+
depends on SYSTEM_DATA_VERIFICATION
134+
help
135+
If set, provide the ability to load new blacklist keys at run time if
136+
they are signed and vouched by a certificate from the builtin trusted
137+
keyring. The PKCS#7 signature of the description is set in the key
138+
payload. Blacklist keys cannot be removed.
139+
130140
endmenu

certs/blacklist.c

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/err.h>
1616
#include <linux/seq_file.h>
1717
#include <linux/uidgid.h>
18+
#include <linux/verification.h>
1819
#include <keys/system_keyring.h>
1920
#include "blacklist.h"
2021
#include "common.h"
@@ -26,6 +27,9 @@
2627
*/
2728
#define MAX_HASH_LEN 128
2829

30+
#define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | KEY_POS_VIEW | \
31+
KEY_USR_SEARCH | KEY_USR_VIEW)
32+
2933
static const char tbs_prefix[] = "tbs";
3034
static const char bin_prefix[] = "bin";
3135

@@ -80,19 +84,51 @@ static int blacklist_vet_description(const char *desc)
8084
return 0;
8185
}
8286

83-
/*
84-
* The hash to be blacklisted is expected to be in the description. There will
85-
* be no payload.
86-
*/
87-
static int blacklist_preparse(struct key_preparsed_payload *prep)
87+
static int blacklist_key_instantiate(struct key *key,
88+
struct key_preparsed_payload *prep)
8889
{
89-
if (prep->datalen > 0)
90-
return -EINVAL;
91-
return 0;
90+
#ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
91+
int err;
92+
#endif
93+
94+
/* Sets safe default permissions for keys loaded by user space. */
95+
key->perm = BLACKLIST_KEY_PERM;
96+
97+
/*
98+
* Skips the authentication step for builtin hashes, they are not
99+
* signed but still trusted.
100+
*/
101+
if (key->flags & (1 << KEY_FLAG_BUILTIN))
102+
goto out;
103+
104+
#ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
105+
/*
106+
* Verifies the description's PKCS#7 signature against the builtin
107+
* trusted keyring.
108+
*/
109+
err = verify_pkcs7_signature(key->description,
110+
strlen(key->description), prep->data, prep->datalen,
111+
NULL, VERIFYING_UNSPECIFIED_SIGNATURE, NULL, NULL);
112+
if (err)
113+
return err;
114+
#else
115+
/*
116+
* It should not be possible to come here because the keyring doesn't
117+
* have KEY_USR_WRITE and the only other way to call this function is
118+
* for builtin hashes.
119+
*/
120+
WARN_ON_ONCE(1);
121+
return -EPERM;
122+
#endif
123+
124+
out:
125+
return generic_key_instantiate(key, prep);
92126
}
93127

94-
static void blacklist_free_preparse(struct key_preparsed_payload *prep)
128+
static int blacklist_key_update(struct key *key,
129+
struct key_preparsed_payload *prep)
95130
{
131+
return -EPERM;
96132
}
97133

98134
static void blacklist_describe(const struct key *key, struct seq_file *m)
@@ -103,9 +139,8 @@ static void blacklist_describe(const struct key *key, struct seq_file *m)
103139
static struct key_type key_type_blacklist = {
104140
.name = "blacklist",
105141
.vet_description = blacklist_vet_description,
106-
.preparse = blacklist_preparse,
107-
.free_preparse = blacklist_free_preparse,
108-
.instantiate = generic_key_instantiate,
142+
.instantiate = blacklist_key_instantiate,
143+
.update = blacklist_key_update,
109144
.describe = blacklist_describe,
110145
};
111146

@@ -154,8 +189,7 @@ static int mark_raw_hash_blacklisted(const char *hash)
154189
hash,
155190
NULL,
156191
0,
157-
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
158-
KEY_USR_VIEW),
192+
BLACKLIST_KEY_PERM,
159193
KEY_ALLOC_NOT_IN_QUOTA |
160194
KEY_ALLOC_BUILT_IN);
161195
if (IS_ERR(key)) {
@@ -232,8 +266,10 @@ int add_key_to_revocation_list(const char *data, size_t size)
232266
NULL,
233267
data,
234268
size,
235-
((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW),
236-
KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN);
269+
KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
270+
| KEY_USR_VIEW,
271+
KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN
272+
| KEY_ALLOC_BYPASS_RESTRICTION);
237273

238274
if (IS_ERR(key)) {
239275
pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
@@ -260,25 +296,43 @@ int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
260296
}
261297
#endif
262298

299+
static int restrict_link_for_blacklist(struct key *dest_keyring,
300+
const struct key_type *type, const union key_payload *payload,
301+
struct key *restrict_key)
302+
{
303+
if (type == &key_type_blacklist)
304+
return 0;
305+
return -EOPNOTSUPP;
306+
}
307+
263308
/*
264309
* Initialise the blacklist
265310
*/
266311
static int __init blacklist_init(void)
267312
{
268313
const char *const *bl;
314+
struct key_restriction *restriction;
269315

270316
if (register_key_type(&key_type_blacklist) < 0)
271317
panic("Can't allocate system blacklist key type\n");
272318

319+
restriction = kzalloc(sizeof(*restriction), GFP_KERNEL);
320+
if (!restriction)
321+
panic("Can't allocate blacklist keyring restriction\n");
322+
restriction->check = restrict_link_for_blacklist;
323+
273324
blacklist_keyring =
274325
keyring_alloc(".blacklist",
275326
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
276-
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
277-
KEY_USR_VIEW | KEY_USR_READ |
278-
KEY_USR_SEARCH,
279-
KEY_ALLOC_NOT_IN_QUOTA |
327+
KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
328+
KEY_POS_WRITE |
329+
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH
330+
#ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
331+
| KEY_USR_WRITE
332+
#endif
333+
, KEY_ALLOC_NOT_IN_QUOTA |
280334
KEY_ALLOC_SET_KEEP,
281-
NULL, NULL);
335+
restriction, NULL);
282336
if (IS_ERR(blacklist_keyring))
283337
panic("Can't allocate system blacklist keyring\n");
284338

0 commit comments

Comments
 (0)