From patchwork Sat Oct 21 17:14:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SF Markus Elfring X-Patchwork-Id: 10021449 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 1ECB3603B5 for ; Sat, 21 Oct 2017 17:16:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ED08F28886 for ; Sat, 21 Oct 2017 17:16:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D049628A8C; Sat, 21 Oct 2017 17:16:07 +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=-6.9 required=2.0 tests=BAYES_00,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 662EB28886 for ; Sat, 21 Oct 2017 17:16:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932213AbdJURQG (ORCPT ); Sat, 21 Oct 2017 13:16:06 -0400 Received: from mout.web.de ([212.227.15.14]:53890 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932180AbdJURQE (ORCPT ); Sat, 21 Oct 2017 13:16:04 -0400 Received: from [192.168.1.2] ([78.49.207.83]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MD8Au-1e0WO23P80-00GXOZ; Sat, 21 Oct 2017 19:14:54 +0200 Subject: [PATCH 1/3] crypto-ansi_cprng: Use common error handling code in get_prng_bytes() From: SF Markus Elfring To: linux-crypto@vger.kernel.org, "David S. Miller" , Herbert Xu , Neil Horman Cc: LKML , kernel-janitors@vger.kernel.org References: Message-ID: <58606d55-38e7-7fa4-ca5b-84609d16625d@users.sourceforge.net> Date: Sat, 21 Oct 2017 19:14:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-GB X-Provags-ID: V03:K0:Q0XJzECFv473nM2Dbg7fVJPZO+VnpbIQ7jaRsewPVwzGFoN1gpZ tZw6hNmhk6nQT3++dBILtvZjVz+4rUEgLhCWhF5PL5C/EScKc9bXJypp2K96LU9mihw0fVB d7ACjQVDyFwcsQDkX2sbkLPmxA7zUHMijfiHUWNgXFuvCJvgzqkrXJg0rO0UJW3+NDS0Y57 nGexWL0Q+pGbULakE1oUw== X-UI-Out-Filterresults: notjunk:1; V01:K0:Nyh2g9SE9JE=:ebL6Qo3CW1h4XHnW8pUTvY e8oj2QYzo96pyihJcVzyvZ2XTLLlPAPSUMTw1/Fx93A1q+2pnK5fB4hM+klOUmADCQWIm5A5C iPT036DT979nX/PnzSKK50/RCcP2CoEPplQYbzNpgyu0ppN+cTg/b1MYVw+aWqVd1thJUsiqs Eyx80QUMGPiBoOpoNEowVR27RZIFGfAu6HHHWG4ofjSKX9ZaH2JkxANdT3+4JwNtLEPn7GwJA 4vFxrX4TiGx5YOXHL3F8ngzQaMEEcLAXn8lgSA02tvoHEb4YvqKEVJ6R/X19ywC0S4Yt/GYEq cYUYzXQqXJXIhHdk2nTHxSBQ8x7Poc+YbNZlaoxzgD5E77QfVtcA2PzKKTrlB/dWnNlFOTaK0 GLE5qdCJQHae5388x4mro44b4yHpRo0fieE2MDLgoE79S35x514RW3un7sRuva6mUPtQwXjJn 6vUDxqYHz1IUuqLX5xsA94dmgYV+sP1giTvRb6OEyukTVLnFfCG7GxpqonQ5fZqV4O6oKh/5D pJ1ndapxxY1Mhl6mAReSHBqnVkU8//PlWtRu0CqEwVkSdpSxPXttvXjk/R2iIKg/HXRhsxbi8 ikTMctAOrCH2qxaXe7x2I73AfWlqWDSHMVGuwpkm5dEOGAXllvuOtFQTswWKR+KhHDXkAKaWF ZgGOdZ0UG6XIZrWB3MPR1khnpIJE8eKZTU5pS07swddL1/weRjR1mSeIlDZz6cWnBOWoiBN1U +iHzpcUWFOZzue3VRNqOo69f7KQ1jrAtudKTdd6EKqcWgi2nRPZG9nM2/ux+BIo6MbfvNo0Hp ltq0impqeu7WzJABwMuxzIK2umPAqe7b+F3bsLFeoOpCTxprfs= 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: Markus Elfring Date: Sat, 21 Oct 2017 15:17:52 +0200 Adjust jump targets so that a bit of exception handling can be better reused at the end of this function. This issue was detected by using the Coccinelle software. Signed-off-by: Markus Elfring --- crypto/ansi_cprng.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/crypto/ansi_cprng.c b/crypto/ansi_cprng.c index eff337ce9003..111c8982a47f 100644 --- a/crypto/ansi_cprng.c +++ b/crypto/ansi_cprng.c @@ -195,7 +195,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, err = -EINVAL; if (ctx->flags & PRNG_NEED_RESET) - goto done; + goto unlock; /* * If the FIXED_SIZE flag is on, only return whole blocks of @@ -204,7 +204,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, err = -EINVAL; if (ctx->flags & PRNG_FIXED_SIZE) { if (nbytes < DEFAULT_BLK_SZ) - goto done; + goto unlock; byte_count = DEFAULT_BLK_SZ; } @@ -219,13 +219,9 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, remainder: - if (ctx->rand_data_valid == DEFAULT_BLK_SZ) { - if (_get_more_prng_bytes(ctx, do_cont_test) < 0) { - memset(buf, 0, nbytes); - err = -EINVAL; - goto done; - } - } + if (ctx->rand_data_valid == DEFAULT_BLK_SZ && + _get_more_prng_bytes(ctx, do_cont_test) < 0) + goto reset_memory; /* * Copy any data less than an entire block @@ -238,7 +234,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, byte_count--; ctx->rand_data_valid++; if (byte_count == 0) - goto done; + goto unlock; } } @@ -246,13 +242,10 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, * Now copy whole blocks */ for (; byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) { - if (ctx->rand_data_valid == DEFAULT_BLK_SZ) { - if (_get_more_prng_bytes(ctx, do_cont_test) < 0) { - memset(buf, 0, nbytes); - err = -EINVAL; - goto done; - } - } + if (ctx->rand_data_valid == DEFAULT_BLK_SZ && + _get_more_prng_bytes(ctx, do_cont_test) < 0) + goto reset_memory; + if (ctx->rand_data_valid > 0) goto empty_rbuf; memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ); @@ -266,11 +259,16 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, if (byte_count) goto remainder; -done: +unlock: spin_unlock_bh(&ctx->prng_lock); dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n", err, ctx); return err; + +reset_memory: + memset(buf, 0, nbytes); + err = -EINVAL; + goto unlock; } static void free_prng_context(struct prng_context *ctx)