Skip to contentSkip to conversation

KEYS: keyctl overflow fixes

PATCH In review Jarkko Sakkinen 3 participants 8 messages Nov 23 – Nov 24

Subsystems (3)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

cover KEYS: keyctl overflow fixes

Cc David Howells <[email protected]>
Cc Guicai Ma <[email protected]>
Cc James Morris <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Paul Moore <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>

Guicai Ma (1):
KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()

Jarkko Sakkinen (1):
KEYS: Fix asym key overflow in keyctl_pkey_verify()

 security/keys/keyctl_pkey.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

--
2.47.3

author maintainer
#1 lore raw

PATCH 1/2 KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()

From: Guicai Ma <[email protected]>

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.

Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
---
 security/keys/keyctl_pkey.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
security/keys/keyctl_pkey.c +12 -3
index e7121523b9c9..b5f82804b5ad 100644
@@ -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;
 
Signature · 1 line
2.47.3
author maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()

Quoted 87 lines · Jarkko Sakkinen

From: Guicai Ma <[email protected]>

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.

Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
---
 security/keys/keyctl_pkey.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
security/keys/keyctl_pkey.c +12 -3
index e7121523b9c9..b5f82804b5ad 100644
@@ -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;
 
Signature · 1 line
2.47.3

Br, Jarkko

#3 lore raw

Re: [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()

In reply to Jarkko Sakkinen
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: - [High] KEYS: sensitive plaintext leaked to…

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:
- [High] KEYS: sensitive plaintext leaked to slab in keyctl_pkey_e_d_s()
[1]
--

commit e1794f1dd0090bbea27485536c98f990c8a30f19
Author: Guicai Ma <[email protected]>

KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()

This commit passes the caller's output buffer length back from keyctl_pkey_params_get_2() to keyctl_pkey_e_d_s() and returns -EOVERFLOW if the operation output exceeds it before copying to userspace.

Quoted 4 lines
security/keys/keyctl_pkey.c +0 -0
index e7121523b9c9a..b5f82804b5ad1 100644

[ ... ]

security/keys/keyctl_pkey.c long keyctl_pkey_e_d_s(int op, · line 323

Quoted 10 lines
security/keys/keyctl_pkey.c +4 -0
@@ -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;

[Severity: High]
This problem wasn't introduced by this patch, but should the error_out and error_in paths in keyctl_pkey_e_d_s() use kfree_sensitive() instead of kfree() [1]?

security/keys/keyctl_pkey.c:keyctl_pkey_e_d_s() {
	...
error_out:
	kfree(out);
error_in:
	kfree(in);
	...
}

Because in and out hold plaintext or other cryptographic payloads for KEYCTL_PKEY_ENCRYPT, KEYCTL_PKEY_DECRYPT, and KEYCTL_PKEY_SIGN, freeing them with kfree() leaves sensitive material in the slab cache.

[1] https://sashiko.dev/#/bug/linux-4199b169-0b8d-4055-9a71-6b5b79e38eb3

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#4 lore raw

PATCH 2/2 KEYS: Fix asym key overflow in keyctl_pkey_verify()

In reply to Jarkko Sakkinen

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.

Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +1 -1
index b5f82804b5ad..662226576954 100644
@@ -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;
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

Re: [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify()

Quoted 30 lines · Jarkko Sakkinen

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.

Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")
---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +1 -1
index b5f82804b5ad..662226576954 100644
@@ -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;
Signature · 1 line
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

Reviewed-by Tested-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify()

In reply to Jarkko Sakkinen
Quoted 5 lines · Jarkko Sakkinen

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.

[...]

security/keys/keyctl_pkey.c static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par · line 182

Quoted 9 lines
security/keys/keyctl_pkey.c +1 -1
@@ -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;

Tested this series with an RSA-2048 key (enc=pkcs1 hash=sha256) on arm64. A truncated signature with the remaining bytes still present in userspace memory and a short signature placed right before an unmapped page now both fail with -EINVAL, and normal signature verification still passes.

#7 lore raw
In reply to Jarkko Sakkinen
Sashiko has finished reviewing this 2-patch series:

Sashiko has finished reviewing this 2-patch series:

- Patch 2: no issues found.
- Patch 1 ("KEYS: Fix userspace buffer overflow in keyctl_pkey_e..."): 1 related pre-existing bug noted

A detailed review has been sent as a separate reply to patch 1.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]