Received: by 10.223.185.116 with SMTP id b49csp1086414wrg; Sat, 3 Mar 2018 15:10:21 -0800 (PST) X-Google-Smtp-Source: AG47ELvN+AB2mq101cJ5k894+9MeolDwZUcGZnjF1KOt505qgOUFBxqKXlTOkYJN8VkTn86MJxC8 X-Received: by 10.99.110.70 with SMTP id j67mr8319771pgc.202.1520118621377; Sat, 03 Mar 2018 15:10:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520118621; cv=none; d=google.com; s=arc-20160816; b=S+PZoMJqoFHXHGf9RSnKGzHTE0wVsZq4uPPSNxrPIL58ncbicDg4rwfWKq2da+PIzE Vv6euIrBlONG9vHj+sWi1DcNS9LrkquY3nQe1UTkvpa3sDYNctT6BH8ZxGnmzEY5Sk5Z 0FCGdyvAdVCJtFVliExGO8YU/vW3hmXd67BBJsD2hbWJbaOdjf3n7DzBm9p+SPv4e6S5 584p7gZ4YTlbV+bqI+hkacRxY8XtBJFY1JcsBXXR+sthboitESwc3nb+stD2vyMARW6c GKv9dSN9QHF0gEXvGBnytwc3m0PeQIjD++XG84qTXpjl+Z8LhpvzrNLS6vr8yH/geCfh ZltQ== 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=XCuG21pviwqVjaflM04vQ7skPM1h2Dxh3ws+cIs+e9M=; b=x2BJD48nn//CLRdyBXPqRQOlQf5NHBrcLpbrckEOin+trgZ94Uru6yt6klP9shV1D/ Yyv41AjPP6cnyiy4CjKJA2nVjmNUDxo4Rb/3bhbmLTl6u9R4biAQBG721kLA8MSBM5e+ CESvN/agQMwKHMQfCR3nFAMkP2Zl91j4SEAjBP1gsL5bsDHy7+C1uAGGEOtf7CJnUjA3 qEDSICFZ498RvU1ic1Lo9+GQtXihda08Xx3KuNKt4SgjP12izFAObPaFBVXobE/rFu4G oOoCu75TxNgej541oTeKHXJBfmxxUuALNmVHDcvElmYDRS91ZVBaLdbd6dfM58+L5vt7 R3Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=GRxBcK4P; 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 a31-v6si6905107pla.427.2018.03.03.15.10.07; Sat, 03 Mar 2018 15:10:21 -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=GRxBcK4P; 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 S934966AbeCCXJT (ORCPT + 99 others); Sat, 3 Mar 2018 18:09:19 -0500 Received: from mail-co1nam03on0096.outbound.protection.outlook.com ([104.47.40.96]:45422 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S934399AbeCCWjy (ORCPT ); Sat, 3 Mar 2018 17:39:54 -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=XCuG21pviwqVjaflM04vQ7skPM1h2Dxh3ws+cIs+e9M=; b=GRxBcK4PMupQXtYv/oyOrs4+5ZXJqM5b6ZDiv+cQl5xlT03Ir3UBCtpu6gogf9ll0+KF5UtW+E+kzVyahvEdLXb5zFZn8ODytsayp0aLHDupFlCBzCPfVR06UCa7VyabjIm4AUSPc/O2kP7XzyEq2C9p7XQJhUzusZ1GfBUF81M= Received: from MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) by MW2PR2101MB1113.namprd21.prod.outlook.com (52.132.149.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.588.1; Sat, 3 Mar 2018 22:39:52 +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:39:52 +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.4 061/115] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Thread-Topic: [PATCH AUTOSEL for 4.4 061/115] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Thread-Index: AQHTsz9cK4spez15EEG6Y6t2XKzQeQ== Date: Sat, 3 Mar 2018 22:31:24 +0000 Message-ID: <20180303223010.27106-61-alexander.levin@microsoft.com> References: <20180303223010.27106-1-alexander.levin@microsoft.com> In-Reply-To: <20180303223010.27106-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;MW2PR2101MB1113;7:wCvottOP1b7U8htBhMY6VzKc1Yv8GqXo86YmkJUz/tX2+WtQCIBVwoEi/c1hXPCXnEeBohmXvgnzK1lyhkdAfcpD7x0oMWzp3QohtBm+Qq6u0Ua0t9/i0487yKnavixhGjEUsTg5zcgpfldJynj9f3dXwwwR4vLHNebxCxN4SCkxnl9OSF/7wBDLQgSqPgRZKCwrSyHW7EtISMJpPAS372UVYmuM73+z+W5FMj+ouZbfY7h0L8S8tKI6SVaZWYp5 x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 07493800-d432-448c-cf31-08d58157adfc x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603307)(7193020);SRVR:MW2PR2101MB1113; x-ms-traffictypediagnostic: MW2PR2101MB1113: 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)(3002001)(3231220)(944501244)(52105095)(10201501046)(93006095)(93001095)(6055026)(61426038)(61427038)(6041288)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123564045)(20161123558120)(20161123562045)(6072148)(201708071742011);SRVR:MW2PR2101MB1113;BCL:0;PCL:0;RULEID:;SRVR:MW2PR2101MB1113; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(366004)(346002)(39860400002)(396003)(376002)(199004)(189003)(68736007)(7736002)(10090500001)(6116002)(3846002)(1076002)(2906002)(5250100002)(6486002)(2501003)(6436002)(76176011)(575784001)(86362001)(22452003)(6512007)(53936002)(66066001)(86612001)(99286004)(59450400001)(2900100001)(316002)(36756003)(54906003)(106356001)(110136005)(6506007)(102836004)(186003)(97736004)(26005)(6346003)(14454004)(3660700001)(8936002)(107886003)(10290500003)(5660300001)(81166006)(478600001)(3280700002)(105586002)(8676002)(81156014)(4326008)(72206003)(6666003)(2950100002)(305945005)(25786009)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:MW2PR2101MB1113;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) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: EdFPW96PUlYT3hrFZ1ODpxy9FFYFaZN71idpqCfm9WXIiOfmdu8wP0EyvZVwROclN+gtF0hwaSQsUSCdf0YBEoKJKLxPvGMvi2DtMiCgkCLeYtifvAvgrFC0KvoLARDR5OFmdxIgELmRCZwCQAEU0icrkkvHeKuUZUrJRubgiiw= 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: 07493800-d432-448c-cf31-08d58157adfc X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 22:31:24.2759 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR2101MB1113 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 fa0adef32bd6..62739766b60b 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -126,7 +126,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