From patchwork Tue Mar 22 12:12:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8641861 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9D1DAC0553 for ; Tue, 22 Mar 2016 12:15:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B71172035B for ; Tue, 22 Mar 2016 12:15:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CA64B202FE for ; Tue, 22 Mar 2016 12:15:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758914AbcCVMPu (ORCPT ); Tue, 22 Mar 2016 08:15:50 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:35947 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758364AbcCVMOK (ORCPT ); Tue, 22 Mar 2016 08:14:10 -0400 Received: by mail-wm0-f65.google.com with SMTP id l68so29153924wml.3; Tue, 22 Mar 2016 05:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MUN/nxAW5zMhj+6P81NDocOCtBF5Zub6eKNevmaHh1g=; b=wdDM4kC2QS5W114mIdi5nUkXnC1dbzWbRQMEg9bqzJpHL/PySXCAzrST0TMb5DaUJd 99fO/aPS8NixgjjtsrCl1EW45LVfXSMiHJ7UivEGBFmkb7piAhpMSuyU5jM/bFfkdV3C 64B24CBXzhE6vo/fA9NOW6c8wGbZSH/O/DPAPXq4vB5bUHJOAuxA23LDUVnrnxy95nWL k5rDANkrcscC8b5D0E/iWx+XYfWoNo63+0GKLlMg5vFon0CKiKnUmKBYnnQaXbMtN14G 9a+L32mlomaLcMY5vj6/hVGcHU7+mwyZFG2SQRny+IFBMnYIKgVKVeyKyRuJzZ91sWVn sk4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=MUN/nxAW5zMhj+6P81NDocOCtBF5Zub6eKNevmaHh1g=; b=NVgQKVoTBgecTgHJ85at/ETYtShA3VUhdTPgR9lkKF0GFzP/qW8RjzkbvxaHRd7A09 KPbMV2aOjgQCZ7VchugrsHQAh/C+Ta9AJKNAh6I6a5IfAOUdGyAge+qL3Ohs3yLCLDaN +0XcPQKp7gD3XoT6CBpSiChMCZKe7g5+ytENAjYTqh/i0ba1jyy7yebuMhtwrnojqf2c 67nlv5KO/uwCTvphVlV8yxkumHpotOyl1/9BdmhyxbXxilQ+BJ8e81BoqLNKx1nfjdOc ns8CLrromNJO7xZxP3OQPcK4TDFF7YTbwUAQLJKV+D9m0InaAZVYuFM7AO8Pjypzm6mG QvCQ== X-Gm-Message-State: AD7BkJLkesQpKHOldOrj3HZbF0prbqiApH9R9tE1e7tX7Ikla8SM1n3DP6pLMc38MsRXPw== X-Received: by 10.28.10.149 with SMTP id 143mr18822339wmk.38.1458648848977; Tue, 22 Mar 2016 05:14:08 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id w15sm17048058wmd.10.2016.03.22.05.14.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Mar 2016 05:14:08 -0700 (PDT) From: Nicolai Stange To: Herbert Xu , "David S. Miller" Cc: Tadeusz Struk , Michal Marek , Andrzej Zaborowski , Stephan Mueller , Arnd Bergmann , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH v3 10/14] lib/mpi: mpi_read_raw_from_sgl(): don't include leading zero SGEs in nbytes Date: Tue, 22 Mar 2016 13:12:44 +0100 Message-Id: <1458648768-1469-11-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1458648768-1469-1-git-send-email-nicstange@gmail.com> References: <1458648768-1469-1-git-send-email-nicstange@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP At the very beginning of mpi_read_raw_from_sgl(), the leading zeros of the input scatterlist are counted: lzeros = 0; for_each_sg(sgl, sg, ents, i) { ... if (/* sg contains nonzero bytes */) break; /* sg contains nothing but zeros here */ ents--; lzeros = 0; } Later on, the total number of trailing nonzero bytes is calculated by subtracting the number of leading zero bytes from the total number of input bytes: nbytes -= lzeros; However, since lzeros gets reset to zero for each completely zero leading sg in the loop above, it doesn't include those. Besides wasting resources by allocating a too large output buffer, this mistake propagates into the calculation of x, the number of leading zeros within the most significant output limb: x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; What's more, the low order bytes of the output, equal in number to the extra bytes in nbytes, are left uninitialized. Fix this by adjusting nbytes for each completely zero leading scatterlist entry. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 048f0aa..4ba0f23 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -447,16 +447,12 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) break; ents--; + nbytes -= lzeros; lzeros = 0; } sgl = sg; - - if (!ents) - nbytes = 0; - else - nbytes -= lzeros; - + nbytes -= lzeros; nbits = nbytes * 8; if (nbits > MAX_EXTERN_MPI_BITS) { pr_info("MPI: mpi too large (%u bits)\n", nbits);