Skip to contentSkip to conversation

KEYS: two fixes for the asymmetric keyctl operations

PATCH Superseded Florian Bauckholt 6 participants 13 messages Nov 11 – Nov 20

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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'm posting separately to linux-crypto.

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]/

---
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: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261111-keyctl-pkey-fixes-26358a8707b6

Best regards,

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

PATCH 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. It copies max_sig_size bytes from the signature pointer and passes max_sig_size as the signature size to the verification.

This makes no difference for signatures that are always max_sig_size bytes long, such as RSA. A shorter signature, e.g. a DER-encoded ECDSA signature, is followed by whatever is behind 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 the signature, which hides the problem.

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()")
---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +1 -1
index 15b6bf6399b5..794fa3d01911 100644
@@ -160,7 +160,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 		    uparams.in2_len > info.max_sig_size)
 			return -EINVAL;
 
-		params->out_len = info.max_sig_size;
+		params->in2_len = uparams.in2_len;
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 1 line
2.56.0
#2 lore raw

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

Sashiko has reviewed this patch and found no issues.
Quoted 20 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. It copies max_sig_size bytes from the signature pointer and passes max_sig_size as the signature size to the verification.

This makes no difference for signatures that are always max_sig_size bytes long, such as RSA. A shorter signature, e.g. a DER-encoded ECDSA signature, is followed by whatever is behind 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 the signature, which hides the problem.

Quoted 7 lines

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()")

Sashiko has reviewed this patch and found no issues.

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

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

In reply to Florian Bauckholt
Quoted 49 lines · On Wed, Nov 11, 2026 at 04:56:07PM +0100, 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. It copies max_sig_size bytes from the signature pointer and passes max_sig_size as the signature size to the verification.

This makes no difference for signatures that are always max_sig_size bytes long, such as RSA. A shorter signature, e.g. a DER-encoded ECDSA signature, is followed by whatever is behind 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 the signature, which hides the problem.

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()")
---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +1 -1
index 15b6bf6399b5..794fa3d01911 100644
@@ -160,7 +160,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 		    uparams.in2_len > info.max_sig_size)
 			return -EINVAL;
 
-		params->out_len = info.max_sig_size;
+		params->in2_len = uparams.in2_len;
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 2 lines
-- 
2.56.0

Thanks, good catch, and apologies for causing this bug.

Br, Jarkko

#4 lore raw

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

In reply to Florian Bauckholt
Quoted 11 lines · Florian Bauckholt

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. It copies max_sig_size bytes from the signature pointer and passes max_sig_size as the signature size to the verification.

Quoted 6 lines

This makes no difference for signatures that are always max_sig_size bytes long, such as RSA. A shorter signature, e.g. a DER-encoded ECDSA signature, is followed by whatever is behind 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 the signature, which hides the problem.

The test suite of craf (https://github.com/giometti/crypto-afalg,
test3008) hits this with RSA too: it cuts a genuine 256-byte RSA-2048 PKCS#1 signature to 255 bytes and expects the verify to fail. On Ubuntu's 6.8.0-147 kernel, which carries the stable backport of cb481e59ea6c, the cut signature verifies about once in 256 runs. Over 2000 runs per build:

    plain: ok=1991 ko=9
    asan: ok=1997 ko=3

and a failing run prints (line wrapped here):

    libkcrypto  [pkey-cert]: kc-check pkey-verify cert:test3008.cert
        enc=pkcs1 hash=sha256 test3008.d256 test3008.bad: exit 0, not 1:
    KO!

To reproduce it (the count printed is the number of failures; here it gave 3 out of 1000 runs):

    git clone https://github.com/giometti/crypto-afalg
    cd crypto-afalg/tests
    export TESTS_TMPDIR=$(mktemp -d)
    for i in $(seq 1000); do ./do_test.sh 3008; done | grep -c 'KO!'

I also replayed the inputs of one failing run on a UML kernel of current mainline (666539eecdf4, after v7.4-rc3): the 255-byte signature verifies 20 times out of 20, and with your patch applied it is refused 20 times out of 20.

Maybe it is worth saying so in the commit message, since this is going to stable?

I'll send my Tested-by once test3008 has run 2000 times on a kernel with your patch applied.

Quoted 18 lines

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()")
---
 security/keys/keyctl_pkey.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +0 -0
index 15b6bf6399b5..794fa3d01911 100644

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

Quoted 8 lines
security/keys/keyctl_pkey.c +1 -1
@@ -160,7 +160,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 		    uparams.in2_len > info.max_sig_size)
 			return -EINVAL;
 
-		params->out_len = info.max_sig_size;
+		params->in2_len = uparams.in2_len;
 		break;
 	default:
 		return -EOPNOTSUPP;

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
#5 lore raw

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

Quoted 6 lines · Rodolfo Giometti

The test suite of craf (https://github.com/giometti/crypto-afalg,
test3008) hits this with RSA too: it cuts a genuine 256-byte RSA-2048 PKCS#1 signature to 255 bytes and expects the verify to fail.

Maybe it is worth saying so in the commit message, since this is going to stable?

Yes, I'll add it to v2 and link to your report.

Quoted 2 lines

I'll send my Tested-by once test3008 has run 2000 times on a kernel with your patch applied.

Thanks. v2 is rebased but the code change is the same.

Florian

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

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

In reply to Florian Bauckholt
Quoted 20 lines · On 11/11/26 10:56 AM, 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. It copies max_sig_size bytes from the signature pointer and passes max_sig_size as the signature size to the verification.

This makes no difference for signatures that are always max_sig_size bytes long, such as RSA. A shorter signature, e.g. a DER-encoded ECDSA signature, is followed by whatever is behind 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 the signature, which hides the problem.

Quoted 7 lines

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()")
Quoted 18 lines
---
  security/keys/keyctl_pkey.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/keyctl_pkey.c +1 -1
index 15b6bf6399b5..794fa3d01911 100644
@@ -160,7 +160,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
  		    uparams.in2_len > info.max_sig_size)
  			return -EINVAL;
 
-		params->out_len = info.max_sig_size;
+		params->in2_len = uparams.in2_len;
  		break;
  	default:
  		return -EOPNOTSUPP;
maintainer
#7 lore raw

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

Quoted 26 lines · Stefan Berger
Quoted 23 lines · On 11/11/26 10:56 AM, 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. It copies max_sig_size bytes from the signature pointer and passes max_sig_size as the signature size to the verification.

This makes no difference for signatures that are always max_sig_size bytes long, such as RSA. A shorter signature, e.g. a DER-encoded ECDSA signature, is followed by whatever is behind 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 the signature, which hides the problem.

Use the length provided by the caller, which has already been checked against max_sig_size.

Quoted 6 lines
Quoted 4 lines
Fixes cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()")

Can these patches be rebased? They don't apply.

I've probably missed b4 shazam issue last time.

Was just wondering why I don't have these yet.

Br, Jarkko

#8 lore raw

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

Quoted 1 line · Jarkko Sakkinen

Can these patches be rebased? They don't apply.

Sure, I'll rebase and send a v2.

I'll keep 2/2 in v2 in case you take it. Let me know if I should send it separately to linux-crypto instead.

Thanks,
Florian

#9 lore raw

PATCH 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
#10 lore raw

Re: [PATCH 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 6 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
maintainer
#12 lore raw

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

In reply to Florian Bauckholt
Quoted 21 lines · On Wed, Nov 11, 2026 at 04:56:08PM +0100, Florian Bauckholt via B4 Relay wrote:

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.

Apply the mapping to encryption only.

Quoted 7 lines
Fixes 63ba4d67594a ("KEYS: asymmetric: Use new crypto interface without scatterlists")
---
 crypto/asymmetric_keys/public_key.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt