Skip to content

Commit 4b34968

Browse files
ebiggersdhowells
authored andcommitted
X.509: fix NULL dereference when restricting key with unsupported_sig
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: a511e1a ("KEYS: Move the point of trust determination to __key_link()") Cc: <stable@vger.kernel.org> # v4.7+ Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 437499e commit 4b34968

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

crypto/asymmetric_keys/restrict.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ __setup("ca_keys=", ca_keys_setup);
6767
*
6868
* Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
6969
* matching parent certificate in the trusted list, -EKEYREJECTED if the
70-
* signature check fails or the key is blacklisted and some other error if
71-
* there is a matching certificate but the signature check cannot be performed.
70+
* signature check fails or the key is blacklisted, -ENOPKG if the signature
71+
* uses unsupported crypto, or some other error if there is a matching
72+
* certificate but the signature check cannot be performed.
7273
*/
7374
int restrict_link_by_signature(struct key *dest_keyring,
7475
const struct key_type *type,
@@ -88,6 +89,8 @@ int restrict_link_by_signature(struct key *dest_keyring,
8889
return -EOPNOTSUPP;
8990

9091
sig = payload->data[asym_auth];
92+
if (!sig)
93+
return -ENOPKG;
9194
if (!sig->auth_ids[0] && !sig->auth_ids[1])
9295
return -ENOKEY;
9396

@@ -139,6 +142,8 @@ static int key_or_keyring_common(struct key *dest_keyring,
139142
return -EOPNOTSUPP;
140143

141144
sig = payload->data[asym_auth];
145+
if (!sig)
146+
return -ENOPKG;
142147
if (!sig->auth_ids[0] && !sig->auth_ids[1])
143148
return -ENOKEY;
144149

@@ -222,9 +227,9 @@ static int key_or_keyring_common(struct key *dest_keyring,
222227
*
223228
* Returns 0 if the new certificate was accepted, -ENOKEY if we
224229
* couldn't find a matching parent certificate in the trusted list,
225-
* -EKEYREJECTED if the signature check fails, and some other error if
226-
* there is a matching certificate but the signature check cannot be
227-
* performed.
230+
* -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
231+
* unsupported crypto, or some other error if there is a matching certificate
232+
* but the signature check cannot be performed.
228233
*/
229234
int restrict_link_by_key_or_keyring(struct key *dest_keyring,
230235
const struct key_type *type,
@@ -249,9 +254,9 @@ int restrict_link_by_key_or_keyring(struct key *dest_keyring,
249254
*
250255
* Returns 0 if the new certificate was accepted, -ENOKEY if we
251256
* couldn't find a matching parent certificate in the trusted list,
252-
* -EKEYREJECTED if the signature check fails, and some other error if
253-
* there is a matching certificate but the signature check cannot be
254-
* performed.
257+
* -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
258+
* unsupported crypto, or some other error if there is a matching certificate
259+
* but the signature check cannot be performed.
255260
*/
256261
int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
257262
const struct key_type *type,

0 commit comments

Comments
 (0)