Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp157446lqb; Tue, 28 May 2024 11:31:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW4+e+M0auhiibI/pJV0GVfZjWh4sI9CvcQ8+tLE3mps/zHw373QnXGDW+CLuUkzA8KaYvi2h9dyzzmLxUAxAybhIiWeC7gJGpcSxqHUQ== X-Google-Smtp-Source: AGHT+IE7pwkiYNCmR4nE8GWW433WTPonxo08C5TDU9U3v4u8uMuwlDTraRKcCxV0wISJYV3utqKu X-Received: by 2002:a50:8d59:0:b0:578:6558:4cb0 with SMTP id 4fb4d7f45d1cf-57865584d45mr6398549a12.33.1716921098113; Tue, 28 May 2024 11:31:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716921098; cv=pass; d=google.com; s=arc-20160816; b=Au5gms4ehge1AN1zbsGMANShDyUS474L5hbs8/JxveWplKlCTYzPiQwAcJhnPvFBFL +4URknnEQU/AVXAJN/ATg6vo6FLBjsLczEKmvTgYEab+t4v4LHn8JsKiJU+P3AXl5+bB heLNJGxFEbMMSk2SFse6AgunDh3nlj+T8ZppiEKlGOw1agnodkRwjKV5IhuvwJsfcpXI ht75MVVM0/bnxfMSpp8DUFLGpa8jdraePws4QFjpI9zbNo8IkEepqFI8k5AlnamqUSYA GYDrOSJEyIkKYpr+uYAxXM1bkhX2KWT0b3MnTpfiJ4bXdp5iTRF1ymCxQbROMkcWSz5C /Ziw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:content-language:from :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=WwM/3+NLQ6u6jsqyMBpedYXPtVmXLt6OHYPQVFW3ieM=; fh=ScY1k9ts0NTjcqEh0cFbltaaMRv0Qj8PBTS43RYGIu4=; b=aNooWBkt++3gOid92R9WNjNIYuPG1mUb1pdT03FApyAG28sOJQ8IA7AOcdOiotp095 W2o3o935fxjHXrLfh8cJ5WQVaOjp+JYg+v/xMmjuChXi4MQXGoH+FyDLLMJC0tZMI8g5 M3g6673XcdYbNX/zj90Z2AS/+ZpFgNjAy8LdkPJhVoo6UHoBgr5fP+xOStWED4HLHog2 bW+QdxwfRygeh0/MlZK+2ocwXIyVefVUnhO0H0yZj/k9B0gnsxFS7xsRGMV1itE9bhgH cNdI9zTOeJr4WvLkt8kCyyA3WL3Fb2Qhcoc6g6ze0TYtL4/THBiYdsNKbsHlR4hPyTnl g12g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-192869-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192869-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-5785234a106si5229825a12.54.2024.05.28.11.31.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 11:31:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192869-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-192869-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192869-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id D23A41F23AAD for ; Tue, 28 May 2024 18:31:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9376A502B2; Tue, 28 May 2024 18:31:32 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB46517E8E6 for ; Tue, 28 May 2024 18:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716921092; cv=none; b=Nr7KbeTXWVJFU3ykYv2iO0pCjuJCzIpPQ2vOqJwhZYnXb7lwfyU6t5N4mgBiH+3jnnSYsXenUaz8V/GIMQJpvbdNGHiDlwP3Y5Uxu4DiQNk914QQ7pf0r/2RcLkDAACBQHMxWRPzPLpbScs7kAw4VvFMdTM0DI+yGb3LVzM11vY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716921092; c=relaxed/simple; bh=vCmeOebE2/PfKzBSGDN/NHIlwucHYheNUwR33EE922k=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=irelf9n6F2U4lcVlHlCeqdDSkGcyHzKbU0gFSKzeLDXzn8OVPHH5ftBuymS9uK1miHr287poNOLEEyw7DbATh9jRo4I9Sb7n6V2JZyN9tJsZpB01SrF0mnSdEdNr3b0VzjgsY898KC5KcZWQywGH7SlR8yoLynWKMYiQ1RBFjFM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 45609339; Tue, 28 May 2024 11:31:53 -0700 (PDT) Received: from [10.57.39.137] (unknown [10.57.39.137]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D1C733F641; Tue, 28 May 2024 11:31:27 -0700 (PDT) Message-ID: Date: Tue, 28 May 2024 19:31:25 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/1] iommu/dma: Make SG mapping and syncing robust against empty tables To: Andy Shevchenko , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Joerg Roedel , Will Deacon , =?UTF-8?Q?N=C3=ADcolas_F_=2E_R_=2E_A_=2E_Prado?= References: <20240527232625.462045-1-andriy.shevchenko@linux.intel.com> From: Robin Murphy Content-Language: en-GB In-Reply-To: <20240527232625.462045-1-andriy.shevchenko@linux.intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2024-05-28 12:26 am, Andy Shevchenko wrote: > DMA mapping and syncing API might be called for the empty SG table where > number of the original entries is 0 and a pointer to SG list may be not > initialised at all. This all worked until the change to the code that > started dereferensing SG list without checking the number of the > original entries against 0. This might lead to the NULL pointer > dereference if the caller won't perform a preliminary check for that. > Statistically there are only a few cases in the kernel that do such a > check. Yes, the ones which play the rather fragile trick of keeping a potentially-invalid scatterlist around and relying on its orig_nents value to be able to tell whether they're supposed to be using it or not. It's not the DMA API's job to hide bugs in callers trying to be clever but failing. > However, any attempt to make it alinged with the rest 99%+ cases > will be a regression due to above mentioned relatively recent change. > Instead of asking a caller to perform the checks, just return status quo > to SG mapping and syncing callbacks, so they won't crash on > uninitialised SG list. > > Reported-by: NĂ­colas F. R. A. Prado > Closes: https://lore.kernel.org/all/d3679496-2e4e-4a7c-97ed-f193bd53af1d@notapiano > Fixes: 861370f49ce4 ("iommu/dma: force bouncing if the size is not cacheline-aligned") > Fixes: 8cc3bad9d9d6 ("spi: Remove unneded check for orig_nents") > Signed-off-by: Andy Shevchenko > --- > drivers/iommu/dma-iommu.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index f731e4b2a417..83c9013aa341 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -1108,6 +1108,9 @@ static void iommu_dma_sync_sg_for_cpu(struct device *dev, > struct scatterlist *sg; > int i; > > + if (nelems < 1) > + return; > + This nicely illustrates how wrong said callers are, since even if it *were* legitimate to attempt to map a 0-length scatterlist, it still wouldn't be valid to sync it after the initial mapping didn't succeed. Thanks, Robin. > if (sg_dma_is_swiotlb(sgl)) > for_each_sg(sgl, sg, nelems, i) > iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg), > @@ -1124,6 +1127,9 @@ static void iommu_dma_sync_sg_for_device(struct device *dev, > struct scatterlist *sg; > int i; > > + if (nelems < 1) > + return; > + > if (sg_dma_is_swiotlb(sgl)) > for_each_sg(sgl, sg, nelems, i) > iommu_dma_sync_single_for_device(dev, > @@ -1324,6 +1330,9 @@ static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg, > struct scatterlist *s; > int i; > > + if (nents < 1) > + return nents; > + > sg_dma_mark_swiotlb(sg); > > for_each_sg(sg, s, nents, i) {