Skip to contentSkip to conversation

KEYS: two fixes for the asymmetric keyctl operations

PATCH In review Florian Bauckholt 5 participants 14 messages Nov 18 – Nov 24

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover KEYS: two fixes for the asymmetric keyctl operations

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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
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/[email protected]

---
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,

Signature · 1 line
Florian Bauckholt <[email protected]>
#1 lore raw

PATCH v2 1/2 KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

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.

---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +1 -1
index e7121523b9c9..722521a014ab 100644
@@ -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;
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Sashiko has reviewed this patch and found no issues.
Quoted 23 lines

From: Florian Bauckholt <[email protected]>

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.

Quoted 7 lines

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by Tested-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

In reply to Florian Bauckholt
Quoted 5 lines · Florian Bauckholt

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.

maintainer
#4 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Quoted 17 lines · Guicai Ma
Quoted 5 lines · Florian Bauckholt

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.

Quoted 2 lines

Thanks.

Br, Jarkko

Tested-by on v2 1/2
#5 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

In reply to Florian Bauckholt
Quoted 40 lines · On 19/11/2026 00:19, Florian Bauckholt via B4 Relay wrote:

From: Florian Bauckholt <[email protected]>

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.

---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +0 -0
index e7121523b9c9..722521a014ab 100644

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

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

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming
maintainer Reviewed-by
#6 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Quoted 54 lines · Rodolfo Giometti
Quoted 40 lines · On 19/11/2026 00:19, Florian Bauckholt via B4 Relay wrote:

From: Florian Bauckholt <[email protected]>

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.

---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +0 -0
index e7121523b9c9..722521a014ab 100644

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

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

Quoted 1 line

Thanks.

Quoted 10 lines

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming

Br, Jarkko

author Tested-by Stefan Berger Reviewed-by Guicai Ma Tested-by Guicai Ma Tested-by Rodolfo Giometti
#7 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Quoted 1 line · Jarkko Sakkinen

Hi,

I saw that for-next-keys now has 94cd21592349 ("KEYS: Fix asym key overflow in keyctl_pkey_verify()") with the same (trivial) patch. I'm not sure how this goes, but if you'd like to add some context, a link to this thread and these tags can probably be moved over:

https://lore.kernel.org/all/[email protected]/

Thanks,
Florian

maintainer
#8 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Quoted 12 lines · Florian Bauckholt
Quoted 1 line · Jarkko Sakkinen

Hi,

I saw that for-next-keys now has 94cd21592349 ("KEYS: Fix asym key overflow in keyctl_pkey_verify()") with the same (trivial) patch. I'm not sure how this goes, but if you'd like to add some context, a link to this thread and these tags can probably be moved over:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Thanks,
Florian

OK, so I pulled that by mistake as I was picking patch for keys 1/2; not asymmetric keys.

I'll drop it.

Br, Jarkko

maintainer
#9 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Florian Bauckholt
Quoted 1 line · Jarkko Sakkinen

Hi,

I saw that for-next-keys now has 94cd21592349 ("KEYS: Fix asym key overflow in keyctl_pkey_verify()") with the same (trivial) patch. I'm not sure how this goes, but if you'd like to add some context, a link to this thread and these tags can probably be moved over:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 2 lines

Thanks,
Florian

OK, so I pulled that by mistake as I was picking patch for keys 1/2; not asymmetric keys.

I'll drop it.

I'm confused. The patch set had fixes for to separate subsystems so I picked the one that I should.

I assume it will be picked by someone. If not send a new patch.

Br, Jarkko

maintainer
#10 lore raw

Re: [PATCH v2 1/2] KEYS: fix signature length passed to KEYCTL_PKEY_VERIFY

Quoted 25 lines · Jarkko Sakkinen
Quoted 24 lines · Jarkko Sakkinen
Quoted 12 lines · Florian Bauckholt
Quoted 1 line · Jarkko Sakkinen

Hi,

I saw that for-next-keys now has 94cd21592349 ("KEYS: Fix asym key overflow in keyctl_pkey_verify()") with the same (trivial) patch. I'm not sure how this goes, but if you'd like to add some context, a link to this thread and these tags can probably be moved over:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Thanks,
Florian

OK, so I pulled that by mistake as I was picking patch for keys 1/2; not asymmetric keys.

Quoted 6 lines
Quoted 1 line

I'll drop it.

I'm confused. The patch set had fixes for to separate subsystems so I picked the one that I should.

I assume it will be picked by someone. If not send a new patch.

I don't have your patch in my for-next-keys branch so perhaps it is unsynced.

I guess I call this done from my side.

Br, Jarkko

#11 lore raw

PATCH v2 2/2 KEYS: asymmetric: Fix PKCS#1 decryption of an empty message

In reply to Florian Bauckholt

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")
---
 crypto/asymmetric_keys/public_key.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
crypto/asymmetric_keys/public_key.c +3 -3
index 09a0b83d5d77..d71747d93b2c 100644
@@ -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);
Signature · 1 line
2.56.0
#12 lore raw

Re: [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message

Sashiko has reviewed this patch and found no issues.
Quoted 19 lines

From: Florian Bauckholt <[email protected]>

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.

Quoted 7 lines

Apply the mapping to encryption only.

Fixes 63ba4d67594a ("KEYS: asymmetric: Use new crypto interface without scatterlists")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Reviewed-by Tested-by on v2 2/2
#13 lore raw

Re: [PATCH v2 2/2] KEYS: asymmetric: Fix PKCS#1 decryption of an empty message

In reply to Florian Bauckholt

crypto/asymmetric_keys/public_key.c static int software_key_eds_op(struct kernel_pkey_params *params, · line 347

Quoted 9 lines · Florian Bauckholt
crypto/asymmetric_keys/public_key.c +3 -0
 	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.