Linux s390 Architecture development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Heiko Carstens" <hca@linux.ibm.com>,
	"Jarkko Sakkinen" <jarkko@kernel.org>,
	"Anastasia Eskova" <anastasia.eskova@ibm.com>
Cc: "Sven Schnelle" <svens@linux.ibm.com>,
	"Peter Oberparleiter" <oberpar@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>,
	keyrings@vger.kernel.org, linux-s390@vger.kernel.org
Subject: Re: [PATCH 05/15] s390/cert_store: Unlink before invalidate in invalidate_keyring_keys()
Date: Mon, 16 Nov 2026 17:53:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-217454@kernel.org> (raw)
In-Reply-To: <20261116143534.158375-6-hca@linux.ibm.com>

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 <hca@linux.ibm.com>

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.

> diff --git a/arch/s390/kernel/cert_store.c b/arch/s390/kernel/cert_store.c
> index 8eb32497ca24d..a69eccf6affd3 100644
> --- a/arch/s390/kernel/cert_store.c
> +++ b/arch/s390/kernel/cert_store.c
> @@ -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;

[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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116143534.158375-2-hca@linux.ibm.com?part=5

  reply	other threads:[~2026-11-16 17:53 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 14:35 [PATCH 00/15] s390/cert_store: Various fixes Heiko Carstens
2026-11-16 14:35 ` [PATCH 01/15] s390/cert_store: Hold keyring->sem while reading the keyring Heiko Carstens
2026-11-16 17:57   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 02/15] s390/cert_store: Fix return code handling in invalidate_keyring_keys() Heiko Carstens
2026-11-16 17:55   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 03/15] s390/cert_store: Fix potential use-after-free " Heiko Carstens
2026-11-16 17:08   ` Jarkko Sakkinen
2026-11-16 18:01   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 04/15] s390/cert_store: Check key_lookup() return code " Heiko Carstens
2026-11-16 17:09   ` Jarkko Sakkinen
2026-11-16 17:52   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 05/15] s390/cert_store: Unlink before invalidate " Heiko Carstens
2026-11-16 17:53   ` sashiko-bot [this message]
2026-11-16 14:35 ` [PATCH 06/15] s390/cert_store: Fix cs_keyring reference leak in fill_cs_keyring() Heiko Carstens
2026-11-16 17:57   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 07/15] s390/cert_store: Fix potential use-after-free caused by find_cs_keyring() Heiko Carstens
2026-11-16 18:00   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 08/15] s390/cert_store: Remove unnecessary static specifier in create_cs_keyring() Heiko Carstens
2026-11-16 17:48   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 09/15] s390/cert_store: Fix error handling of get_user_session_keyring() Heiko Carstens
2026-11-16 17:58   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 10/15] s390/cert_store: Fix keyring refcount handling in get_user_session_keyring() Heiko Carstens
2026-11-16 17:49   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 11/15] s390/cert_store: Do not search the process keyrings for the cert_store keyring Heiko Carstens
2026-11-16 17:53   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 12/15] s390/cert_store: Validate hash length in check_certificate_valid() Heiko Carstens
2026-11-16 17:51   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 13/15] s390/cert_store: Fix max_vcxe_length output in pr_dbf_vcssb() Heiko Carstens
2026-11-16 17:53   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 14/15] s390/cert_store: Fix out-of-bounds read in pr_dbf_vce() Heiko Carstens
2026-11-16 17:56   ` sashiko-bot
2026-11-16 14:35 ` [PATCH 15/15] s390/cert_store: Initialize ism array in query_diag320_subcodes() Heiko Carstens
2026-11-16 17:55   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-217454@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=anastasia.eskova@ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=jarkko@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=oberpar@linux.ibm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=svens@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox