Received: by 10.223.185.116 with SMTP id b49csp1106449wrg; Sat, 3 Mar 2018 15:44:56 -0800 (PST) X-Google-Smtp-Source: AG47ELtSD5Wm0HCLRU/kiIgtBnhZYEoWHuQthj3mzctLdK7v0qVpgoNuoQtxTlfRr4uYDUqrR7KU X-Received: by 10.98.35.68 with SMTP id j65mr10351229pfj.5.1520120696560; Sat, 03 Mar 2018 15:44:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520120696; cv=none; d=google.com; s=arc-20160816; b=dDwZ0eeaib5JCTqSsGLV78+qXXfb5h3LSjuU8ePWmA6fzKmFW0TU6CxXP8jgNycYy/ BAtoLkbz3GxmvK+V17dmptrwrZxE2VWH2VDIXSjys/ej4XlHE2IFzefQnEZjjyrbEYaK VG0qKztbyyK5Vpm1ZP7SeXq5GgROHbB1bV927nt6dEFsGx+Mms3lTHbkJqo2RBjnnMpI f1z0ZfX8Ld7YJEREnThM2qky+oLi4rOXAySigr2rPIUgvv5+wSvu4YQgR2T0zZ3Kd4hk c5AhnOxBDkzmiLEYSZIREzKZXxuWZQNgQtQlckqrDjHkt/nutTSrud+ag7SZLpU4FqsT 4k2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=DrirjzM4/1VGyTFBEqdZpNEfEgoG6H6oQqsLICGqPFk=; b=zJbkRtnNi7QUQybEGEeKtoa2P+rCj6h9WZD9kngum9YV1eBtewS58EIbNP3vCvYoQC F2dt0ejrkZvpqlZ/HGRW0QX6NMBMFdxbCXtFGs1bmTdjexFmGAGisENIdgHam3Ti6jXd k59Jz06XX1/9oJnH2vcqkpMNY+AsB82NQ6U3jTH/V3VdSCQpA9gQHEeYvYDLjLeXtD1g 45uMut+eNbQBu2mkpZzAq+KNaKQjq4eMyPEcdPzIeowSjfmlZrf+2+cB67wlpsumFMlv jzTjQHpYwaNpsjgOzKOxyslPt7H+bmklk1YqnHbrG7Mh/IfE09TU69CYJvuHgRntTo6R jOCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=FXRkC9a9; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f30-v6si6934118plf.654.2018.03.03.15.44.42; Sat, 03 Mar 2018 15:44:56 -0800 (PST) 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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=FXRkC9a9; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934313AbeCCXm6 (ORCPT + 99 others); Sat, 3 Mar 2018 18:42:58 -0500 Received: from mail-by2nam03on0119.outbound.protection.outlook.com ([104.47.42.119]:12016 "EHLO NAM03-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933741AbeCCWer (ORCPT ); Sat, 3 Mar 2018 17:34:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=DrirjzM4/1VGyTFBEqdZpNEfEgoG6H6oQqsLICGqPFk=; b=FXRkC9a9650M2UPt+bMCr0TJFyu+n8ywCVg710hvcNhOe881mXV2l6OoE4vYE1OTArk0XIxbt1xMeSU1qIH3XciQc7rq20QuN2ZEtrPHPKI3Xt2bUX8Oxth08L8L1gm6Od84eaDDsgfJkcYGfycnoUEl9Euo27YRwvYq0s+sbr0= Received: from MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) by MW2PR2101MB1068.namprd21.prod.outlook.com (52.132.149.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.567.3; Sat, 3 Mar 2018 22:34:42 +0000 Received: from MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0]) by MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0%3]) with mapi id 15.20.0567.006; Sat, 3 Mar 2018 22:34:42 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Nate Watterson , Joerg Roedel , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 123/219] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Thread-Topic: [PATCH AUTOSEL for 4.9 123/219] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Thread-Index: AQHTsz8L08V1DDph70mUi7bdTnlA/Q== Date: Sat, 3 Mar 2018 22:29:07 +0000 Message-ID: <20180303222716.26640-123-alexander.levin@microsoft.com> References: <20180303222716.26640-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222716.26640-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;MW2PR2101MB1068;7:ZE0FVx3eT4i9cgMwEDBGY7hu+0RWGbXNZ3Mgk0e0VQc5SxGIo1zv/3Y+QLRAS3kuvI9ZJ7CgSZhrJMdB7IQUED2erOBGvvE+ZBsiePNSGmKC6Ryd//R6ogfen6+JpQZI8jL6vogO5ns142zQjTnqiuxdYjNE5NlZ2r5hsNLrDiMB1XnTUxKexpcjbQOJL6ySgkELNY5a8pKI4JKHsgH95hZHlBlifDnBt8bb1pWSAotkrH7Orh9h+CLMTSYEA+lz x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 49f274b3-148b-4e36-83c2-08d58156f578 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:MW2PR2101MB1068; x-ms-traffictypediagnostic: MW2PR2101MB1068: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040501)(2401047)(5005006)(8121501046)(93006095)(93001095)(3002001)(3231220)(944501244)(52105095)(10201501046)(6055026)(61426038)(61427038)(6041288)(20161123560045)(20161123558120)(20161123564045)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:MW2PR2101MB1068;BCL:0;PCL:0;RULEID:;SRVR:MW2PR2101MB1068; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(39380400002)(366004)(376002)(39860400002)(346002)(199004)(189003)(102836004)(59450400001)(6506007)(26005)(66066001)(4326008)(1076002)(105586002)(10290500003)(478600001)(72206003)(86612001)(14454004)(186003)(2906002)(2501003)(305945005)(7736002)(36756003)(3660700001)(5250100002)(25786009)(2950100002)(5660300001)(3846002)(2900100001)(3280700002)(6116002)(106356001)(22452003)(97736004)(316002)(575784001)(81166006)(10090500001)(81156014)(8936002)(8676002)(76176011)(6436002)(110136005)(53936002)(6486002)(54906003)(6512007)(86362001)(107886003)(68736007)(99286004)(6666003)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:MW2PR2101MB1068;H:MW2PR2101MB1034.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: QHIparo8U0ymA174eJeqVZLMEE/AB5imH1oeFC/TQpPJbdK9DMXpNKBrY77TSf3o/TRoFJFgWmg/ebA4qGz23i8YljvvMpbHrRzqpLZKO8UfqFshlHKYiMNUdl3AImH6wFQrXVijdMtRfIyB3NUBLOSpiG+nbjSG+7cvL42u7UE= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 49f274b3-148b-4e36-83c2-08d58156f578 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 22:29:07.5257 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR2101MB1068 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 e23001bfcfee..f106fd9782bf 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -138,7 +138,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