From patchwork Thu Sep 6 22:58:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10591285 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 89E0A921 for ; Thu, 6 Sep 2018 23:00:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7729F2B163 for ; Thu, 6 Sep 2018 23:00:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6ABA72B1BC; Thu, 6 Sep 2018 23:00:12 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E618A2B163 for ; Thu, 6 Sep 2018 23:00:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=BcnIxsdPtGc9t5GKrGnnUTIRwNUS5/Q9sMfobO1qoBo=; b=ffZI43RGi6mUkgOKtrr9M9pVpx Tp5d88n+eW0Ks9CyYINqokAMqTl9tQMb/v9tpzRihv8c0pfSWMntfuaOOvp0ib+SCN+ACBRSu+lV6 3dZrtBIyXIjxRfLqWCrBc5zf+Fun0o9SIsVBJBg0eofq/x4dWLl/Xqqgby8Z0YTeDzwg2r9fDoYfv QGw4R99b74N5qWkzuymU8Ppb3D3dsfGCzAr9FwbqXzqcLBOXsmx63vHXCRy4pBXJT3hLO4MWqpnnx 3M/yFvKBSKcOSFrqmq1hZz4KMwlXzWLdiaCeu76vki6mVtK5eg5C8o+qXqwrZV+E4Nm3a3zogAkvQ k3v+Dr3A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fy3Fo-0005lk-HQ; Thu, 06 Sep 2018 23:00:00 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fy3F4-0005UR-Dt for linux-arm-kernel@lists.infradead.org; Thu, 06 Sep 2018 22:59:25 +0000 Received: by mail-pg1-x542.google.com with SMTP id 7-v6so5976371pgf.2 for ; Thu, 06 Sep 2018 15:59:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=J0G2Sx5hOKWq7psD4bTWlVfsrIiRfpFq057sVXNCifs=; b=GjDA8xNVXsZrNpIfV9oXjhaeLfPK6jjhTs0b8gPIiuk8PT2k2SniQfrMgTvBQn8xAK UTD+ujqsvkff6p+rWV9KgoXU4lhSz4i4fa9CjMWssKMVraSHJoAx92pjwIQAtwTEOhzi jAIwr3lFdn08CWSiEUfoJrhyRsUWdhT3El2mU= 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=J0G2Sx5hOKWq7psD4bTWlVfsrIiRfpFq057sVXNCifs=; b=SuKu8tOFd690xcVMBm3Xnzbj6FOjpe1X4Gk84+So+ni6DBNFSToLCJ+4TTzeYCS7ev heV1CzzwCiCHAtwvQN9MXNdJHkx+E+kxn+738uyLjVe6KAW/UcmG1fe/CJcN4toKzUUJ xzdP7YrFGKPOWyhqfPFdxCmjihGq6DuRjTGHaQYd/x83wV8NGEmlsvYMQPvlV5lpwt5c qpeKJTRlfFUyELPc8jJ9RU9jUITSRfUHm2SQjW/3oOqTJ5Z98jVunthRSO9mjgAeIG+r WeSVlJAUIxZFS7AUGgvxqFdaNwhbDjykDBmjgHQv2Y8FBrexk5Rah3FPnQl9Nj0MAV9I 1y6A== X-Gm-Message-State: APzg51BPooqpijFOanMaO3+R04a1XdHTCMC402pxjDLXhQMJzjZ8lqRu GcC4iACpS4vc+zcXbYIIljO8bw== X-Google-Smtp-Source: ANB0VdYbCvFB1nkMmwQhASRHb+MkF34upfkOQqjT6t0RWpXujZd0U1xvwwCMI6ypbfArp60hVrpjFg== X-Received: by 2002:a63:2605:: with SMTP id m5-v6mr5004148pgm.225.1536274744595; Thu, 06 Sep 2018 15:59:04 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id w12-v6sm8657686pfd.110.2018.09.06.15.59.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 06 Sep 2018 15:59:01 -0700 (PDT) From: Kees Cook To: Herbert Xu Subject: [PATCH v2 1/4] crypto: skcipher - Consolidate encrypt/decrypt sanity check Date: Thu, 6 Sep 2018 15:58:51 -0700 Message-Id: <20180906225854.40989-2-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180906225854.40989-1-keescook@chromium.org> References: <20180906225854.40989-1-keescook@chromium.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180906_155914_503674_105C108F X-CRM114-Status: GOOD ( 15.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Ripard , Arnaud Ebalard , Kees Cook , Christian Lamparter , Ard Biesheuvel , Antoine Tenart , Boris Brezillon , Eric Biggers , linux-kernel@vger.kernel.org, Gilad Ben-Yossef , Chen-Yu Tsai , Corentin Labbe , linux-crypto@vger.kernel.org, Jonathan Cameron , Philippe Ombredanne , linux-arm-kernel@lists.infradead.org, Alexander Stein MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP In preparation to adding additional sanity checks before running an skcipher request, this consolidates the open-coded checks into a single function. Instead of passing both req and tfm into the new check this just returns the tfm on success and an ERR_PTR on failure, keeping things as clean as possible. Signed-off-by: Kees Cook --- include/crypto/skcipher.h | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h index 2f327f090c3e..6e954d398e0f 100644 --- a/include/crypto/skcipher.h +++ b/include/crypto/skcipher.h @@ -422,6 +422,27 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm( return __crypto_skcipher_cast(req->base.tfm); } +/** + * crypto_skcipher_reqtfm_check() - obtain and check cipher handle from request + * @req: skcipher_request out of which the cipher handle is to be obtained + * + * Return the crypto_skcipher handle when furnishing an skcipher_request + * data structure. Check for error conditions that would make it unusable + * for an encrypt/decrypt call. + * + * Return: crypto_skcipher handle, or ERR_PTR on error. + */ +static inline struct crypto_skcipher *crypto_skcipher_reqtfm_check( + struct skcipher_request *req) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + + if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) + return ERR_PTR(-ENOKEY); + + return tfm; +} + /** * crypto_skcipher_encrypt() - encrypt plaintext * @req: reference to the skcipher_request handle that holds all information @@ -435,10 +456,10 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm( */ static inline int crypto_skcipher_encrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm_check(req); - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) - return -ENOKEY; + if (IS_ERR(tfm)) + return PTR_ERR(tfm); return tfm->encrypt(req); } @@ -456,10 +477,10 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req) */ static inline int crypto_skcipher_decrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm_check(req); - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) - return -ENOKEY; + if (IS_ERR(tfm)) + return PTR_ERR(tfm); return tfm->decrypt(req); }