From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796381539; b=hCOmrwLBd0rBtWhAx2XsKyDKwu7zXmolbk1JrhubBUpwbYxrSJ8yeD0XHtefPNIQe/mr oFIDut6R/kqlJWR9UtqWD+qIEnV6t9Ib9DZyO82JrYafxHQXLxaE4lgtqTzriCY03VhG5 PxCv3RYD5o7HFwpkiMhX2Q6Fl/USJp1YDyXrbZ0aQYrRI/o0vExX9j6baJdfcGItgsa04 i0ugcAXfAMHrHK8XjKYIjErLjmIDTybN9v2wyQZZYr5QCILPp3vBlW46OJttkFgN2XDj/ oJMK425KyFOsEx2n85ZlfWIpue/+7V9Pnt+9yH5gc1g/GnLBaTHh2u3Pf6XFY9t+hJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381539; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=b41IDuiQWBuT7DwcgnxWm95vJshMI5KzLsvlGw5oCe8=; b=MdK1wTB9zbUJKiHXjhThlXvP/bZQtvxQEiDYPj7gyfgXtXOHiCYkB3/tT1qcapDvVXFc oTmDEHYIkToIlG7ubshs43KNQaYXWU5hfiifEYuFI0g1NtUA4B9Lq254oi8fPf32yV+XX uIDZD9q9yPgzp9oVIOv0FzL9xL2/Qq93mujI9ZlbTsgsx1Ibx668w4/8UTr+d0DktVYn+ Le7RqNYpZESNVL7uhQfpTghxfYwUYNpJcdVwHbyEr0UcevTwCM1QpMVn/JaQ8T/MddUtt oRu16b46V37b6UOojTrxzMFmYzeqHDGERN6AQ8IouITGqRsZduNkaQu8UmBsX9MCVGQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=hFSXXB6C; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00893606A33E; Fri, 4 Dec 2026 10:52:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=hFSXXB6C Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F0981F000CE; Fri, 4 Dec 2026 10:52:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381538; bh=b41IDuiQWBuT7DwcgnxWm95vJshMI5KzLsvlGw5oCe8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hFSXXB6C8j/gTZ0K+WA96lzOYFmn8pFV6P136omDqylIdDrXDwNjqfhPLjVgg99Bj 2LgaRt6IUs0a7l6xzcfMICpwBOP0CQlPFqImune5SeyBtD5nXV/huMfqR+r/QonFcU Rj86Kca9jZR9P74QHHN7TzXHwubot0i3+fXFiH0M= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sinan Turunc , Jarkko Sakkinen Subject: [PATCH 7.3 708/742] KEYS: serialise key instantiation against keyctl_chown_key() Date: Fri, 4 Dec 2026 19:09:32 +0900 Message-ID: <20261204100653.866647978@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sinan Turunc commit 899af9ffd2671351e5402d572934dd59b5e91ae5 upstream. keyctl_chown_key() looks up the target key by calling lookup_user_key() with the KEY_LOOKUP_PARTIAL flag. That flag lets a caller with CAP_SYS_ADMIN change the UID of an uninstantiated key during construction. When the UID changes, keyctl_chown_key() replaces key->user under key->sem and drops the old reference with key_user_put(). __key_instantiate_and_link() and key_reject_and_link() hold key_construction_mutex during instantiation, but neither function takes key->sem. Both functions increment key->user->nikeys, and __key_instantiate_and_link() also calls key->type->instantiate() without holding key->sem. When a key is in quota and its size changes, key_payload_reserve() reads key->user seven times without key->sem. Those reads include spin_lock_irqsave(&key->user->lock, flags) at the start and spin_unlock_irqrestore(&key->user->lock, flags) at the end. If keyctl_chown_key() swaps key->user between those two calls, key_payload_reserve() unlocks the new owner's spinlock and leaves the old spinlock held. By source inspection, key_user_put() can also kfree() the old struct key_user while instantiation still dereferences it. That struct key_user use-after-free was found by reading the code and did not trigger during our KASAN runs. ===================================== WARNING: bad unlock balance detected! 7.4.0-rc2-g23c62304dde2 #3 Not tainted ------------------------------------- request-key/163 is trying to release lock (&candidate->lock) at: [] key_payload_reserve+0x29f/0x370 but there are no more locks to release! other info that might help us debug this: locks held by request-key/163: 2, on CPU#3: #0: ffffffff871c10a0 (key_construction_mutex){+.+.}-{4:4}, at: __key_instantiate_and_link+0x38/0x370 #1: ffff88800de4a8b8 (&candidate->lock){..-.}-{3:3}, at: key_payload_reserve+0x160/0x370 Call Trace: lock_release+0x286/0x2e0 _raw_spin_unlock_irqrestore+0x1a/0x50 key_payload_reserve+0x29f/0x370 generic_key_instantiate+0x42/0x230 __key_instantiate_and_link+0xd7/0x370 key_instantiate_and_link+0x40d/0x4b0 keyctl_instantiate_key_common+0x2d2/0x450 keyctl_instantiate_key+0x167/0x180 __do_sys_keyctl+0x391/0x550 Take key->sem for write with down_write_nested(&key->sem, 2) inside key_construction_mutex in __key_instantiate_and_link() and key_reject_and_link(). Fixes: 5801649d8b83 ("[PATCH] keys: let keyctl_chown() change a key's owner") Cc: stable@vger.kernel.org Signed-off-by: Sinan Turunc Reviewed-by: Jarkko Sakkinen Link: https://lore.kernel.org/r/20261121111246.3478-1-enscrpt@pm.me Signed-off-by: Jarkko Sakkinen Signed-off-by: Greg Kroah-Hartman --- security/keys/key.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/security/keys/key.c +++ b/security/keys/key.c @@ -441,6 +441,7 @@ static int __key_instantiate_and_link(st ret = -EBUSY; mutex_lock(&key_construction_mutex); + down_write_nested(&key->sem, 2); /* can't instantiate twice */ if (key->state == KEY_IS_UNINSTANTIATED) { @@ -473,6 +474,7 @@ static int __key_instantiate_and_link(st } } + up_write(&key->sem); mutex_unlock(&key_construction_mutex); /* wake up anyone waiting for a key to be constructed */ @@ -609,6 +611,7 @@ int key_reject_and_link(struct key *key, } mutex_lock(&key_construction_mutex); + down_write_nested(&key->sem, 2); /* can't instantiate twice */ if (key->state == KEY_IS_UNINSTANTIATED) { @@ -632,6 +635,7 @@ int key_reject_and_link(struct key *key, key_invalidate(authkey); } + up_write(&key->sem); mutex_unlock(&key_construction_mutex); if (keyring && link_ret == 0)