From patchwork Fri Dec 1 12:49:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Zhandarovich X-Patchwork-Id: 172475 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1091568vqy; Fri, 1 Dec 2023 04:49:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjmuVAtCvRD7G7W4spn4cwaijtHeyR8O7h3+iPfSymhKAvDz3CpJ2H9Z971e1PoFG4e7Oz X-Received: by 2002:a17:902:cec8:b0:1cf:c018:b4e9 with SMTP id d8-20020a170902cec800b001cfc018b4e9mr22278104plg.43.1701434986384; Fri, 01 Dec 2023 04:49:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701434986; cv=none; d=google.com; s=arc-20160816; b=kRv3bKk8bdTVWHeC+oKKmZu4g8+NkG3Wkd8Yq/BjbDPeE7xajqi1oh2IufPLIQFZOb lrwjN+2H/c9Xi3veN3RvlLIP4ypudxDvoYzRGT3HkFWrlek7Sks5DrZnBJNccFG6hqT3 O93Do/jx8247NOwGnRnwVaiZmFT7/5aj/n1E+3KQBYoVp2MjSd1QxUwmxQOxo08epdQb bxtPISQ5rqEMMlsRgNLptieciGva7VZUaqxqoJzEFOMvrAdCRg6JYaJbmJByPCCemwCw hEy2Ne8F5r0oPSUvAOLto/nw4ZcnIvbGD6VVl0+ic3fQsVWlv+yTkzAE439jWElQNcjn w0hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=zRGIa+S22l/O7sxukXR80JcwNi+/4Df4c3dIbzWEDkE=; fh=4vHd1j/kScLGkSY4YWnndCvDHe1tKYHnRzmDXQeYA0s=; b=zUdg+boHicyMsCVeFphzKtzWDlQY1soYwwtzGYO8G0uhwOBamW5yZK0tL1ns/Bnd7C 7E9rAlX7gNLwbzeQdyWfykKeOXlVF6KG825Co0mJUAPYyOP/nHsTlCL4kCbnMvvZF5+x pHShnwqdbo1hCLK0aVC8f0UWwB32rry9VIspvgXy+ptiW9dVuGsTTQtYGF2KWtWKSbra yzzgUq1Q8scq2+ZE0j5cZ+cftJhiXAlwhln+ypSDXswZcg21PYM4w/8rK22a+qMnLJTV Wzo3niqJgAmOU8dpVKJibjHb/fvh+WTRUoxYOSkQ3VkGoXAhU9UKE8pJPfQ/VDQP3F3Q siMA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id d1-20020a170902cec100b001d014c43a94si3449044plg.517.2023.12.01.04.49.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 04:49:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 331088106718; Fri, 1 Dec 2023 04:49:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378853AbjLAMtg (ORCPT + 99 others); Fri, 1 Dec 2023 07:49:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378839AbjLAMte (ORCPT ); Fri, 1 Dec 2023 07:49:34 -0500 Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B6B410E2; Fri, 1 Dec 2023 04:49:39 -0800 (PST) Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Fri, 1 Dec 2023 15:49:35 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Fri, 1 Dec 2023 15:49:34 +0300 From: Nikita Zhandarovich To: Antoine Tenart CC: Nikita Zhandarovich , "David S. Miller" , , Subject: [PATCH v2] crypto: safexcel - Add error handling for dma_map_sg() calls Date: Fri, 1 Dec 2023 04:49:29 -0800 Message-ID: <20231201124929.12448-1-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.0.253.138] X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 01 Dec 2023 04:49:45 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784083892290017808 X-GMAIL-MSGID: 1784083892290017808 Macro dma_map_sg() may return 0 on error. This patch enables checks in case of the macro failure and ensures unmapping of previously mapped buffers with dma_unmap_sg(). Found by Linux Verification Center (linuxtesting.org) with static analysis tool SVACE. Fixes: 49186a7d9e46 ("crypto: inside_secure - Avoid dma map if size is zero") Signed-off-by: Nikita Zhandarovich --- v2: remove extra level of parentheses and change return error code from -ENOMEM to EIO per Antoine Tenart's suggestion drivers/crypto/inside-secure/safexcel_cipher.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c index 272c28b5a088..b83818634ae4 100644 --- a/drivers/crypto/inside-secure/safexcel_cipher.c +++ b/drivers/crypto/inside-secure/safexcel_cipher.c @@ -742,9 +742,9 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring, max(totlen_src, totlen_dst)); return -EINVAL; } - if (sreq->nr_src > 0) - dma_map_sg(priv->dev, src, sreq->nr_src, - DMA_BIDIRECTIONAL); + if (sreq->nr_src > 0 && + !dma_map_sg(priv->dev, src, sreq->nr_src, DMA_BIDIRECTIONAL)) + return -EIO; } else { if (unlikely(totlen_src && (sreq->nr_src <= 0))) { dev_err(priv->dev, "Source buffer not large enough (need %d bytes)!", @@ -752,8 +752,9 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring, return -EINVAL; } - if (sreq->nr_src > 0) - dma_map_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVICE); + if (sreq->nr_src > 0 && + !dma_map_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVICE)) + return -EIO; if (unlikely(totlen_dst && (sreq->nr_dst <= 0))) { dev_err(priv->dev, "Dest buffer not large enough (need %d bytes)!", @@ -762,9 +763,11 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring, goto unmap; } - if (sreq->nr_dst > 0) - dma_map_sg(priv->dev, dst, sreq->nr_dst, - DMA_FROM_DEVICE); + if (sreq->nr_dst > 0 && + !dma_map_sg(priv->dev, dst, sreq->nr_dst, DMA_FROM_DEVICE)) { + ret = -EIO; + goto unmap; + } } memcpy(ctx->base.ctxr->data, ctx->key, ctx->key_len);