Skip to content

Commit 5580b4a

Browse files
author
James Morris
committed
Merge branch 'next-integrity' of git://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity into next-integrity
From Mimi: In Linux 4.19, a new LSM hook named security_kernel_load_data was upstreamed, allowing LSMs and IMA to prevent the kexec_load syscall.  Different signature verification methods exist for verifying the kexec'ed kernel image.  This pull request adds additional support in IMA to prevent loading unsigned kernel images via the kexec_load syscall, independently of the IMA policy rules, based on the runtime "secure boot" flag.  An initial IMA kselftest is included. In addition, this pull request defines a new, separate keyring named ".platform" for storing the preboot/firmware keys needed for verifying the kexec'ed kernel image's signature and includes the associated IMA kexec usage of the ".platform" keyring. (David Howell's and Josh Boyer's patches for reading the preboot/firmware keys, which were previously posted for a different use case scenario, are included here.)
2 parents 8bd8ea1 + eed9de3 commit 5580b4a

20 files changed

Lines changed: 862 additions & 92 deletions

File tree

‎Documentation/security/keys/trusted-encrypted.rst‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,33 @@ integrity verifications match. A loaded Trusted Key can be updated with new
1818
when the kernel and initramfs are updated. The same key can have many saved
1919
blobs under different PCR values, so multiple boots are easily supported.
2020

21+
TPM 1.2
22+
-------
23+
2124
By default, trusted keys are sealed under the SRK, which has the default
2225
authorization value (20 zeros). This can be set at takeownership time with the
2326
trouser's utility: "tpm_takeownership -u -z".
2427

28+
TPM 2.0
29+
-------
30+
31+
The user must first create a storage key and make it persistent, so the key is
32+
available after reboot. This can be done using the following commands.
33+
34+
With the IBM TSS 2 stack::
35+
36+
#> tsscreateprimary -hi o -st
37+
Handle 80000000
38+
#> tssevictcontrol -hi o -ho 80000000 -hp 81000001
39+
40+
Or with the Intel TSS 2 stack::
41+
42+
#> tpm2_createprimary --hierarchy o -G rsa2048 -o key.ctxt
43+
[...]
44+
handle: 0x800000FF
45+
#> tpm2_evictcontrol -c key.ctxt -p 0x81000001
46+
persistentHandle: 0x81000001
47+
2548
Usage::
2649

2750
keyctl add trusted name "new keylen [options]" ring
@@ -30,7 +53,9 @@ Usage::
3053
keyctl print keyid
3154

3255
options:
33-
keyhandle= ascii hex value of sealing key default 0x40000000 (SRK)
56+
keyhandle= ascii hex value of sealing key
57+
TPM 1.2: default 0x40000000 (SRK)
58+
TPM 2.0: no default; must be passed every time
3459
keyauth= ascii hex auth for sealing key default 0x00...i
3560
(40 ascii zeros)
3661
blobauth= ascii hex auth for sealed data default 0x00...
@@ -84,6 +109,10 @@ Examples of trusted and encrypted key usage:
84109

85110
Create and save a trusted key named "kmk" of length 32 bytes::
86111

112+
Note: When using a TPM 2.0 with a persistent key with handle 0x81000001,
113+
append 'keyhandle=0x81000001' to statements between quotes, such as
114+
"new 32 keyhandle=0x81000001".
115+
87116
$ keyctl add trusted kmk "new 32" @u
88117
440502848
89118

‎arch/x86/kernel/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,7 @@ ifeq ($(CONFIG_X86_64),y)
150150
obj-$(CONFIG_MMCONF_FAM10H) += mmconf-fam10h_64.o
151151
obj-y += vsmp_64.o
152152
endif
153+
154+
ifdef CONFIG_EFI
155+
obj-$(CONFIG_IMA) += ima_arch.o
156+
endif

‎arch/x86/kernel/ima_arch.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
/*
3+
* Copyright (C) 2018 IBM Corporation
4+
*/
5+
#include <linux/efi.h>
6+
#include <linux/ima.h>
7+
8+
extern struct boot_params boot_params;
9+
10+
static enum efi_secureboot_mode get_sb_mode(void)
11+
{
12+
efi_char16_t efi_SecureBoot_name[] = L"SecureBoot";
13+
efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
14+
efi_status_t status;
15+
unsigned long size;
16+
u8 secboot;
17+
18+
size = sizeof(secboot);
19+
20+
/* Get variable contents into buffer */
21+
status = efi.get_variable(efi_SecureBoot_name, &efi_variable_guid,
22+
NULL, &size, &secboot);
23+
if (status == EFI_NOT_FOUND) {
24+
pr_info("ima: secureboot mode disabled\n");
25+
return efi_secureboot_mode_disabled;
26+
}
27+
28+
if (status != EFI_SUCCESS) {
29+
pr_info("ima: secureboot mode unknown\n");
30+
return efi_secureboot_mode_unknown;
31+
}
32+
33+
if (secboot == 0) {
34+
pr_info("ima: secureboot mode disabled\n");
35+
return efi_secureboot_mode_disabled;
36+
}
37+
38+
pr_info("ima: secureboot mode enabled\n");
39+
return efi_secureboot_mode_enabled;
40+
}
41+
42+
bool arch_ima_get_secureboot(void)
43+
{
44+
static enum efi_secureboot_mode sb_mode;
45+
static bool initialized;
46+
47+
if (!initialized && efi_enabled(EFI_BOOT)) {
48+
sb_mode = boot_params.secure_boot;
49+
50+
if (sb_mode == efi_secureboot_mode_unset)
51+
sb_mode = get_sb_mode();
52+
initialized = true;
53+
}
54+
55+
if (sb_mode == efi_secureboot_mode_enabled)
56+
return true;
57+
else
58+
return false;
59+
}
60+
61+
/* secureboot arch rules */
62+
static const char * const sb_arch_rules[] = {
63+
#if !IS_ENABLED(CONFIG_KEXEC_VERIFY_SIG)
64+
"appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
65+
#endif /* CONFIG_KEXEC_VERIFY_SIG */
66+
"measure func=KEXEC_KERNEL_CHECK",
67+
NULL
68+
};
69+
70+
const char * const *arch_get_ima_policy(void)
71+
{
72+
if (IS_ENABLED(CONFIG_IMA_ARCH_POLICY) && arch_ima_get_secureboot())
73+
return sb_arch_rules;
74+
return NULL;
75+
}

‎include/linux/efi.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ void efi_native_runtime_setup(void);
663663
#define EFI_IMAGE_SECURITY_DATABASE_GUID EFI_GUID(0xd719b2cb, 0x3d3a, 0x4596, 0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f)
664664
#define EFI_SHIM_LOCK_GUID EFI_GUID(0x605dab50, 0xe046, 0x4300, 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
665665

666+
#define EFI_CERT_SHA256_GUID EFI_GUID(0xc1c41626, 0x504c, 0x4092, 0xac, 0xa9, 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28)
667+
#define EFI_CERT_X509_GUID EFI_GUID(0xa5c059a1, 0x94e4, 0x4aa7, 0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72)
668+
#define EFI_CERT_X509_SHA256_GUID EFI_GUID(0x3bd2a492, 0x96c0, 0x4079, 0xb4, 0x20, 0xfc, 0xf9, 0x8e, 0xf1, 0x03, 0xed)
669+
666670
/*
667671
* This GUID is used to pass to the kernel proper the struct screen_info
668672
* structure that was populated by the stub based on the GOP protocol instance
@@ -934,6 +938,27 @@ typedef struct {
934938
efi_memory_desc_t entry[0];
935939
} efi_memory_attributes_table_t;
936940

941+
typedef struct {
942+
efi_guid_t signature_owner;
943+
u8 signature_data[];
944+
} efi_signature_data_t;
945+
946+
typedef struct {
947+
efi_guid_t signature_type;
948+
u32 signature_list_size;
949+
u32 signature_header_size;
950+
u32 signature_size;
951+
u8 signature_header[];
952+
/* efi_signature_data_t signatures[][] */
953+
} efi_signature_list_t;
954+
955+
typedef u8 efi_sha256_hash_t[32];
956+
957+
typedef struct {
958+
efi_sha256_hash_t to_be_signed_hash;
959+
efi_time_t time_of_revocation;
960+
} efi_cert_x509_sha256_t;
961+
937962
/*
938963
* All runtime access to EFI goes through this structure:
939964
*/
@@ -1116,6 +1141,15 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm,
11161141
char * __init efi_md_typeattr_format(char *buf, size_t size,
11171142
const efi_memory_desc_t *md);
11181143

1144+
1145+
typedef void (*efi_element_handler_t)(const char *source,
1146+
const void *element_data,
1147+
size_t element_size);
1148+
extern int __init parse_efi_signature_list(
1149+
const char *source,
1150+
const void *data, size_t size,
1151+
efi_element_handler_t (*get_handler_for_guid)(const efi_guid_t *));
1152+
11191153
/**
11201154
* efi_range_is_wc - check the WC bit on an address range
11211155
* @start: starting kvirt address

‎include/linux/ima.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ extern void ima_post_path_mknod(struct dentry *dentry);
3030
extern void ima_add_kexec_buffer(struct kimage *image);
3131
#endif
3232

33+
#if defined(CONFIG_X86) && defined(CONFIG_EFI)
34+
extern bool arch_ima_get_secureboot(void);
35+
extern const char * const *arch_get_ima_policy(void);
36+
#else
37+
static inline bool arch_ima_get_secureboot(void)
38+
{
39+
return false;
40+
}
41+
42+
static inline const char * const *arch_get_ima_policy(void)
43+
{
44+
return NULL;
45+
}
46+
#endif
47+
3348
#else
3449
static inline int ima_bprm_check(struct linux_binprm *bprm)
3550
{

‎security/integrity/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ config INTEGRITY_TRUSTED_KEYRING
5151
.evm keyrings be signed by a key on the system trusted
5252
keyring.
5353

54+
config INTEGRITY_PLATFORM_KEYRING
55+
bool "Provide keyring for platform/firmware trusted keys"
56+
depends on INTEGRITY_ASYMMETRIC_KEYS
57+
depends on SYSTEM_BLACKLIST_KEYRING
58+
depends on EFI
59+
help
60+
Provide a separate, distinct keyring for platform trusted keys, which
61+
the kernel automatically populates during initialization from values
62+
provided by the platform for verifying the kexec'ed kerned image
63+
and, possibly, the initramfs signature.
64+
5465
config INTEGRITY_AUDIT
5566
bool "Enables integrity auditing support "
5667
depends on AUDIT

‎security/integrity/Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ integrity-y := iint.o
99
integrity-$(CONFIG_INTEGRITY_AUDIT) += integrity_audit.o
1010
integrity-$(CONFIG_INTEGRITY_SIGNATURE) += digsig.o
1111
integrity-$(CONFIG_INTEGRITY_ASYMMETRIC_KEYS) += digsig_asymmetric.o
12+
integrity-$(CONFIG_INTEGRITY_PLATFORM_KEYRING) += platform_certs/platform_keyring.o \
13+
platform_certs/efi_parser.o \
14+
platform_certs/load_uefi.o
15+
obj-$(CONFIG_LOAD_UEFI_KEYS) += platform_certs/load_uefi.o
16+
$(obj)/load_uefi.o: KBUILD_CFLAGS += -fshort-wchar
1217

1318
subdir-$(CONFIG_IMA) += ima
1419
obj-$(CONFIG_IMA) += ima/

‎security/integrity/digsig.c‎

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static const char * const keyring_name[INTEGRITY_KEYRING_MAX] = {
3535
".ima",
3636
#endif
3737
"_module",
38+
".platform",
3839
};
3940

4041
#ifdef CONFIG_IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY
@@ -73,12 +74,38 @@ int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
7374
return -EOPNOTSUPP;
7475
}
7576

76-
int __init integrity_init_keyring(const unsigned int id)
77+
static int __integrity_init_keyring(const unsigned int id, key_perm_t perm,
78+
struct key_restriction *restriction)
7779
{
7880
const struct cred *cred = current_cred();
79-
struct key_restriction *restriction;
8081
int err = 0;
8182

83+
keyring[id] = keyring_alloc(keyring_name[id], KUIDT_INIT(0),
84+
KGIDT_INIT(0), cred, perm,
85+
KEY_ALLOC_NOT_IN_QUOTA, restriction, NULL);
86+
if (IS_ERR(keyring[id])) {
87+
err = PTR_ERR(keyring[id]);
88+
pr_info("Can't allocate %s keyring (%d)\n",
89+
keyring_name[id], err);
90+
keyring[id] = NULL;
91+
}
92+
93+
return err;
94+
}
95+
96+
int __init integrity_init_keyring(const unsigned int id)
97+
{
98+
struct key_restriction *restriction;
99+
key_perm_t perm;
100+
101+
perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW
102+
| KEY_USR_READ | KEY_USR_SEARCH;
103+
104+
if (id == INTEGRITY_KEYRING_PLATFORM) {
105+
restriction = NULL;
106+
goto out;
107+
}
108+
82109
if (!IS_ENABLED(CONFIG_INTEGRITY_TRUSTED_KEYRING))
83110
return 0;
84111

@@ -87,32 +114,43 @@ int __init integrity_init_keyring(const unsigned int id)
87114
return -ENOMEM;
88115

89116
restriction->check = restrict_link_to_ima;
117+
perm |= KEY_USR_WRITE;
90118

91-
keyring[id] = keyring_alloc(keyring_name[id], KUIDT_INIT(0),
92-
KGIDT_INIT(0), cred,
93-
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
94-
KEY_USR_VIEW | KEY_USR_READ |
95-
KEY_USR_WRITE | KEY_USR_SEARCH),
96-
KEY_ALLOC_NOT_IN_QUOTA,
97-
restriction, NULL);
98-
if (IS_ERR(keyring[id])) {
99-
err = PTR_ERR(keyring[id]);
100-
pr_info("Can't allocate %s keyring (%d)\n",
101-
keyring_name[id], err);
102-
keyring[id] = NULL;
119+
out:
120+
return __integrity_init_keyring(id, perm, restriction);
121+
}
122+
123+
int __init integrity_add_key(const unsigned int id, const void *data,
124+
off_t size, key_perm_t perm)
125+
{
126+
key_ref_t key;
127+
int rc = 0;
128+
129+
if (!keyring[id])
130+
return -EINVAL;
131+
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);
103142
}
104-
return err;
143+
144+
return rc;
145+
105146
}
106147

107148
int __init integrity_load_x509(const unsigned int id, const char *path)
108149
{
109-
key_ref_t key;
110150
void *data;
111151
loff_t size;
112152
int rc;
113-
114-
if (!keyring[id])
115-
return -EINVAL;
153+
key_perm_t perm;
116154

117155
rc = kernel_read_file_from_path(path, &data, &size, 0,
118156
READING_X509_CERTIFICATE);
@@ -121,23 +159,21 @@ int __init integrity_load_x509(const unsigned int id, const char *path)
121159
return rc;
122160
}
123161

124-
key = key_create_or_update(make_key_ref(keyring[id], 1),
125-
"asymmetric",
126-
NULL,
127-
data,
128-
size,
129-
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
130-
KEY_USR_VIEW | KEY_USR_READ),
131-
KEY_ALLOC_NOT_IN_QUOTA);
132-
if (IS_ERR(key)) {
133-
rc = PTR_ERR(key);
134-
pr_err("Problem loading X.509 certificate (%d): %s\n",
135-
rc, path);
136-
} else {
137-
pr_notice("Loaded X.509 cert '%s': %s\n",
138-
key_ref_to_ptr(key)->description, path);
139-
key_ref_put(key);
140-
}
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+
141167
vfree(data);
142-
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);
143179
}

0 commit comments

Comments
 (0)