KEYS: asymmetric: fix OOB read in KEYCTL_PKEY_DECRYPT on zero-length message
Subsystems (3)
PATCH 1/1 KEYS: asymmetric: fix OOB read in KEYCTL_PKEY_DECRYPT on zero-length message
From: HanQuan <[email protected]>
In software_key_eds_op(), the condition
if (!issig && ret == 0)
ret = crypto_akcipher_maxsize(tfm);replaces ret with the key size when crypto_akcipher_sync_decrypt() returns 0. This was added as a workaround for encrypt, where crypto_akcipher_sync_encrypt() returns 0 on success (it lacks the "?:" data.dlen" tail that sync_decrypt has). However, for decrypt, ret == 0 is legitimate: pkcs1pad_decrypt_complete() sets req->dst_len = 0 for a zero-length PKCS#1 message, causing crypto_akcipher_sync_decrypt() to return 0 via "0 ?: data.dlen".
When ret is replaced with maxsize, the caller keyctl_pkey_e_d_s() does copy_to_user(_out, out, ret) with ret = key_size (e.g. 256 for RSA-2048) on a buffer allocated with kmalloc(params.out_len), which can be as small as 1 byte. This reads key_size - out_len bytes beyond the allocation.
Restrict the maxsize substitution to the encrypt operation only, where ret == 0 genuinely indicates success and the full key size is the correct output length.
--- crypto/asymmetric_keys/public_key.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/public_key.c +4 -1
@@ -358,7 +358,10 @@ static int software_key_eds_op(struct kernel_pkey_params *params,BUG (); } -if (!issig && ret ==0 ) +/* Decrypt may legitimately return 0 (zero-length message); only +* replace ret with maxsize for encrypt, which returns 0 on success. +*/ +if (!issig && ret ==0 && params->op == kernel_pkey_encrypt) ret =crypto_akcipher_maxsize (tfm); error_free_tfm:
Signature · 1 line
2.43.0
Lukas Wunner
maintainer[cc += Ignat, Jarkko, keyrings; start of thread is here:
https://lore.kernel.org/r/[email protected]
]
Quoted 5 lines · azraelxuemo
When ret is replaced with maxsize, the caller
keyctl_pkey_e_d_s()doescopy_to_user(_out, out, ret)with ret =key_size(e.g. 256 for RSA-2048) on a buffer allocated withkmalloc(params.out_len), which can be as small as 1 byte. This readskey_size-out_lenbytes beyond the allocation.
It would probably make sense to tighten security in keyctl_pkey_e_d_s() by using kzalloc() instead of kmalloc() and by capping the amount of data copied with min(ret, params.out_len).
Quoted 2 lines
Fixes 63ba4d67594a ("KEYS: asymmetric: Use new crypto interface without scatterlists")Signed-off-by HanQuan <[email protected]>
Please add:
You don't need to cc that address when submitting the patch, but including the tag in the commit message helps stable maintainers identify patches that need backporting.
crypto/asymmetric_keys/public_key.c static int software_key_eds_op(struct kernel_pkey_params *params, · line 365
Quoted 11 lines
+++
b/crypto/asymmetric_keys/public_key.ccrypto/asymmetric_keys/public_key.c +4
-1@@ -358,7 +358,10 @@ static int software_key_eds_op(struct kernel_pkey_params *params,BUG (); } -if (!issig && ret ==0 ) +/* Decrypt may legitimately return 0 (zero-length message); only +* replace ret with maxsize for encrypt, which returns 0 on success. +*/ +if (!issig && ret ==0 && params->op == kernel_pkey_encrypt) ret =crypto_akcipher_maxsize (tfm);
Given that out of 3 operations (encrypt, decrypt, sign),
2 already return the size, I think a better approach would be to let crypto_akcipher_sync_encrypt() return crypto_akcipher_maxsize() on success, i.e.:
return crypto_akcipher_sync_prep(&data) ?:
crypto_akcipher_sync_post(&data,
- crypto_akcipher_encrypt(data.req));
+ crypto_akcipher_encrypt(data.req)) ?:
+ crypto_akcipher_maxsize(tfm);
and then remove the if-clause in software_key_eds_op() altogether which overwrites ret with the maxsize.
Do you agree?
Your patch wasn't cc'ed to all maintainers of this file. Please double-check that you've checked out Linus' current master when running scripts/get_maintainer.pl.
Thanks,
Lukas
Jarkko Sakkinen
maintainerQuoted 54 lines · Lukas Wunner
[cc += Ignat, Jarkko, keyrings; start of thread is here:
https://lore.kernel.org/r/[email protected]
]Quoted 5 lines · azraelxuemo
When ret is replaced with maxsize, the caller
keyctl_pkey_e_d_s()doescopy_to_user(_out, out, ret)with ret =key_size(e.g. 256 for RSA-2048) on a buffer allocated withkmalloc(params.out_len), which can be as small as 1 byte. This readskey_size-out_lenbytes beyond the allocation.It would probably make sense to tighten security in
keyctl_pkey_e_d_s()by usingkzalloc()instead ofkmalloc()and by capping the amount of data copied withmin(ret, params.out_len).Quoted 2 lines
Fixes 63ba4d67594a ("KEYS: asymmetric: Use new crypto interface without scatterlists")Signed-off-by HanQuan <[email protected]>Please add:
Cc [email protected] # v6.5+You don't need to cc that address when submitting the patch, but including the tag in the commit message helps stable maintainers identify patches that need backporting.
crypto/asymmetric_keys/public_key.c static int software_key_eds_op(struct kernel_pkey_params *params, · line 365
Quoted 11 lines
+++
b/crypto/asymmetric_keys/public_key.ccrypto/asymmetric_keys/public_key.c +4
-1@@ -358,7 +358,10 @@ static int software_key_eds_op(struct kernel_pkey_params *params,BUG (); } -if (!issig && ret ==0 ) +/* Decrypt may legitimately return 0 (zero-length message); only +* replace ret with maxsize for encrypt, which returns 0 on success. +*/ +if (!issig && ret ==0 && params->op == kernel_pkey_encrypt) ret =crypto_akcipher_maxsize (tfm);Given that out of 3 operations (encrypt, decrypt, sign),
2 already return the size, I think a better approach would be to letcrypto_akcipher_sync_encrypt()returncrypto_akcipher_maxsize()on success, i.e.:return
crypto_akcipher_sync_prep(&data)?:
crypto_akcipher_sync_post(&data,(quoted diff) +2
-1- crypto_akcipher_encrypt(data.req)); + crypto_akcipher_encrypt(data.req)) ?: + crypto_akcipher_maxsize(tfm);and then remove the if-clause in
software_key_eds_op()altogether which overwrites ret with the maxsize.Do you agree?
Quoted 7 lines
Your patch wasn't cc'ed to all maintainers of this file. Please double-check that you've checked out Linus' current master when running
scripts/get_maintainer.pl.Thanks,
Lukas
Lukas, thank you for forwarding this, and with a quick sim I do.
Can this be next time be also CC'd to [email protected], in addition of me and rest? What I can then do is to look this in detail, test the change, apply and eventually forward to Linus...
BR, Jarkko