Skip to content

Commit 72d6f5d

Browse files
esnowbergSasha Levin
authored andcommitted
certs: Move load_system_certificate_list to a common function
[ Upstream commit 2565ca7 ] Move functionality within load_system_certificate_list to a common function, so it can be reused in the future. DH Changes: - Added inclusion of common.h to common.c (Eric [1]). Signed-off-by: Eric Snowberg <eric.snowberg@oracle.com> Acked-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: David Howells <dhowells@redhat.com> cc: keyrings@vger.kernel.org Link: https://lore.kernel.org/r/EDA280F9-F72D-4181-93C7-CDBE95976FF7@oracle.com/ [1] Link: https://lore.kernel.org/r/20200930201508.35113-2-eric.snowberg@oracle.com/ Link: https://lore.kernel.org/r/20210122181054.32635-3-eric.snowberg@oracle.com/ # v5 Link: https://lore.kernel.org/r/161428672825.677100.7545516389752262918.stgit@warthog.procyon.org.uk/ Link: https://lore.kernel.org/r/161433311696.902181.3599366124784670368.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/161529605850.163428.7786675680201528556.stgit@warthog.procyon.org.uk/ # v3 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 4510906 commit 72d6f5d

4 files changed

Lines changed: 70 additions & 47 deletions

File tree

certs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Makefile for the linux kernel signature checking certificates.
44
#
55

6-
obj-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += system_keyring.o system_certificates.o
6+
obj-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += system_keyring.o system_certificates.o common.o
77
obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist.o
88
ifneq ($(CONFIG_SYSTEM_BLACKLIST_HASH_LIST),"")
99
obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_hashes.o

certs/common.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#include <linux/kernel.h>
4+
#include <linux/key.h>
5+
#include "common.h"
6+
7+
int load_certificate_list(const u8 cert_list[],
8+
const unsigned long list_size,
9+
const struct key *keyring)
10+
{
11+
key_ref_t key;
12+
const u8 *p, *end;
13+
size_t plen;
14+
15+
p = cert_list;
16+
end = p + list_size;
17+
while (p < end) {
18+
/* Each cert begins with an ASN.1 SEQUENCE tag and must be more
19+
* than 256 bytes in size.
20+
*/
21+
if (end - p < 4)
22+
goto dodgy_cert;
23+
if (p[0] != 0x30 &&
24+
p[1] != 0x82)
25+
goto dodgy_cert;
26+
plen = (p[2] << 8) | p[3];
27+
plen += 4;
28+
if (plen > end - p)
29+
goto dodgy_cert;
30+
31+
key = key_create_or_update(make_key_ref(keyring, 1),
32+
"asymmetric",
33+
NULL,
34+
p,
35+
plen,
36+
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
37+
KEY_USR_VIEW | KEY_USR_READ),
38+
KEY_ALLOC_NOT_IN_QUOTA |
39+
KEY_ALLOC_BUILT_IN |
40+
KEY_ALLOC_BYPASS_RESTRICTION);
41+
if (IS_ERR(key)) {
42+
pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
43+
PTR_ERR(key));
44+
} else {
45+
pr_notice("Loaded X.509 cert '%s'\n",
46+
key_ref_to_ptr(key)->description);
47+
key_ref_put(key);
48+
}
49+
p += plen;
50+
}
51+
52+
return 0;
53+
54+
dodgy_cert:
55+
pr_err("Problem parsing in-kernel X.509 certificate list\n");
56+
return 0;
57+
}

certs/common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
3+
#ifndef _CERT_COMMON_H
4+
#define _CERT_COMMON_H
5+
6+
int load_certificate_list(const u8 cert_list[], const unsigned long list_size,
7+
const struct key *keyring);
8+
9+
#endif

certs/system_keyring.c

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <keys/asymmetric-type.h>
1616
#include <keys/system_keyring.h>
1717
#include <crypto/pkcs7.h>
18+
#include "common.h"
1819

1920
static struct key *builtin_trusted_keys;
2021
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
@@ -136,54 +137,10 @@ device_initcall(system_trusted_keyring_init);
136137
*/
137138
static __init int load_system_certificate_list(void)
138139
{
139-
key_ref_t key;
140-
const u8 *p, *end;
141-
size_t plen;
142-
143140
pr_notice("Loading compiled-in X.509 certificates\n");
144141

145-
p = system_certificate_list;
146-
end = p + system_certificate_list_size;
147-
while (p < end) {
148-
/* Each cert begins with an ASN.1 SEQUENCE tag and must be more
149-
* than 256 bytes in size.
150-
*/
151-
if (end - p < 4)
152-
goto dodgy_cert;
153-
if (p[0] != 0x30 &&
154-
p[1] != 0x82)
155-
goto dodgy_cert;
156-
plen = (p[2] << 8) | p[3];
157-
plen += 4;
158-
if (plen > end - p)
159-
goto dodgy_cert;
160-
161-
key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
162-
"asymmetric",
163-
NULL,
164-
p,
165-
plen,
166-
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
167-
KEY_USR_VIEW | KEY_USR_READ),
168-
KEY_ALLOC_NOT_IN_QUOTA |
169-
KEY_ALLOC_BUILT_IN |
170-
KEY_ALLOC_BYPASS_RESTRICTION);
171-
if (IS_ERR(key)) {
172-
pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
173-
PTR_ERR(key));
174-
} else {
175-
pr_notice("Loaded X.509 cert '%s'\n",
176-
key_ref_to_ptr(key)->description);
177-
key_ref_put(key);
178-
}
179-
p += plen;
180-
}
181-
182-
return 0;
183-
184-
dodgy_cert:
185-
pr_err("Problem parsing in-kernel X.509 certificate list\n");
186-
return 0;
142+
return load_certificate_list(system_certificate_list, system_certificate_list_size,
143+
builtin_trusted_keys);
187144
}
188145
late_initcall(load_system_certificate_list);
189146

0 commit comments

Comments
 (0)