Skip to content

Commit d3bfe84

Browse files
committed
certs: Add a secondary system keyring that can be added to dynamically
Add a secondary system keyring that can be added to by root whilst the system is running - provided the key being added is vouched for by a key built into the kernel or already added to the secondary keyring. Rename .system_keyring to .builtin_trusted_keys to distinguish it more obviously from the new keyring (called .secondary_trusted_keys). The new keyring needs to be enabled with CONFIG_SECONDARY_TRUSTED_KEYRING. If the secondary keyring is enabled, a link is created from that to .builtin_trusted_keys so that the the latter will automatically be searched too if the secondary keyring is searched. Signed-off-by: David Howells <[email protected]>
1 parent 77f68ba commit d3bfe84

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
lines changed

certs/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ config SYSTEM_EXTRA_CERTIFICATE_SIZE
5656
This is the number of bytes reserved in the kernel image for a
5757
certificate to be inserted.
5858

59+
config SECONDARY_TRUSTED_KEYRING
60+
bool "Provide a keyring to which extra trustable keys may be added"
61+
depends on SYSTEM_TRUSTED_KEYRING
62+
help
63+
If set, provide a keyring to which extra keys may be added, provided
64+
those keys are not blacklisted and are vouched for by a key built
65+
into the kernel or already in the secondary trusted keyring.
66+
5967
endmenu

certs/system_keyring.c

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,88 @@
1818
#include <keys/system_keyring.h>
1919
#include <crypto/pkcs7.h>
2020

21-
static struct key *system_trusted_keyring;
21+
static struct key *builtin_trusted_keys;
22+
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
23+
static struct key *secondary_trusted_keys;
24+
#endif
2225

2326
extern __initconst const u8 system_certificate_list[];
2427
extern __initconst const unsigned long system_certificate_list_size;
2528

2629
/**
27-
* restrict_link_by_builtin_trusted - Restrict keyring addition by system CA
30+
* restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
2831
*
2932
* Restrict the addition of keys into a keyring based on the key-to-be-added
30-
* being vouched for by a key in the system keyring.
33+
* being vouched for by a key in the built in system keyring.
3134
*/
3235
int restrict_link_by_builtin_trusted(struct key *keyring,
3336
const struct key_type *type,
3437
const union key_payload *payload)
3538
{
36-
return restrict_link_by_signature(system_trusted_keyring,
37-
type, payload);
39+
return restrict_link_by_signature(builtin_trusted_keys, type, payload);
3840
}
3941

42+
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
43+
/**
44+
* restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
45+
* addition by both builtin and secondary keyrings
46+
*
47+
* Restrict the addition of keys into a keyring based on the key-to-be-added
48+
* being vouched for by a key in either the built-in or the secondary system
49+
* keyrings.
50+
*/
51+
int restrict_link_by_builtin_and_secondary_trusted(
52+
struct key *keyring,
53+
const struct key_type *type,
54+
const union key_payload *payload)
55+
{
56+
/* If we have a secondary trusted keyring, then that contains a link
57+
* through to the builtin keyring and the search will follow that link.
58+
*/
59+
if (type == &key_type_keyring &&
60+
keyring == secondary_trusted_keys &&
61+
payload == &builtin_trusted_keys->payload)
62+
/* Allow the builtin keyring to be added to the secondary */
63+
return 0;
64+
65+
return restrict_link_by_signature(secondary_trusted_keys, type, payload);
66+
}
67+
#endif
68+
4069
/*
41-
* Load the compiled-in keys
70+
* Create the trusted keyrings
4271
*/
4372
static __init int system_trusted_keyring_init(void)
4473
{
45-
pr_notice("Initialise system trusted keyring\n");
74+
pr_notice("Initialise system trusted keyrings\n");
4675

47-
system_trusted_keyring =
48-
keyring_alloc(".system_keyring",
76+
builtin_trusted_keys =
77+
keyring_alloc(".builtin_trusted_keys",
4978
KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
5079
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
5180
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
5281
KEY_ALLOC_NOT_IN_QUOTA,
53-
restrict_link_by_builtin_trusted, NULL);
54-
if (IS_ERR(system_trusted_keyring))
55-
panic("Can't allocate system trusted keyring\n");
82+
NULL, NULL);
83+
if (IS_ERR(builtin_trusted_keys))
84+
panic("Can't allocate builtin trusted keyring\n");
85+
86+
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
87+
secondary_trusted_keys =
88+
keyring_alloc(".secondary_trusted_keys",
89+
KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
90+
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
91+
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
92+
KEY_USR_WRITE),
93+
KEY_ALLOC_NOT_IN_QUOTA,
94+
restrict_link_by_builtin_and_secondary_trusted,
95+
NULL);
96+
if (IS_ERR(secondary_trusted_keys))
97+
panic("Can't allocate secondary trusted keyring\n");
98+
99+
if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
100+
panic("Can't link trusted keyrings\n");
101+
#endif
102+
56103
return 0;
57104
}
58105

@@ -88,7 +135,7 @@ static __init int load_system_certificate_list(void)
88135
if (plen > end - p)
89136
goto dodgy_cert;
90137

91-
key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
138+
key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
92139
"asymmetric",
93140
NULL,
94141
p,
@@ -125,7 +172,8 @@ late_initcall(load_system_certificate_list);
125172
* @len: Size of @data.
126173
* @raw_pkcs7: The PKCS#7 message that is the signature.
127174
* @pkcs7_len: The size of @raw_pkcs7.
128-
* @trusted_keys: Trusted keys to use (NULL for system_trusted_keyring).
175+
* @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
176+
* (void *)1UL for all trusted keys).
129177
* @usage: The use to which the key is being put.
130178
* @view_content: Callback to gain access to content.
131179
* @ctx: Context for callback.
@@ -157,8 +205,15 @@ int verify_pkcs7_signature(const void *data, size_t len,
157205
if (ret < 0)
158206
goto error;
159207

160-
if (!trusted_keys)
161-
trusted_keys = system_trusted_keyring;
208+
if (!trusted_keys) {
209+
trusted_keys = builtin_trusted_keys;
210+
} else if (trusted_keys == (void *)1UL) {
211+
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
212+
trusted_keys = secondary_trusted_keys;
213+
#else
214+
trusted_keys = builtin_trusted_keys;
215+
#endif
216+
}
162217
ret = pkcs7_validate_trust(pkcs7, trusted_keys);
163218
if (ret < 0) {
164219
if (ret == -ENOKEY)

include/keys/system_keyring.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ extern int restrict_link_by_builtin_trusted(struct key *keyring,
2424
#define restrict_link_by_builtin_trusted restrict_link_reject
2525
#endif
2626

27+
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
28+
extern int restrict_link_by_builtin_and_secondary_trusted(
29+
struct key *keyring,
30+
const struct key_type *type,
31+
const union key_payload *payload);
32+
#else
33+
#define restrict_link_by_builtin_and_secondary_trusted restrict_link_by_builtin_trusted
34+
#endif
35+
2736
#ifdef CONFIG_IMA_MOK_KEYRING
2837
extern struct key *ima_mok_keyring;
2938
extern struct key *ima_blacklist_keyring;

0 commit comments

Comments
 (0)