From patchwork Wed Feb 7 01:10:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10204391 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A5F1A6037E for ; Wed, 7 Feb 2018 01:16:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97FC128D8B for ; Wed, 7 Feb 2018 01:16:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CFFA28D98; Wed, 7 Feb 2018 01:16:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1126528D8B for ; Wed, 7 Feb 2018 01:16:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932306AbeBGBQF (ORCPT ); Tue, 6 Feb 2018 20:16:05 -0500 Received: from mail-io0-f193.google.com ([209.85.223.193]:35265 "EHLO mail-io0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932304AbeBGBQD (ORCPT ); Tue, 6 Feb 2018 20:16:03 -0500 Received: by mail-io0-f193.google.com with SMTP id m11so191789iob.2; Tue, 06 Feb 2018 17:16:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xtjn5/xDSADR6ij5UTo4IUfuas5BG7flcUrvlrjAJ5M=; b=l0dW+qwu3sU37ETBvsX8+10ln1nx0Kp6cUvCGXhjuB7tgkqrhEEzXMYY1JfK38vwc5 FEdFXBDBm9vKk0jtl4JLewchZ9atSOj2FoKGzEKByeV2z3oC6AP9Tl6OwZxwAVzOLgkO iqug0K80IhzhScCBenW2FRS9jt4KaoTvPKVFpJ4jZERFphz5yIk0YR1C0/KtMYOMlkCr ykiGat5p4tQMwMAQubWiC+NME6CRovx7LULMws8YNDN7tYTTnxyHLrlf3fuHdImssRvR ZEbDySHdAQknJT7KzIIxAblDT6gLKRluWD1lj2khWLcdnaKEnYXiCVhrsLJUG5NbfMYX Xo2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xtjn5/xDSADR6ij5UTo4IUfuas5BG7flcUrvlrjAJ5M=; b=ZfcAqgO/vd5BCyhA9h8z1Ra9TX7LoazavNGJWi4j1wBEjyGWhZCsSOvHt9yG5twBHv 2wVh3cPre2jjOgpc9KXUZN2WLptHGqSbKYfIOtkBCv6IxMHKfG0acTU7XpnIZ15vSg6i vbcOvjuagdENqSqC7Oq0ebhZyso6PrD4qx8gZLt9ZFN90/cVw4PfFJTyU2ODmPSiOR9W Zl26qB4nxXFjGiPyjseKpOW+ONuYHgBM8gBg8fNqoRXasqjmHfBnScNi++Da9v5xWNRx tYV3ml/PT2fnAhD9lZi6cBAdtAx236EFvhSHlUoQapN9yNlA02k4WgH/+/AdHCb70hxt /L2A== X-Gm-Message-State: APf1xPDOCv/vRJejcM4yzOdbD3t5Ua08P75z0zvL0zfGeUEoeVjFn/X3 gKrpjWERk2aCtNzBEfwN2dQ= X-Google-Smtp-Source: AH8x2279NCZPAhM5YlqYssk9plYhor1zPB60bb3njKz2aG9NLW+jjOLxhr+bhaXYY6EwxHB2Lc4TCA== X-Received: by 10.107.68.10 with SMTP id r10mr5369634ioa.199.1517966162932; Tue, 06 Feb 2018 17:16:02 -0800 (PST) Received: from ebiggers-linuxstation.kir.corp.google.com ([2620:15c:17:3:dc28:5c82:b905:e8a8]) by smtp.gmail.com with ESMTPSA id c9sm186364iod.5.2018.02.06.17.16.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2018 17:16:02 -0800 (PST) From: Eric Biggers To: David Howells , keyrings@vger.kernel.org Cc: linux-crypto@vger.kernel.org, Michael Halcrow , Eric Biggers , Paolo Valente , stable@vger.kernel.org Subject: [PATCH 4/9] X.509: fix BUG_ON() when hash algorithm is unsupported Date: Tue, 6 Feb 2018 17:10:07 -0800 Message-Id: <20180207011012.5928-5-ebiggers3@gmail.com> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog In-Reply-To: <20180207011012.5928-1-ebiggers3@gmail.com> References: <20180207011012.5928-1-ebiggers3@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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