* [PATCH v2 0/2] KEYS: two fixes for the asymmetric keyctl operations
@ 2026-11-18 23:19 Florian Bauckholt via B4 Relay
2026-11-18 23:19 ` [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY Florian Bauckholt via B4 Relay
2026-11-18 23:19 ` [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message Florian Bauckholt via B4 Relay
0 siblings, 2 replies; 8+ messages in thread
From: Florian Bauckholt via B4 Relay @ 2026-11-18 23:19 UTC (permalink / raw)
To: David Howells, Jarkko Sakkinen, Lukas Wunner, Ignat Korchagin,
Herbert Xu, David S. Miller
Cc: Paul Moore, James Morris, Serge E. Hallyn, Hui Peng,
Stefan Berger, Rodolfo Giometti, keyrings, linux-crypto,
linux-security-module, linux-kernel, stable, Florian Bauckholt
Hi,
two small fixes for KEYCTL_PKEY_VERIFY and KEYCTL_PKEY_DECRYPT that I
ran into while doing differential testing of crypto implementations
with the kernel hooked up via UML:
Patch 1 fixes a regression from commit cb481e59ea6c ("KEYS: fix overflow
in keyctl_pkey_params_get_2()"), which was tagged for stable:
KEYCTL_PKEY_VERIFY ignores the signature length passed by the caller.
It also is a prerequisite for making the x962 ECDSA template reject
signatures that are not DER-encoded, which I posted separately to
linux-crypto [2].
Patch 2 fixes PKCS#1 decryption of an empty message returning the
ciphertext.
Both changes were part of a patch posted by Hui Peng in September that
did not get a reply [1]. That patch also changed the out_len checks in
keyctl_pkey_params_get_2(), which is not included here.
Thanks!
[1] https://lore.kernel.org/all/20260919221742.3709658-1-benquike@gmail.com/
[2] https://lore.kernel.org/all/20261112-ecdsa-x962-der-v1-1-36b24c5f5e11@cispa.de/
---
Changes in v2:
- Rebased onto jarkko/linux-tpmdd.git for-next-keys (141b164bbe18).
Patch 1 no longer applied after commit bab48a104447 ("KEYS: Protect
pkey method checks against type removal"). The code change itself is
the same.
- Patch 1: the commit message now mentions that a truncated RSA
signature can be accepted (Rodolfo Giometti).
- Patch 2: unchanged.
- Link to v1: https://patch.msgid.link/20261111-keyctl-pkey-fixes-v1-0-934ebaa267a7@cispa.de
---
Florian Bauckholt (2):
KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY
KEYS: asymmetric: Fix PKCS#1 decryption of an empty message
crypto/asymmetric_keys/public_key.c | 6 +++---
security/keys/keyctl_pkey.c | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
---
base-commit: 141b164bbe186423e295b139e40e8e2d11cf1341
change-id: 20261111-keyctl-pkey-fixes-26358a8707b6
Best regards,
--
Florian Bauckholt <florian.bauckholt@cispa.de>
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY 2026-11-18 23:19 [PATCH v2 0/2] KEYS: two fixes for the asymmetric keyctl operations Florian Bauckholt via B4 Relay @ 2026-11-18 23:19 ` Florian Bauckholt via B4 Relay 2026-11-18 23:24 ` sashiko-bot ` (2 more replies) 2026-11-18 23:19 ` [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message Florian Bauckholt via B4 Relay 1 sibling, 3 replies; 8+ messages in thread From: Florian Bauckholt via B4 Relay @ 2026-11-18 23:19 UTC (permalink / raw) To: David Howells, Jarkko Sakkinen, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S. Miller Cc: Paul Moore, James Morris, Serge E. Hallyn, Hui Peng, Stefan Berger, Rodolfo Giometti, keyrings, linux-crypto, linux-security-module, linux-kernel, stable, Florian Bauckholt From: Florian Bauckholt <florian.bauckholt@cispa.de> Commit cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") made keyctl_pkey_params_get_2() set params->out_len to the maximum output size of the operation instead of the size provided by the caller. It does so for KEYCTL_PKEY_VERIFY as well, but verify has no output buffer: out_len shares a union with in2_len, the length of the signature. As a result keyctl_pkey_verify() ignores the signature length passed by the caller and always reads max_sig_size bytes from the signature pointer. A shorter signature is extended with whatever follows it in the caller's memory, or the call fails with -EFAULT if that memory is not readable. The x962 ECDSA template currently ignores data following a DER-encoded signature, which hides the problem there. A truncated RSA signature, however, is accepted if the bytes following it happen to match the ones that were cut off, as reported by Rodolfo Giometti [1]. Use the length provided by the caller, which has already been checked against max_sig_size. Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/20260919221742.3709658-1-benquike@gmail.com/ Link: https://lore.kernel.org/all/0e20f799-e389-40d2-aaee-2f174748faae@enneenne.com/ [1] Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Tested-by: Stefan Berger <stefanb@linux.ibm.com> Signed-off-by: Florian Bauckholt <florian.bauckholt@cispa.de> --- 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 e7121523b9c9..722521a014ab 100644 --- a/security/keys/keyctl_pkey.c +++ b/security/keys/keyctl_pkey.c @@ -175,7 +175,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.56.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY 2026-11-18 23:19 ` [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY Florian Bauckholt via B4 Relay @ 2026-11-18 23:24 ` sashiko-bot 2026-11-19 7:50 ` Guicai Ma 2026-11-19 10:25 ` Rodolfo Giometti 2 siblings, 0 replies; 8+ messages in thread From: sashiko-bot @ 2026-11-18 23:24 UTC (permalink / raw) Cc: linux-security-module > From: Florian Bauckholt <florian.bauckholt@cispa.de> > > Commit cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") > made keyctl_pkey_params_get_2() set params->out_len to the maximum > output size of the operation instead of the size provided by the caller. > It does so for KEYCTL_PKEY_VERIFY as well, but verify has no output > buffer: out_len shares a union with in2_len, the length of the > signature. > > As a result keyctl_pkey_verify() ignores the signature length passed by > the caller and always reads max_sig_size bytes from the signature > pointer. A shorter signature is extended with whatever follows it in > the caller's memory, or the call fails with -EFAULT if that memory is > not readable. > > The x962 ECDSA template currently ignores data following a DER-encoded > signature, which hides the problem there. A truncated RSA signature, > however, is accepted if the bytes following it happen to match the ones > that were cut off, as reported by Rodolfo Giometti [1]. > > Use the length provided by the caller, which has already been checked > against max_sig_size. > > Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") > Cc: stable@vger.kernel.org > Link: https://lore.kernel.org/all/20260919221742.3709658-1-benquike@gmail.com/ > Link: https://lore.kernel.org/all/0e20f799-e389-40d2-aaee-2f174748faae@enneenne.com/ [1] > Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> > Tested-by: Stefan Berger <stefanb@linux.ibm.com> > Signed-off-by: Florian Bauckholt <florian.bauckholt@cispa.de> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261119-keyctl-pkey-fixes-v2-0-9e2d1c7db7ec@cispa.de?part=1 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY 2026-11-18 23:19 ` [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY Florian Bauckholt via B4 Relay 2026-11-18 23:24 ` sashiko-bot @ 2026-11-19 7:50 ` Guicai Ma 2026-11-19 10:25 ` Rodolfo Giometti 2 siblings, 0 replies; 8+ messages in thread From: Guicai Ma @ 2026-11-19 7:50 UTC (permalink / raw) To: Florian Bauckholt, David Howells, Jarkko Sakkinen, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S . Miller Cc: Paul Moore, James Morris, Serge E . Hallyn, Hui Peng, Stefan Berger, Rodolfo Giometti, keyrings, linux-crypto, linux-security-module, linux-kernel, Guicai Ma On Wed, Nov 18, 2026 at 11:19:28PM +0000, Florian Bauckholt wrote: > As a result keyctl_pkey_verify() ignores the signature length passed by > the caller and always reads max_sig_size bytes from the signature > pointer. A shorter signature is extended with whatever follows it in > the caller's memory, or the call fails with -EFAULT if that memory is > not readable. Verified the union usage in struct kernel_pkey_params and reproduced both failure modes on arm64 with an RSA-2048 PKCS#8 key (enc=pkcs1 hash=sha256): - A truncated 200-byte signature with the remaining 56 bytes still intact in userspace memory was falsely accepted (0) before this patch, and is now rejected with -EINVAL. - A 16-byte signature placed right before a PROT_NONE guard page (as well as a 0-byte signature at the page boundary) returned -EFAULT before this patch, and now returns -EINVAL without faulting. Reviewed-by: Guicai Ma <maguicai@kylinos.cn> Tested-by: Guicai Ma <maguicai@kylinos.cn> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY 2026-11-18 23:19 ` [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY Florian Bauckholt via B4 Relay 2026-11-18 23:24 ` sashiko-bot 2026-11-19 7:50 ` Guicai Ma @ 2026-11-19 10:25 ` Rodolfo Giometti 2 siblings, 0 replies; 8+ messages in thread From: Rodolfo Giometti @ 2026-11-19 10:25 UTC (permalink / raw) To: florian.bauckholt, David Howells, Jarkko Sakkinen, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S. Miller Cc: Paul Moore, James Morris, Serge E. Hallyn, Hui Peng, Stefan Berger, keyrings, linux-crypto, linux-security-module, linux-kernel, stable On 19/11/2026 00:19, Florian Bauckholt via B4 Relay wrote: > From: Florian Bauckholt <florian.bauckholt@cispa.de> > > Commit cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") > made keyctl_pkey_params_get_2() set params->out_len to the maximum > output size of the operation instead of the size provided by the caller. > It does so for KEYCTL_PKEY_VERIFY as well, but verify has no output > buffer: out_len shares a union with in2_len, the length of the > signature. > > As a result keyctl_pkey_verify() ignores the signature length passed by > the caller and always reads max_sig_size bytes from the signature > pointer. A shorter signature is extended with whatever follows it in > the caller's memory, or the call fails with -EFAULT if that memory is > not readable. > > The x962 ECDSA template currently ignores data following a DER-encoded > signature, which hides the problem there. A truncated RSA signature, > however, is accepted if the bytes following it happen to match the ones > that were cut off, as reported by Rodolfo Giometti [1]. > > Use the length provided by the caller, which has already been checked > against max_sig_size. > > Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") > Cc: stable@vger.kernel.org > Link: https://lore.kernel.org/all/20260919221742.3709658-1-benquike@gmail.com/ > Link: https://lore.kernel.org/all/0e20f799-e389-40d2-aaee-2f174748faae@enneenne.com/ [1] > Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> > Tested-by: Stefan Berger <stefanb@linux.ibm.com> > Signed-off-by: Florian Bauckholt <florian.bauckholt@cispa.de> > --- > 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 e7121523b9c9..722521a014ab 100644 > --- a/security/keys/keyctl_pkey.c > +++ b/security/keys/keyctl_pkey.c > @@ -175,7 +175,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; > I ran test3008 of craf 2000 times on a UML kernel of current mainline (666539eecdf4) with this change applied: no failures, against 4 out of 2000 without it. Tested-by: Rodolfo Giometti <giometti@enneenne.com> Ciao, Rodolfo -- GNU/Linux Solutions e-mail: giometti@enneenne.com Linux Device Driver giometti@linux.it Embedded Systems phone: +39 349 2432127 UNIX programming ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message 2026-11-18 23:19 [PATCH v2 0/2] KEYS: two fixes for the asymmetric keyctl operations Florian Bauckholt via B4 Relay 2026-11-18 23:19 ` [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY Florian Bauckholt via B4 Relay @ 2026-11-18 23:19 ` Florian Bauckholt via B4 Relay 2026-11-18 23:25 ` sashiko-bot 2026-11-19 7:50 ` Guicai Ma 1 sibling, 2 replies; 8+ messages in thread From: Florian Bauckholt via B4 Relay @ 2026-11-18 23:19 UTC (permalink / raw) To: David Howells, Jarkko Sakkinen, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S. Miller Cc: Paul Moore, James Morris, Serge E. Hallyn, Hui Peng, Stefan Berger, Rodolfo Giometti, keyrings, linux-crypto, linux-security-module, linux-kernel, stable, Florian Bauckholt From: Florian Bauckholt <florian.bauckholt@cispa.de> KEYCTL_PKEY_DECRYPT with enc=pkcs1 on a valid ciphertext of an empty message returns the key size instead of 0, and the output buffer is filled with the ciphertext. crypto_akcipher_sync_encrypt() returns 0 on success, so software_key_eds_op() maps a result of 0 to crypto_akcipher_maxsize(). The mapping is applied to decryption as well, but crypto_akcipher_sync_decrypt() returns the length of the plaintext, which is 0 for an empty message. By then crypto_akcipher_sync_post() has copied its scratch buffer to the output. pkcs1pad copied nothing into that buffer, so it still holds the ciphertext. Only the caller's own input is returned, so nothing is disclosed, and messages that are not empty are decrypted correctly. An empty message is valid, though: RFC 8017 section 7.2 only limits the message length to k - 11 octets. Apply the mapping to encryption only. Fixes: 63ba4d67594a ("KEYS: asymmetric: Use new crypto interface without scatterlists") Cc: stable@vger.kernel.org # v6.5+ Link: https://lore.kernel.org/all/20260919221742.3709658-1-benquike@gmail.com/ Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Florian Bauckholt <florian.bauckholt@cispa.de> --- crypto/asymmetric_keys/public_key.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c index 09a0b83d5d77..d71747d93b2c 100644 --- a/crypto/asymmetric_keys/public_key.c +++ b/crypto/asymmetric_keys/public_key.c @@ -341,6 +341,9 @@ static int software_key_eds_op(struct kernel_pkey_params *params, break; ret = crypto_akcipher_sync_encrypt(tfm, in, params->in_len, out, params->out_len); + /* Returns 0 on success and always fills the whole output. */ + if (ret == 0) + ret = crypto_akcipher_maxsize(tfm); break; case kernel_pkey_decrypt: if (issig) @@ -358,9 +361,6 @@ static int software_key_eds_op(struct kernel_pkey_params *params, BUG(); } - if (!issig && ret == 0) - ret = crypto_akcipher_maxsize(tfm); - error_free_tfm: if (issig) crypto_free_sig(sig); -- 2.56.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message 2026-11-18 23:19 ` [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message Florian Bauckholt via B4 Relay @ 2026-11-18 23:25 ` sashiko-bot 2026-11-19 7:50 ` Guicai Ma 1 sibling, 0 replies; 8+ messages in thread From: sashiko-bot @ 2026-11-18 23:25 UTC (permalink / raw) Cc: linux-security-module > From: Florian Bauckholt <florian.bauckholt@cispa.de> > > KEYCTL_PKEY_DECRYPT with enc=pkcs1 on a valid ciphertext of an empty > message returns the key size instead of 0, and the output buffer is > filled with the ciphertext. > > crypto_akcipher_sync_encrypt() returns 0 on success, so > software_key_eds_op() maps a result of 0 to crypto_akcipher_maxsize(). > The mapping is applied to decryption as well, but > crypto_akcipher_sync_decrypt() returns the length of the plaintext, > which is 0 for an empty message. By then crypto_akcipher_sync_post() > has copied its scratch buffer to the output. pkcs1pad copied nothing > into that buffer, so it still holds the ciphertext. > > Only the caller's own input is returned, so nothing is disclosed, and > messages that are not empty are decrypted correctly. An empty message > is valid, though: RFC 8017 section 7.2 only limits the message length > to k - 11 octets. > > Apply the mapping to encryption only. > > Fixes: 63ba4d67594a ("KEYS: asymmetric: Use new crypto interface without scatterlists") > Cc: stable@vger.kernel.org # v6.5+ > Link: https://lore.kernel.org/all/20260919221742.3709658-1-benquike@gmail.com/ > Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> > Signed-off-by: Florian Bauckholt <florian.bauckholt@cispa.de> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261119-keyctl-pkey-fixes-v2-0-9e2d1c7db7ec@cispa.de?part=2 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message 2026-11-18 23:19 ` [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message Florian Bauckholt via B4 Relay 2026-11-18 23:25 ` sashiko-bot @ 2026-11-19 7:50 ` Guicai Ma 1 sibling, 0 replies; 8+ messages in thread From: Guicai Ma @ 2026-11-19 7:50 UTC (permalink / raw) To: Florian Bauckholt, David Howells, Jarkko Sakkinen, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S . Miller Cc: Paul Moore, James Morris, Serge E . Hallyn, Hui Peng, Stefan Berger, Rodolfo Giometti, keyrings, linux-crypto, linux-security-module, linux-kernel, Guicai Ma On Wed, Nov 18, 2026 at 11:19:29PM +0000, Florian Bauckholt wrote: > case kernel_pkey_encrypt: > if (issig) > break; > ret = crypto_akcipher_sync_encrypt(tfm, in, params->in_len, > out, params->out_len); > + /* Returns 0 on success and always fills the whole output. */ > + if (ret == 0) > + ret = crypto_akcipher_maxsize(tfm); > break; Checked both the pkcs1pad and raw RSA (mpi_write_to_sgl()) paths for !issig, and tested with an RSA-2048 PKCS#8 key on arm64: - KEYCTL_PKEY_DECRYPT (enc=pkcs1) on a valid ciphertext of an empty message returned 256 and copied the ciphertext back to userspace before this patch; with the patch applied it returns 0 and leaves the userspace output buffer untouched. - Non-empty PKCS#1 and raw RSA (enc=raw) encrypt/decrypt roundtrips both continue to work as expected. Reviewed-by: Guicai Ma <maguicai@kylinos.cn> Tested-by: Guicai Ma <maguicai@kylinos.cn> ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-19 10:25 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-18 23:19 [PATCH v2 0/2] KEYS: two fixes for the asymmetric keyctl operations Florian Bauckholt via B4 Relay 2026-11-18 23:19 ` [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY Florian Bauckholt via B4 Relay 2026-11-18 23:24 ` sashiko-bot 2026-11-19 7:50 ` Guicai Ma 2026-11-19 10:25 ` Rodolfo Giometti 2026-11-18 23:19 ` [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message Florian Bauckholt via B4 Relay 2026-11-18 23:25 ` sashiko-bot 2026-11-19 7:50 ` Guicai Ma
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox