Skip to content

Commit 3cb92fe

Browse files
author
James Morris
committed
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity into next
2 parents 5beb0c4 + 0112721 commit 3cb92fe

23 files changed

Lines changed: 657 additions & 84 deletions

File tree

‎crypto/asymmetric_keys/x509_public_key.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
321321
goto error_free_cert;
322322
} else if (!prep->trusted) {
323323
ret = x509_validate_trust(cert, get_system_trusted_keyring());
324+
if (ret)
325+
ret = x509_validate_trust(cert, get_ima_mok_keyring());
324326
if (!ret)
325327
prep->trusted = 1;
326328
}

‎include/keys/system_keyring.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,28 @@ extern int system_verify_data(const void *data, unsigned long len,
3535
enum key_being_used_for usage);
3636
#endif
3737

38+
#ifdef CONFIG_IMA_MOK_KEYRING
39+
extern struct key *ima_mok_keyring;
40+
extern struct key *ima_blacklist_keyring;
41+
42+
static inline struct key *get_ima_mok_keyring(void)
43+
{
44+
return ima_mok_keyring;
45+
}
46+
static inline struct key *get_ima_blacklist_keyring(void)
47+
{
48+
return ima_blacklist_keyring;
49+
}
50+
#else
51+
static inline struct key *get_ima_mok_keyring(void)
52+
{
53+
return NULL;
54+
}
55+
static inline struct key *get_ima_blacklist_keyring(void)
56+
{
57+
return NULL;
58+
}
59+
#endif /* CONFIG_IMA_MOK_KEYRING */
60+
61+
3862
#endif /* _KEYS_SYSTEM_KEYRING_H */

‎include/linux/evm.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
struct integrity_iint_cache;
1515

1616
#ifdef CONFIG_EVM
17+
extern int evm_set_key(void *key, size_t keylen);
1718
extern enum integrity_status evm_verifyxattr(struct dentry *dentry,
1819
const char *xattr_name,
1920
void *xattr_value,
@@ -42,6 +43,12 @@ static inline int posix_xattr_acl(const char *xattrname)
4243
}
4344
#endif
4445
#else
46+
47+
static inline int evm_set_key(void *key, size_t keylen)
48+
{
49+
return -EOPNOTSUPP;
50+
}
51+
4552
#ifdef CONFIG_INTEGRITY
4653
static inline enum integrity_status evm_verifyxattr(struct dentry *dentry,
4754
const char *xattr_name,

‎include/linux/key.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ struct key {
177177
#define KEY_FLAG_TRUSTED_ONLY 9 /* set if keyring only accepts links to trusted keys */
178178
#define KEY_FLAG_BUILTIN 10 /* set if key is builtin */
179179
#define KEY_FLAG_ROOT_CAN_INVAL 11 /* set if key can be invalidated by root without permission */
180+
#define KEY_FLAG_KEEP 12 /* set if key should not be removed */
180181

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

‎security/integrity/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ config INTEGRITY_ASYMMETRIC_KEYS
4141
This option enables digital signature verification using
4242
asymmetric keys.
4343

44+
config INTEGRITY_TRUSTED_KEYRING
45+
bool "Require all keys on the integrity keyrings be signed"
46+
depends on SYSTEM_TRUSTED_KEYRING
47+
depends on INTEGRITY_ASYMMETRIC_KEYS
48+
select KEYS_DEBUG_PROC_KEYS
49+
default y
50+
help
51+
This option requires that all keys added to the .ima and
52+
.evm keyrings be signed by a key on the system trusted
53+
keyring.
54+
4455
config INTEGRITY_AUDIT
4556
bool "Enables integrity auditing support "
4657
depends on AUDIT

‎security/integrity/digsig.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@
2424
static struct key *keyring[INTEGRITY_KEYRING_MAX];
2525

2626
static const char *keyring_name[INTEGRITY_KEYRING_MAX] = {
27+
#ifndef CONFIG_INTEGRITY_TRUSTED_KEYRING
2728
"_evm",
28-
"_module",
29-
#ifndef CONFIG_IMA_TRUSTED_KEYRING
3029
"_ima",
3130
#else
31+
".evm",
3232
".ima",
3333
#endif
34+
"_module",
3435
};
3536

37+
#ifdef CONFIG_INTEGRITY_TRUSTED_KEYRING
38+
static bool init_keyring __initdata = true;
39+
#else
40+
static bool init_keyring __initdata;
41+
#endif
42+
3643
int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
3744
const char *digest, int digestlen)
3845
{
@@ -68,6 +75,9 @@ int __init integrity_init_keyring(const unsigned int id)
6875
const struct cred *cred = current_cred();
6976
int err = 0;
7077

78+
if (!init_keyring)
79+
return 0;
80+
7181
keyring[id] = keyring_alloc(keyring_name[id], KUIDT_INIT(0),
7282
KGIDT_INIT(0), cred,
7383
((KEY_POS_ALL & ~KEY_POS_SETATTR) |

‎security/integrity/digsig_asymmetric.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/key-type.h>
1818
#include <crypto/public_key.h>
1919
#include <keys/asymmetric-type.h>
20+
#include <keys/system_keyring.h>
2021

2122
#include "integrity.h"
2223

@@ -32,9 +33,22 @@ static struct key *request_asymmetric_key(struct key *keyring, uint32_t keyid)
3233

3334
pr_debug("key search: \"%s\"\n", name);
3435

36+
key = get_ima_blacklist_keyring();
37+
if (key) {
38+
key_ref_t kref;
39+
40+
kref = keyring_search(make_key_ref(key, 1),
41+
&key_type_asymmetric, name);
42+
if (!IS_ERR(kref)) {
43+
pr_err("Key '%s' is in ima_blacklist_keyring\n", name);
44+
return ERR_PTR(-EKEYREJECTED);
45+
}
46+
}
47+
3548
if (keyring) {
3649
/* search in specific keyring */
3750
key_ref_t kref;
51+
3852
kref = keyring_search(make_key_ref(keyring, 1),
3953
&key_type_asymmetric, name);
4054
if (IS_ERR(kref))

‎security/integrity/evm/Kconfig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,20 @@ config EVM_EXTRA_SMACK_XATTRS
4242
additional info to the calculation, requires existing EVM
4343
labeled file systems to be relabeled.
4444

45+
config EVM_LOAD_X509
46+
bool "Load an X509 certificate onto the '.evm' trusted keyring"
47+
depends on EVM && INTEGRITY_TRUSTED_KEYRING
48+
default n
49+
help
50+
Load an X509 certificate onto the '.evm' trusted keyring.
51+
52+
This option enables X509 certificate loading from the kernel
53+
onto the '.evm' trusted keyring. A public key can be used to
54+
verify EVM integrity starting from the 'init' process.
55+
56+
config EVM_X509_PATH
57+
string "EVM X509 certificate path"
58+
depends on EVM_LOAD_X509
59+
default "/etc/keys/x509_evm.der"
60+
help
61+
This option defines X509 certificate path.

‎security/integrity/evm/evm.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#include "../integrity.h"
2323

24+
#define EVM_INIT_HMAC 0x0001
25+
#define EVM_INIT_X509 0x0002
26+
2427
extern int evm_initialized;
2528
extern char *evm_hmac;
2629
extern char *evm_hash;

‎security/integrity/evm/evm_crypto.c‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/module.h>
1919
#include <linux/crypto.h>
2020
#include <linux/xattr.h>
21+
#include <linux/evm.h>
2122
#include <keys/encrypted-type.h>
2223
#include <crypto/hash.h>
2324
#include "evm.h"
@@ -32,6 +33,44 @@ struct crypto_shash *hash_tfm;
3233

3334
static DEFINE_MUTEX(mutex);
3435

36+
#define EVM_SET_KEY_BUSY 0
37+
38+
static unsigned long evm_set_key_flags;
39+
40+
/**
41+
* evm_set_key() - set EVM HMAC key from the kernel
42+
* @key: pointer to a buffer with the key data
43+
* @size: length of the key data
44+
*
45+
* This function allows setting the EVM HMAC key from the kernel
46+
* without using the "encrypted" key subsystem keys. It can be used
47+
* by the crypto HW kernel module which has its own way of managing
48+
* keys.
49+
*
50+
* key length should be between 32 and 128 bytes long
51+
*/
52+
int evm_set_key(void *key, size_t keylen)
53+
{
54+
int rc;
55+
56+
rc = -EBUSY;
57+
if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
58+
goto busy;
59+
rc = -EINVAL;
60+
if (keylen > MAX_KEY_SIZE)
61+
goto inval;
62+
memcpy(evmkey, key, keylen);
63+
evm_initialized |= EVM_INIT_HMAC;
64+
pr_info("key initialized\n");
65+
return 0;
66+
inval:
67+
clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
68+
busy:
69+
pr_err("key initialization failed\n");
70+
return rc;
71+
}
72+
EXPORT_SYMBOL_GPL(evm_set_key);
73+
3574
static struct shash_desc *init_desc(char type)
3675
{
3776
long rc;
@@ -40,6 +79,10 @@ static struct shash_desc *init_desc(char type)
4079
struct shash_desc *desc;
4180

4281
if (type == EVM_XATTR_HMAC) {
82+
if (!(evm_initialized & EVM_INIT_HMAC)) {
83+
pr_err("HMAC key is not set\n");
84+
return ERR_PTR(-ENOKEY);
85+
}
4386
tfm = &hmac_tfm;
4487
algo = evm_hmac;
4588
} else {
@@ -240,20 +283,17 @@ int evm_init_key(void)
240283
{
241284
struct key *evm_key;
242285
struct encrypted_key_payload *ekp;
243-
int rc = 0;
286+
int rc;
244287

245288
evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
246289
if (IS_ERR(evm_key))
247290
return -ENOENT;
248291

249292
down_read(&evm_key->sem);
250293
ekp = evm_key->payload.data[0];
251-
if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
252-
rc = -EINVAL;
253-
goto out;
254-
}
255-
memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
256-
out:
294+
295+
rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
296+
257297
/* burn the original key contents */
258298
memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
259299
up_read(&evm_key->sem);

0 commit comments

Comments
 (0)