All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] keys: fix crash on big_key read or revoke after a failed update
@ 2026-11-24  2:57 Henry Martin
  2026-11-24  3:03 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Henry Martin @ 2026-11-24  2:57 UTC (permalink / raw)
  To: dhowells, jarkko, paul, jmorris, serge
  Cc: keyrings, linux-security-module, Henry Martin, stable

big_key_update() reserves quota with the raw payload length and then
destroys the old payload before instantiating the new one.  For a payload
of N < 16 bytes that reserve releases 16-N bytes, so the reserve of 16
done by generic_key_instantiate() charges them again and can fail with
-EDQUOT once the payload is already destroyed.  big_key_destroy() keeps
payload->length but clears path and data, so the key stays positive with
a stale length that KEYCTL_READ and KEYCTL_REVOKE then follow:

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

Instantiate the new payload first and destroy the old one only on success;
a positive key's reservation is already 16 bytes, so it cannot fail.

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>
---
Changes in v2:
- Drop the comment in big_key_update(); it belongs in the changelog.
- Rework the changelog per the review below: the quota is released by the
  removed key_payload_reserve() call, not by big_key_destroy(), which only
  drops path and data.

Link: https://sashiko.dev/#/patchset/20261123165443.4092852-1-bsdhenrymartin@gmail.com?part=1

 security/keys/big_key.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/security/keys/big_key.c b/security/keys/big_key.c
index 268f702df380e..aa3e82ac93ee2 100644
--- a/security/keys/big_key.c
+++ b/security/keys/big_key.c
@@ -193,16 +193,24 @@ 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);
+
+	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] 4+ messages in thread

* Re: [PATCH v2] keys: fix crash on big_key read or revoke after a failed update
  2026-11-24  2:57 [PATCH v2] keys: fix crash on big_key read or revoke after a failed update Henry Martin
@ 2026-11-24  3:03 ` sashiko-bot
  2026-11-24  9:25 ` Jarkko Sakkinen
  2026-11-24 11:20 ` Guicai Ma
  2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-24  3:03 UTC (permalink / raw)
  Cc: linux-security-module

> big_key_update() reserves quota with the raw payload length and then
> destroys the old payload before instantiating the new one.  For a payload
> of N < 16 bytes that reserve releases 16-N bytes, so the reserve of 16
> done by generic_key_instantiate() charges them again and can fail with
> -EDQUOT once the payload is already destroyed.  big_key_destroy() keeps
> payload->length but clears path and data, so the key stays positive with
> a stale length that KEYCTL_READ and KEYCTL_REVOKE then follow:
> 
> 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
> 
> Instantiate the new payload first and destroy the old one only on success;
> a positive key's reservation is already 16 bytes, so it cannot fail.
> 
> 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>

Sashiko has reviewed this patch and found no issues.

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

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

* Re: [PATCH v2] keys: fix crash on big_key read or revoke after a failed update
  2026-11-24  2:57 [PATCH v2] keys: fix crash on big_key read or revoke after a failed update Henry Martin
  2026-11-24  3:03 ` sashiko-bot
@ 2026-11-24  9:25 ` Jarkko Sakkinen
  2026-11-24 11:20 ` Guicai Ma
  2 siblings, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-11-24  9:25 UTC (permalink / raw)
  To: Henry Martin
  Cc: dhowells, paul, jmorris, serge, keyrings, linux-security-module,
	stable

On Tue, Nov 24, 2026 at 10:57:59AM +0800, Henry Martin wrote:
> big_key_update() reserves quota with the raw payload length and then
> destroys the old payload before instantiating the new one.  For a payload
> of N < 16 bytes that reserve releases 16-N bytes, so the reserve of 16
> done by generic_key_instantiate() charges them again and can fail with
> -EDQUOT once the payload is already destroyed.  big_key_destroy() keeps
> payload->length but clears path and data, so the key stays positive with
> a stale length that KEYCTL_READ and KEYCTL_REVOKE then follow:
> 
> 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
> 
> Instantiate the new payload first and destroy the old one only on success;
> a positive key's reservation is already 16 bytes, so it cannot fail.
> 
> 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>
> ---
> Changes in v2:
> - Drop the comment in big_key_update(); it belongs in the changelog.
> - Rework the changelog per the review below: the quota is released by the
>   removed key_payload_reserve() call, not by big_key_destroy(), which only
>   drops path and data.
> 
> Link: https://sashiko.dev/#/patchset/20261123165443.4092852-1-bsdhenrymartin@gmail.com?part=1
> 
>  security/keys/big_key.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/security/keys/big_key.c b/security/keys/big_key.c
> index 268f702df380e..aa3e82ac93ee2 100644
> --- a/security/keys/big_key.c
> +++ b/security/keys/big_key.c
> @@ -193,16 +193,24 @@ 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);
> +
> +	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
> 

LGTM, thank you.

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

Br, Jarkko

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

* Re: [PATCH v2] keys: fix crash on big_key read or revoke after a failed update
  2026-11-24  2:57 [PATCH v2] keys: fix crash on big_key read or revoke after a failed update Henry Martin
  2026-11-24  3:03 ` sashiko-bot
  2026-11-24  9:25 ` Jarkko Sakkinen
@ 2026-11-24 11:20 ` Guicai Ma
  2 siblings, 0 replies; 4+ messages in thread
From: Guicai Ma @ 2026-11-24 11:20 UTC (permalink / raw)
  To: Henry Martin
  Cc: David Howells, Jarkko Sakkinen, Paul Moore, James Morris,
	Serge E . Hallyn, keyrings, linux-security-module, Guicai Ma

On Tue, Nov 24, 2026 at 10:57:59AM +0800, Henry Martin wrote:
> Instantiate the new payload first and destroy the old one only on success;
> a positive key's reservation is already 16 bytes, so it cannot fail.

Looks good. Saving the old big_key_payload on the stack before calling
generic_key_instantiate() also keeps negative keys untouched if the
quota reserve fails, and the caller still frees prep via
big_key_free_preparse().

I tested this on 7.4.0-rc3 arm64 in QEMU (KASAN, lockdep,
CONFIG_BIG_KEYS=y). After creating a 4096-byte big_key and setting
root_maxbytes=1, a 1-byte KEYCTL_UPDATE fails with -EDQUOT on the
unpatched kernel and the next KEYCTL_READ hits the BUG at
fs/open.c:1145 in dentry_open(). With this patch the 1-byte update and
read succeed, as do file/buff update transitions and KEYCTL_REVOKE.

Reviewed-by: Guicai Ma <maguicai@kylinos.cn>
Tested-by: Guicai Ma <maguicai@kylinos.cn>

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

end of thread, other threads:[~2026-11-24 11:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  2:57 [PATCH v2] keys: fix crash on big_key read or revoke after a failed update Henry Martin
2026-11-24  3:03 ` sashiko-bot
2026-11-24  9:25 ` Jarkko Sakkinen
2026-11-24 11:20 ` Guicai Ma

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.