Skip to contentSkip to conversation

s390/cert_store: Restrict link creation and key type creation

PATCH In review Heiko Carstens 3 participants 9 messages Nov 23

Subsystems (2)
lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

cover s390/cert_store: Restrict link creation and key type creation

Two more patches from Anastasia, which apply on top of linux-next 20261120, which includes all the previous cert_store fixes:

Patch 1 allocates the keyring with a restriction which rejects all links, so a possessor of the keyring can no longer link arbitrary keys into it before the certificates have been added.

Patch 2 only allows the kernel to create cert_store_key keys. Fixes potential disclosure of adjacent kernel heap memory to user space.

Thanks,
Heiko

Anastasia Eskova (2):
s390/cert_store: Allocate keyring with link restriction s390/cert_store: Only allow the kernel to create cert_store_key keys

 arch/s390/kernel/cert_store.c | 38 ++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 10 deletions(-)

base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/2 s390/cert_store: Allocate keyring with link restriction

From: Anastasia Eskova <[email protected]>

The keyring is linked into the user session keyring on allocation, but keyring_restrict() is only called after all certificates were added. Until then a possessor of the keyring can link arbitrary keys into it. If keyring_restrict() fails, the error is only logged and the keyring stays writable.

Allocate the keyring with a restriction which rejects all links, create the certificate keys with KEY_ALLOC_BYPASS_RESTRICTION and drop the late keyring_restrict() call.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
arch/s390/kernel/cert_store.c +11 -10
index e3cc9389a50a..6f5c63cd907f 100644
@@ -409,6 +409,7 @@ static void cleanup_cs_keys(void)
 
 static struct key *create_cs_keyring(void)
 {
+	struct key_restriction *restriction;
 	struct key *cs_keyring, *us_keyring;
 
 	/* Cleanup previous cs_keyring and all associated keys if any. */
@@ -416,13 +417,21 @@ static struct key *create_cs_keyring(void)
 	us_keyring = get_user_session_keyring();
 	if (IS_ERR(us_keyring))
 		return NULL;
+	restriction = kzalloc_obj(*restriction);
+	if (!restriction) {
+		key_put(us_keyring);
+		return NULL;
+	}
+	/* Reject all links, the kernel bypasses the restriction. */
+	restriction->check = restrict_link_reject;
 	cs_keyring = keyring_alloc(CERT_STORE_KEYRING_NAME, GLOBAL_ROOT_UID,
 				   GLOBAL_ROOT_GID, current_cred(),
 				   (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ,
 				   KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_SET_KEEP,
-				   NULL, us_keyring);
+				   restriction, us_keyring);
 	key_put(us_keyring);
 	if (IS_ERR(cs_keyring)) {
+		kfree(restriction);
 		pr_dbf_msg("Can't allocate cert_store keyring");
 		return NULL;
 	}
@@ -485,7 +494,7 @@ static int create_key_from_vce(struct vcssb *vcssb, struct vce *vce,
 		desc, (u8 *)vce + vce->vce_hdr.vc_offset,
 		vce->vce_hdr.vc_length,
 		(KEY_POS_ALL & ~KEY_POS_SETATTR)  | KEY_USR_VIEW | KEY_USR_READ,
-		KEY_ALLOC_NOT_IN_QUOTA);
+		KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BYPASS_RESTRICTION);
 
 	rc = PTR_ERR_OR_ZERO(newkey);
 	if (rc) {
@@ -653,14 +662,6 @@ static int add_certificates_to_keyring(struct vcssb *vcssb, struct key *keyring)
 
 	pr_info("Added %d of %d keys to cert_store keyring", added, count);
 
-	/*
-	 * Do not allow to link more keys to certificate store keyring after all
-	 * the VCEs were processed.
-	 */
-	rc = keyring_restrict(make_key_ref(keyring, true), NULL, NULL);
-	if (rc)
-		pr_dbf_msg("Failed to set restriction to cert_store keyring (%d)", rc);
-
 	return 0;
 }
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] s390/cert_store: Allocate keyring with link restriction

Quoted 8 lines · Heiko Carstens

From: Anastasia Eskova <[email protected]>

The keyring is linked into the user session keyring on allocation, but keyring_restrict() is only called after all certificates were added. Until then a possessor of the keyring can link arbitrary keys into it. If keyring_restrict() fails, the error is only logged and the keyring stays writable.

Quoted 7 lines

Allocate the keyring with a restriction which rejects all links, create the certificate keys with KEY_ALLOC_BYPASS_RESTRICTION and drop the late keyring_restrict() call.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")

I think it is best for overall good that earliest possible release for my intersecting changes is v4.7. I think that it is good to do that architectural change but since it is a problem solved there's no need to rush it in.

Br, Jarkko

author maintainer
#3 lore raw

Re: [PATCH 1/2] s390/cert_store: Allocate keyring with link restriction

Quoted 13 lines · Jarkko Sakkinen
Quoted 12 lines · Heiko Carstens

From: Anastasia Eskova <[email protected]>

The keyring is linked into the user session keyring on allocation, but keyring_restrict() is only called after all certificates were added. Until then a possessor of the keyring can link arbitrary keys into it. If keyring_restrict() fails, the error is only logged and the keyring stays writable.

Allocate the keyring with a restriction which rejects all links, create the certificate keys with KEY_ALLOC_BYPASS_RESTRICTION and drop the late keyring_restrict() call.

Quoted 6 lines
Quoted 3 lines
Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")

I think it is best for overall good that earliest possible release for my intersecting changes is v4.7. I think that it is good to

I guess you meant to write 7.4?

Quoted 2 lines

do that architectural change but since it is a problem solved there's no need to rush it in.

All the fixes are planned to hit upstream before 7.4 is released.

Quoted 1 line

Thanks a lot!

maintainer
#4 lore raw

Re: [PATCH 1/2] s390/cert_store: Allocate keyring with link restriction

Quoted 14 lines · Heiko Carstens
Quoted 13 lines · Jarkko Sakkinen
Quoted 12 lines · Heiko Carstens

From: Anastasia Eskova <[email protected]>

The keyring is linked into the user session keyring on allocation, but keyring_restrict() is only called after all certificates were added. Until then a possessor of the keyring can link arbitrary keys into it. If keyring_restrict() fails, the error is only logged and the keyring stays writable.

Allocate the keyring with a restriction which rejects all links, create the certificate keys with KEY_ALLOC_BYPASS_RESTRICTION and drop the late keyring_restrict() call.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")

I think it is best for overall good that earliest possible release for my intersecting changes is v4.7. I think that it is good to

I guess you meant to write 7.4?

Yes, 7.4 :-)

Quoted 9 lines
Quoted 3 lines

do that architectural change but since it is a problem solved there's no need to rush it in.

All the fixes are planned to hit upstream before 7.4 is released.

Quoted 1 line

Thanks a lot!

NP :-)

Br, Jarkko

maintainer
#5 lore raw

Re: [PATCH 1/2] s390/cert_store: Allocate keyring with link restriction

Quoted 19 lines · Jarkko Sakkinen
Quoted 18 lines · Heiko Carstens
Quoted 17 lines · Jarkko Sakkinen
Quoted 15 lines · Heiko Carstens

From: Anastasia Eskova <[email protected]>

The keyring is linked into the user session keyring on allocation, but keyring_restrict() is only called after all certificates were added. Until then a possessor of the keyring can link arbitrary keys into it. If keyring_restrict() fails, the error is only logged and the keyring stays writable.

Allocate the keyring with a restriction which rejects all links, create the certificate keys with KEY_ALLOC_BYPASS_RESTRICTION and drop the late keyring_restrict() call.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I think it is best for overall good that earliest possible release for my intersecting changes is v4.7. I think that it is good to

I guess you meant to write 7.4?

Yes, 7.4 :-)

I'll open code this fully.

My plan for keyrings is:

1. v7.5: assoc_array to rhashtable migration.
2. v7.6: make keys to bind key type's LKM.

This went all wrong but anyway. Both are architectural overturns so I think it is better to stage them like this.

Br, Jarkko

author maintainer
#6 lore raw

PATCH 2/2 s390/cert_store: Only allow the kernel to create cert_store_key keys

In reply to Heiko Carstens

From: Anastasia Eskova <[email protected]>

Keys of type cert_store_key can be created by any user with add_key() in any keyring, as the key type has no vet_description() callback. The type is registered whenever CONFIG_CERT_STORE is enabled, even if the machine has no DIAG 320 support.

cert_store_key_describe() expects a description in the format created by get_key_description(), i.e. a 64 byte key name followed by the VCE index and the certificate store token. If a key with a shorter description is created, seq_puts() may read beyond the end of the description allocation. Reading /proc/keys may then disclose adjacent kernel heap memory to the user space. Also, a consumer which trusts keys by type can be fooled by keys that were not created from the certificate store.

Add a vet_description() callback which only accepts the task currently executing refresh_store(), which creates all valid keys of this type.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/s390/kernel/cert_store.c +17 -0
index 6f5c63cd907f..6e518ff38322 100644
@@ -46,6 +46,9 @@
 static debug_info_t *cert_store_dbf;
 static debug_info_t *cert_store_hexdump;
 
+/* Task currently running refresh_store(). Written under cs_refresh_lock. */
+static struct task_struct *cs_refresh_task;
+
 #define pr_dbf_msg(fmt, ...) \
 	debug_sprintf_event(cert_store_dbf, 3, fmt "\n", ## __VA_ARGS__)
 
@@ -148,12 +151,24 @@ static void cert_store_key_describe(const struct key *key, struct seq_file *m)
 		seq_printf(m, ": %u", key->datalen);
 }
 
+/*
+ * Only the kernel may create keys of this type. Accept the task which runs
+ * refresh_store(), as it cannot execute userspace during the refresh.
+ */
+static int cert_store_key_vet_description(const char *description)
+{
+	if (READ_ONCE(cs_refresh_task) != current)
+		return -EPERM;
+	return 0;
+}
+
 /*
  * Certificate store key type takes over properties of
  * user key but cannot be updated.
  */
 static struct key_type key_type_cert_store_key = {
 	.name		= CERT_STORE_KEY_TYPE_NAME,
+	.vet_description = cert_store_key_vet_description,
 	.preparse	= user_preparse,
 	.free_preparse	= user_free_preparse,
 	.instantiate	= generic_key_instantiate,
@@ -768,6 +783,7 @@ static ssize_t refresh_store(struct kobject *kobj, struct kobj_attribute *attr,
 	if (rc)
 		return rc;
 
+	WRITE_ONCE(cs_refresh_task, current);
 	for (retries = 0; retries < DIAG_MAX_RETRIES; retries++) {
 		/* Request certificates from certificate store. */
 		rc = fill_cs_keyring();
@@ -776,6 +792,7 @@ static ssize_t refresh_store(struct kobject *kobj, struct kobj_attribute *attr,
 		if (rc != -EAGAIN)
 			break;
 	}
+	WRITE_ONCE(cs_refresh_task, NULL);
 	cs_status_val = rc;
 	mutex_unlock(&cs_refresh_lock);
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] s390/cert_store: Only allow the kernel to create cert_store_key keys

Quoted 83 lines · Heiko Carstens

From: Anastasia Eskova <[email protected]>

Keys of type cert_store_key can be created by any user with add_key() in any keyring, as the key type has no vet_description() callback. The type is registered whenever CONFIG_CERT_STORE is enabled, even if the machine has no DIAG 320 support.

cert_store_key_describe() expects a description in the format created by get_key_description(), i.e. a 64 byte key name followed by the VCE index and the certificate store token. If a key with a shorter description is created, seq_puts() may read beyond the end of the description allocation. Reading /proc/keys may then disclose adjacent kernel heap memory to the user space. Also, a consumer which trusts keys by type can be fooled by keys that were not created from the certificate store.

Add a vet_description() callback which only accepts the task currently executing refresh_store(), which creates all valid keys of this type.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/s390/kernel/cert_store.c +17 -0
index 6f5c63cd907f..6e518ff38322 100644
@@ -46,6 +46,9 @@
 static debug_info_t *cert_store_dbf;
 static debug_info_t *cert_store_hexdump;
 
+/* Task currently running refresh_store(). Written under cs_refresh_lock. */
+static struct task_struct *cs_refresh_task;
+
 #define pr_dbf_msg(fmt, ...) \
 	debug_sprintf_event(cert_store_dbf, 3, fmt "\n", ## __VA_ARGS__)
 
@@ -148,12 +151,24 @@ static void cert_store_key_describe(const struct key *key, struct seq_file *m)
 		seq_printf(m, ": %u", key->datalen);
 }
 
+/*
+ * Only the kernel may create keys of this type. Accept the task which runs
+ * refresh_store(), as it cannot execute userspace during the refresh.
+ */
+static int cert_store_key_vet_description(const char *description)
+{
+	if (READ_ONCE(cs_refresh_task) != current)
+		return -EPERM;
+	return 0;
+}
+
 /*
  * Certificate store key type takes over properties of
  * user key but cannot be updated.
  */
 static struct key_type key_type_cert_store_key = {
 	.name		= CERT_STORE_KEY_TYPE_NAME,
+	.vet_description = cert_store_key_vet_description,
 	.preparse	= user_preparse,
 	.free_preparse	= user_free_preparse,
 	.instantiate	= generic_key_instantiate,
@@ -768,6 +783,7 @@ static ssize_t refresh_store(struct kobject *kobj, struct kobj_attribute *attr,
 	if (rc)
 		return rc;
 
+	WRITE_ONCE(cs_refresh_task, current);
 	for (retries = 0; retries < DIAG_MAX_RETRIES; retries++) {
 		/* Request certificates from certificate store. */
 		rc = fill_cs_keyring();
@@ -776,6 +792,7 @@ static ssize_t refresh_store(struct kobject *kobj, struct kobj_attribute *attr,
 		if (rc != -EAGAIN)
 			break;
 	}
+	WRITE_ONCE(cs_refresh_task, NULL);
 	cs_status_val = rc;
 	mutex_unlock(&cs_refresh_lock);
 
Signature · 1 line
2.53.0

I can give this despite a bit out of context for me because I'm already somewhat familiar with the implementation overall:

Br, Jarkko