Skip to contentSkip to conversation

s390/cert_store: Various fixes

PATCH In review Heiko Carstens 3 participants 44 messages Nov 16 – Nov 17

Subsystems (2)
lore
Share & tools
lore
author maintainer
#40 lore raw

PATCH 14/15 s390/cert_store: Fix out-of-bounds read in pr_dbf_vce()

In reply to Heiko Carstens

pr_dbf_vce() dumps the last 128 bytes of the certificate data of a Verification Certificate Entry (VCE) into a debug area. However the address calculation is wrong: vce_length is the length of the whole entry including the header, while cert_data_buf starts behind the header. Therefore the dump starts at the end of the entry and starts reading behind it.

Dump the last 128 bytes of the entry like intended. Limit dumps to the data length.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/s390/kernel/cert_store.c +6 -3
index 996ca3025629..12fc0ee87f3b 100644
@@ -181,6 +181,7 @@ static void pr_dbf_vce(const struct vce *e)
 {
 	unsigned char vc_name[VC_NAME_LEN_BYTES + 1];
 	char log_string[VC_NAME_LEN_BYTES + 40];
+	u32 data_len = 0;
 
 	pr_dbf_msg("VCE Header:");
 	pr_dbf_msg("vce_hdr.vce_length: %d", e->vce_hdr.vce_length);
@@ -206,11 +207,13 @@ static void pr_dbf_vce(const struct vce *e)
 	debug_text_event(cert_store_hexdump, 3, log_string);
 
 	/* Certificate data. */
+	if (e->vce_hdr.vce_length > sizeof(e->vce_hdr))
+		data_len = e->vce_hdr.vce_length - sizeof(e->vce_hdr);
 	debug_text_event(cert_store_hexdump, 3, "VCE: Certificate data start");
-	debug_event(cert_store_hexdump, 3, (u8 *)e->cert_data_buf, 128);
+	debug_event(cert_store_hexdump, 3, (u8 *)e->cert_data_buf, min(data_len, 128U));
 	debug_text_event(cert_store_hexdump, 3, "VCE: Certificate data end");
-	debug_event(cert_store_hexdump, 3,
-		    (u8 *)e->cert_data_buf + e->vce_hdr.vce_length - 128, 128);
+	if (data_len > 128)
+		debug_event(cert_store_hexdump, 3, (u8 *)e + e->vce_hdr.vce_length - 128, 128);
 }
 
 static void pr_dbf_vcssb(const struct vcssb *s)
Signature · 1 line
2.56.1
author maintainer
#21 lore raw

PATCH 8/15 s390/cert_store: Remove unnecessary static specifier in create_cs_keyring()

In reply to Heiko Carstens

The local variable cs_keyring in create_cs_keyring() never reuses its assigned value, therefore remove the static specifier. Callers are serialized by cs_refresh_lock, so this does not fix a bug.

Even though this is not a bug fix, this contains a Fixes tag since other commits require this as a prereq.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/cert_store.c +1 -1
index b720dd26a557..aefbefb40c74 100644
@@ -401,7 +401,7 @@ static void cleanup_cs_keys(void)
 
 static struct key *create_cs_keyring(void)
 {
-	static struct key *cs_keyring;
+	struct key *cs_keyring;
 
 	/* Cleanup previous cs_keyring and all associated keys if any. */
 	cleanup_cs_keys();
Signature · 1 line
2.56.1
author maintainer
#38 lore raw

PATCH 13/15 s390/cert_store: Fix max_vcxe_length output in pr_dbf_vcssb()

In reply to Heiko Carstens

Print the correct value for max_vcxe_length.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/cert_store.c +1 -1
index ecfb7641d427..996ca3025629 100644
@@ -225,7 +225,7 @@ static void pr_dbf_vcssb(const struct vcssb *s)
 	pr_dbf_msg("total_vc_index_count: %u", s->total_vc_index_count);
 	pr_dbf_msg("max_vc_index_count: %u", s->max_vc_index_count);
 	pr_dbf_msg("max_vce_length: %u", s->max_vce_length);
-	pr_dbf_msg("max_vcxe_length: %u", s->max_vce_length);
+	pr_dbf_msg("max_vcxe_length: %u", s->max_vcxe_length);
 	pr_dbf_msg("max_single_vcb_length: %u", s->max_single_vcb_length);
 	pr_dbf_msg("total_vcb_length: %u", s->total_vcb_length);
 	pr_dbf_msg("max_single_vcxb_length: %u", s->max_single_vcxb_length);
Signature · 1 line
2.56.1
author maintainer
#15 lore raw

PATCH 6/15 s390/cert_store: Fix cs_keyring reference leak in fill_cs_keyring()

In reply to Heiko Carstens

create_cs_keyring() returns cs_keyring with a reference held. On the success path fill_cs_keyring() never drops it, leaking the reference. The keyring is kept alive by its link to the session keyring.

Drop the reference after add_certificates_to_keyring() returns.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 1 +
 1 file changed, 1 insertion(+)
arch/s390/kernel/cert_store.c +1 -0
index a69eccf6affd..386fe39fe8bd 100644
@@ -723,6 +723,7 @@ static int fill_cs_keyring(void)
 	if (rc)
 		goto cleanup_cs_keyring;
 
+	key_put(cs_keyring);
 	goto out;
 
 cleanup_cs_keyring:
Signature · 1 line
2.56.1
author maintainer
#18 lore raw

PATCH 7/15 s390/cert_store: Fix potential use-after-free caused by find_cs_keyring()

In reply to Heiko Carstens

find_cs_keyring() drops the refcount of cs_keyring before returning it. Which means that callers operate on potentially already freed memory.

Let find_cs_keyring() return cs_keyring with a reference. Since cleanup_cs_keys() now performs a balanced put via find_cs_keyring(), remove the separate cleanup_cs_keyring error label in fill_cs_keyring() and drop the reference unconditionally instead.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
arch/s390/kernel/cert_store.c +6 -12
index 386fe39fe8bd..b720dd26a557 100644
@@ -367,17 +367,13 @@ static struct key *find_cs_keyring(void)
 	cs_keyring_ref = keyring_search(make_key_ref(get_user_session_keyring(), true),
 					&key_type_keyring, CERT_STORE_KEYRING_NAME,
 					false);
-	if (!IS_ERR(cs_keyring_ref)) {
-		cs_keyring = key_ref_to_ptr(cs_keyring_ref);
-		key_ref_put(cs_keyring_ref);
-		goto found;
-	}
+	if (!IS_ERR(cs_keyring_ref))
+		return key_ref_to_ptr(cs_keyring_ref);
+
 	/* Search default locations: thread, process, session keyrings */
 	cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
 	if (IS_ERR(cs_keyring))
 		return NULL;
-	key_put(cs_keyring);
-found:
 	return cs_keyring;
 }
 
@@ -720,14 +716,12 @@ static int fill_cs_keyring(void)
 		goto cleanup_keys;
 
 	rc = add_certificates_to_keyring(vcssb, cs_keyring);
-	if (rc)
-		goto cleanup_cs_keyring;
-
 	key_put(cs_keyring);
+	if (rc)
+		goto cleanup_keys;
+
 	goto out;
 
-cleanup_cs_keyring:
-	key_put(cs_keyring);
 cleanup_keys:
 	cleanup_cs_keys();
 out:
Signature · 1 line
2.56.1
author maintainer
#6 lore raw

PATCH 3/15 s390/cert_store: Fix potential use-after-free in invalidate_keyring_keys()

In reply to Heiko Carstens

Drop the refcount of current_key behind all users of current_key which dereference it, like e.g. error logging.

Fixes a potential use-after-free bug.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kernel/cert_store.c +3 -3
index 89cc3a1683a3..e5060777ce6f 100644
@@ -344,12 +344,12 @@ static int invalidate_keyring_keys(struct key *keyring)
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
 		key_invalidate(current_key);
-		key_put(current_key);
 		rc = key_unlink(keyring, current_key);
-		if (rc) {
+		if (rc)
 			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
+		key_put(current_key);
+		if (rc)
 			break;
-		}
 	}
 out:
 	kfree(key_array);
Signature · 1 line
2.56.1
author maintainer
#30 lore raw

PATCH 11/15 s390/cert_store: Do not search the process keyrings for the cert_store keyring

In reply to Heiko Carstens

find_cs_keyring() first searches the user session keyring for the cert_store keyring, and falls back to request_key(), which searches the thread, process and session keyrings of the calling task. Any keyring named "cert_store" found this way is accepted.

cleanup_cs_keys() then invalidates and unlinks all of its keys, and clears and invalidates the keyring itself, without checking permissions. If the refresh is triggered by a task which inherited a session keyring that contains a keyring named "cert_store" not created by this code, this keyring and its keys are destroyed.

create_cs_keyring() always links the keyring into the user session keyring, so the first search is sufficient. Remove the request_key() fallback.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/s390/kernel/cert_store.c +2 -6
index ec6352528a2b..551b538ea930 100644
@@ -360,7 +360,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 
 static struct key *find_cs_keyring(void)
 {
-	struct key *cs_keyring, *us_keyring;
+	struct key *us_keyring;
 	key_ref_t cs_keyring_ref;
 
 	us_keyring = get_user_session_keyring();
@@ -373,11 +373,7 @@ static struct key *find_cs_keyring(void)
 		if (!IS_ERR(cs_keyring_ref))
 			return key_ref_to_ptr(cs_keyring_ref);
 	}
-	/* Search default locations: thread, process, session keyrings */
-	cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
-	if (IS_ERR(cs_keyring))
-		return NULL;
-	return cs_keyring;
+	return NULL;
 }
 
 static void cleanup_cs_keys(void)
Signature · 1 line
2.56.1
author maintainer
#9 lore raw

PATCH 4/15 s390/cert_store: Check key_lookup() return code in invalidate_keyring_keys()

In reply to Heiko Carstens

keyring->sem is released after writing the key serial numbers into key_array. Keys corresponding to those serial numbers may have been removed before key_lookup() is called.

Therefore check the return code of key_lookup() before dereferencing it.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 3 +++
 1 file changed, 3 insertions(+)
arch/s390/kernel/cert_store.c +3 -0
index e5060777ce6f..8eb32497ca24 100644
@@ -339,8 +339,11 @@ static int invalidate_keyring_keys(struct key *keyring)
 		goto out;
 	}
 
+	rc = 0;
 	for (key_index = 0; key_index < num_keys; key_index++) {
 		current_key = key_lookup(key_array[key_index]);
+		if (IS_ERR(current_key))
+			continue;
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
 		key_invalidate(current_key);
Signature · 1 line
2.56.1
author maintainer
#12 lore raw

PATCH 5/15 s390/cert_store: Unlink before invalidate in invalidate_keyring_keys()

In reply to Heiko Carstens

If a key is invalidated, the key garbage collector may remove its link from the keyring at any time. If that happens the subsequent key_unlink() fails.

invalidate_keyring_keys() would then incorrectly return an error, which would lead to unregistering the cert_store key type.

Unlink the key first and invalidate it afterwards to avoid this race.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/cert_store.c +1 -1
index 8eb32497ca24..a69eccf6affd 100644
@@ -346,10 +346,10 @@ static int invalidate_keyring_keys(struct key *keyring)
 			continue;
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
-		key_invalidate(current_key);
 		rc = key_unlink(keyring, current_key);
 		if (rc)
 			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
+		key_invalidate(current_key);
 		key_put(current_key);
 		if (rc)
 			break;
Signature · 1 line
2.56.1
author maintainer
#27 lore raw

PATCH 10/15 s390/cert_store: Fix keyring refcount handling in get_user_session_keyring()

In reply to Heiko Carstens

get_user_session_keyring() drops the reference of the looked up keyring before returning it. Callers therefore use the keyring without holding a reference.

Return the keyring with the reference and let callers drop the reference.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/s390/kernel/cert_store.c +5 -2
index d11637c85063..ec6352528a2b 100644
@@ -308,7 +308,6 @@ static struct key *get_user_session_keyring(void)
 			   PTR_ERR(us_keyring_ref));
 		return ERR_PTR(-ENOKEY);
 	}
-	key_ref_put(us_keyring_ref);
 	return key_ref_to_ptr(us_keyring_ref);
 }
 
@@ -370,6 +369,7 @@ static struct key *find_cs_keyring(void)
 						&key_type_keyring,
 						CERT_STORE_KEYRING_NAME,
 						false);
+		key_put(us_keyring);
 		if (!IS_ERR(cs_keyring_ref))
 			return key_ref_to_ptr(cs_keyring_ref);
 	}
@@ -399,8 +399,10 @@ static void cleanup_cs_keys(void)
 	keyring_clear(cs_keyring);
 	key_invalidate(cs_keyring);
 	us_keyring = get_user_session_keyring();
-	if (!IS_ERR(us_keyring))
+	if (!IS_ERR(us_keyring)) {
 		key_unlink(us_keyring, cs_keyring);
+		key_put(us_keyring);
+	}
 	key_put(cs_keyring);
 }
 
@@ -418,6 +420,7 @@ static struct key *create_cs_keyring(void)
 				   (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ,
 				   KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_SET_KEEP,
 				   NULL, us_keyring);
+	key_put(us_keyring);
 	if (IS_ERR(cs_keyring)) {
 		pr_dbf_msg("Can't allocate cert_store keyring");
 		return NULL;
Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 3/15
#7 lore raw

Re: [PATCH 03/15] s390/cert_store: Fix potential use-after-free in invalidate_keyring_keys()

In reply to Heiko Carstens
Quoted 34 lines · Heiko Carstens

Drop the refcount of current_key behind all users of current_key which dereference it, like e.g. error logging.

Fixes a potential use-after-free bug.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kernel/cert_store.c +3 -3
index 89cc3a1683a3..e5060777ce6f 100644
@@ -344,12 +344,12 @@ static int invalidate_keyring_keys(struct key *keyring)
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
 		key_invalidate(current_key);
-		key_put(current_key);
 		rc = key_unlink(keyring, current_key);
-		if (rc) {
+		if (rc)
 			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
+		key_put(current_key);
+		if (rc)
 			break;
-		}
 	}
 out:
 	kfree(key_array);
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 4/15
#10 lore raw

Re: [PATCH 04/15] s390/cert_store: Check key_lookup() return code in invalidate_keyring_keys()

In reply to Heiko Carstens
Quoted 31 lines · Heiko Carstens

keyring->sem is released after writing the key serial numbers into key_array. Keys corresponding to those serial numbers may have been removed before key_lookup() is called.

Therefore check the return code of key_lookup() before dereferencing it.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 3 +++
 1 file changed, 3 insertions(+)
arch/s390/kernel/cert_store.c +3 -0
index e5060777ce6f..8eb32497ca24 100644
@@ -339,8 +339,11 @@ static int invalidate_keyring_keys(struct key *keyring)
 		goto out;
 	}
 
+	rc = 0;
 	for (key_index = 0; key_index < num_keys; key_index++) {
 		current_key = key_lookup(key_array[key_index]);
+		if (IS_ERR(current_key))
+			continue;
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
 		key_invalidate(current_key);
Signature · 1 line
2.56.1

Br, Jarkko

author maintainer
#33 lore raw

PATCH 12/15 s390/cert_store: Validate hash length in check_certificate_valid()

In reply to Heiko Carstens

Validate that vc_hash_length is SHA256_DIGEST_SIZE before checking the hash and use sizeof(hash) in memcmp(). This avoids an out-of-bounds read if vc_hash_length is incorrect.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kernel/cert_store.c +5 -3
index 551b538ea930..ecfb7641d427 100644
@@ -261,13 +261,11 @@ static int diag320(unsigned long subcode, void *addr)
 static int check_certificate_hash(const struct vce *vce)
 {
 	u8 hash[SHA256_DIGEST_SIZE];
-	u16 vc_hash_length;
 	u8 *vce_hash;
 
 	vce_hash = (u8 *)vce + vce->vce_hdr.vc_hash_offset;
-	vc_hash_length = vce->vce_hdr.vc_hash_length;
 	sha256((u8 *)vce + vce->vce_hdr.vc_offset, vce->vce_hdr.vc_length, hash);
-	if (memcmp(vce_hash, hash, vc_hash_length) == 0)
+	if (memcmp(vce_hash, hash, sizeof(hash)) == 0)
 		return 0;
 
 	pr_dbf_msg("SHA256 hash of received certificate does not match");
@@ -293,6 +291,10 @@ static int check_certificate_valid(const struct vce *vce)
 		pr_dbf_msg("Hash type is not supported");
 		return -EINVAL;
 	}
+	if (vce->vce_hdr.vc_hash_length != SHA256_DIGEST_SIZE) {
+		pr_dbf_msg("Hash length is not supported");
+		return -EINVAL;
+	}
 
 	return check_certificate_hash(vce);
 }
Signature · 1 line
2.56.1
author maintainer
#42 lore raw

PATCH 15/15 s390/cert_store: Initialize ism array in query_diag320_subcodes()

In reply to Heiko Carstens

The Installed Subcode Block returned by DIAG 320 subcode 0 may be smaller than the ism array. Initialize it to avoid copying uninitialized data to the debug area.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/cert_store.c +1 -1
index 12fc0ee87f3b..e3cc9389a50a 100644
@@ -670,7 +670,7 @@ static int add_certificates_to_keyring(struct vcssb *vcssb, struct key *keyring)
  */
 static int query_diag320_subcodes(void)
 {
-	unsigned long ism[ISM_LEN_DWORDS];
+	unsigned long ism[ISM_LEN_DWORDS] = {};
 	int rc;
 
 	rc = diag320(0, ism);
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 1/15 s390/cert_store: Hold keyring->sem while reading the keyring

In reply to Heiko Carstens

From: Jarkko Sakkinen <[email protected]>

invalidate_keyring_keys() does not hold keyring->sem while calling keyring_read(), which is contrary to what keyring assumes from its callers, and can lead to recurring bugs.

KEY_FLAG_KEEP alone does not protect from e.g. the following call path:

1. keyctl_keyring_move()
2. key_move()
3. __key_unlink()

Address the issue by guarding the critical section.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
arch/s390/kernel/cert_store.c +11 -6
index 998f2da56175..d35b1ea22507 100644
@@ -18,6 +18,7 @@
 #include <linux/keyctl.h>
 #include <linux/kobject.h>
 #include <linux/module.h>
+#include <linux/rwsem.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
@@ -320,13 +321,17 @@ static int invalidate_keyring_keys(struct key *keyring)
 	struct key *current_key;
 	int rc;
 
-	keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
-	num_keys = keyring_payload_len / sizeof(key_serial_t);
-	key_array = kzalloc_objs(key_serial_t, num_keys);
-	if (!key_array)
-		return -ENOMEM;
+	scoped_guard(rwsem_read, &keyring->sem) {
+		keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
+		num_keys = keyring_payload_len / sizeof(key_serial_t);
+
+		key_array = kzalloc_objs(key_serial_t, num_keys);
+		if (!key_array)
+			return -ENOMEM;
+
+		rc = key_type_keyring.read(keyring, (char *)key_array, keyring_payload_len);
+	}
 
-	rc = key_type_keyring.read(keyring, (char *)key_array, keyring_payload_len);
 	if (rc != keyring_payload_len) {
 		pr_dbf_msg("Couldn't read keyring payload");
 		goto out;
Signature · 1 line
2.56.1
author maintainer
#24 lore raw

PATCH 9/15 s390/cert_store: Fix error handling of get_user_session_keyring()

In reply to Heiko Carstens

create_cs_keyring(), cleanup_cs_keys(), and find_cs_keyring() all pass the return value of get_user_session_keyring() to other functions without checking it. This can lead to dereferencing an error pointer.

Check the return value before using it.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
arch/s390/kernel/cert_store.c +19 -11
index aefbefb40c74..d11637c85063 100644
@@ -361,15 +361,18 @@ static int invalidate_keyring_keys(struct key *keyring)
 
 static struct key *find_cs_keyring(void)
 {
+	struct key *cs_keyring, *us_keyring;
 	key_ref_t cs_keyring_ref;
-	struct key *cs_keyring;
-
-	cs_keyring_ref = keyring_search(make_key_ref(get_user_session_keyring(), true),
-					&key_type_keyring, CERT_STORE_KEYRING_NAME,
-					false);
-	if (!IS_ERR(cs_keyring_ref))
-		return key_ref_to_ptr(cs_keyring_ref);
 
+	us_keyring = get_user_session_keyring();
+	if (!IS_ERR(us_keyring)) {
+		cs_keyring_ref = keyring_search(make_key_ref(us_keyring, true),
+						&key_type_keyring,
+						CERT_STORE_KEYRING_NAME,
+						false);
+		if (!IS_ERR(cs_keyring_ref))
+			return key_ref_to_ptr(cs_keyring_ref);
+	}
 	/* Search default locations: thread, process, session keyrings */
 	cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
 	if (IS_ERR(cs_keyring))
@@ -379,7 +382,7 @@ static struct key *find_cs_keyring(void)
 
 static void cleanup_cs_keys(void)
 {
-	struct key *cs_keyring;
+	struct key *cs_keyring, *us_keyring;
 
 	cs_keyring = find_cs_keyring();
 	if (!cs_keyring)
@@ -395,21 +398,26 @@ static void cleanup_cs_keys(void)
 
 	keyring_clear(cs_keyring);
 	key_invalidate(cs_keyring);
+	us_keyring = get_user_session_keyring();
+	if (!IS_ERR(us_keyring))
+		key_unlink(us_keyring, cs_keyring);
 	key_put(cs_keyring);
-	key_unlink(get_user_session_keyring(), cs_keyring);
 }
 
 static struct key *create_cs_keyring(void)
 {
-	struct key *cs_keyring;
+	struct key *cs_keyring, *us_keyring;
 
 	/* Cleanup previous cs_keyring and all associated keys if any. */
 	cleanup_cs_keys();
+	us_keyring = get_user_session_keyring();
+	if (IS_ERR(us_keyring))
+		return NULL;
 	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, get_user_session_keyring());
+				   NULL, us_keyring);
 	if (IS_ERR(cs_keyring)) {
 		pr_dbf_msg("Can't allocate cert_store keyring");
 		return NULL;
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 2/15 s390/cert_store: Fix return code handling in invalidate_keyring_keys()

In reply to Heiko Carstens

Add missing error handling in case key_type_keyring.read() returns an error.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/s390/kernel/cert_store.c +6 -4
index d35b1ea22507..89cc3a1683a3 100644
@@ -316,15 +316,16 @@ static struct key *get_user_session_keyring(void)
 static int invalidate_keyring_keys(struct key *keyring)
 {
 	unsigned long num_keys, key_index;
-	size_t keyring_payload_len;
+	long keyring_payload_len, rc;
 	key_serial_t *key_array;
 	struct key *current_key;
-	int rc;
 
 	scoped_guard(rwsem_read, &keyring->sem) {
 		keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
-		num_keys = keyring_payload_len / sizeof(key_serial_t);
+		if (keyring_payload_len <= 0)
+			return keyring_payload_len;
 
+		num_keys = keyring_payload_len / sizeof(key_serial_t);
 		key_array = kzalloc_objs(key_serial_t, num_keys);
 		if (!key_array)
 			return -ENOMEM;
@@ -334,6 +335,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 
 	if (rc != keyring_payload_len) {
 		pr_dbf_msg("Couldn't read keyring payload");
+		rc = rc < 0 ? rc : -EINVAL;
 		goto out;
 	}
 
@@ -345,7 +347,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 		key_put(current_key);
 		rc = key_unlink(keyring, current_key);
 		if (rc) {
-			pr_dbf_msg("Couldn't unlink key %08x: %d", current_key->serial, rc);
+			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
 			break;
 		}
 	}
Signature · 1 line
2.56.1

Heiko Carstens

author maintainer
#0 lore raw

cover s390/cert_store: Various fixes

Hi Jarrko,

as already previously written here a patch series, including your patch, which addresses various cert_store bugs. Patches are very fine grained. Also it looks to me as if you addressed most / many of this with your patches already.

I guess the best way forward would be if you take those patches of this series which address things not already fixed by you(?). Otherwise let me know how we should proceed without causing too much churn.

Thanks,
Heiko

Heiko Carstens (14):
s390/cert_store: Fix return code handling in invalidate_keyring_keys() s390/cert_store: Fix potential use-after-free in invalidate_keyring_keys() s390/cert_store: Check key_lookup() return code in invalidate_keyring_keys() s390/cert_store: Unlink before invalidate in invalidate_keyring_keys() s390/cert_store: Fix cs_keyring reference leak in fill_cs_keyring() s390/cert_store: Fix potential use-after-free caused by find_cs_keyring() s390/cert_store: Remove unnecessary static specifier in create_cs_keyring() s390/cert_store: Fix error handling of get_user_session_keyring() s390/cert_store: Fix keyring refcount handling in get_user_session_keyring() s390/cert_store: Do not search the process keyrings for the cert_store keyring s390/cert_store: Validate hash length in check_certificate_valid() s390/cert_store: Fix max_vcxe_length output in pr_dbf_vcssb() s390/cert_store: Fix out-of-bounds read in pr_dbf_vce() s390/cert_store: Initialize ism array in query_diag320_subcodes()

Jarkko Sakkinen (1):
s390/cert_store: Hold keyring->sem while reading the keyring

 arch/s390/kernel/cert_store.c | 103 ++++++++++++++++++++--------------
 1 file changed, 60 insertions(+), 43 deletions(-)
Signature · 1 line
2.56.1
#22 lore raw

Re: [PATCH 08/15] s390/cert_store: Remove unnecessary static specifier in create_cs_keyring()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

The local variable cs_keyring in create_cs_keyring() never reuses its assigned value, therefore remove the static specifier. Callers are serialized by cs_refresh_lock, so this does not fix a bug.

Even though this is not a bug fix, this contains a Fixes tag since other commits require this as a prereq.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#28 lore raw

Re: [PATCH 10/15] s390/cert_store: Fix keyring refcount handling in get_user_session_keyring()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

get_user_session_keyring() drops the reference of the looked up keyring before returning it. Callers therefore use the keyring without holding a reference.

Return the keyring with the reference and let callers drop the reference.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#34 lore raw

Re: [PATCH 12/15] s390/cert_store: Validate hash length in check_certificate_valid()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

Validate that vc_hash_length is SHA256_DIGEST_SIZE before checking the hash and use sizeof(hash) in memcmp(). This avoids an out-of-bounds read if vc_hash_length is incorrect.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#11 lore raw

Re: [PATCH 04/15] s390/cert_store: Check key_lookup() return code in invalidate_keyring_keys()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

keyring->sem is released after writing the key serial numbers into key_array. Keys corresponding to those serial numbers may have been removed before key_lookup() is called.

Therefore check the return code of key_lookup() before dereferencing it.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#39 lore raw

Re: [PATCH 13/15] s390/cert_store: Fix max_vcxe_length output in pr_dbf_vcssb()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 4 lines

Print the correct value for max_vcxe_length.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#31 lore raw

Re: [PATCH 11/15] s390/cert_store: Do not search the process keyrings for the cert_store keyring

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

find_cs_keyring() first searches the user session keyring for the cert_store keyring, and falls back to request_key(), which searches the thread, process and session keyrings of the calling task. Any keyring named "cert_store" found this way is accepted.

cleanup_cs_keys() then invalidates and unlinks all of its keys, and clears and invalidates the keyring itself, without checking permissions. If the refresh is triggered by a task which inherited a session keyring that contains a keyring named "cert_store" not created by this code, this keyring and its keys are destroyed.

Quoted 6 lines

create_cs_keyring() always links the keyring into the user session keyring, so the first search is sufficient. Remove the request_key() fallback.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#13 lore raw

Re: [PATCH 05/15] s390/cert_store: Unlink before invalidate in invalidate_keyring_keys()

In reply to Heiko Carstens
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] s390/cert_store: key reference…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] s390/cert_store: key reference count underflow and use-after-free in fill_cs_keyring() [1]
--

commit fbd12b78eb8c69fc439e6febd3b649bd06084a33
Author: Heiko Carstens <[email protected]>

s390/cert_store: Unlink before invalidate in invalidate_keyring_keys()

This commit reorders key_unlink() before key_invalidate() in invalidate_keyring_keys() so that the key garbage collector cannot unlink the key first and cause key_unlink() to fail.

Quoted 6 lines
arch/s390/kernel/cert_store.c +0 -0
index 8eb32497ca24d..a69eccf6affd3 100644
@@ -346,10 +346,10 @@ static int invalidate_keyring_keys(struct key *keyring)
 			continue;

arch/s390/kernel/cert_store.c static int invalidate_keyring_keys(struct key *keyring) · line 355

Quoted 10 lines
arch/s390/kernel/cert_store.c +1 -1
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
-		key_invalidate(current_key);
 		rc = key_unlink(keyring, current_key);
 		if (rc)
 			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
+		key_invalidate(current_key);
 		key_put(current_key);
 		if (rc)
 			break;

[Severity: Critical]
This problem wasn't introduced by this patch, but can cleanup_cs_keys() and fill_cs_keyring() underflow cs_keyring->usage and cause a use-after-free [1]?

In create_cs_keyring(), keyring_alloc() links cs_keyring into the user session keyring, starting cs_keyring->usage at 2 (one allocation reference and one link reference). However, find_cs_keyring() drops its lookup reference before returning the raw pointer, and cleanup_cs_keys() drops another reference before unlinking cs_keyring:

arch/s390/kernel/cert_store.c:find_cs_keyring() {
	...
	if (!IS_ERR(cs_keyring_ref)) {
		cs_keyring = key_ref_to_ptr(cs_keyring_ref);
		key_ref_put(cs_keyring_ref);
		goto found;
	}
	...
	key_put(cs_keyring);
found:
	return cs_keyring;
}
arch/s390/kernel/cert_store.c:cleanup_cs_keys() {
	...
	cs_keyring = find_cs_keyring();
	if (!cs_keyring)
		return;
	...
	key_put(cs_keyring);
	key_unlink(get_user_session_keyring(), cs_keyring);
}
arch/s390/kernel/cert_store.c:fill_cs_keyring() {
	...
	rc = add_certificates_to_keyring(vcssb, cs_keyring);
	if (rc)
		goto cleanup_cs_keyring;

	goto out;

cleanup_cs_keyring:

	key_put(cs_keyring);
cleanup_keys:
	cleanup_cs_keys();
}

When add_certificates_to_keyring() fails in fill_cs_keyring(), the cleanup_cs_keyring label drops cs_keyring->usage from 2 to 1 and falls through to cleanup_cs_keys(). There, find_cs_keyring() returns cs_keyring without holding a lookup reference, key_put(cs_keyring) drops the remaining reference from 1 to 0 (scheduling the keyring for garbage collection), and key_unlink() then accesses the zero-refcount key and drops the link reference an extra time. The same extra key_put() happens when create_cs_keyring() calls cleanup_cs_keys() to purge an existing keyring whose initial allocation reference was already dropped.

[1] https://sashiko.dev/#/bug/linux-6c517b55-b772-4ca6-8a47-30730e6a9177

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#43 lore raw

Re: [PATCH 15/15] s390/cert_store: Initialize ism array in query_diag320_subcodes()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

The Installed Subcode Block returned by DIAG 320 subcode 0 may be smaller than the ism array. Initialize it to avoid copying uninitialized data to the debug area.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#4 lore raw

Re: [PATCH 02/15] s390/cert_store: Fix return code handling in invalidate_keyring_keys()

In reply to Heiko Carstens
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] s390/cert_store: unchecked…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] s390/cert_store: unchecked key_lookup() and UAF in invalidate_keyring_keys() [1]
--

commit ad52ff18526194809b9921405599d976809c02b9
Author: Heiko Carstens <[email protected]>

s390/cert_store: Fix return code handling in invalidate_keyring_keys()

This commit adds error handling to invalidate_keyring_keys() when key_type_keyring.read() returns an error or a short read.

Quoted 4 lines
arch/s390/kernel/cert_store.c +0 -0
index d35b1ea22507d..89cc3a1683a32 100644

[ ... ]

arch/s390/kernel/cert_store.c static int invalidate_keyring_keys(struct key *keyring) · line 353

Quoted 9 lines
arch/s390/kernel/cert_store.c +1 -1
@@ -345,7 +347,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 		key_put(current_key);
 		rc = key_unlink(keyring, current_key);
 		if (rc) {
-			pr_dbf_msg("Couldn't unlink key %08x: %d", current_key->serial, rc);
+			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
 			break;
 		}
 	}

[Severity: Critical]
This problem wasn't introduced by this patch, but can current_key in this loop
[1] be an error pointer or be used after free?

keyring->sem is released after reading key_array, without holding references to the keys. If a concurrent keyctl operation unlinks a key and garbage collection frees it before key_lookup(key_array[key_index]) runs, key_lookup() returns ERR_PTR(-ENOKEY), and dereferencing current_key->serial or passing current_key to key_invalidate() crashes.

In addition, when key_lookup() succeeds, calling key_put(current_key) before key_unlink(keyring, current_key) and the current_key->serial debug print drops the caller's reference early, which allows a concurrent unlink and garbage collection to free current_key and cause a use-after-free.

[1] https://sashiko.dev/#/bug/linux-3ea8bbf3-6016-4165-bad4-2a5916ce887e

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#41 lore raw

Re: [PATCH 14/15] s390/cert_store: Fix out-of-bounds read in pr_dbf_vce()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

pr_dbf_vce() dumps the last 128 bytes of the certificate data of a Verification Certificate Entry (VCE) into a debug area. However the address calculation is wrong: vce_length is the length of the whole entry including the header, while cert_data_buf starts behind the header. Therefore the dump starts at the end of the entry and starts reading behind it.

Quoted 5 lines

Dump the last 128 bytes of the entry like intended. Limit dumps to the data length.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#16 lore raw

Re: [PATCH 06/15] s390/cert_store: Fix cs_keyring reference leak in fill_cs_keyring()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

create_cs_keyring() returns cs_keyring with a reference held. On the success path fill_cs_keyring() never drops it, leaking the reference. The keyring is kept alive by its link to the session keyring.

Drop the reference after add_certificates_to_keyring() returns.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#2 lore raw

Re: [PATCH 01/15] s390/cert_store: Hold keyring->sem while reading the keyring

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

From: Jarkko Sakkinen <[email protected]>

invalidate_keyring_keys() does not hold keyring->sem while calling keyring_read(), which is contrary to what keyring assumes from its callers, and can lead to recurring bugs.

KEY_FLAG_KEEP alone does not protect from e.g. the following call path:

1. keyctl_keyring_move()
2. key_move()
3. __key_unlink()

Quoted 5 lines

Address the issue by guarding the critical section.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#25 lore raw

Re: [PATCH 09/15] s390/cert_store: Fix error handling of get_user_session_keyring()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

create_cs_keyring(), cleanup_cs_keys(), and find_cs_keyring() all pass the return value of get_user_session_keyring() to other functions without checking it. This can lead to dereferencing an error pointer.

Check the return value before using it.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#19 lore raw

Re: [PATCH 07/15] s390/cert_store: Fix potential use-after-free caused by find_cs_keyring()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

find_cs_keyring() drops the refcount of cs_keyring before returning it. Which means that callers operate on potentially already freed memory.

Let find_cs_keyring() return cs_keyring with a reference. Since cleanup_cs_keys() now performs a balanced put via find_cs_keyring(), remove the separate cleanup_cs_keyring error label in fill_cs_keyring() and drop the reference unconditionally instead.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#8 lore raw

Re: [PATCH 03/15] s390/cert_store: Fix potential use-after-free in invalidate_keyring_keys()

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Drop the refcount of current_key behind all users of current_key which dereference it, like e.g. error logging.

Fixes a potential use-after-free bug.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v1 5/15
#14 lore raw

Re: [PATCH 05/15] s390/cert_store: Unlink before invalidate in invalidate_keyring_keys()

In reply to Heiko Carstens
Quoted 34 lines · Heiko Carstens

If a key is invalidated, the key garbage collector may remove its link from the keyring at any time. If that happens the subsequent key_unlink() fails.

invalidate_keyring_keys() would then incorrectly return an error, which would lead to unregistering the cert_store key type.

Unlink the key first and invalidate it afterwards to avoid this race.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/cert_store.c +1 -1
index 8eb32497ca24..a69eccf6affd 100644
@@ -346,10 +346,10 @@ static int invalidate_keyring_keys(struct key *keyring)
 			continue;
 		pr_dbf_msg("Invalidating key %08x", current_key->serial);
 
-		key_invalidate(current_key);
 		rc = key_unlink(keyring, current_key);
 		if (rc)
 			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
+		key_invalidate(current_key);
 		key_put(current_key);
 		if (rc)
 			break;
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 6/15
#17 lore raw

Re: [PATCH 06/15] s390/cert_store: Fix cs_keyring reference leak in fill_cs_keyring()

In reply to Heiko Carstens
Quoted 27 lines · Heiko Carstens

create_cs_keyring() returns cs_keyring with a reference held. On the success path fill_cs_keyring() never drops it, leaking the reference. The keyring is kept alive by its link to the session keyring.

Drop the reference after add_certificates_to_keyring() returns.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 1 +
 1 file changed, 1 insertion(+)
arch/s390/kernel/cert_store.c +1 -0
index a69eccf6affd..386fe39fe8bd 100644
@@ -723,6 +723,7 @@ static int fill_cs_keyring(void)
 	if (rc)
 		goto cleanup_cs_keyring;
 
+	key_put(cs_keyring);
 	goto out;
 
 cleanup_cs_keyring:
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 7/15
#20 lore raw

Re: [PATCH 07/15] s390/cert_store: Fix potential use-after-free caused by find_cs_keyring()

In reply to Heiko Carstens
Quoted 60 lines · Heiko Carstens

find_cs_keyring() drops the refcount of cs_keyring before returning it. Which means that callers operate on potentially already freed memory.

Let find_cs_keyring() return cs_keyring with a reference. Since cleanup_cs_keys() now performs a balanced put via find_cs_keyring(), remove the separate cleanup_cs_keyring error label in fill_cs_keyring() and drop the reference unconditionally instead.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
arch/s390/kernel/cert_store.c +6 -12
index 386fe39fe8bd..b720dd26a557 100644
@@ -367,17 +367,13 @@ static struct key *find_cs_keyring(void)
 	cs_keyring_ref = keyring_search(make_key_ref(get_user_session_keyring(), true),
 					&key_type_keyring, CERT_STORE_KEYRING_NAME,
 					false);
-	if (!IS_ERR(cs_keyring_ref)) {
-		cs_keyring = key_ref_to_ptr(cs_keyring_ref);
-		key_ref_put(cs_keyring_ref);
-		goto found;
-	}
+	if (!IS_ERR(cs_keyring_ref))
+		return key_ref_to_ptr(cs_keyring_ref);
+
 	/* Search default locations: thread, process, session keyrings */
 	cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
 	if (IS_ERR(cs_keyring))
 		return NULL;
-	key_put(cs_keyring);
-found:
 	return cs_keyring;
 }
 
@@ -720,14 +716,12 @@ static int fill_cs_keyring(void)
 		goto cleanup_keys;
 
 	rc = add_certificates_to_keyring(vcssb, cs_keyring);
-	if (rc)
-		goto cleanup_cs_keyring;
-
 	key_put(cs_keyring);
+	if (rc)
+		goto cleanup_keys;
+
 	goto out;
 
-cleanup_cs_keyring:
-	key_put(cs_keyring);
 cleanup_keys:
 	cleanup_cs_keys();
 out:
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 8/15
#23 lore raw

Re: [PATCH 08/15] s390/cert_store: Remove unnecessary static specifier in create_cs_keyring()

In reply to Heiko Carstens
Quoted 29 lines · Heiko Carstens

The local variable cs_keyring in create_cs_keyring() never reuses its assigned value, therefore remove the static specifier. Callers are serialized by cs_refresh_lock, so this does not fix a bug.

Even though this is not a bug fix, this contains a Fixes tag since other commits require this as a prereq.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/cert_store.c +1 -1
index b720dd26a557..aefbefb40c74 100644
@@ -401,7 +401,7 @@ static void cleanup_cs_keys(void)
 
 static struct key *create_cs_keyring(void)
 {
-	static struct key *cs_keyring;
+	struct key *cs_keyring;
 
 	/* Cleanup previous cs_keyring and all associated keys if any. */
 	cleanup_cs_keys();
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 9/15
#26 lore raw

Re: [PATCH 09/15] s390/cert_store: Fix error handling of get_user_session_keyring()

In reply to Heiko Carstens
Quoted 84 lines · Heiko Carstens

create_cs_keyring(), cleanup_cs_keys(), and find_cs_keyring() all pass the return value of get_user_session_keyring() to other functions without checking it. This can lead to dereferencing an error pointer.

Check the return value before using it.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
arch/s390/kernel/cert_store.c +19 -11
index aefbefb40c74..d11637c85063 100644
@@ -361,15 +361,18 @@ static int invalidate_keyring_keys(struct key *keyring)
 
 static struct key *find_cs_keyring(void)
 {
+	struct key *cs_keyring, *us_keyring;
 	key_ref_t cs_keyring_ref;
-	struct key *cs_keyring;
-
-	cs_keyring_ref = keyring_search(make_key_ref(get_user_session_keyring(), true),
-					&key_type_keyring, CERT_STORE_KEYRING_NAME,
-					false);
-	if (!IS_ERR(cs_keyring_ref))
-		return key_ref_to_ptr(cs_keyring_ref);
 
+	us_keyring = get_user_session_keyring();
+	if (!IS_ERR(us_keyring)) {
+		cs_keyring_ref = keyring_search(make_key_ref(us_keyring, true),
+						&key_type_keyring,
+						CERT_STORE_KEYRING_NAME,
+						false);
+		if (!IS_ERR(cs_keyring_ref))
+			return key_ref_to_ptr(cs_keyring_ref);
+	}
 	/* Search default locations: thread, process, session keyrings */
 	cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
 	if (IS_ERR(cs_keyring))
@@ -379,7 +382,7 @@ static struct key *find_cs_keyring(void)
 
 static void cleanup_cs_keys(void)
 {
-	struct key *cs_keyring;
+	struct key *cs_keyring, *us_keyring;
 
 	cs_keyring = find_cs_keyring();
 	if (!cs_keyring)
@@ -395,21 +398,26 @@ static void cleanup_cs_keys(void)
 
 	keyring_clear(cs_keyring);
 	key_invalidate(cs_keyring);
+	us_keyring = get_user_session_keyring();
+	if (!IS_ERR(us_keyring))
+		key_unlink(us_keyring, cs_keyring);
 	key_put(cs_keyring);
-	key_unlink(get_user_session_keyring(), cs_keyring);
 }
 
 static struct key *create_cs_keyring(void)
 {
-	struct key *cs_keyring;
+	struct key *cs_keyring, *us_keyring;
 
 	/* Cleanup previous cs_keyring and all associated keys if any. */
 	cleanup_cs_keys();
+	us_keyring = get_user_session_keyring();
+	if (IS_ERR(us_keyring))
+		return NULL;
 	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, get_user_session_keyring());
+				   NULL, us_keyring);
 	if (IS_ERR(cs_keyring)) {
 		pr_dbf_msg("Can't allocate cert_store keyring");
 		return NULL;
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 10/15
#29 lore raw

Re: [PATCH 10/15] s390/cert_store: Fix keyring refcount handling in get_user_session_keyring()

In reply to Heiko Carstens
Quoted 55 lines · Heiko Carstens

get_user_session_keyring() drops the reference of the looked up keyring before returning it. Callers therefore use the keyring without holding a reference.

Return the keyring with the reference and let callers drop the reference.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/s390/kernel/cert_store.c +5 -2
index d11637c85063..ec6352528a2b 100644
@@ -308,7 +308,6 @@ static struct key *get_user_session_keyring(void)
 			   PTR_ERR(us_keyring_ref));
 		return ERR_PTR(-ENOKEY);
 	}
-	key_ref_put(us_keyring_ref);
 	return key_ref_to_ptr(us_keyring_ref);
 }
 
@@ -370,6 +369,7 @@ static struct key *find_cs_keyring(void)
 						&key_type_keyring,
 						CERT_STORE_KEYRING_NAME,
 						false);
+		key_put(us_keyring);
 		if (!IS_ERR(cs_keyring_ref))
 			return key_ref_to_ptr(cs_keyring_ref);
 	}
@@ -399,8 +399,10 @@ static void cleanup_cs_keys(void)
 	keyring_clear(cs_keyring);
 	key_invalidate(cs_keyring);
 	us_keyring = get_user_session_keyring();
-	if (!IS_ERR(us_keyring))
+	if (!IS_ERR(us_keyring)) {
 		key_unlink(us_keyring, cs_keyring);
+		key_put(us_keyring);
+	}
 	key_put(cs_keyring);
 }
 
@@ -418,6 +420,7 @@ static struct key *create_cs_keyring(void)
 				   (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ,
 				   KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_SET_KEEP,
 				   NULL, us_keyring);
+	key_put(us_keyring);
 	if (IS_ERR(cs_keyring)) {
 		pr_dbf_msg("Can't allocate cert_store keyring");
 		return NULL;
Signature · 1 line
2.56.1

Br, Jarkko

maintainer Reviewed-by on v1 11/15
#32 lore raw

Re: [PATCH 11/15] s390/cert_store: Do not search the process keyrings for the cert_store keyring

In reply to Heiko Carstens
Quoted 50 lines · Heiko Carstens

find_cs_keyring() first searches the user session keyring for the cert_store keyring, and falls back to request_key(), which searches the thread, process and session keyrings of the calling task. Any keyring named "cert_store" found this way is accepted.

cleanup_cs_keys() then invalidates and unlinks all of its keys, and clears and invalidates the keyring itself, without checking permissions. If the refresh is triggered by a task which inherited a session keyring that contains a keyring named "cert_store" not created by this code, this keyring and its keys are destroyed.

create_cs_keyring() always links the keyring into the user session keyring, so the first search is sufficient. Remove the request_key() fallback.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/s390/kernel/cert_store.c +2 -6
index ec6352528a2b..551b538ea930 100644
@@ -360,7 +360,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 
 static struct key *find_cs_keyring(void)
 {
-	struct key *cs_keyring, *us_keyring;
+	struct key *us_keyring;
 	key_ref_t cs_keyring_ref;
 
 	us_keyring = get_user_session_keyring();
@@ -373,11 +373,7 @@ static struct key *find_cs_keyring(void)
 		if (!IS_ERR(cs_keyring_ref))
 			return key_ref_to_ptr(cs_keyring_ref);
 	}
-	/* Search default locations: thread, process, session keyrings */
-	cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
-	if (IS_ERR(cs_keyring))
-		return NULL;
-	return cs_keyring;
+	return NULL;
 }
 
 static void cleanup_cs_keys(void)
Signature · 1 line
2.56.1

Br, Jarkko

maintainer
#35 lore raw

Re: [PATCH 12/15] s390/cert_store: Validate hash length in check_certificate_valid()

In reply to Heiko Carstens
Quoted 43 lines · Heiko Carstens

Validate that vc_hash_length is SHA256_DIGEST_SIZE before checking the hash and use sizeof(hash) in memcmp(). This avoids an out-of-bounds read if vc_hash_length is incorrect.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kernel/cert_store.c +5 -3
index 551b538ea930..ecfb7641d427 100644
@@ -261,13 +261,11 @@ static int diag320(unsigned long subcode, void *addr)
 static int check_certificate_hash(const struct vce *vce)
 {
 	u8 hash[SHA256_DIGEST_SIZE];
-	u16 vc_hash_length;
 	u8 *vce_hash;
 
 	vce_hash = (u8 *)vce + vce->vce_hdr.vc_hash_offset;
-	vc_hash_length = vce->vce_hdr.vc_hash_length;
 	sha256((u8 *)vce + vce->vce_hdr.vc_offset, vce->vce_hdr.vc_length, hash);
-	if (memcmp(vce_hash, hash, vc_hash_length) == 0)
+	if (memcmp(vce_hash, hash, sizeof(hash)) == 0)
 		return 0;
 
 	pr_dbf_msg("SHA256 hash of received certificate does not match");
@@ -293,6 +291,10 @@ static int check_certificate_valid(const struct vce *vce)
 		pr_dbf_msg("Hash type is not supported");
 		return -EINVAL;
 	}
+	if (vce->vce_hdr.vc_hash_length != SHA256_DIGEST_SIZE) {
+		pr_dbf_msg("Hash length is not supported");
+		return -EINVAL;
+	}
 
 	return check_certificate_hash(vce);
 }
Signature · 1 line
2.56.1

I'll stop here :-)

Br, Jarkko

maintainer Reviewed-by on v1 2/15
#5 lore raw

Re: [PATCH 02/15] s390/cert_store: Fix return code handling in invalidate_keyring_keys()

In reply to Heiko Carstens
Quoted 53 lines · Heiko Carstens

Add missing error handling in case key_type_keyring.read() returns an error.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/s390/kernel/cert_store.c +6 -4
index d35b1ea22507..89cc3a1683a3 100644
@@ -316,15 +316,16 @@ static struct key *get_user_session_keyring(void)
 static int invalidate_keyring_keys(struct key *keyring)
 {
 	unsigned long num_keys, key_index;
-	size_t keyring_payload_len;
+	long keyring_payload_len, rc;
 	key_serial_t *key_array;
 	struct key *current_key;
-	int rc;
 
 	scoped_guard(rwsem_read, &keyring->sem) {
 		keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
-		num_keys = keyring_payload_len / sizeof(key_serial_t);
+		if (keyring_payload_len <= 0)
+			return keyring_payload_len;
 
+		num_keys = keyring_payload_len / sizeof(key_serial_t);
 		key_array = kzalloc_objs(key_serial_t, num_keys);
 		if (!key_array)
 			return -ENOMEM;
@@ -334,6 +335,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 
 	if (rc != keyring_payload_len) {
 		pr_dbf_msg("Couldn't read keyring payload");
+		rc = rc < 0 ? rc : -EINVAL;
 		goto out;
 	}
 
@@ -345,7 +347,7 @@ static int invalidate_keyring_keys(struct key *keyring)
 		key_put(current_key);
 		rc = key_unlink(keyring, current_key);
 		if (rc) {
-			pr_dbf_msg("Couldn't unlink key %08x: %d", current_key->serial, rc);
+			pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
 			break;
 		}
 	}
Signature · 1 line
2.56.1

Br, Jarkko

author maintainer
#36 lore raw

Re: [PATCH 12/15] s390/cert_store: Validate hash length in check_certificate_valid()

In reply to Jarkko Sakkinen
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Heiko Carstens

Validate that vc_hash_length is SHA256_DIGEST_SIZE before checking the hash and use sizeof(hash) in memcmp(). This avoids an out-of-bounds read if vc_hash_length is incorrect.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

...

Quoted 1 line

I'll stop here :-)

Thanks for taking the time to review most of this series! So, I guess we will end up with this series going upstream via the s390 tree. This will result with a larger merge conflict with your "keyring_gc" branch, but that can be solved of course.

Is this the way we should proceed?

maintainer
#37 lore raw

Re: [PATCH 12/15] s390/cert_store: Validate hash length in check_certificate_valid()

Quoted 13 lines · Heiko Carstens
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Heiko Carstens

Validate that vc_hash_length is SHA256_DIGEST_SIZE before checking the hash and use sizeof(hash) in memcmp(). This avoids an out-of-bounds read if vc_hash_length is incorrect.

Fixes 8cf57d7217c3 ("s390: add support for user-defined certificates")
---
 arch/s390/kernel/cert_store.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

...

Quoted 8 lines
Quoted 1 line

I'll stop here :-)

Thanks for taking the time to review most of this series! So, I guess we will end up with this series going upstream via the s390 tree. This will result with a larger merge conflict with your "keyring_gc" branch, but that can be solved of course.

Is this the way we should proceed?

Fixes come first, so I'll just resolve whatever comes up.

Be, Jarkko