Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()
       [not found] <20261123211959.4102708-1-jarkko@kernel.org>
@ 2026-11-23 21:19 ` Jarkko Sakkinen
  2026-11-23 21:25   ` Jarkko Sakkinen
  2026-11-23 21:19 ` [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify() Jarkko Sakkinen
  1 sibling, 1 reply; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-11-23 21:19 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, linux-kernel, linux-security-module,
	David Howells, Guicai Ma, James Morris, Paul Moore,
	Serge E. Hallyn, stable

From: Guicai Ma <maguicai@kylinos.cn>

Commit cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
changed keyctl_pkey_params_get_2() to set params->out_len to the maximum
output size of the operation instead of uparams.out_len, so that the
kernel buffer allocated in keyctl_pkey_e_d_s() is large enough.

Because uparams.out_len is no longer kept, keyctl_pkey_e_d_s() copies
ret bytes to _out without checking whether the result fits in the
caller's buffer. If a smaller out_len was passed, copy_to_user() writes
past the end of _out instead of failing with -EOVERFLOW.

uparams.out_len cannot simply be required to be at least the maximum
output size in keyctl_pkey_params_get_2(), since KEYCTL_PKEY_DECRYPT
with enc=pkcs1 can return a plaintext shorter than max_dec_size into a
matching smaller buffer.

Pass uparams.out_len back to keyctl_pkey_e_d_s() and return -EOVERFLOW
before copy_to_user() if ret exceeds it.

Cc: stable@vger.kernel.org # v7.2+
Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
Signed-off-by: Guicai Ma <maguicai@kylinos.cn>
---
 security/keys/keyctl_pkey.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/security/keys/keyctl_pkey.c b/security/keys/keyctl_pkey.c
index e7121523b9c9..b5f82804b5ad 100644
--- a/security/keys/keyctl_pkey.c
+++ b/security/keys/keyctl_pkey.c
@@ -113,7 +113,8 @@ static int keyctl_pkey_params_get(key_serial_t id,
 static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_params,
 				    const char __user *_info,
 				    int op,
-				    struct kernel_pkey_params *params)
+				    struct kernel_pkey_params *params,
+				    u32 *user_out_len)
 {
 	struct keyctl_pkey_params uparams;
 	struct kernel_pkey_query info;
@@ -183,6 +184,8 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 	}
 
 	params->in_len  = uparams.in_len;
+	if (user_out_len)
+		*user_out_len = uparams.out_len;
 
 	switch (op) {
 	case KEYCTL_PKEY_VERIFY:
@@ -264,10 +267,12 @@ long keyctl_pkey_e_d_s(int op,
 		       void __user *_out)
 {
 	struct kernel_pkey_params params;
+	u32 user_out_len;
 	void *in, *out;
 	long ret;
 
-	ret = keyctl_pkey_params_get_2(_params, _info, op, &params);
+	ret = keyctl_pkey_params_get_2(_params, _info, op, &params,
+				       &user_out_len);
 	if (ret < 0)
 		goto error_params;
 
@@ -310,6 +315,10 @@ long keyctl_pkey_e_d_s(int op,
 	if (ret < 0)
 		goto error_out;
 
+	if (ret > user_out_len) {
+		ret = -EOVERFLOW;
+		goto error_out;
+	}
 	if (copy_to_user(_out, out, ret) != 0)
 		ret = -EFAULT;
 
@@ -349,7 +358,7 @@ long keyctl_pkey_verify(const struct keyctl_pkey_params __user *_params,
 	long ret;
 
 	ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
-				       &params);
+				       &params, NULL);
 	if (ret < 0)
 		goto out_free_params;
 
-- 
2.47.3


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

* [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify()
       [not found] <20261123211959.4102708-1-jarkko@kernel.org>
  2026-11-23 21:19 ` [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s() Jarkko Sakkinen
@ 2026-11-23 21:19 ` Jarkko Sakkinen
  2026-11-23 21:26   ` Jarkko Sakkinen
  1 sibling, 1 reply; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-11-23 21:19 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, linux-kernel, linux-security-module,
	David Howells, Guicai Ma, James Morris, Paul Moore,
	Serge E. Hallyn, stable

While verifying a variable-length signature shorter than info.max_sig_size,
memdup_user() reads past the end of the caller's _in2 and passes trailing
bytes to ->asym_verify_signature().

Set params->in2_len to uparams.in2_len in the KEYCTL_PKEY_VERIFY case.

Cc: stable@vger.kernel.org # v7.2+
Closes: https://sashiko.dev/#/bug/linux-dd9ad2ca-a846-4d0d-89d6-aa262ffd40f1
Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/security/keys/keyctl_pkey.c b/security/keys/keyctl_pkey.c
index b5f82804b5ad..662226576954 100644
--- a/security/keys/keyctl_pkey.c
+++ b/security/keys/keyctl_pkey.c
@@ -176,7 +176,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 			goto out_up_key_sem;
 		}
 
-		params->out_len = info.max_sig_size;
+		params->in2_len = uparams.in2_len;
 		break;
 	default:
 		ret = -EOPNOTSUPP;
-- 
2.47.3


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

* Re: [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()
  2026-11-23 21:19 ` [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s() Jarkko Sakkinen
@ 2026-11-23 21:25   ` Jarkko Sakkinen
  0 siblings, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-11-23 21:25 UTC (permalink / raw)
  To: keyrings
  Cc: linux-kernel, linux-security-module, David Howells, Guicai Ma,
	James Morris, Paul Moore, Serge E. Hallyn, stable

On Mon, Nov 23, 2026 at 11:19:58PM +0200, Jarkko Sakkinen wrote:
> From: Guicai Ma <maguicai@kylinos.cn>
> 
> Commit cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
> changed keyctl_pkey_params_get_2() to set params->out_len to the maximum
> output size of the operation instead of uparams.out_len, so that the
> kernel buffer allocated in keyctl_pkey_e_d_s() is large enough.
> 
> Because uparams.out_len is no longer kept, keyctl_pkey_e_d_s() copies
> ret bytes to _out without checking whether the result fits in the
> caller's buffer. If a smaller out_len was passed, copy_to_user() writes
> past the end of _out instead of failing with -EOVERFLOW.
> 
> uparams.out_len cannot simply be required to be at least the maximum
> output size in keyctl_pkey_params_get_2(), since KEYCTL_PKEY_DECRYPT
> with enc=pkcs1 can return a plaintext shorter than max_dec_size into a
> matching smaller buffer.
> 
> Pass uparams.out_len back to keyctl_pkey_e_d_s() and return -EOVERFLOW
> before copy_to_user() if ret exceeds it.
> 
> Cc: stable@vger.kernel.org # v7.2+
> Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
> Signed-off-by: Guicai Ma <maguicai@kylinos.cn>
> ---
>  security/keys/keyctl_pkey.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/security/keys/keyctl_pkey.c b/security/keys/keyctl_pkey.c
> index e7121523b9c9..b5f82804b5ad 100644
> --- a/security/keys/keyctl_pkey.c
> +++ b/security/keys/keyctl_pkey.c
> @@ -113,7 +113,8 @@ static int keyctl_pkey_params_get(key_serial_t id,
>  static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_params,
>  				    const char __user *_info,
>  				    int op,
> -				    struct kernel_pkey_params *params)
> +				    struct kernel_pkey_params *params,
> +				    u32 *user_out_len)
>  {
>  	struct keyctl_pkey_params uparams;
>  	struct kernel_pkey_query info;
> @@ -183,6 +184,8 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
>  	}
>  
>  	params->in_len  = uparams.in_len;
> +	if (user_out_len)
> +		*user_out_len = uparams.out_len;
>  
>  	switch (op) {
>  	case KEYCTL_PKEY_VERIFY:
> @@ -264,10 +267,12 @@ long keyctl_pkey_e_d_s(int op,
>  		       void __user *_out)
>  {
>  	struct kernel_pkey_params params;
> +	u32 user_out_len;
>  	void *in, *out;
>  	long ret;
>  
> -	ret = keyctl_pkey_params_get_2(_params, _info, op, &params);
> +	ret = keyctl_pkey_params_get_2(_params, _info, op, &params,
> +				       &user_out_len);
>  	if (ret < 0)
>  		goto error_params;
>  
> @@ -310,6 +315,10 @@ long keyctl_pkey_e_d_s(int op,
>  	if (ret < 0)
>  		goto error_out;
>  
> +	if (ret > user_out_len) {
> +		ret = -EOVERFLOW;
> +		goto error_out;
> +	}
>  	if (copy_to_user(_out, out, ret) != 0)
>  		ret = -EFAULT;
>  
> @@ -349,7 +358,7 @@ long keyctl_pkey_verify(const struct keyctl_pkey_params __user *_params,
>  	long ret;
>  
>  	ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
> -				       &params);
> +				       &params, NULL);
>  	if (ret < 0)
>  		goto out_free_params;
>  
> -- 
> 2.47.3
> 


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

Br, Jarkko

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

* Re: [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify()
  2026-11-23 21:19 ` [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify() Jarkko Sakkinen
@ 2026-11-23 21:26   ` Jarkko Sakkinen
  0 siblings, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-11-23 21:26 UTC (permalink / raw)
  To: keyrings
  Cc: linux-kernel, linux-security-module, David Howells, Guicai Ma,
	James Morris, Paul Moore, Serge E. Hallyn, stable

On Mon, Nov 23, 2026 at 11:19:59PM +0200, Jarkko Sakkinen wrote:
> While verifying a variable-length signature shorter than info.max_sig_size,
> memdup_user() reads past the end of the caller's _in2 and passes trailing
> bytes to ->asym_verify_signature().
> 
> Set params->in2_len to uparams.in2_len in the KEYCTL_PKEY_VERIFY case.
> 
> Cc: stable@vger.kernel.org # v7.2+
> Closes: https://sashiko.dev/#/bug/linux-dd9ad2ca-a846-4d0d-89d6-aa262ffd40f1
> Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> ---
>  security/keys/keyctl_pkey.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/security/keys/keyctl_pkey.c b/security/keys/keyctl_pkey.c
> index b5f82804b5ad..662226576954 100644
> --- a/security/keys/keyctl_pkey.c
> +++ b/security/keys/keyctl_pkey.c
> @@ -176,7 +176,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
>  			goto out_up_key_sem;
>  		}
>  
> -		params->out_len = info.max_sig_size;
> +		params->in2_len = uparams.in2_len;
>  		break;
>  	default:
>  		ret = -EOPNOTSUPP;
> -- 
> 2.47.3
> 

I'll apply this with or without r-by's and/or t-by's but they will be appreciated.

Br, Jarkko

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261123211959.4102708-1-jarkko@kernel.org>
2026-11-23 21:19 ` [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s() Jarkko Sakkinen
2026-11-23 21:25   ` Jarkko Sakkinen
2026-11-23 21:19 ` [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify() Jarkko Sakkinen
2026-11-23 21:26   ` Jarkko Sakkinen

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