From patchwork Wed Nov 29 15:21:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Zhandarovich X-Patchwork-Id: 171422 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp412025vqn; Wed, 29 Nov 2023 07:21:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwPTe9SFePInibf/yRoaDaVCvJJyVVeyPxfJFHoP1RlD8Mo8Q+H3t1hMah8gXw83yr7sfL X-Received: by 2002:a05:6a21:7896:b0:18b:4e8c:471d with SMTP id bf22-20020a056a21789600b0018b4e8c471dmr26341213pzc.59.1701271319304; Wed, 29 Nov 2023 07:21:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701271319; cv=none; d=google.com; s=arc-20160816; b=lMAUHAaStN1Ka4z8NOKVxu288d1+wBj0oarX8Pk6nlj64D/12Gk/3Z7sXqrxEeaAeB VKEvSsW6TaKi1+XVBxEb+lE1cDTJG6nYeNXwLO/CLp6KWxU+IqNRiE7EiOUgm7mFD/2n 9RgspVPbMHtSQlCV4o0CMxdeDfEZev8SM6TpqAvz1AHLCkIRjRd31e+gi4C7Sf5fSQ9J BRL+ru7jotVN5usGbfKZ06MFm4UJ+g29gITsmqc0asP29TkIp+ZbsztxS6yZPN5UuWUv ZJX/F1J9kVgZFcI+5WdME9AnETMv6bCF9JCnHw0o9HgTZFbyG5yYyWoGfeS5w/UigXkN 0WIQ== 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=PIBWCubcDObO1zEswJYqTbKHW/j4xGorbYQE7SjlL0E=; fh=6FyO0wd9Kk/FNtd22ji0aApFi0F/pH+83rOWkkMumQw=; b=Wndd3z7UjnsG/ui+kSCo7tFNp5YpEhtFe9wO9mtN2ZpOpp91LvQ2CiNY4sRHQEh3/t tU3goeL+L0icO4JXyGexOZWwX3ovl1i/T0pKZ2vy4CB3bZj3mt3Ztcjuu+s6B0iF5M0H BYOyEnb+3AkrJevCPkXLILVmjVYaeFUtQiFgMXiKV0gy+gIEvy1XarwId+yuoySqHoNL ptKjwNHm7G6kQWFYn4yjs15EmQGXPwH9Dyt8GZp8oRb1l2GLPo3hovO/ZMnHtbno8D12 wfa3mqCq/v8QZU2KxJJXWlSF6rPRwsMK3sfXC7AqNfyEentc4IGwt9zFuURbxzGfHtHa qVcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id u8-20020a63df08000000b005c602e138b6si1952084pgg.639.2023.11.29.07.21.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 07:21:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 140318051977; Wed, 29 Nov 2023 07:21:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234820AbjK2PVr (ORCPT + 99 others); Wed, 29 Nov 2023 10:21:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234629AbjK2PVp (ORCPT ); Wed, 29 Nov 2023 10:21:45 -0500 Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C05CEC9; Wed, 29 Nov 2023 07:21:51 -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; Wed, 29 Nov 2023 18:21:50 +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; Wed, 29 Nov 2023 18:21:49 +0300 From: Nikita Zhandarovich To: Antoine Tenart CC: Nikita Zhandarovich , Herbert Xu , "David S. Miller" , , Subject: [PATCH] crypto: safexcel - Add error handling for dma_map_sg() calls Date: Wed, 29 Nov 2023 07:21:45 -0800 Message-ID: <20231129152145.7767-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=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email 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 (morse.vger.email [0.0.0.0]); Wed, 29 Nov 2023 07:21:56 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783912206473765846 X-GMAIL-MSGID: 1783912275037197286 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 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..ca660f31c15f 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 -ENOMEM; } 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 -ENOMEM; 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 = -ENOMEM; + goto unmap; + } } memcpy(ctx->base.ctxr->data, ctx->key, ctx->key_len);