Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1003759rdb; Fri, 1 Dec 2023 04:49:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGF0BA4tNXZsNy1odpMgzLpB62Rz4RG/wK+BZyBlYAweLRNjQjvhglhwNb1bB5RGzyM5bce X-Received: by 2002:a17:902:da84:b0:1cf:e9c5:7382 with SMTP id j4-20020a170902da8400b001cfe9c57382mr14993313plx.68.1701434987408; Fri, 01 Dec 2023 04:49:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701434987; cv=none; d=google.com; s=arc-20160816; b=wsHD6p7FKP+a8B+4JkQkHYjIuFqbG/1zTIkRNK7600hLkzWe8/txsrxWDlreQZ4H9h c4AwzQ83/NaOolv5rNHGNdXPiy1W1QbLzghUrpxLar71wpNPHY9ZXD52zMDDplu7BfSw V5kz2z/ZfOurvGHjvt84V6iS9Zrl0Ycpx2YV2RgY4wPDEo3IKoJI7pKtThgWXP6e/oN2 TAx18Qizr3p4SNL1VuLzp08uZW7v/cMxDXYv7UIOXJtwEEqwguy01xAP69rcv8Cl6gB/ gD26703dgYnamrVcijyXCwDYmmlj433ohuWA0hBaBNrBy8zE4RUQxQvETZ64sPsWjE2p drqQ== 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 Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n3-20020a1709026a8300b001c7345bc01csi3139090plk.450.2023.12.01.04.49.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 04:49:47 -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 EF097805DC0D; 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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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:46 -0800 (PST) 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);