From patchwork Thu May 26 21:19:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 9137431 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 A611E602A7 for ; Thu, 26 May 2016 21:21:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97C36282F6 for ; Thu, 26 May 2016 21:21:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8C7CE282FA; Thu, 26 May 2016 21:21:43 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 3BC9D282F7 for ; Thu, 26 May 2016 21:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755398AbcEZVVb (ORCPT ); Thu, 26 May 2016 17:21:31 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36404 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755346AbcEZVUK (ORCPT ); Thu, 26 May 2016 17:20:10 -0400 Received: by mail-wm0-f65.google.com with SMTP id q62so9062279wmg.3; Thu, 26 May 2016 14:20: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=WDAfiwcNYvB1LKBLdqaOEzQ/4Bi87yu/j9lvTSS0G28=; b=er7sLbG2R1JMwteI0Zxyc/m/6ObQHlfqko94X7kFLA9ms8sNa3REnO/dopBbyxuoAP TRy7M/PLkdwp0lrwTMwDzv3J+dAV4ePoxlFdsGqzxwbfRzjiDQT9PTa9yF88wD+ekIZe k4f/hfmMpCksb/I77v1UoM02WmITNqEtByFcMq9brK7Cj8n10xmk7xsMEUlaJTOqEhXw IuDbJQVvb9KC5XieTlhWH3879FGTfIGOC9aX/Lqx6aRGrweV/7egODPkpiVhGdoyQoFz AmcGHxCUoJqWppGv4sf3ei7o7JmWy033TztOkkDng89KF2xJby8uyYy45AlUDWILppgo vStw== 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=WDAfiwcNYvB1LKBLdqaOEzQ/4Bi87yu/j9lvTSS0G28=; b=I8eE5LDe/Isau2HmvbR8hrOlCIwZ5jGrwFL8350Vurhnau24AcXdAUyNW6iS8OHmXc 4NLRZOBDyAejxZBT7HV+MgAs7kloH0d746XiIggheafq0DvSMDg3igqISQtZX2GlAhqt QZwrsSMy6+ZSONl3wsEy7Nb4UjwJ7LGEDy5auloCUtsuIaoENMwuQcs1d7eqecTdl57L i8MRJvygLT1O1Txgq1dJ0z9fIQcSa2r/8oaUdPC6GHxlGDda3fDsFavBF8Osegr7Bncx SF6xGiaxohKEX2i/7RofnYIaMbiD5HRyEeK0dAttVIYyDW/knbFfqtxrWV0G9sbWynDh nf9Q== X-Gm-Message-State: ALyK8tIE/ubOyuccDah9UtiH5aCcFL+xyYLXgNbxoMLY92oPuTax9pMUo7StZiBOGkt97g== X-Received: by 10.28.232.152 with SMTP id f24mr5292972wmi.45.1464297608560; Thu, 26 May 2016 14:20:08 -0700 (PDT) Received: from localhost (x55b166e2.dyn.telefonica.de. [85.177.102.226]) by smtp.gmail.com with ESMTPSA id ib1sm16120065wjb.48.2016.05.26.14.20.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 May 2016 14:20:07 -0700 (PDT) From: Nicolai Stange To: Herbert Xu Cc: David Howells , Tadeusz Struk , Michal Marek , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH 1/5] lib/mpi: mpi_read_from_buffer(): return error code Date: Thu, 26 May 2016 23:19:51 +0200 Message-Id: <1464297595-24032-2-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1464297595-24032-1-git-send-email-nicstange@gmail.com> References: <1464297595-24032-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-Virus-Scanned: ClamAV using ClamSMTP mpi_read_from_buffer() reads a MPI from a buffer into a newly allocated MPI instance. It expects the buffer's leading two bytes to contain the number of bits, followed by the actual payload. On failure, it returns NULL and updates the in/out argument ret_nread somewhat inconsistently: - If the given buffer is too short to contain the leading two bytes encoding the number of bits or their value is unsupported, then ret_nread will be cleared. - If the allocation of the resulting MPI instance fails, ret_nread is left as is. The only user of mpi_read_from_buffer(), digsig_verify_rsa(), simply checks for a return value of NULL and returns -ENOMEM if that happens. While this is all of cosmetic nature only, there is another error condition which currently isn't detectable by the caller of mpi_read_from_buffer(): if the given buffer is too small to hold the number of bits as encoded in its first two bytes, the return value will be non-NULL and *ret_nread > 0. In preparation of communicating this condition to the caller, let mpi_read_from_buffer() return error values by means of the ERR_PTR() mechanism. Make the sole caller of mpi_read_from_buffer(), digsig_verify_rsa(), check the return value for IS_ERR() rather than == NULL. If IS_ERR() is true, return the associated error value rather than the fixed -ENOMEM. Signed-off-by: Nicolai Stange --- lib/digsig.c | 12 ++++++++---- lib/mpi/mpicoder.c | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/digsig.c b/lib/digsig.c index 07be6c1..a121cbc 100644 --- a/lib/digsig.c +++ b/lib/digsig.c @@ -104,16 +104,18 @@ static int digsig_verify_rsa(struct key *key, datap = pkh->mpi; endp = ukp->data + ukp->datalen; - err = -ENOMEM; - for (i = 0; i < pkh->nmpi; i++) { unsigned int remaining = endp - datap; pkey[i] = mpi_read_from_buffer(datap, &remaining); - if (!pkey[i]) + if (IS_ERR(pkey[i])) { + err = PTR_ERR(pkey[i]); goto err; + } datap += remaining; } + err = -ENOMEM; + mblen = mpi_get_nbits(pkey[0]); mlen = DIV_ROUND_UP(mblen, 8); @@ -126,8 +128,10 @@ static int digsig_verify_rsa(struct key *key, nret = siglen; in = mpi_read_from_buffer(sig, &nret); - if (!in) + if (IS_ERR(in)) { + err = PTR_ERR(in); goto err; + } res = mpi_alloc(mpi_get_nlimbs(in) * 2); if (!res) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 747606f..275c71e 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -88,12 +88,12 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) MPI val = NULL; if (*ret_nread < 2) - goto leave; + return ERR_PTR(-EINVAL); nbits = buffer[0] << 8 | buffer[1]; if (nbits > MAX_EXTERN_MPI_BITS) { pr_info("MPI: mpi too large (%u bits)\n", nbits); - goto leave; + return ERR_PTR(-EINVAL); } buffer += 2; nread = 2; @@ -102,7 +102,7 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); val = mpi_alloc(nlimbs); if (!val) - return NULL; + return ERR_PTR(-ENOMEM); i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; i %= BYTES_PER_MPI_LIMB; val->nbits = nbits;