Skip to content

Commit a511e1a

Browse files
committed
KEYS: Move the point of trust determination to __key_link()
Move the point at which a key is determined to be trustworthy to __key_link() so that we use the contents of the keyring being linked in to to determine whether the key being linked in is trusted or not. What is 'trusted' then becomes a matter of what's in the keyring. Currently, the test is done when the key is parsed, but given that at that point we can only sensibly refer to the contents of the system trusted keyring, we can only use that as the basis for working out the trustworthiness of a new key. With this change, a trusted keyring is a set of keys that once the trusted-only flag is set cannot be added to except by verification through one of the contained keys. Further, adding a key into a trusted keyring, whilst it might grant trustworthiness in the context of that keyring, does not automatically grant trustworthiness in the context of a second keyring to which it could be secondarily linked. To accomplish this, the authentication data associated with the key source must now be retained. For an X.509 cert, this means the contents of the AuthorityKeyIdentifier and the signature data. If system keyrings are disabled then restrict_link_by_builtin_trusted() resolves to restrict_link_reject(). The integrity digital signature code still works correctly with this as it was previously using KEY_FLAG_TRUSTED_ONLY, which doesn't permit anything to be added if there is no system keyring against which trust can be determined. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 99716b7 commit a511e1a

File tree

9 files changed

+100
-76
lines changed

9 files changed

+100
-76
lines changed

certs/system_keyring.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,26 @@
1818
#include <keys/system_keyring.h>
1919
#include <crypto/pkcs7.h>
2020

21-
struct key *system_trusted_keyring;
22-
EXPORT_SYMBOL_GPL(system_trusted_keyring);
21+
static struct key *system_trusted_keyring;
2322

2423
extern __initconst const u8 system_certificate_list[];
2524
extern __initconst const unsigned long system_certificate_list_size;
2625

26+
/**
27+
* restrict_link_by_builtin_trusted - Restrict keyring addition by system CA
28+
*
29+
* 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.
31+
*/
32+
int restrict_link_by_builtin_trusted(struct key *keyring,
33+
const struct key_type *type,
34+
unsigned long flags,
35+
const union key_payload *payload)
36+
{
37+
return restrict_link_by_signature(system_trusted_keyring,
38+
type, payload);
39+
}
40+
2741
/*
2842
* Load the compiled-in keys
2943
*/
@@ -37,7 +51,7 @@ static __init int system_trusted_keyring_init(void)
3751
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
3852
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
3953
KEY_ALLOC_NOT_IN_QUOTA,
40-
keyring_restrict_trusted_only, NULL);
54+
restrict_link_by_builtin_trusted, NULL);
4155
if (IS_ERR(system_trusted_keyring))
4256
panic("Can't allocate system trusted keyring\n");
4357
return 0;

crypto/asymmetric_keys/restrict.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* Instantiate a public key crypto key from an X.509 Certificate
22
*
3-
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
3+
* Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
44
* Written by David Howells (dhowells@redhat.com)
55
*
66
* This program is free software; you can redistribute it and/or
@@ -9,20 +9,12 @@
99
* 2 of the Licence, or (at your option) any later version.
1010
*/
1111

12-
#define pr_fmt(fmt) "X.509: "fmt
12+
#define pr_fmt(fmt) "ASYM: "fmt
1313
#include <linux/module.h>
1414
#include <linux/kernel.h>
15-
#include <linux/slab.h>
1615
#include <linux/err.h>
17-
#include <linux/mpi.h>
18-
#include <linux/asn1_decoder.h>
19-
#include <keys/asymmetric-subtype.h>
20-
#include <keys/asymmetric-parser.h>
21-
#include <keys/system_keyring.h>
22-
#include <crypto/hash.h>
2316
#include <crypto/public_key.h>
2417
#include "asymmetric_keys.h"
25-
#include "x509_parser.h"
2618

2719
static bool use_builtin_keys;
2820
static struct asymmetric_key_id *ca_keyid;
@@ -62,45 +54,55 @@ static int __init ca_keys_setup(char *str)
6254
__setup("ca_keys=", ca_keys_setup);
6355
#endif
6456

65-
/*
57+
/**
58+
* restrict_link_by_signature - Restrict additions to a ring of public keys
59+
* @trust_keyring: A ring of keys that can be used to vouch for the new cert.
60+
* @type: The type of key being added.
61+
* @payload: The payload of the new key.
62+
*
6663
* Check the new certificate against the ones in the trust keyring. If one of
6764
* those is the signing key and validates the new certificate, then mark the
6865
* new certificate as being trusted.
6966
*
70-
* Return 0 if the new certificate was successfully validated, 1 if we couldn't
71-
* find a matching parent certificate in the trusted list and an error if there
72-
* is a matching certificate but the signature check fails.
67+
* Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
68+
* matching parent certificate in the trusted list, -EKEYREJECTED if the
69+
* signature check fails or the key is blacklisted and some other error if
70+
* there is a matching certificate but the signature check cannot be performed.
7371
*/
74-
int x509_validate_trust(struct x509_certificate *cert,
75-
struct key *trust_keyring)
72+
int restrict_link_by_signature(struct key *trust_keyring,
73+
const struct key_type *type,
74+
const union key_payload *payload)
7675
{
77-
struct public_key_signature *sig = cert->sig;
76+
const struct public_key_signature *sig;
7877
struct key *key;
79-
int ret = 1;
78+
int ret;
8079

81-
if (!sig->auth_ids[0] && !sig->auth_ids[1])
82-
return 1;
80+
pr_devel("==>%s()\n", __func__);
8381

8482
if (!trust_keyring)
83+
return -ENOKEY;
84+
85+
if (type != &key_type_asymmetric)
8586
return -EOPNOTSUPP;
87+
88+
sig = payload->data[asym_auth];
89+
if (!sig->auth_ids[0] && !sig->auth_ids[1])
90+
return 0;
91+
8692
if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
8793
return -EPERM;
88-
if (cert->unsupported_sig)
89-
return -ENOPKG;
9094

95+
/* See if we have a key that signed this one. */
9196
key = find_asymmetric_key(trust_keyring,
9297
sig->auth_ids[0], sig->auth_ids[1],
9398
false);
9499
if (IS_ERR(key))
95-
return PTR_ERR(key);
100+
return -ENOKEY;
96101

97-
if (!use_builtin_keys ||
98-
test_bit(KEY_FLAG_BUILTIN, &key->flags)) {
99-
ret = verify_signature(key, cert->sig);
100-
if (ret == -ENOPKG)
101-
cert->unsupported_sig = true;
102-
}
102+
if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
103+
ret = -ENOKEY;
104+
else
105+
ret = verify_signature(key, sig);
103106
key_put(key);
104107
return ret;
105108
}
106-
EXPORT_SYMBOL_GPL(x509_validate_trust);

crypto/asymmetric_keys/x509_parser.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,3 @@ extern int x509_decode_time(time64_t *_t, size_t hdrlen,
5858
*/
5959
extern int x509_get_sig_params(struct x509_certificate *cert);
6060
extern int x509_check_for_self_signed(struct x509_certificate *cert);
61-
62-
/*
63-
* public_key_trust.c
64-
*/
65-
extern int x509_validate_trust(struct x509_certificate *cert,
66-
struct key *trust_keyring);

crypto/asymmetric_keys/x509_public_key.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,12 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
178178

179179
cert->pub->id_type = "X509";
180180

181-
/* See if we can derive the trustability of this certificate.
182-
*
183-
* When it comes to self-signed certificates, we cannot evaluate
184-
* trustedness except by the fact that we obtained it from a trusted
185-
* location. So we just rely on x509_validate_trust() failing in this
186-
* case.
187-
*
188-
* Note that there's a possibility of a self-signed cert matching a
189-
* cert that we have (most likely a duplicate that we already trust) -
190-
* in which case it will be marked trusted.
191-
*/
192-
if (cert->unsupported_sig || cert->self_signed) {
181+
if (cert->unsupported_sig) {
193182
public_key_signature_free(cert->sig);
194183
cert->sig = NULL;
195184
} else {
196185
pr_devel("Cert Signature: %s + %s\n",
197186
cert->sig->pkey_algo, cert->sig->hash_algo);
198-
199-
ret = x509_validate_trust(cert, get_system_trusted_keyring());
200-
if (ret)
201-
ret = x509_validate_trust(cert, get_ima_mok_keyring());
202-
if (ret == -EKEYREJECTED)
203-
goto error_free_cert;
204-
if (!ret)
205-
prep->trusted = true;
206187
}
207188

208189
/* Propose a description */

include/crypto/public_key.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ extern void public_key_signature_free(struct public_key_signature *sig);
4747
extern struct asymmetric_key_subtype public_key_subtype;
4848

4949
struct key;
50+
struct key_type;
51+
union key_payload;
52+
53+
extern int restrict_link_by_signature(struct key *trust_keyring,
54+
const struct key_type *type,
55+
const union key_payload *payload);
56+
5057
extern int verify_signature(const struct key *key,
5158
const struct public_key_signature *sig);
5259

include/keys/system_keyring.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@
1212
#ifndef _KEYS_SYSTEM_KEYRING_H
1313
#define _KEYS_SYSTEM_KEYRING_H
1414

15+
#include <linux/key.h>
16+
1517
#ifdef CONFIG_SYSTEM_TRUSTED_KEYRING
1618

17-
#include <linux/key.h>
18-
#include <linux/verification.h>
19-
#include <crypto/public_key.h>
19+
extern int restrict_link_by_builtin_trusted(struct key *keyring,
20+
const struct key_type *type,
21+
unsigned long flags,
22+
const union key_payload *payload);
2023

21-
extern struct key *system_trusted_keyring;
22-
static inline struct key *get_system_trusted_keyring(void)
23-
{
24-
return system_trusted_keyring;
25-
}
2624
#else
27-
static inline struct key *get_system_trusted_keyring(void)
28-
{
29-
return NULL;
30-
}
25+
#define restrict_link_by_builtin_trusted restrict_link_reject
3126
#endif
3227

3328
#ifdef CONFIG_IMA_MOK_KEYRING

kernel/module_signing.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <linux/kernel.h>
1313
#include <linux/errno.h>
1414
#include <linux/string.h>
15-
#include <keys/system_keyring.h>
15+
#include <linux/verification.h>
1616
#include <crypto/public_key.h>
1717
#include "module-internal.h"
1818

security/integrity/digsig.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <linux/cred.h>
1919
#include <linux/key-type.h>
2020
#include <linux/digsig.h>
21+
#include <crypto/public_key.h>
22+
#include <keys/system_keyring.h>
2123

2224
#include "integrity.h"
2325

@@ -40,6 +42,35 @@ static bool init_keyring __initdata = true;
4042
static bool init_keyring __initdata;
4143
#endif
4244

45+
#ifdef CONFIG_SYSTEM_TRUSTED_KEYRING
46+
/*
47+
* Restrict the addition of keys into the IMA keyring.
48+
*
49+
* Any key that needs to go in .ima keyring must be signed by CA in
50+
* either .system or .ima_mok keyrings.
51+
*/
52+
static int restrict_link_by_ima_mok(struct key *keyring,
53+
const struct key_type *type,
54+
unsigned long flags,
55+
const union key_payload *payload)
56+
{
57+
int ret;
58+
59+
ret = restrict_link_by_builtin_trusted(keyring, type, flags, payload);
60+
if (ret != -ENOKEY)
61+
return ret;
62+
63+
return restrict_link_by_signature(get_ima_mok_keyring(),
64+
type, payload);
65+
}
66+
#else
67+
/*
68+
* If there's no system trusted keyring, then keys cannot be loaded into
69+
* .ima_mok and added keys cannot be marked trusted.
70+
*/
71+
#define restrict_link_by_ima_mok restrict_link_reject
72+
#endif
73+
4374
int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
4475
const char *digest, int digestlen)
4576
{
@@ -84,7 +115,7 @@ int __init integrity_init_keyring(const unsigned int id)
84115
KEY_USR_VIEW | KEY_USR_READ |
85116
KEY_USR_WRITE | KEY_USR_SEARCH),
86117
KEY_ALLOC_NOT_IN_QUOTA,
87-
NULL, NULL);
118+
restrict_link_by_ima_mok, NULL);
88119
if (IS_ERR(keyring[id])) {
89120
err = PTR_ERR(keyring[id]);
90121
pr_info("Can't allocate %s keyring (%d)\n",

security/integrity/ima/ima_mok.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <linux/cred.h>
1818
#include <linux/err.h>
1919
#include <linux/init.h>
20-
#include <keys/asymmetric-type.h>
20+
#include <keys/system_keyring.h>
2121

2222

2323
struct key *ima_mok_keyring;
@@ -36,15 +36,15 @@ __init int ima_mok_init(void)
3636
KEY_USR_VIEW | KEY_USR_READ |
3737
KEY_USR_WRITE | KEY_USR_SEARCH,
3838
KEY_ALLOC_NOT_IN_QUOTA,
39-
keyring_restrict_trusted_only, NULL);
39+
restrict_link_by_builtin_trusted, NULL);
4040

4141
ima_blacklist_keyring = keyring_alloc(".ima_blacklist",
4242
KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
4343
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
4444
KEY_USR_VIEW | KEY_USR_READ |
4545
KEY_USR_WRITE | KEY_USR_SEARCH,
4646
KEY_ALLOC_NOT_IN_QUOTA,
47-
keyring_restrict_trusted_only, NULL);
47+
restrict_link_by_builtin_trusted, NULL);
4848

4949
if (IS_ERR(ima_mok_keyring) || IS_ERR(ima_blacklist_keyring))
5050
panic("Can't allocate IMA MOK or blacklist keyrings.");

0 commit comments

Comments
 (0)