Return-Path: Received: from mail-by2nam01on0129.outbound.protection.outlook.com ([104.47.34.129]:5824 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S934475AbeCCWlo (ORCPT ); Sat, 3 Mar 2018 17:41:44 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Nate Watterson , Joerg Roedel , Sasha Levin Subject: [PATCH AUTOSEL for 3.18 33/63] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Date: Sat, 3 Mar 2018 22:33:29 +0000 Message-ID: <20180303223228.27323-33-alexander.levin@microsoft.com> References: <20180303223228.27323-1-alexander.levin@microsoft.com> In-Reply-To: <20180303223228.27323-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Nate Watterson [ Upstream commit 5016bdb796b3726eec043ca0ce3be981f712c756 ] Normally, calling alloc_iova() using an iova_domain with insufficient pfns remaining between start_pfn and dma_limit will fail and return a NULL pointer. Unexpectedly, if such a "full" iova_domain contains an iova with pfn_lo =3D=3D 0, the alloc_iova() call will instead succeed and return an iova containing invalid pfns. This is caused by an underflow bug in __alloc_and_insert_iova_range() that occurs after walking the "full" iova tree when the search ends at the iova with pfn_lo =3D=3D 0 and limit_pfn is then adjusted to be just below that (-1). This (now huge) limit_pfn gives the impression that a vast amount of space is available between it and start_pfn and thus a new iova is allocated with the invalid pfn_hi value, 0xFFF.... . To rememdy this, a check is introduced to ensure that adjustments to limit_pfn will not underflow. This issue has been observed in the wild, and is easily reproduced with the following sample code. struct iova_domain *iovad =3D kzalloc(sizeof(*iovad), GFP_KERNEL); struct iova *rsvd_iova, *good_iova, *bad_iova; unsigned long limit_pfn =3D 3; unsigned long start_pfn =3D 1; unsigned long va_size =3D 2; init_iova_domain(iovad, SZ_4K, start_pfn, limit_pfn); rsvd_iova =3D reserve_iova(iovad, 0, 0); good_iova =3D alloc_iova(iovad, va_size, limit_pfn, true); bad_iova =3D alloc_iova(iovad, va_size, limit_pfn, true); Prior to the patch, this yielded: *rsvd_iova =3D=3D {0, 0} /* Expected */ *good_iova =3D=3D {2, 3} /* Expected */ *bad_iova =3D=3D {-2, -1} /* Oh no... */ After the patch, bad_iova is NULL as expected since inadequate space remains between limit_pfn and start_pfn after allocating good_iova. Signed-off-by: Nate Watterson Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/iova.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index f6b17e6af2fb..c9e54a0b4bd1 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -118,7 +118,7 @@ static int __alloc_and_insert_iova_range(struct iova_do= main *iovad, break; /* found a free slot */ } adjust_limit_pfn: - limit_pfn =3D curr_iova->pfn_lo - 1; + limit_pfn =3D curr_iova->pfn_lo ? (curr_iova->pfn_lo - 1) : 0; move_left: prev =3D curr; curr =3D rb_prev(curr); --=20 2.14.1