Received: by 10.213.65.68 with SMTP id h4csp1792955imn; Mon, 19 Mar 2018 13:24:56 -0700 (PDT) X-Google-Smtp-Source: AG47ELumXsJ845QNH7IpgLpuM3EL59toTUXoKCULbNhrZPTy778R2z/UcmEFUdOhThcCSkOfH2Q3 X-Received: by 10.99.173.79 with SMTP id y15mr10021072pgo.136.1521491096516; Mon, 19 Mar 2018 13:24:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521491096; cv=none; d=google.com; s=arc-20160816; b=o7swZ8LcUjvEU6EAk+Lj8nkpMsD5US78OS+nON1vPq3vpmKNIGDUq5FFKtMC+SuTaN b+Pa2BBIC7qwMMdFP2fKowq4IAwWfxqzxm/Lfdnggra9njKi0hGPMARyJg/0ApLezNXT Uqh5/HRH5FzSj1xiUYHtVWFXuwJGyryUjnfpd+CSKp0IfQclFuYYV1r3F/cq6tdsxg/c KFqql5thpLi9xnwmvWXsEwwFLHz3IrRJL4XxG2a/zKNyo1s8NO7DEHogTA1/QolsFi9B MGZSPuSrtD0C5iqU81Lk+7vSxDz1oaMQrS/VZDEhNKdxammHvQJbJ/D28+s1+ojvpJ3p ZXiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=5UNdbxpeW/5c87aX9lYDFPAGGSgzMRNoHRwfeOgC8DE=; b=MN2gJELsb6bk9Z0bySom2aq4LoRWVGSnT4lI+Xw1gGDtGQ/EiJk+M5ioxciwRtsDgQ 3/MDaauq/F+ZsUOBS3ncTIEUkuWq4a+CmO00RbJE6ZS68vQwo/VBGGXzptWKII4bz1VB JmuJLl3oOrFa7eMGrhIFTffvcrV/zVabtBQQ77POdoexFWBIbhaNx3Ald+xk7hwWI/lY I5M6M/FrcnoU7+7qITkHpD34U9PFnvnEnJZu2bdea5JfdhHqC4Bk4fXmV2deAY28R3TW RzHqbpt6uxaxi5NCe/A8luzun+1kU0JY7vUF3s+b6O4M71Cg/CE6A4q+2vqIKzh5XVLK aD2w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w15-v6si24477plq.266.2018.03.19.13.24.42; Mon, 19 Mar 2018 13:24:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030673AbeCSSOY (ORCPT + 99 others); Mon, 19 Mar 2018 14:14:24 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:44062 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968844AbeCSSOS (ORCPT ); Mon, 19 Mar 2018 14:14:18 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 0E92BEFB; Mon, 19 Mar 2018 18:14:17 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nate Watterson , Joerg Roedel , Sasha Levin Subject: [PATCH 4.4 057/134] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Date: Mon, 19 Mar 2018 19:05:40 +0100 Message-Id: <20180319171857.581263008@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319171849.024066323@linuxfoundation.org> References: <20180319171849.024066323@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ 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 == 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 == 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 = kzalloc(sizeof(*iovad), GFP_KERNEL); struct iova *rsvd_iova, *good_iova, *bad_iova; unsigned long limit_pfn = 3; unsigned long start_pfn = 1; unsigned long va_size = 2; init_iova_domain(iovad, SZ_4K, start_pfn, limit_pfn); rsvd_iova = reserve_iova(iovad, 0, 0); good_iova = alloc_iova(iovad, va_size, limit_pfn, true); bad_iova = alloc_iova(iovad, va_size, limit_pfn, true); Prior to the patch, this yielded: *rsvd_iova == {0, 0} /* Expected */ *good_iova == {2, 3} /* Expected */ *bad_iova == {-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 Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/iova.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -126,7 +126,7 @@ static int __alloc_and_insert_iova_range break; /* found a free slot */ } adjust_limit_pfn: - limit_pfn = curr_iova->pfn_lo - 1; + limit_pfn = curr_iova->pfn_lo ? (curr_iova->pfn_lo - 1) : 0; move_left: prev = curr; curr = rb_prev(curr);