Received: by 10.223.185.116 with SMTP id b49csp2116019wrg; Thu, 22 Feb 2018 08:22:27 -0800 (PST) X-Google-Smtp-Source: AH8x227UfqZUg+9hHOoYi6MiUWdDowhnpfrU9MhubeeW0M7joU0aQQyNm6OIHHwtcnhkmGcc7l5h X-Received: by 10.99.64.197 with SMTP id n188mr6097062pga.21.1519316547603; Thu, 22 Feb 2018 08:22:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519316547; cv=none; d=google.com; s=arc-20160816; b=A1nlgC35XwI0ToG9MzcOkbIWxksVdDitmJiIlf5hoh3Oex7Um1qwofh/I5G6rplffP bFKjuJeK8hYhp7FSuYf/OYPuNCtKi2veYGQGTSLI7yiwMvoJzzHlLOpUsRKW4h6jhur6 npu44HLXEEJy02ZESPiIaVHuZ1uK2+ToFXiGu89koqPAOf06tMk4DoC0tAliurM5cIzl fJoOb7aokOv3dYA5XoOG6V32k/kR2JukYMBh0dZwjO5f0tW372u/2RYLbhC3hDKhT+r5 WpIRWuTCr8riC7PvSco6NxdGbyL6wrgCDYooGnGwh78Vj/8kGkkYUxktyfT7buAWSnpk 9cSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:organization:message-id:date :subject:cc:to:from:arc-authentication-results; bh=A53B2X7IghRFKRJIEiJOcRjPCD4TKclX/dHbu8CH/OU=; b=whv/OZUuTmWoNQ89/2kJzlY3hfUP8kP8mZ3Cn1LZgpIToQizhSUjFPvAlsxBnUg4X/ fnC01D7cO/SAn+2tLh6UHPItvSH8kK0GOjBJYKr3kGxvdxQTVwadMebhjwOzamQEFlI6 VNPiBDB90uG/5hbEktC2rGitKOr3+bhiEfQhxQgXRdCTGCq30Ua6rU6jm3xiOVgRLtlx fuE2n8tngNDic7APzRekU9WiP6NSPyW7MOi5SHcLJO5pG4fZjNzCswD2CJYaLgmrVkw/ tkr0MbRn64Nq7fTK8rTInLKXhHayJqGHthEBZo2xzcXJisz+VBvowxm/K5YR5kJvREs6 MP2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 64-v6si260563ply.277.2018.02.22.08.22.12; Thu, 22 Feb 2018 08:22:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933174AbeBVQVI (ORCPT + 99 others); Thu, 22 Feb 2018 11:21:08 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:35322 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933134AbeBVQVE (ORCPT ); Thu, 22 Feb 2018 11:21:04 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1459C404085A; Thu, 22 Feb 2018 16:21:04 +0000 (UTC) Received: from warthog.procyon.org.uk.com (ovpn-120-250.rdu2.redhat.com [10.10.120.250]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29F6A2026987; Thu, 22 Feb 2018 16:21:03 +0000 (UTC) From: David Howells To: jmorris@namei.org Cc: dhowells@redhat.com, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Eric Biggers Subject: [PATCH 5/6] X.509: fix NULL dereference when restricting key with unsupported_sig Date: Thu, 22 Feb 2018 16:21:02 +0000 Message-Id: <151931646253.27713.5857757999911191687.stgit@warthog.procyon.org.uk> Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 In-Reply-To: <151931642737.27713.5082532296556836948.stgit@warthog.procyon.org.uk> References: <151931642737.27713.5082532296556836948.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 22 Feb 2018 16:21:04 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 22 Feb 2018 16:21:04 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'dhowells@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers 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: # v4.7+ Signed-off-by: Eric Biggers Signed-off-by: David Howells --- crypto/asymmetric_keys/restrict.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crypto/asymmetric_keys/restrict.c b/crypto/asymmetric_keys/restrict.c index 86fb68508952..7c93c7728454 100644 --- a/crypto/asymmetric_keys/restrict.c +++ b/crypto/asymmetric_keys/restrict.c @@ -67,8 +67,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 *dest_keyring, const struct key_type *type, @@ -88,6 +89,8 @@ int restrict_link_by_signature(struct key *dest_keyring, return -EOPNOTSUPP; sig = payload->data[asym_auth]; + if (!sig) + return -ENOPKG; if (!sig->auth_ids[0] && !sig->auth_ids[1]) return -ENOKEY; @@ -139,6 +142,8 @@ static int key_or_keyring_common(struct key *dest_keyring, return -EOPNOTSUPP; sig = payload->data[asym_auth]; + if (!sig) + return -ENOPKG; if (!sig->auth_ids[0] && !sig->auth_ids[1]) return -ENOKEY; @@ -222,9 +227,9 @@ static int key_or_keyring_common(struct key *dest_keyring, * * 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, and some other error if - * there is a matching certificate but the signature check cannot be - * performed. + * -EKEYREJECTED if the signature check fails, -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_key_or_keyring(struct key *dest_keyring, const struct key_type *type, @@ -249,9 +254,9 @@ int restrict_link_by_key_or_keyring(struct key *dest_keyring, * * 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, and some other error if - * there is a matching certificate but the signature check cannot be - * performed. + * -EKEYREJECTED if the signature check fails, -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_key_or_keyring_chain(struct key *dest_keyring, const struct key_type *type,