Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1080526rdb; Fri, 1 Dec 2023 06:39:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEyE3bltXJJGD/9GLUQK1jf92FT+fNZy+qFM3IOB5zaoiLl9nJy7w9ULPCX8Sl3Y6nLIUsL X-Received: by 2002:a17:90b:4c8b:b0:286:50a0:78dd with SMTP id my11-20020a17090b4c8b00b0028650a078ddmr3690664pjb.19.1701441547539; Fri, 01 Dec 2023 06:39:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701441547; cv=none; d=google.com; s=arc-20160816; b=pG0SWHOTqaw3TchyT6o6gFAC58PfXFWiAkH++wgK0wZHS8k3DDASykdISvvmUkPc+e lqta/O0/xonEIEO2E8T6JpXtsWepIWPTxVIbqDwdTIWJW/W9f9oHsRG2L3d4vvgz1kky 0rWNmUpRXq9fS7iMsCmE6J7BEfgbtbwMW1coVMT7RQII09hc4S4qqyHcZHXNrjzWVY1i bV2HHWfyUJbTULTjBfEa/n+Twz6fH+f7kXKoI2QmVBdYTEm+MM1kf0EcRp4s8mK07INQ 3/k56Ttfgeg+io/QuP9zKlKAgKijwtfJee/m53PrMn22463O/GMr26DyGMS1e2ySF7yB +oTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:to:cc:from:subject:references:in-reply-to :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=N3BopAZ+8TrB2dzHPqBo9yfkgKKaNOFdJJ5PYEHASbc=; fh=Ve6GX9rytCS3A3noMtA6N7cXj4L7Hgutv1zZdhQDpUM=; b=LD9A2jMyuW1Rjokp8QAP3uogsv6EbHqqwOkdW4fSOFYZ1Hvk2jxxsYTnP3QETHUjMz r3orreh4jknBZuYkfBZelkP+cRay19RkiPUtFzG7bEuWWjDPJHXYO3iYFORfRs3kToXV 5Kox57mCoFxfV8K8+AvEO513BVC31RaXy9Ui5wcKw+s6Kx8NcY+PJ6LuvsBgTr9QTWpr 91jbQ6OR8hNjJJclfoR/SfkCVcmmn+Z7trl2GfXDA20G+c44QNLIYKMxJBzc56ZhxLS6 C3Hy+RSXaMrDQjtov8yIqViy8fiRWdgsgmevczasg7XrNVuYCOgaIXoDypm4RrVxhjjr bfEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=D2tvSGjv; spf=pass (google.com: domain of linux-crypto+bounces-460-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-crypto+bounces-460-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id pi17-20020a17090b1e5100b0028103a8ad9csi3709596pjb.68.2023.12.01.06.39.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 06:39:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-460-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=D2tvSGjv; spf=pass (google.com: domain of linux-crypto+bounces-460-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-crypto+bounces-460-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 721DAB20C32 for ; Fri, 1 Dec 2023 14:39:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EDDFC3E467 for ; Fri, 1 Dec 2023 14:39:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="D2tvSGjv" X-Original-To: linux-crypto@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B321F25551 for ; Fri, 1 Dec 2023 13:39:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AFBACC433C7; Fri, 1 Dec 2023 13:39:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701437991; bh=YkfWSmsAy25wgJ78nihdMN18So6x4EQUS07prH0NBmo=; h=In-Reply-To:References:Subject:From:Cc:To:Date:From; b=D2tvSGjvvHiWzBwIVjNd2X6msgGnq0ut9JPk45HvVYurOI41AtrWarv5+50AP7Xbz nDTX5q7PT9buFzxYdXxba1MwMjMEJjP9ACafbhaUpOBFS+lA06qj2UcMeAt+ztSy8a JPg2i6p9MxJeddTanJ5cwxD/HqVSaCDKghdXDgKgPQcBZ2NH+Xx3BEUWwp+iRI9z6M EG41RmQgtWN/GtEGBAhvZP8NhLH0SccEU3+SQbQi+i3PPn2Zic//a8FIMkZqnT2ivJ IobCIVPy5K59K2SZbyQEe2ZKpxp87jRxyvLrWq2peIEljZ4chLmnBsH2J18nRna5HK Xuv0ygMSp1lnw== Content-Type: text/plain; charset="utf-8" Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable In-Reply-To: <20231201124929.12448-1-n.zhandarovich@fintech.ru> References: <20231201124929.12448-1-n.zhandarovich@fintech.ru> Subject: Re: [PATCH v2] crypto: safexcel - Add error handling for dma_map_sg() calls From: Antoine Tenart Cc: Nikita Zhandarovich , David S. Miller , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org To: Nikita Zhandarovich Date: Fri, 01 Dec 2023 14:39:47 +0100 Message-ID: <170143798791.35162.11594364771392098948@kwain> Quoting Nikita Zhandarovich (2023-12-01 13:49:29) > 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(). >=20 > Found by Linux Verification Center (linuxtesting.org) with static > analysis tool SVACE. >=20 > Fixes: 49186a7d9e46 ("crypto: inside_secure - Avoid dma map if size is ze= ro") > Signed-off-by: Nikita Zhandarovich Reviewed-by: Antoine Tenart Thanks! Antoine > --- > v2: remove extra level of parentheses and > change return error code from -ENOMEM to EIO > per Antoine Tenart's suggestion >=20 > drivers/crypto/inside-secure/safexcel_cipher.c | 19 +++++++++++-------- > 1 file changed, 11 insertions(+), 8 deletions(-) >=20 > diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/cry= pto/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_requ= est *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_BIDIREC= TIONAL)) > + return -EIO; > } else { > if (unlikely(totlen_src && (sreq->nr_src <=3D 0))) { > dev_err(priv->dev, "Source buffer not large enoug= h (need %d bytes)!", > @@ -752,8 +752,9 @@ static int safexcel_send_req(struct crypto_async_requ= est *base, int ring, > return -EINVAL; > } > =20 > - if (sreq->nr_src > 0) > - dma_map_sg(priv->dev, src, sreq->nr_src, DMA_TO_D= EVICE); > + if (sreq->nr_src > 0 && > + !dma_map_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVI= CE)) > + return -EIO; > =20 > if (unlikely(totlen_dst && (sreq->nr_dst <=3D 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_req= uest *base, int ring, > goto unmap; > } > =20 > - 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_DE= VICE)) { > + ret =3D -EIO; > + goto unmap; > + } > } > =20 > memcpy(ctx->base.ctxr->data, ctx->key, ctx->key_len); >