Received: by 10.213.65.68 with SMTP id h4csp13831imn; Mon, 19 Mar 2018 17:55:13 -0700 (PDT) X-Google-Smtp-Source: AG47ELtARv6YEX2kGp2jmKwg/2T+/YMys4tmGv4J+Y4r/WXYCLqzD4igxfUmsrBeNy8ngchmW7o2 X-Received: by 10.98.11.19 with SMTP id t19mr10708290pfi.218.1521507313862; Mon, 19 Mar 2018 17:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521507313; cv=none; d=google.com; s=arc-20160816; b=Py5ThILMUFTmW2ro83hTyJ2B8+6Z9GQ8ys/hwwF6GOSpCwy9EM3NKbC/H4APj6d1aC 7eypl1Da90uzV98H1RFYWsYqB5V3r+iK5oT2N+5ZpSbZLL/Xu7hdjsxr1CerkUSg9Iqe oTkXJ0zl2GyXXS/9lsSz0bEl0ei/Td0RtUC5yGT7Xo63kmOlkoQoe71gJvgUzCazc+fr RuQOQlyHE36ktuVVeY3MUSl7EXeUf4G/wlBul35N1rH19WH8Upn6z9qmM7OJV3wdRWhW s+QMB9SC08G1YgEZe3l1VeVGKqs8ArMYe+LfzyFixLALSkOa0RbZTbONdKx59Szduehd HRzQ== 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=zfQ6+Rh1IJJ35n/fsJiEOevMPjozhzSkWj7jsT+pRX4=; b=YQsYOs5Sa/y2xlhfcZl6bf4TkxBSXOEszoFva5G6XaGTT8bLU6CLiiKFx59cgR3+cD OGTaPppEeFCgSlxDjOqBG/4Xmzoa0SWVvJVpaiEo2EElFRW5QpmbcOTdbfYouqB1SejI FNZknnRu7tgeW+8PULVA7jEfw3GCNhx/HUj9/YEuafZw5LdDQTBBLiiCwizQQOd6Ja6L IOnx2Y/vvhVWtCiT5sUXs4JCPxvtdQDrnK3C+zOOhN7SAd5c8k3Q/zEcK6WQWKNdW+eU pLWYwmBxRbrnNHHRvb3YwGui6l5n401DC+X2JIBdSZjry/dlPwYheX96aYt18qjbniWR J/QQ== 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 k17si334350pgf.534.2018.03.19.17.54.59; Mon, 19 Mar 2018 17:55:13 -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 S1031402AbeCSSXQ (ORCPT + 99 others); Mon, 19 Mar 2018 14:23:16 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:48774 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031363AbeCSSXH (ORCPT ); Mon, 19 Mar 2018 14:23:07 -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 D67DD12AB; Mon, 19 Mar 2018 18:23:05 +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.9 117/241] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Date: Mon, 19 Mar 2018 19:06:22 +0100 Message-Id: <20180319180756.045874894@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180751.172155436@linuxfoundation.org> References: <20180319180751.172155436@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.9-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 @@ -138,7 +138,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);