Skip to content

Commit 64724cf

Browse files
committed
Merge remote-tracking branch 'integrity/next-with-keys' into keys-next
Signed-off-by: David Howells <dhowells@redhat.com>
2 parents 6204e00 + 7d2ce23 commit 64724cf

13 files changed

Lines changed: 225 additions & 25 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
566566
possible to determine what the correct size should be.
567567
This option provides an override for these situations.
568568

569+
ca_keys= [KEYS] This parameter identifies a specific key(s) on
570+
the system trusted keyring to be used for certificate
571+
trust validation.
572+
format: { id:<keyid> | builtin }
573+
569574
ccw_timeout_log [S390]
570575
See Documentation/s390/CommonIO for details.
571576

‎crypto/asymmetric_keys/asymmetric_keys.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* 2 of the Licence, or (at your option) any later version.
1010
*/
1111

12+
int asymmetric_keyid_match(const char *kid, const char *id);
13+
1214
static inline const char *asymmetric_key_id(const struct key *key)
1315
{
1416
return key->type_data.p[1];

‎crypto/asymmetric_keys/asymmetric_type.c‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@ MODULE_LICENSE("GPL");
2222
static LIST_HEAD(asymmetric_key_parsers);
2323
static DECLARE_RWSEM(asymmetric_key_parsers_sem);
2424

25+
/*
26+
* Match asymmetric key id with partial match
27+
* @id: key id to match in a form "id:<id>"
28+
*/
29+
int asymmetric_keyid_match(const char *kid, const char *id)
30+
{
31+
size_t idlen, kidlen;
32+
33+
if (!kid || !id)
34+
return 0;
35+
36+
/* make it possible to use id as in the request: "id:<id>" */
37+
if (strncmp(id, "id:", 3) == 0)
38+
id += 3;
39+
40+
/* Anything after here requires a partial match on the ID string */
41+
idlen = strlen(id);
42+
kidlen = strlen(kid);
43+
if (idlen > kidlen)
44+
return 0;
45+
46+
kid += kidlen - idlen;
47+
if (strcasecmp(id, kid) != 0)
48+
return 0;
49+
50+
return 1;
51+
}
52+
EXPORT_SYMBOL_GPL(asymmetric_keyid_match);
53+
2554
/*
2655
* Match asymmetric keys on (part of) their name
2756
* We have some shorthand methods for matching keys. We allow:
@@ -34,9 +63,8 @@ static int asymmetric_key_match(const struct key *key, const void *description)
3463
{
3564
const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
3665
const char *spec = description;
37-
const char *id, *kid;
66+
const char *id;
3867
ptrdiff_t speclen;
39-
size_t idlen, kidlen;
4068

4169
if (!subtype || !spec || !*spec)
4270
return 0;
@@ -55,23 +83,8 @@ static int asymmetric_key_match(const struct key *key, const void *description)
5583
speclen = id - spec;
5684
id++;
5785

58-
/* Anything after here requires a partial match on the ID string */
59-
kid = asymmetric_key_id(key);
60-
if (!kid)
61-
return 0;
62-
63-
idlen = strlen(id);
64-
kidlen = strlen(kid);
65-
if (idlen > kidlen)
66-
return 0;
67-
68-
kid += kidlen - idlen;
69-
if (strcasecmp(id, kid) != 0)
70-
return 0;
71-
72-
if (speclen == 2 &&
73-
memcmp(spec, "id", 2) == 0)
74-
return 1;
86+
if (speclen == 2 && memcmp(spec, "id", 2) == 0)
87+
return asymmetric_keyid_match(asymmetric_key_id(key), id);
7588

7689
if (speclen == subtype->name_len &&
7790
memcmp(spec, subtype->name, speclen) == 0)

‎crypto/asymmetric_keys/x509_public_key.c‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,80 @@
1818
#include <linux/asn1_decoder.h>
1919
#include <keys/asymmetric-subtype.h>
2020
#include <keys/asymmetric-parser.h>
21+
#include <keys/system_keyring.h>
2122
#include <crypto/hash.h>
2223
#include "asymmetric_keys.h"
2324
#include "public_key.h"
2425
#include "x509_parser.h"
2526

27+
static bool use_builtin_keys;
28+
static char *ca_keyid;
29+
30+
#ifndef MODULE
31+
static int __init ca_keys_setup(char *str)
32+
{
33+
if (!str) /* default system keyring */
34+
return 1;
35+
36+
if (strncmp(str, "id:", 3) == 0)
37+
ca_keyid = str; /* owner key 'id:xxxxxx' */
38+
else if (strcmp(str, "builtin") == 0)
39+
use_builtin_keys = true;
40+
41+
return 1;
42+
}
43+
__setup("ca_keys=", ca_keys_setup);
44+
#endif
45+
46+
/*
47+
* Find a key in the given keyring by issuer and authority.
48+
*/
49+
static struct key *x509_request_asymmetric_key(struct key *keyring,
50+
const char *signer,
51+
size_t signer_len,
52+
const char *authority,
53+
size_t auth_len)
54+
{
55+
key_ref_t key;
56+
char *id;
57+
58+
/* Construct an identifier. */
59+
id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL);
60+
if (!id)
61+
return ERR_PTR(-ENOMEM);
62+
63+
memcpy(id, signer, signer_len);
64+
id[signer_len + 0] = ':';
65+
id[signer_len + 1] = ' ';
66+
memcpy(id + signer_len + 2, authority, auth_len);
67+
id[signer_len + 2 + auth_len] = 0;
68+
69+
pr_debug("Look up: \"%s\"\n", id);
70+
71+
key = keyring_search(make_key_ref(keyring, 1),
72+
&key_type_asymmetric, id);
73+
if (IS_ERR(key))
74+
pr_debug("Request for module key '%s' err %ld\n",
75+
id, PTR_ERR(key));
76+
kfree(id);
77+
78+
if (IS_ERR(key)) {
79+
switch (PTR_ERR(key)) {
80+
/* Hide some search errors */
81+
case -EACCES:
82+
case -ENOTDIR:
83+
case -EAGAIN:
84+
return ERR_PTR(-ENOKEY);
85+
default:
86+
return ERR_CAST(key);
87+
}
88+
}
89+
90+
pr_devel("<==%s() = 0 [%x]\n", __func__,
91+
key_serial(key_ref_to_ptr(key)));
92+
return key_ref_to_ptr(key);
93+
}
94+
2695
/*
2796
* Set up the signature parameters in an X.509 certificate. This involves
2897
* digesting the signed data and extracting the signature.
@@ -102,6 +171,40 @@ int x509_check_signature(const struct public_key *pub,
102171
}
103172
EXPORT_SYMBOL_GPL(x509_check_signature);
104173

174+
/*
175+
* Check the new certificate against the ones in the trust keyring. If one of
176+
* those is the signing key and validates the new certificate, then mark the
177+
* new certificate as being trusted.
178+
*
179+
* Return 0 if the new certificate was successfully validated, 1 if we couldn't
180+
* find a matching parent certificate in the trusted list and an error if there
181+
* is a matching certificate but the signature check fails.
182+
*/
183+
static int x509_validate_trust(struct x509_certificate *cert,
184+
struct key *trust_keyring)
185+
{
186+
struct key *key;
187+
int ret = 1;
188+
189+
if (!trust_keyring)
190+
return -EOPNOTSUPP;
191+
192+
if (ca_keyid && !asymmetric_keyid_match(cert->authority, ca_keyid))
193+
return -EPERM;
194+
195+
key = x509_request_asymmetric_key(trust_keyring,
196+
cert->issuer, strlen(cert->issuer),
197+
cert->authority,
198+
strlen(cert->authority));
199+
if (!IS_ERR(key)) {
200+
if (!use_builtin_keys
201+
|| test_bit(KEY_FLAG_BUILTIN, &key->flags))
202+
ret = x509_check_signature(key->payload.data, cert);
203+
key_put(key);
204+
}
205+
return ret;
206+
}
207+
105208
/*
106209
* Attempt to parse a data blob for a key as an X509 certificate.
107210
*/
@@ -155,9 +258,13 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
155258
/* Check the signature on the key if it appears to be self-signed */
156259
if (!cert->authority ||
157260
strcmp(cert->fingerprint, cert->authority) == 0) {
158-
ret = x509_check_signature(cert->pub, cert);
261+
ret = x509_check_signature(cert->pub, cert); /* self-signed */
159262
if (ret < 0)
160263
goto error_free_cert;
264+
} else if (!prep->trusted) {
265+
ret = x509_validate_trust(cert, get_system_trusted_keyring());
266+
if (!ret)
267+
prep->trusted = 1;
161268
}
162269

163270
/* Propose a description */

‎include/keys/system_keyring.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
#include <linux/key.h>
1818

1919
extern struct key *system_trusted_keyring;
20-
20+
static inline struct key *get_system_trusted_keyring(void)
21+
{
22+
return system_trusted_keyring;
23+
}
24+
#else
25+
static inline struct key *get_system_trusted_keyring(void)
26+
{
27+
return NULL;
28+
}
2129
#endif
2230

2331
#endif /* _KEYS_SYSTEM_KEYRING_H */

‎include/linux/key.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ struct key {
170170
#define KEY_FLAG_INVALIDATED 7 /* set if key has been invalidated */
171171
#define KEY_FLAG_TRUSTED 8 /* set if key is trusted */
172172
#define KEY_FLAG_TRUSTED_ONLY 9 /* set if keyring only accepts links to trusted keys */
173+
#define KEY_FLAG_BUILTIN 10 /* set if key is builtin */
173174

174175
/* the key type and key description string
175176
* - the desc is used to match a key against search criteria

‎kernel/system_keyring.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static __init int load_system_certificate_list(void)
8989
pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
9090
PTR_ERR(key));
9191
} else {
92+
set_bit(KEY_FLAG_BUILTIN, &key_ref_to_ptr(key)->flags);
9293
pr_notice("Loaded X.509 cert '%s'\n",
9394
key_ref_to_ptr(key)->description);
9495
key_ref_put(key);

‎security/integrity/digsig.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1414

1515
#include <linux/err.h>
16+
#include <linux/sched.h>
1617
#include <linux/rbtree.h>
18+
#include <linux/cred.h>
1719
#include <linux/key-type.h>
1820
#include <linux/digsig.h>
1921

@@ -24,7 +26,11 @@ static struct key *keyring[INTEGRITY_KEYRING_MAX];
2426
static const char *keyring_name[INTEGRITY_KEYRING_MAX] = {
2527
"_evm",
2628
"_module",
29+
#ifndef CONFIG_IMA_TRUSTED_KEYRING
2730
"_ima",
31+
#else
32+
".ima",
33+
#endif
2834
};
2935

3036
int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
@@ -56,3 +62,25 @@ int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
5662

5763
return -EOPNOTSUPP;
5864
}
65+
66+
int integrity_init_keyring(const unsigned int id)
67+
{
68+
const struct cred *cred = current_cred();
69+
int err = 0;
70+
71+
keyring[id] = keyring_alloc(keyring_name[id], KUIDT_INIT(0),
72+
KGIDT_INIT(0), cred,
73+
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
74+
KEY_USR_VIEW | KEY_USR_READ |
75+
KEY_USR_WRITE | KEY_USR_SEARCH),
76+
KEY_ALLOC_NOT_IN_QUOTA, NULL);
77+
if (!IS_ERR(keyring[id]))
78+
set_bit(KEY_FLAG_TRUSTED_ONLY, &keyring[id]->flags);
79+
else {
80+
err = PTR_ERR(keyring[id]);
81+
pr_info("Can't allocate %s keyring (%d)\n",
82+
keyring_name[id], err);
83+
keyring[id] = NULL;
84+
}
85+
return err;
86+
}

‎security/integrity/ima/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,13 @@ config IMA_APPRAISE
123123
For more information on integrity appraisal refer to:
124124
<http://linux-ima.sourceforge.net>
125125
If unsure, say N.
126+
127+
config IMA_TRUSTED_KEYRING
128+
bool "Require all keys on the .ima keyring be signed"
129+
depends on IMA_APPRAISE && SYSTEM_TRUSTED_KEYRING
130+
depends on INTEGRITY_ASYMMETRIC_KEYS
131+
select KEYS_DEBUG_PROC_KEYS
132+
default y
133+
help
134+
This option requires that all keys added to the .ima
135+
keyring be signed by a key on the system trusted keyring.

‎security/integrity/ima/ima.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,16 @@ static inline int security_filter_rule_match(u32 secid, u32 field, u32 op,
249249
return -EINVAL;
250250
}
251251
#endif /* CONFIG_IMA_LSM_RULES */
252+
253+
#ifdef CONFIG_IMA_TRUSTED_KEYRING
254+
static inline int ima_init_keyring(const unsigned int id)
255+
{
256+
return integrity_init_keyring(id);
257+
}
258+
#else
259+
static inline int ima_init_keyring(const unsigned int id)
260+
{
261+
return 0;
262+
}
263+
#endif /* CONFIG_IMA_TRUSTED_KEYRING */
252264
#endif

0 commit comments

Comments
 (0)