Received: by 10.223.185.116 with SMTP id b49csp2117285wrg; Thu, 22 Feb 2018 08:23:40 -0800 (PST) X-Google-Smtp-Source: AH8x224W56nihh3xUziqS18YCOI/mz+MGIcqg+WT4JmIplunEBVorLqO/EIp/71/6rAZsRaDZOex X-Received: by 2002:a17:902:f81:: with SMTP id 1-v6mr6867763plz.265.1519316620104; Thu, 22 Feb 2018 08:23:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519316620; cv=none; d=google.com; s=arc-20160816; b=vLTItk7RvQmsi8OXL4UOFHzHgCGEcoZ+P8RlooyfP7hRU8dk3NU+7NgnT6XsbaTuHL Av1edGPAZYOo4Y311JzqCU0yXXB4lKfjIzUibsIzJfh4NfaD07QZKqiVxDDZt3epJV1c e69USpJuI55vvEv0QgYDuki8DUty4Uh79T8rTQ7s3kfBUfDaThnVzYV2ZHvECwIe6qWq zikrBpkX3/hsdy/Br8x49J3ahUvqjlCfnRX3oezg19WZkRMjIRkR2IrO7hEZuZJ9JC2v ci+w8Ypy5hcknTOhwu+rZrG992CoUMCQpMwswBZW0w2+jYF70PP+VKKkybT5jpTVujHz xD1w== 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=8F1aLesqNETVIOIuy2z+zVr7gfAKhjharxa1qSBjLT0=; b=hXzcSwRY/Gl6sSy9fhdJYOs3BLzUstOwNE1h6y7mbTaQ3LyC3KEKy5hYsRHlt/ZEdf MIVBQD7NhgVyE55Fapv9w/K4aJr3dCs1y87kxOqwkNvmcvbIiWvxtA/Qo6QRWk5bJTi/ XEKtsFiR3j4azp702m5d+LTwY5VgPbqEZMFitXVstslGZk4bxmoTbeaD+hBinN6ivOIf aOTKYnNYqBtkMvOqXUr4Q4qYnhrFem75EixruqZT8L1Mg6MPa6GOhyjozv5aiKOCIyxp 0g5B9VH2VlAFPy6UbPhbyWy2ANL/NrNsYosV5NSpBqydCIa5Imx7LLFR7yU4V3s5NSpn lFIw== 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 g12-v6si261926pla.226.2018.02.22.08.23.24; Thu, 22 Feb 2018 08:23:40 -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 S933390AbeBVQVB (ORCPT + 99 others); Thu, 22 Feb 2018 11:21:01 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:39622 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933134AbeBVQU5 (ORCPT ); Thu, 22 Feb 2018 11:20:57 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 176528182D0C; Thu, 22 Feb 2018 16:20:57 +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 15BE79C07F; Thu, 22 Feb 2018 16:20:55 +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, Paolo Valente , Eric Biggers Subject: [PATCH 4/6] X.509: fix BUG_ON() when hash algorithm is unsupported Date: Thu, 22 Feb 2018 16:20:55 +0000 Message-Id: <151931645546.27713.15009973131656269423.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.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 22 Feb 2018 16:20:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 22 Feb 2018 16:20:57 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.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 X.509 parser mishandles the case where the certificate's signature's hash algorithm is not available in the crypto API. In this case, x509_get_sig_params() doesn't allocate the cert->sig->digest buffer; this part seems to be intentional. However, public_key_verify_signature() is still called via x509_check_for_self_signed(), which triggers the 'BUG_ON(!sig->digest)'. Fix this by making public_key_verify_signature() return -ENOPKG if the hash buffer has not been allocated. Reproducer when all the CONFIG_CRYPTO_SHA512* options are disabled: openssl req -new -sha512 -x509 -batch -nodes -outform der \ | keyctl padd asymmetric desc @s Fixes: 6c2dc5ae4ab7 ("X.509: Extract signature digest and make self-signed cert checks earlier") Reported-by: Paolo Valente Cc: Paolo Valente Cc: # v4.7+ Signed-off-by: Eric Biggers Signed-off-by: David Howells --- crypto/asymmetric_keys/public_key.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c index de996586762a..e929fe1e4106 100644 --- a/crypto/asymmetric_keys/public_key.c +++ b/crypto/asymmetric_keys/public_key.c @@ -79,9 +79,11 @@ int public_key_verify_signature(const struct public_key *pkey, BUG_ON(!pkey); BUG_ON(!sig); - BUG_ON(!sig->digest); BUG_ON(!sig->s); + if (!sig->digest) + return -ENOPKG; + alg_name = sig->pkey_algo; if (strcmp(sig->pkey_algo, "rsa") == 0) { /* The data wangled by the RSA algorithm is typically padded