Skip to contentSkip to conversation

X.509: fix NULL dereference when restricting key with unsupported_sig

PATCH Applied Eric Biggers 2 participants 2 messages Feb 26, 2018

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

PATCH 1/1 X.509: fix NULL dereference when restricting key with unsupported_sig

From: Eric Biggers <[email protected]>

commit 4b34968e77ad09628cfb3c4a7daf2adc2cefc6e8 upstream. [Please apply to 4.9-stable. I dropped the portion of the patch pertaining to KEYCTL_RESTRICT_KEYRING which was added in v4.12.]

The asymmetric key type allows an X.509 certificate to be added even if its signature's hash algorithm is not available in the crypto API. In that case 'payload.data[asym_auth]' will be NULL. But the key restriction code failed to check for this case before trying to use the signature, resulting in a NULL pointer dereference in key_or_keyring_common() or in restrict_link_by_signature().

Fix this by returning -ENOPKG when the signature is unsupported.

Reproducer when all the CONFIG_CRYPTO_SHA512* options are disabled and keyctl has support for the 'restrict_keyring' command:

keyctl new_session
keyctl restrict_keyring @s asymmetric builtin_trusted openssl req -new -sha512 -x509 -batch -nodes -outform der \
| keyctl padd asymmetric desc @s

Fixes a511e1af8b12 ("KEYS: Move the point of trust determination to __key_link()")
Cc <[email protected]> # v4.7+
---
 crypto/asymmetric_keys/restrict.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
crypto/asymmetric_keys/restrict.c +5 -2
index 19d1afb9890f..09b1374dc619 100644
@@ -66,8 +66,9 @@ __setup("ca_keys=", ca_keys_setup);
  *
  * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
  * matching parent certificate in the trusted list, -EKEYREJECTED if the
- * signature check fails or the key is blacklisted and some other error if
- * there is a matching certificate but the signature check cannot be performed.
+ * signature check fails or the key is blacklisted, -ENOPKG if the signature
+ * uses unsupported crypto, or some other error if there is a matching
+ * certificate but the signature check cannot be performed.
  */
 int restrict_link_by_signature(struct key *trust_keyring,
 			       const struct key_type *type,
@@ -86,6 +87,8 @@ int restrict_link_by_signature(struct key *trust_keyring,
 		return -EOPNOTSUPP;
 
 	sig = payload->data[asym_auth];
+	if (!sig)
+		return -ENOPKG;
 	if (!sig->auth_ids[0] && !sig->auth_ids[1])
 		return -ENOKEY;
 
Signature · 1 line
2.16.1.291.g4437f3f132-goog
maintainer Applied
#1 lore raw

Patch "X.509: fix NULL dereference when restricting key with unsupported_sig" has been added to the 4.9-stable tree

This is a note to let you know that I've just added the patch titled

X.509: fix NULL dereference when restricting key with unsupported_sig

to the 4.9-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
x.509-fix-null-dereference-when-restricting-key-with-unsupported_sig.patch
and it can be found in the queue-4.9 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.

Quoted 1 line

From [email protected] Mon Feb 26 20:46:24 2018

From: Eric Biggers <[email protected]>
Date: Mon, 26 Feb 2018 10:17:15 -0800
Subject: X.509: fix NULL dereference when restricting key with unsupported_sig
To: [email protected], Greg Kroah-Hartman <[email protected]>
Cc: [email protected], Eric Biggers <[email protected]>, David Howells <[email protected]>
Message-ID: <[email protected]>

From: Eric Biggers <[email protected]>

commit 4b34968e77ad09628cfb3c4a7daf2adc2cefc6e8 upstream.

The asymmetric key type allows an X.509 certificate to be added even if its signature's hash algorithm is not available in the crypto API. In that case 'payload.data[asym_auth]' will be NULL. But the key restriction code failed to check for this case before trying to use the signature, resulting in a NULL pointer dereference in key_or_keyring_common() or in restrict_link_by_signature().

Fix this by returning -ENOPKG when the signature is unsupported.

Reproducer when all the CONFIG_CRYPTO_SHA512* options are disabled and keyctl has support for the 'restrict_keyring' command:

keyctl new_session
keyctl restrict_keyring @s asymmetric builtin_trusted openssl req -new -sha512 -x509 -batch -nodes -outform der \
| keyctl padd asymmetric desc @s

Fixes a511e1af8b12 ("KEYS: Move the point of trust determination to __key_link()")
Cc <[email protected]> # v4.7+
---
 crypto/asymmetric_keys/restrict.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
crypto/asymmetric_keys/restrict.c +5 -2
@@ -66,8 +66,9 @@ __setup("ca_keys=", ca_keys_setup);
  *
  * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
  * matching parent certificate in the trusted list, -EKEYREJECTED if the
- * signature check fails or the key is blacklisted and some other error if
- * there is a matching certificate but the signature check cannot be performed.
+ * signature check fails or the key is blacklisted, -ENOPKG if the signature
+ * uses unsupported crypto, or some other error if there is a matching
+ * certificate but the signature check cannot be performed.
  */
 int restrict_link_by_signature(struct key *trust_keyring,
 			       const struct key_type *type,
@@ -86,6 +87,8 @@ int restrict_link_by_signature(struct ke
 		return -EOPNOTSUPP;
 
 	sig = payload->data[asym_auth];
+	if (!sig)
+		return -ENOPKG;
 	if (!sig->auth_ids[0] && !sig->auth_ids[1])
 		return -ENOKEY;
 

Patches currently in stable-queue which might be from [email protected] are

queue-4.9/x.509-fix-null-dereference-when-restricting-key-with-unsupported_sig.patch queue-4.9/binder-add-missing-binder_unlock.patch