KEYS: keyctl overflow fixes
Subsystems (3)
Jarkko Sakkinen
maintainercover KEYS: keyctl overflow fixes
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
Jarkko Sakkinen
maintainerPATCH 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.
--- security/keys/keyctl_pkey.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
security/keys/keyctl_pkey.c +12 -3
@@ -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, ¶ms); + ret =keyctl_pkey_params_get_2 (_params, _info, op, ¶ms, + &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 , - ¶ms); + ¶ms,NULL );if (ret <0 ) goto out_free_params;
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH 2/2 KEYS: Fix asym key overflow in keyctl_pkey_verify()
In reply to Jarkko SakkinenWhile 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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 ;
Signature · 1 line
2.47.3
Re: [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()
In reply to Jarkko SakkinenQuoted 87 lines · Jarkko Sakkinen
From: Guicai Ma <[email protected]>
Commit
cb481e59ea6c("KEYS: fix overflow inkeyctl_pkey_params_get_2()") changedkeyctl_pkey_params_get_2()to setparams->out_lento the maximum output size of the operation instead of uparams.out_len, so that the kernel buffer allocated inkeyctl_pkey_e_d_s()is large enough.Because uparams.out_len is no longer kept,
keyctl_pkey_e_d_s()copies ret bytes to_outwithout checking whether the result fits in the caller's buffer. If a smallerout_lenwas passed,copy_to_user()writes past the end of_outinstead 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(), sinceKEYCTL_PKEY_DECRYPTwith enc=pkcs1 can return a plaintext shorter thanmax_dec_sizeinto a matching smaller buffer.Pass uparams.out_len back to
keyctl_pkey_e_d_s()and return-EOVERFLOWbeforecopy_to_user()if ret exceeds it.Cc [email protected] # v7.2+Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")Signed-off-by Guicai Ma <[email protected]>--- security/keys/keyctl_pkey.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)security/keys/keyctl_pkey.c +12
-3@@ -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, ¶ms); + ret =keyctl_pkey_params_get_2 (_params, _info, op, ¶ms, + &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 , - ¶ms); + ¶ms,NULL );if (ret <0 ) goto out_free_params;Signature · 1 line
2.47.3
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify()
In reply to Jarkko SakkinenQuoted 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_in2and passes trailing bytes to ->asym_verify_signature().Set
params->in2_lento uparams.in2_len in theKEYCTL_PKEY_VERIFYcase.Cc [email protected] # v7.2+Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")Signed-off-by Jarkko Sakkinen <[email protected]>--- security/keys/keyctl_pkey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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 ;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
[email protected]
AI reviewRe: [PATCH 1/2] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s()
In reply to Jarkko SakkinenThank 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
[ ... ]
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
[email protected]
AI reviewSashiko 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]
Re: [PATCH 2/2] KEYS: Fix asym key overflow in keyctl_pkey_verify()
In reply to Jarkko SakkinenQuoted 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_in2and passes trailing bytes to ->asym_verify_signature().Set
params->in2_lento uparams.in2_len in theKEYCTL_PKEY_VERIFYcase.
[...]
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.