Skip to contentSkip to conversation

KEYS: Measure keys when they are created or updated

PATCH Superseded Lakshmi Ramasubramanian 2 participants 14 messages Nov 13, 2019

Subsystems (5)

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v6 cover KEYS: Measure keys when they are created or updated

Keys created or updated in the system are currently not measured. Therefore an attestation service, for instance, would not be able to attest whether or not the trusted keys keyring(s), for instance, contain only known good (trusted) keys.

IMA measures system files, command line arguments passed to kexec, boot aggregate, etc. It can be used to measure keys as well. But there is no mechanism available in the kernel for IMA to know when a key is created or updated.

This change aims to address measuring keys created or updated in the system:

To achieve the above the following changes have been made:

- Added a new IMA hook namely, ima_post_key_create_or_update, which measures the key. This IMA hook is called from key_create_or_update function. The key measurement can be controlled through IMA policy.

A new IMA policy function KEY_CHECK has been added to measure keys.

   # measure keys loaded onto any keyring
   measure func=KEY_CHECK

Testing performed:

* Booted the kernel with this change.
* When KEY_CHECK policy is set IMA measures keys loaded onto any keyring.
* Keys are not measured when KEY_CHECK is not set.
* Added a new key to a keyring.
=> Added keys to .ima and .evm keyrings.
* Added the same key again.
=> Add the same key to .ima and .evm keyrings.

Change Log:

v6:

=> Rebased the changes to v5.4-rc7
=> Renamed KEYRING_CHECK to KEY_CHECK per Mimi's suggestion. => Excluded the patches that add support for limiting key
measurement to specific keyrings ("keyrings=" option for "measure func=KEY_CHECK" in the IMA policy). Also, excluded the patches that add support for deferred processing of keys (queue support).
These patches will be added in separate patch sets later.

v5:

=> Reorganized the patches to add measurement of keys through
the IMA hook without any queuing and then added queuing support.
=> Updated the queuing functions to minimize code executed inside mutex. => Process queued keys after custom IMA policies have been applied.

v4:

=> Rebased the changes to v5.4-rc3
=> Applied the following dependent patch set first
and then added new changes.
https://lore.kernel.org/linux-integrity/[email protected]
=> Refactored the patch set to separate out changes related to
func KEYRING_CHECK and options keyrings into different patches.
=> Moved the functions to queue and dequeue keys for measurement
from ima_queue.c to a new file ima_asymmetric_keys.c.
=> Added a new config namely CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
to compile ima_asymmetric_keys.c

v3:

=> Added KEYRING_CHECK for measuring keys. This can optionally specify
keyrings to measure.
=> Updated ima_get_action() and related functions to return
the keyrings if specified in the policy.
=> process_buffer_measurement() function is updated to take keyring
as a parameter. The key will be measured if the policy includes the keyring in the list of measured keyrings. If the policy does not specify any keyrings then all keys are measured.

v2:

=> Per suggestion from Mimi reordered the patch set to first
enable measuring keys added or updated in the system. And, then scope the measurement to keys added to builtin_trusted_keys keyring through ima policy.
=> Removed security_key_create_or_update function and instead
call ima hook, to measure the key, directly from key_create_or_update function.

v1:

=> LSM function for key_create_or_update. It calls ima. => Added ima hook for measuring keys
=> ima measures keys based on ima policy.

v0:

=> Added LSM hook for key_create_or_update.
=> Measure keys added to builtin or secondary trusted keys keyring.

Lakshmi Ramasubramanian (3):
IMA: Add KEY_CHECK func to measure keys
IMA: Define an IMA hook to measure keys
KEYS: Call the IMA hook to measure keys

 Documentation/ABI/testing/ima_policy         |  6 ++-
 include/linux/ima.h                          | 13 +++++
 security/integrity/ima/Kconfig               | 14 ++++++
 security/integrity/ima/Makefile              |  1 +
 security/integrity/ima/ima.h                 |  1 +
 security/integrity/ima/ima_asymmetric_keys.c | 51 ++++++++++++++++++++
 security/integrity/ima/ima_main.c            |  7 +++
 security/integrity/ima/ima_policy.c          |  4 +-
 security/keys/key.c                          |  9 ++++
 9 files changed, 104 insertions(+), 2 deletions(-)
 create mode 100644 security/integrity/ima/ima_asymmetric_keys.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v6 1/3 IMA: Add KEY_CHECK func to measure keys

Measure keys loaded onto any keyring.

This patch defines a new IMA policy func namely KEY_CHECK to measure keys. Updated ima_match_rules() to check for KEY_CHECK and ima_parse_rule() to handle KEY_CHECK.

---
 Documentation/ABI/testing/ima_policy | 6 +++++-
 security/integrity/ima/ima.h         | 1 +
 security/integrity/ima/ima_main.c    | 7 +++++++
 security/integrity/ima/ima_policy.c  | 4 +++-
 4 files changed, 16 insertions(+), 2 deletions(-)
Documentation/ABI/testing/ima_policy +5 -1
index 29aaedf33246..066d32797500 100644
@@ -29,7 +29,7 @@ Description:
 		base: 	func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK]
 				[FIRMWARE_CHECK]
 				[KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK]
-				[KEXEC_CMDLINE]
+				[KEXEC_CMDLINE] [KEY_CHECK]
 			mask:= [[^]MAY_READ] [[^]MAY_WRITE] [[^]MAY_APPEND]
 			       [[^]MAY_EXEC]
 			fsmagic:= hex value
@@ -113,3 +113,7 @@ Description:
 		Example of appraise rule allowing modsig appended signatures:
 
 			appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig|modsig
+
+		Example of measure rule using KEY_CHECK to measure all keys:
+
+			measure func=KEY_CHECK
security/integrity/ima/ima.h +1 -0
index df4ca482fb53..fe6c698617bd 100644
@@ -193,6 +193,7 @@ static inline unsigned long ima_hash_key(u8 *digest)
 	hook(KEXEC_INITRAMFS_CHECK)	\
 	hook(POLICY_CHECK)		\
 	hook(KEXEC_CMDLINE)		\
+	hook(KEY_CHECK)			\
 	hook(MAX_CHECK)
 #define __ima_hook_enumify(ENUM)	ENUM,
 
security/integrity/ima/ima_main.c +7 -0
index d7e987baf127..12684e8d7124 100644
@@ -655,6 +655,13 @@ void process_buffer_measurement(const void *buf, int size,
 	int action = 0;
 	u32 secid;
 
+	/*
+	 * If IMA is not yet initialized or IMA policy is empty
+	 * then there is no need to measure.
+	 */
+	if (!ima_policy_flag)
+		return;
+
 	/*
 	 * Both LSM hooks and auxilary based buffer measurements are
 	 * based on policy.  To avoid code duplication, differentiate
security/integrity/ima/ima_policy.c +3 -1
index f19a895ad7cd..1525a28fd705 100644
@@ -373,7 +373,7 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 {
 	int i;
 
-	if (func = KEXEC_CMDLINE) {
+	if ((func = KEXEC_CMDLINE) || (func = KEY_CHECK)) {
 		if ((rule->flags & IMA_FUNC) && (rule->func = func))
 			return true;
 		return false;
@@ -997,6 +997,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->func = POLICY_CHECK;
 			else if (strcmp(args[0].from, "KEXEC_CMDLINE") = 0)
 				entry->func = KEXEC_CMDLINE;
+			else if (strcmp(args[0].from, "KEY_CHECK") = 0)
+				entry->func = KEY_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH v6 1/3] IMA: Add KEY_CHECK func to measure keys

Quoted 52 lines · Lakshmi Ramasubramanian

Measure keys loaded onto any keyring.

This patch defines a new IMA policy func namely KEY_CHECK to measure keys. Updated ima_match_rules() to check for KEY_CHECK and ima_parse_rule() to handle KEY_CHECK.

---
 Documentation/ABI/testing/ima_policy | 6 +++++-
 security/integrity/ima/ima.h         | 1 +
 security/integrity/ima/ima_main.c    | 7 +++++++
 security/integrity/ima/ima_policy.c  | 4 +++-
 4 files changed, 16 insertions(+), 2 deletions(-)
Documentation/ABI/testing/ima_policy +5 -1
index 29aaedf33246..066d32797500 100644
@@ -29,7 +29,7 @@ Description:
 		base: 	func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK]
 				[FIRMWARE_CHECK]
 				[KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK]
-				[KEXEC_CMDLINE]
+				[KEXEC_CMDLINE] [KEY_CHECK]
 			mask:= [[^]MAY_READ] [[^]MAY_WRITE] [[^]MAY_APPEND]
 			       [[^]MAY_EXEC]
 			fsmagic:= hex value
@@ -113,3 +113,7 @@ Description:
 		Example of appraise rule allowing modsig appended signatures:
 
 			appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig|modsig
+
+		Example of measure rule using KEY_CHECK to measure all keys:
+
+			measure func=KEY_CHECK
security/integrity/ima/ima.h +1 -0
index df4ca482fb53..fe6c698617bd 100644
@@ -193,6 +193,7 @@ static inline unsigned long ima_hash_key(u8 *digest)
 	hook(KEXEC_INITRAMFS_CHECK)	\
 	hook(POLICY_CHECK)		\
 	hook(KEXEC_CMDLINE)		\
+	hook(KEY_CHECK)			\
 	hook(MAX_CHECK)
 #define __ima_hook_enumify(ENUM)	ENUM,
 
security/integrity/ima/ima_main.c +0 -0
index d7e987baf127..12684e8d7124 100644

security/integrity/ima/ima_main.c void process_buffer_measurement(const void *buf, int size, · line 664

Quoted 10 lines
security/integrity/ima/ima_main.c +7 -0
@@ -655,6 +655,13 @@ void process_buffer_measurement(const void *buf, int size,
 	int action = 0;
 	u32 secid;
 
+	/*
+	 * If IMA is not yet initialized or IMA policy is empty
+	 * then there is no need to measure.
+	 */
+	if (!ima_policy_flag)
+		return;
+

This addition has nothing to do with defining a new IMA hook and should be a separate patch.  This can be posted independently of this patch set.

Mimi

Quoted 25 lines
security/integrity/ima/ima_main.c +0 -0
 	/*
 	 * Both LSM hooks and auxilary based buffer measurements are
 	 * based on policy.  To avoid code duplication, differentiate
security/integrity/ima/ima_policy.c +3 -1
index f19a895ad7cd..1525a28fd705 100644
@@ -373,7 +373,7 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 {
 	int i;
 
-	if (func = KEXEC_CMDLINE) {
+	if ((func = KEXEC_CMDLINE) || (func = KEY_CHECK)) {
 		if ((rule->flags & IMA_FUNC) && (rule->func = func))
 			return true;
 		return false;
@@ -997,6 +997,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->func = POLICY_CHECK;
 			else if (strcmp(args[0].from, "KEXEC_CMDLINE") = 0)
 				entry->func = KEXEC_CMDLINE;
+			else if (strcmp(args[0].from, "KEY_CHECK") = 0)
+				entry->func = KEY_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
#3 lore raw

Re: [PATCH v6 1/3] IMA: Add KEY_CHECK func to measure keys

Quoted 16 lines · Mimi Zohar

security/integrity/ima/ima_main.c void process_buffer_measurement(const void *buf, int size, · line 664

Quoted 11 lines
security/integrity/ima/ima_main.c +7 -0
@@ -655,6 +655,13 @@ void process_buffer_measurement(const void *buf, int size,
  	int action = 0;
  	u32 secid;
 
+	/*
+	 * If IMA is not yet initialized or IMA policy is empty
+	 * then there is no need to measure.
+	 */
+	if (!ima_policy_flag)
+		return;
+

This addition has nothing to do with defining a new IMA hook and should be a separate patch.  This can be posted independently of this patch set.

Quoted 1 line

Mimi

I'll move this change to a different patch,
but it has to be either part of this patch set or the above change alone needs to be taken before this patch set for the following reason:

The IMA hook gets called early in the boot process (for example, when builtin_trusted_keys are added). If the above check is not there, ima_get_action() is called and causes kernel panic (since IMA is not yet initialized).

thanks,
-lakshmi

maintainer
#4 lore raw

Re: [PATCH v6 1/3] IMA: Add KEY_CHECK func to measure keys

Quoted 20 lines · Lakshmi Ramasubramanian
Quoted 16 lines · Mimi Zohar

security/integrity/ima/ima_main.c void process_buffer_measurement(const void *buf, int size, · line 664

Quoted 11 lines
security/integrity/ima/ima_main.c +7 -0
@@ -655,6 +655,13 @@ void process_buffer_measurement(const void *buf, int size,
  	int action = 0;
  	u32 secid;
 
+	/*
+	 * If IMA is not yet initialized or IMA policy is empty
+	 * then there is no need to measure.
+	 */
+	if (!ima_policy_flag)
+		return;
+

This addition has nothing to do with defining a new IMA hook and should be a separate patch.  This can be posted independently of this patch set.

Quoted 1 line

Mimi

Quoted 8 lines

I'll move this change to a different patch,
but it has to be either part of this patch set or the above change alone needs to be taken before this patch set for the following reason:

The IMA hook gets called early in the boot process (for example, when builtin_trusted_keys are added). If the above check is not there, ima_get_action() is called and causes kernel panic (since IMA is not yet initialized).

It will be upstreamed prior to this patch set.

Mimi

#5 lore raw

PATCH v6 2/3 IMA: Define an IMA hook to measure keys

In reply to Lakshmi Ramasubramanian

Measure asymmetric keys used for verifying file signatures, certificates, etc.

This patch defines a new IMA hook namely ima_post_key_create_or_update() to measure asymmetric keys.

Note that currently IMA subsystem can be enabled without enabling KEYS subsystem.

Adding support for measuring asymmetric keys in IMA requires KEYS subsystem to be enabled. To handle this dependency a new config namely CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS has been added. Enabling this config requires the following configs to be enabled:
CONFIG_IMA, CONFIG_KEYS, CONFIG_ASYMMETRIC_KEY_TYPE, and CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE.

CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS is off by default.

The IMA hook is defined in a new file namely ima_asymmetric_keys.c which is built only if CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS is enabled.

---
 security/integrity/ima/Kconfig               | 14 ++++++
 security/integrity/ima/Makefile              |  1 +
 security/integrity/ima/ima_asymmetric_keys.c | 51 ++++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 security/integrity/ima/ima_asymmetric_keys.c
security/integrity/ima/Kconfig +14 -0
index 838476d780e5..c6d14884bc19 100644
@@ -310,3 +310,17 @@ config IMA_APPRAISE_SIGNED_INIT
 	default n
 	help
 	   This option requires user-space init to be signed.
+
+config IMA_MEASURE_ASYMMETRIC_KEYS
+	bool "Enable measuring asymmetric keys on key create or update"
+	depends on IMA
+	depends on KEYS
+	depends on ASYMMETRIC_KEY_TYPE
+	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	default n
+	help
+	   This option enables measuring asymmetric keys when
+	   the key is created or updated. Additionally, IMA policy
+	   needs to be configured to either measure keys linked to
+	   any keyring or only measure keys linked to the keyrings
+	   specified in the IMA policy through the keyrings= option.
security/integrity/ima/Makefile +1 -0
index 31d57cdf2421..3e9d0ad68c7b 100644
@@ -12,3 +12,4 @@ ima-$(CONFIG_IMA_APPRAISE) += ima_appraise.o
 ima-$(CONFIG_IMA_APPRAISE_MODSIG) += ima_modsig.o
 ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o
 obj-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o
+obj-$(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) += ima_asymmetric_keys.o
security/integrity/ima/ima_asymmetric_keys.c new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#6 lore raw

Re: [PATCH v6 2/3] IMA: Define an IMA hook to measure keys

Quoted 16 lines · Lakshmi Ramasubramanian

Measure asymmetric keys used for verifying file signatures, certificates, etc.

This patch defines a new IMA hook namely ima_post_key_create_or_update() to measure asymmetric keys.

Note that currently IMA subsystem can be enabled without enabling KEYS subsystem.

Adding support for measuring asymmetric keys in IMA requires KEYS subsystem to be enabled. To handle this dependency a new config namely CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS has been added. Enabling this config requires the following configs to be enabled:
CONFIG_IMA, CONFIG_KEYS, CONFIG_ASYMMETRIC_KEY_TYPE, and CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE.

Quoted 6 lines

CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS is off by default.

The IMA hook is defined in a new file namely ima_asymmetric_keys.c which is built only if CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS is enabled.

All that is is needed is the key and public_key structures, which are defined in include/linux/keys.h and include/crypto/public_key.h.  If the keys subsystem is disabled, then the new IMA hook won't be called. There's no need for a new Kconfig option or a new file.

Please move the hook to just after ima_kexec_cmdline().

Mimi

Quoted 95 lines
---
 security/integrity/ima/Kconfig               | 14 ++++++
 security/integrity/ima/Makefile              |  1 +
 security/integrity/ima/ima_asymmetric_keys.c | 51 ++++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 security/integrity/ima/ima_asymmetric_keys.c
security/integrity/ima/Kconfig +14 -0
index 838476d780e5..c6d14884bc19 100644
@@ -310,3 +310,17 @@ config IMA_APPRAISE_SIGNED_INIT
 	default n
 	help
 	   This option requires user-space init to be signed.
+
+config IMA_MEASURE_ASYMMETRIC_KEYS
+	bool "Enable measuring asymmetric keys on key create or update"
+	depends on IMA
+	depends on KEYS
+	depends on ASYMMETRIC_KEY_TYPE
+	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	default n
+	help
+	   This option enables measuring asymmetric keys when
+	   the key is created or updated. Additionally, IMA policy
+	   needs to be configured to either measure keys linked to
+	   any keyring or only measure keys linked to the keyrings
+	   specified in the IMA policy through the keyrings= option.
security/integrity/ima/Makefile +1 -0
index 31d57cdf2421..3e9d0ad68c7b 100644
@@ -12,3 +12,4 @@ ima-$(CONFIG_IMA_APPRAISE) += ima_appraise.o
 ima-$(CONFIG_IMA_APPRAISE_MODSIG) += ima_modsig.o
 ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o
 obj-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o
+obj-$(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) += ima_asymmetric_keys.o
security/integrity/ima/ima_asymmetric_keys.c new file +51 -0
new file mode 100644
index 000000000000..f6884641a622
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019 Microsoft Corporation
+ *
+ * Author: Lakshmi Ramasubramanian ([email protected])
+ *
+ * File: ima_asymmetric_keys.c
+ *       Defines an IMA hook to measure asymmetric keys on key
+ *       create or update.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <crypto/public_key.h>
+#include <keys/asymmetric-type.h>
+#include "ima.h"
+
+/**
+ * ima_post_key_create_or_update - measure asymmetric keys
+ * @keyring: keyring to which the key is linked to
+ * @key: created or updated key
+ * @flags: key flags
+ * @create: flag indicating whether the key was created or updated
+ *
+ * Keys can only be measured, not appraised.
+ */
+void ima_post_key_create_or_update(struct key *keyring, struct key *key,
+				   unsigned long flags, bool create)
+{
+	const struct public_key *pk;
+
+	/* Only asymmetric keys are handled by this hook. */
+	if (key->type != &key_type_asymmetric)
+		return;
+
+	/* Get the public_key of the given asymmetric key to measure. */
+	pk = key->payload.data[asym_crypto];
+
+	/*
+	 * keyring->description points to the name of the keyring
+	 * (such as ".builtin_trusted_keys", ".ima", etc.) to
+	 * which the given key is linked to.
+	 *
+	 * The name of the keyring is passed in the "eventname"
+	 * parameter to process_buffer_measurement() and is set
+	 * in the "eventname" field in ima_event_data for
+	 * the key measurement IMA event.
+	 */
+	process_buffer_measurement(pk->key, pk->keylen,
+				   keyring->description, KEY_CHECK, 0);
+}
#7 lore raw

Re: [PATCH v6 2/3] IMA: Define an IMA hook to measure keys

Quoted 9 lines · Mimi Zohar

All that is is needed is the key and public_key structures, which are defined in include/linux/keys.h and include/crypto/public_key.h.  If the keys subsystem is disabled, then the new IMA hook won't be called.
 There's no need for a new Kconfig option or a new file.

Please move the hook to just after ima_kexec_cmdline().

Mimi

Yes - IMA hook won't be called when KEYS subsystem is disabled.

But, build dependency is breaking since "struct key" is not defined without CONFIG_KEYS.

Sasha was able to craft a .config that enabled IMA without enabling KEYS and found the build break.

Please see the build output he'd shared.

***********************************************************************

In file included from security/integrity/ima/ima.h:25,
from security/integrity/ima/ima_fs.c:26:
./include/keys/asymmetric-type.h: In function ‘asymmetric_key_ids’:
./include/keys/asymmetric-type.h:72:12: error: dereferencing pointer to incomplete type ‘const struct key’

  return key->payload.data[asym_key_ids];
            ^~

make[3]: *** [scripts/Makefile.build:266:
security/integrity/ima/ima_fs.o] Error 1
make[3]: *** Waiting for unfinished jobs....
In file included from security/integrity/ima/ima.h:25,
from security/integrity/ima/ima_queue.c:22:
./include/keys/asymmetric-type.h: In function ‘asymmetric_key_ids’:
./include/keys/asymmetric-type.h:72:12: error: dereferencing pointer to incomplete type ‘const struct key’
return key->payload.data[asym_key_ids];

***********************************************************************

thanks,
-lakshmi

maintainer
#8 lore raw

Re: [PATCH v6 2/3] IMA: Define an IMA hook to measure keys

Quoted 11 lines · Lakshmi Ramasubramanian
Quoted 9 lines · Mimi Zohar

All that is is needed is the key and public_key structures, which are defined in include/linux/keys.h and include/crypto/public_key.h.  If the keys subsystem is disabled, then the new IMA hook won't be called.
 There's no need for a new Kconfig option or a new file.

Please move the hook to just after ima_kexec_cmdline().

Mimi

Quoted 7 lines

Yes - IMA hook won't be called when KEYS subsystem is disabled.

But, build dependency is breaking since "struct key" is not defined without CONFIG_KEYS.

Sasha was able to craft a .config that enabled IMA without enabling KEYS and found the build break.

Yes, thanks for pointing out the "#ifdef CONFIG_KEYS" in keys.h.  A separate file is needed, as you pointed out, but still no need for a new Kconfig.  The ima/Makefile can be based on CONFIG_KEYS.

Mimi

#9 lore raw

Re: [PATCH v6 2/3] IMA: Define an IMA hook to measure keys

Quoted 5 lines · Mimi Zohar

Yes, thanks for pointing out the "#ifdef CONFIG_KEYS" in keys.h.  A separate file is needed, as you pointed out, but still no need for a new Kconfig.  The ima/Makefile can be based on CONFIG_KEYS.

Mimi

Sure - i'll try it now and send an updated patch set.

thanks,
-lakshmi

#10 lore raw

PATCH v6 3/3 KEYS: Call the IMA hook to measure keys

In reply to Lakshmi Ramasubramanian

Call the IMA hook from key_create_or_update function to measure the key when a new key is created or an existing key is updated.

This patch adds the call to the IMA hook from key_create_or_update function to measure the key on key create or update.

---
 include/linux/ima.h | 13 +++++++++++++
 security/keys/key.c |  9 +++++++++
 2 files changed, 22 insertions(+)
include/linux/ima.h +13 -0
index 6d904754d858..ec5afe319ab7 100644
@@ -25,6 +25,12 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
 extern void ima_post_path_mknod(struct dentry *dentry);
 extern void ima_kexec_cmdline(const void *buf, int size);
 
+#ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
+extern void ima_post_key_create_or_update(struct key *keyring,
+					  struct key *key,
+					  unsigned long flags, bool create);
+#endif
+
 #ifdef CONFIG_IMA_KEXEC
 extern void ima_add_kexec_buffer(struct kimage *image);
 #endif
@@ -101,6 +107,13 @@ static inline void ima_add_kexec_buffer(struct kimage *image)
 {}
 #endif
 
+#ifndef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
+static inline void ima_post_key_create_or_update(struct key *keyring,
+						 struct key *key,
+						 unsigned long flags,
+						 bool create) {}
+#endif
+
 #ifdef CONFIG_IMA_APPRAISE
 extern bool is_ima_appraise_enabled(void);
 extern void ima_inode_post_setattr(struct dentry *dentry);
security/keys/key.c +9 -0
index 764f4c57913e..9782d4d046fd 100644
@@ -13,6 +13,7 @@
 #include <linux/security.h>
 #include <linux/workqueue.h>
 #include <linux/random.h>
+#include <linux/ima.h>
 #include <linux/err.h>
 #include "internal.h"
 
@@ -936,6 +937,9 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
 		goto error_link_end;
 	}
 
+	/* let the ima module know about the created key. */
+	ima_post_key_create_or_update(keyring, key, flags, true);
+
 	key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
 
 error_link_end:
@@ -965,6 +969,11 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
 	}
 
 	key_ref = __key_update(key_ref, &prep);
+
+	/* let the ima module know about the updated key. */
+	if (!IS_ERR(key_ref))
+		ima_post_key_create_or_update(keyring, key, flags, false);
+
 	goto error_free_prep;
 }
 EXPORT_SYMBOL(key_create_or_update);
Signature · 1 line
2.17.1
maintainer
#11 lore raw

Re: [PATCH v6 3/3] KEYS: Call the IMA hook to measure keys

Quoted 56 lines · Lakshmi Ramasubramanian

Call the IMA hook from key_create_or_update function to measure the key when a new key is created or an existing key is updated.

This patch adds the call to the IMA hook from key_create_or_update function to measure the key on key create or update.

---
 include/linux/ima.h | 13 +++++++++++++
 security/keys/key.c |  9 +++++++++
 2 files changed, 22 insertions(+)
include/linux/ima.h +13 -0
index 6d904754d858..ec5afe319ab7 100644
@@ -25,6 +25,12 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
 extern void ima_post_path_mknod(struct dentry *dentry);
 extern void ima_kexec_cmdline(const void *buf, int size);
 
+#ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
+extern void ima_post_key_create_or_update(struct key *keyring,
+					  struct key *key,
+					  unsigned long flags, bool create);
+#endif
+
 #ifdef CONFIG_IMA_KEXEC
 extern void ima_add_kexec_buffer(struct kimage *image);
 #endif
@@ -101,6 +107,13 @@ static inline void ima_add_kexec_buffer(struct kimage *image)
 {}
 #endif
 
+#ifndef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
+static inline void ima_post_key_create_or_update(struct key *keyring,
+						 struct key *key,
+						 unsigned long flags,
+						 bool create) {}
+#endif
+
 #ifdef CONFIG_IMA_APPRAISE
 extern bool is_ima_appraise_enabled(void);
 extern void ima_inode_post_setattr(struct dentry *dentry);
security/keys/key.c +1 -0
index 764f4c57913e..9782d4d046fd 100644
@@ -13,6 +13,7 @@
 #include <linux/security.h>
 #include <linux/workqueue.h>
 #include <linux/random.h>
+#include <linux/ima.h>
 #include <linux/err.h>
 #include "internal.h"
 

security/keys/key.c key_ref_t key_create_or_update(key_ref_t keyring_ref, · line 940

Quoted 4 lines
security/keys/key.c +1 -0
@@ -936,6 +937,9 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
 		goto error_link_end;
 	}
 
+	/* let the ima module know about the created key. */

This comment and the one below doesn't provide any additional information.  Please remove them.

Quoted 17 lines
security/keys/key.c +7 -0
+	ima_post_key_create_or_update(keyring, key, flags, true);
+
 	key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
 
 error_link_end:
@@ -965,6 +969,11 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
 	}
 
 	key_ref = __key_update(key_ref, &prep);
+
+	/* let the ima module know about the updated key. */
+	if (!IS_ERR(key_ref))
+		ima_post_key_create_or_update(keyring, key, flags, false);
+
 	goto error_free_prep;
 }
 EXPORT_SYMBOL(key_create_or_update);
maintainer
#12 lore raw
In reply to Lakshmi Ramasubramanian
Quoted 15 lines · Lakshmi Ramasubramanian

Keys created or updated in the system are currently not measured. Therefore an attestation service, for instance, would not be able to attest whether or not the trusted keys keyring(s), for instance, contain only known good (trusted) keys.

IMA measures system files, command line arguments passed to kexec, boot aggregate, etc. It can be used to measure keys as well. But there is no mechanism available in the kernel for IMA to know when a key is created or updated.

This change aims to address measuring keys created or updated in the system:

To achieve the above the following changes have been made:

Quoted 8 lines

- Added a new IMA hook namely, ima_post_key_create_or_update, which measures the key. This IMA hook is called from key_create_or_update function. The key measurement can be controlled through IMA policy.

A new IMA policy function KEY_CHECK has been added to measure keys.

   # measure keys loaded onto any keyring
   measure func=KEY_CHECK

When re-posting this patch set, please include the support for specifying the "keyrings=" policy option, as an additional patch.

Mimi

#13 lore raw
Quoted 5 lines · Mimi Zohar

When re-posting this patch set, please include the support for specifying the "keyrings=" policy option, as an additional patch.

Mimi

Sure - will do.

thanks,
-lakshmi