Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH] keys: fix crash on big_key read or revoke after a failed update
@ 2026-11-23 16:54 Henry Martin
  2026-11-23 17:04 ` sashiko-bot
  2026-11-23 19:12 ` Jarkko Sakkinen
  0 siblings, 2 replies; 3+ messages in thread
From: Henry Martin @ 2026-11-23 16:54 UTC (permalink / raw)
  To: dhowells, jarkko, paul, jmorris, serge, Jason
  Cc: keyrings, linux-security-module, Henry Martin, stable

big_key_update() destroys the old payload with big_key_destroy() before
calling generic_key_instantiate(), which can fail.  The destroy drops the
payload's path and data but keeps payload->length.  If instantiate fails,
e.g. with -EDQUOT when a concurrent allocation grabs the quota freed by
the destroy, the key stays positive with a gutted payload and a stale
length.

A later KEYCTL_READ on such a key follows the stale length.  For a
file-backed payload it hits BUG_ON(!path->mnt) in dentry_open().  For a
buffer-backed one it memcpy()s from a NULL payload->data.  KEYCTL_REVOKE
passes the NULL path to vfs_truncate().  All of these crash the kernel,
e.g. on mainline v7.4-rc4:

kernel BUG at fs/open.c:1145!
Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
RIP: 0010:dentry_open+0x7b/0x90
 big_key_read+0x141/0x280

Fix the ordering: instantiate the new payload first and destroy the old
one only on success.  There is no need to release the old quota first:
a big_key payload always reserves a fixed 16 bytes, which was already
charged when the key was instantiated, so the instantiate of a positive
key leaves the reservation unchanged and cannot fail.  Releasing it
first was what allowed a concurrent allocation to take the quota and
make the instantiate fail after the payload had been destroyed.

This issue was discovered by Tencent CodeBuddy Security.
Cc: stable@vger.kernel.org
Fixes: b6f61c314649 ("keys: Implement update for the big_key type")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 security/keys/big_key.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/security/keys/big_key.c b/security/keys/big_key.c
index 268f702df380e..3bf371f6c28d3 100644
--- a/security/keys/big_key.c
+++ b/security/keys/big_key.c
@@ -193,16 +193,31 @@ void big_key_destroy(struct key *key)
  */
 int big_key_update(struct key *key, struct key_preparsed_payload *prep)
 {
+	struct big_key_payload old = { };
+	bool positive = key_is_positive(key);
 	int ret;
 
-	ret = key_payload_reserve(key, prep->datalen);
+	if (positive)
+		old = *to_big_key_payload(key->payload);
+
+	/*
+	 * The quota for a big_key payload is a fixed 16 bytes that was
+	 * already reserved when the key was instantiated, so the instantiate
+	 * below does not change the reservation of a positive key.  Do not
+	 * release it first: a concurrent allocation could take the quota and
+	 * make the instantiate fail after the old payload was destroyed.
+	 */
+	ret = generic_key_instantiate(key, prep);
 	if (ret < 0)
 		return ret;
 
-	if (key_is_positive(key))
-		big_key_destroy(key);
+	if (positive) {
+		if (old.length > BIG_KEY_FILE_THRESHOLD)
+			path_put(&old.path);
+		kfree_sensitive(old.data);
+	}
 
-	return generic_key_instantiate(key, prep);
+	return 0;
 }
 
 /*
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] keys: fix crash on big_key read or revoke after a failed update
  2026-11-23 16:54 [PATCH] keys: fix crash on big_key read or revoke after a failed update Henry Martin
@ 2026-11-23 17:04 ` sashiko-bot
  2026-11-23 19:12 ` Jarkko Sakkinen
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-23 17:04 UTC (permalink / raw)
  To: Henry Martin, dhowells, jarkko, paul, jmorris, serge, Jason
  Cc: keyrings, linux-security-module

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] keys: inaccurate comment and changelog on quota handling in
  big_key_update()
--

commit 2c08313937317377d7723fd6ab0f42836903865d
Author: Henry Martin <bsdhenrymartin@gmail.com>

keys: fix crash on big_key read or revoke after a failed update

This commit changes big_key_update() to instantiate the new key payload
before releasing the old payload's path and data, and removes the prior
key_payload_reserve(key, prep->datalen) call.

> diff --git a/security/keys/big_key.c b/security/keys/big_key.c
> index 268f702df380e..3bf371f6c28d3 100644
> --- a/security/keys/big_key.c
> +++ b/security/keys/big_key.c
> @@ -193,16 +193,31 @@ void big_key_destroy(struct key *key)
>   */
>  int big_key_update(struct key *key, struct key_preparsed_payload *prep)
>  {
> +	struct big_key_payload old = { };
> +	bool positive = key_is_positive(key);
>  	int ret;
>  
> -	ret = key_payload_reserve(key, prep->datalen);
> +	if (positive)
> +		old = *to_big_key_payload(key->payload);
> +
> +	/*
> +	 * The quota for a big_key payload is a fixed 16 bytes that was
> +	 * already reserved when the key was instantiated, so the instantiate
> +	 * below does not change the reservation of a positive key.  Do not
> +	 * release it first: a concurrent allocation could take the quota and
> +	 * make the instantiate fail after the old payload was destroyed.
> +	 */
> +	ret = generic_key_instantiate(key, prep);

[Severity: Low]
Are this comment and the commit message accurate about what altered the
quota reservation before generic_key_instantiate()?

big_key_destroy() only drops payload->path and payload->data and never
touches key quota.  What changed the reservation was the removed
key_payload_reserve(key, prep->datalen) call, which passed the raw payload
length prep->datalen instead of prep->quotalen (16 bytes).  When
prep->datalen < 16 (or when prep->datalen >= 65536 and truncates to an
unsigned short key->datalen < 16), key_payload_reserve() left key->datalen
below 16, causing the subsequent key_payload_reserve(key, prep->quotalen)
inside generic_key_instantiate() to charge 16 - key->datalen bytes and
potentially fail with -EDQUOT after big_key_destroy() had already run.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123165443.4092852-1-bsdhenrymartin@gmail.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] keys: fix crash on big_key read or revoke after a failed update
  2026-11-23 16:54 [PATCH] keys: fix crash on big_key read or revoke after a failed update Henry Martin
  2026-11-23 17:04 ` sashiko-bot
@ 2026-11-23 19:12 ` Jarkko Sakkinen
  1 sibling, 0 replies; 3+ messages in thread
From: Jarkko Sakkinen @ 2026-11-23 19:12 UTC (permalink / raw)
  To: Henry Martin
  Cc: dhowells, paul, jmorris, serge, Jason, keyrings,
	linux-security-module, stable

On Tue, Nov 24, 2026 at 12:54:43AM +0800, Henry Martin wrote:
> big_key_update() destroys the old payload with big_key_destroy() before
> calling generic_key_instantiate(), which can fail.  The destroy drops the
> payload's path and data but keeps payload->length.  If instantiate fails,
> e.g. with -EDQUOT when a concurrent allocation grabs the quota freed by
> the destroy, the key stays positive with a gutted payload and a stale
> length.
> 
> A later KEYCTL_READ on such a key follows the stale length.  For a
> file-backed payload it hits BUG_ON(!path->mnt) in dentry_open().  For a
> buffer-backed one it memcpy()s from a NULL payload->data.  KEYCTL_REVOKE
> passes the NULL path to vfs_truncate().  All of these crash the kernel,
> e.g. on mainline v7.4-rc4:
> 
> kernel BUG at fs/open.c:1145!
> Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
> RIP: 0010:dentry_open+0x7b/0x90
>  big_key_read+0x141/0x280
> 
> Fix the ordering: instantiate the new payload first and destroy the old
> one only on success.  There is no need to release the old quota first:
> a big_key payload always reserves a fixed 16 bytes, which was already
> charged when the key was instantiated, so the instantiate of a positive
> key leaves the reservation unchanged and cannot fail.  Releasing it
> first was what allowed a concurrent allocation to take the quota and
> make the instantiate fail after the payload had been destroyed.
> 
> This issue was discovered by Tencent CodeBuddy Security.
> Cc: stable@vger.kernel.org
> Fixes: b6f61c314649 ("keys: Implement update for the big_key type")
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---
>  security/keys/big_key.c | 23 +++++++++++++++++++----
>  1 file changed, 19 insertions(+), 4 deletions(-)
> 
> diff --git a/security/keys/big_key.c b/security/keys/big_key.c
> index 268f702df380e..3bf371f6c28d3 100644
> --- a/security/keys/big_key.c
> +++ b/security/keys/big_key.c
> @@ -193,16 +193,31 @@ void big_key_destroy(struct key *key)
>   */
>  int big_key_update(struct key *key, struct key_preparsed_payload *prep)
>  {
> +	struct big_key_payload old = { };
> +	bool positive = key_is_positive(key);
>  	int ret;
>  
> -	ret = key_payload_reserve(key, prep->datalen);
> +	if (positive)
> +		old = *to_big_key_payload(key->payload);
> +
> +	/*
> +	 * The quota for a big_key payload is a fixed 16 bytes that was
> +	 * already reserved when the key was instantiated, so the instantiate
> +	 * below does not change the reservation of a positive key.  Do not
> +	 * release it first: a concurrent allocation could take the quota and
> +	 * make the instantiate fail after the old payload was destroyed.
> +	 */

I'd leave the comment out. It's very time variant and commit specific.

> +	ret = generic_key_instantiate(key, prep);
>  	if (ret < 0)
>  		return ret;
>  
> -	if (key_is_positive(key))
> -		big_key_destroy(key);
> +	if (positive) {
> +		if (old.length > BIG_KEY_FILE_THRESHOLD)
> +			path_put(&old.path);
> +		kfree_sensitive(old.data);
> +	}
>  
> -	return generic_key_instantiate(key, prep);
> +	return 0;
>  }
>  
>  /*
> -- 
> 2.53.0
> 

I did not specifically check the Sashiko output.

Br, Jarkko

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-23 19:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 16:54 [PATCH] keys: fix crash on big_key read or revoke after a failed update Henry Martin
2026-11-23 17:04 ` sashiko-bot
2026-11-23 19:12 ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox