Skip to content

Commit 6cbdfb3

Browse files
naynajainmimizohar
authored andcommitted
ima: enable loading of build time generated key on .ima keyring
The kernel currently only loads the kernel module signing key onto the builtin trusted keyring. Load the module signing key onto the IMA keyring as well. Signed-off-by: Nayna Jain <[email protected]> Acked-by: Stefan Berger <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 0165f4c commit 6cbdfb3

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

certs/system_certificates.S

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
.globl system_certificate_list
99
system_certificate_list:
1010
__cert_list_start:
11-
#ifdef CONFIG_MODULE_SIG
11+
__module_cert_start:
12+
#if defined(CONFIG_MODULE_SIG) || defined(CONFIG_IMA_APPRAISE_MODSIG)
1213
.incbin "certs/signing_key.x509"
1314
#endif
15+
__module_cert_end:
1416
.incbin "certs/x509_certificate_list"
1517
__cert_list_end:
1618

@@ -35,3 +37,12 @@ system_certificate_list_size:
3537
#else
3638
.long __cert_list_end - __cert_list_start
3739
#endif
40+
41+
.align 8
42+
.globl module_cert_size
43+
module_cert_size:
44+
#ifdef CONFIG_64BIT
45+
.quad __module_cert_end - __module_cert_start
46+
#else
47+
.long __module_cert_end - __module_cert_start
48+
#endif

certs/system_keyring.c

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static struct key *platform_trusted_keys;
2727

2828
extern __initconst const u8 system_certificate_list[];
2929
extern __initconst const unsigned long system_certificate_list_size;
30+
extern __initconst const unsigned long module_cert_size;
3031

3132
/**
3233
* restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
@@ -132,19 +133,11 @@ static __init int system_trusted_keyring_init(void)
132133
*/
133134
device_initcall(system_trusted_keyring_init);
134135

135-
/*
136-
* Load the compiled-in list of X.509 certificates.
137-
*/
138-
static __init int load_system_certificate_list(void)
136+
static __init int load_cert(const u8 *p, const u8 *end, struct key *keyring)
139137
{
140138
key_ref_t key;
141-
const u8 *p, *end;
142139
size_t plen;
143140

144-
pr_notice("Loading compiled-in X.509 certificates\n");
145-
146-
p = system_certificate_list;
147-
end = p + system_certificate_list_size;
148141
while (p < end) {
149142
/* Each cert begins with an ASN.1 SEQUENCE tag and must be more
150143
* than 256 bytes in size.
@@ -159,7 +152,7 @@ static __init int load_system_certificate_list(void)
159152
if (plen > end - p)
160153
goto dodgy_cert;
161154

162-
key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
155+
key = key_create_or_update(make_key_ref(keyring, 1),
163156
"asymmetric",
164157
NULL,
165158
p,
@@ -186,6 +179,43 @@ static __init int load_system_certificate_list(void)
186179
pr_err("Problem parsing in-kernel X.509 certificate list\n");
187180
return 0;
188181
}
182+
183+
__init int load_module_cert(struct key *keyring)
184+
{
185+
const u8 *p, *end;
186+
187+
if (!IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG))
188+
return 0;
189+
190+
pr_notice("Loading compiled-in module X.509 certificates\n");
191+
192+
p = system_certificate_list;
193+
end = p + module_cert_size;
194+
195+
return load_cert(p, end, keyring);
196+
}
197+
198+
/*
199+
* Load the compiled-in list of X.509 certificates.
200+
*/
201+
static __init int load_system_certificate_list(void)
202+
{
203+
const u8 *p, *end;
204+
unsigned long size;
205+
206+
pr_notice("Loading compiled-in X.509 certificates\n");
207+
208+
#ifdef CONFIG_MODULE_SIG
209+
p = system_certificate_list;
210+
size = system_certificate_list_size;
211+
#else
212+
p = system_certificate_list + module_cert_size;
213+
size = system_certificate_list_size - module_cert_size;
214+
#endif
215+
216+
end = p + size;
217+
return load_cert(p, end, builtin_trusted_keys);
218+
}
189219
late_initcall(load_system_certificate_list);
190220

191221
#ifdef CONFIG_SYSTEM_DATA_VERIFICATION

include/keys/system_keyring.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ extern int restrict_link_by_builtin_trusted(struct key *keyring,
1616
const struct key_type *type,
1717
const union key_payload *payload,
1818
struct key *restriction_key);
19+
extern __init int load_module_cert(struct key *keyring);
1920

2021
#else
2122
#define restrict_link_by_builtin_trusted restrict_link_reject
23+
24+
static inline __init int load_module_cert(struct key *keyring)
25+
{
26+
return 0;
27+
}
28+
2229
#endif
2330

2431
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING

security/integrity/digsig.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ static int __init __integrity_init_keyring(const unsigned int id,
111111
} else {
112112
if (id == INTEGRITY_KEYRING_PLATFORM)
113113
set_platform_trusted_keys(keyring[id]);
114+
if (id == INTEGRITY_KEYRING_IMA)
115+
load_module_cert(keyring[id]);
114116
}
115117

116118
return err;

0 commit comments

Comments
 (0)