Skip to content

Commit 60740ac

Browse files
naynajainmimizohar
authored andcommitted
integrity: Load certs to the platform keyring
The patch refactors integrity_load_x509(), making it a wrapper for a new function named integrity_add_key(). This patch also defines a new function named integrity_load_cert() for loading the platform keys. Signed-off-by: Nayna Jain <nayna@linux.ibm.com> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com> Acked-by: Serge Hallyn <serge@hallyn.com> Reviewed-by: James Morris <james.morris@microsoft.com> Reviewed-by: Thiago Jung Bauermann <bauerman@linux.ibm.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 9dc92c4 commit 60740ac

3 files changed

Lines changed: 86 additions & 24 deletions

File tree

‎security/integrity/digsig.c‎

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ static int __integrity_init_keyring(const unsigned int id, key_perm_t perm,
8282

8383
keyring[id] = keyring_alloc(keyring_name[id], KUIDT_INIT(0),
8484
KGIDT_INIT(0), cred, perm,
85-
KEY_ALLOC_NOT_IN_QUOTA,
86-
restriction, NULL);
85+
KEY_ALLOC_NOT_IN_QUOTA, restriction, NULL);
8786
if (IS_ERR(keyring[id])) {
8887
err = PTR_ERR(keyring[id]);
8988
pr_info("Can't allocate %s keyring (%d)\n",
@@ -121,40 +120,60 @@ int __init integrity_init_keyring(const unsigned int id)
121120
return __integrity_init_keyring(id, perm, restriction);
122121
}
123122

124-
int __init integrity_load_x509(const unsigned int id, const char *path)
123+
int __init integrity_add_key(const unsigned int id, const void *data,
124+
off_t size, key_perm_t perm)
125125
{
126126
key_ref_t key;
127-
void *data;
128-
loff_t size;
129-
int rc;
127+
int rc = 0;
130128

131129
if (!keyring[id])
132130
return -EINVAL;
133131

132+
key = key_create_or_update(make_key_ref(keyring[id], 1), "asymmetric",
133+
NULL, data, size, perm,
134+
KEY_ALLOC_NOT_IN_QUOTA);
135+
if (IS_ERR(key)) {
136+
rc = PTR_ERR(key);
137+
pr_err("Problem loading X.509 certificate %d\n", rc);
138+
} else {
139+
pr_notice("Loaded X.509 cert '%s'\n",
140+
key_ref_to_ptr(key)->description);
141+
key_ref_put(key);
142+
}
143+
144+
return rc;
145+
146+
}
147+
148+
int __init integrity_load_x509(const unsigned int id, const char *path)
149+
{
150+
void *data;
151+
loff_t size;
152+
int rc;
153+
key_perm_t perm;
154+
134155
rc = kernel_read_file_from_path(path, &data, &size, 0,
135156
READING_X509_CERTIFICATE);
136157
if (rc < 0) {
137158
pr_err("Unable to open file: %s (%d)", path, rc);
138159
return rc;
139160
}
140161

141-
key = key_create_or_update(make_key_ref(keyring[id], 1),
142-
"asymmetric",
143-
NULL,
144-
data,
145-
size,
146-
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
147-
KEY_USR_VIEW | KEY_USR_READ),
148-
KEY_ALLOC_NOT_IN_QUOTA);
149-
if (IS_ERR(key)) {
150-
rc = PTR_ERR(key);
151-
pr_err("Problem loading X.509 certificate (%d): %s\n",
152-
rc, path);
153-
} else {
154-
pr_notice("Loaded X.509 cert '%s': %s\n",
155-
key_ref_to_ptr(key)->description, path);
156-
key_ref_put(key);
157-
}
162+
perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ;
163+
164+
pr_info("Loading X.509 certificate: %s\n", path);
165+
rc = integrity_add_key(id, (const void *)data, size, perm);
166+
158167
vfree(data);
159-
return 0;
168+
return rc;
169+
}
170+
171+
int __init integrity_load_cert(const unsigned int id, const char *source,
172+
const void *data, size_t len, key_perm_t perm)
173+
{
174+
if (!data)
175+
return -EINVAL;
176+
177+
pr_info("Loading X.509 certificate: %s\n", source);
178+
return integrity_add_key(id, data, len, perm);
160179
}

‎security/integrity/integrity.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
154154

155155
int __init integrity_init_keyring(const unsigned int id);
156156
int __init integrity_load_x509(const unsigned int id, const char *path);
157+
int __init integrity_load_cert(const unsigned int id, const char *source,
158+
const void *data, size_t len, key_perm_t perm);
157159
#else
158160

159161
static inline int integrity_digsig_verify(const unsigned int id,
@@ -167,6 +169,14 @@ static inline int integrity_init_keyring(const unsigned int id)
167169
{
168170
return 0;
169171
}
172+
173+
static inline int __init integrity_load_cert(const unsigned int id,
174+
const char *source,
175+
const void *data, size_t len,
176+
key_perm_t perm)
177+
{
178+
return 0;
179+
}
170180
#endif /* CONFIG_INTEGRITY_SIGNATURE */
171181

172182
#ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
@@ -223,3 +233,13 @@ integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
223233
}
224234

225235
#endif
236+
237+
#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
238+
void __init add_to_platform_keyring(const char *source, const void *data,
239+
size_t len);
240+
#else
241+
static inline void __init add_to_platform_keyring(const char *source,
242+
const void *data, size_t len)
243+
{
244+
}
245+
#endif

‎security/integrity/platform_certs/platform_keyring.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414
#include <linux/slab.h>
1515
#include "../integrity.h"
1616

17+
/**
18+
* add_to_platform_keyring - Add to platform keyring without validation.
19+
* @source: Source of key
20+
* @data: The blob holding the key
21+
* @len: The length of the data blob
22+
*
23+
* Add a key to the platform keyring without checking its trust chain. This
24+
* is available only during kernel initialisation.
25+
*/
26+
void __init add_to_platform_keyring(const char *source, const void *data,
27+
size_t len)
28+
{
29+
key_perm_t perm;
30+
int rc;
31+
32+
perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW;
33+
34+
rc = integrity_load_cert(INTEGRITY_KEYRING_PLATFORM, source, data, len,
35+
perm);
36+
if (rc)
37+
pr_info("Error adding keys to platform keyring %s\n", source);
38+
}
39+
1740
/*
1841
* Create the trusted keyrings.
1942
*/

0 commit comments

Comments
 (0)