Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp341561ybl; Fri, 9 Aug 2019 06:51:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqzEN2c2Y5kKQMROU+5e50CniKMo4nWk72tHYqz2PjVo53TkH6YW40S2MQxxwFiGk3hB+XHj X-Received: by 2002:a63:31c1:: with SMTP id x184mr18133097pgx.128.1565358691285; Fri, 09 Aug 2019 06:51:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565358691; cv=none; d=google.com; s=arc-20160816; b=NLU/0+h1MYYhHSAcUdubJe2urV7OVMH6mJjcukucSUy95syE2G+RyvICaMO4ThM13B vMFqSXjar+1u9qCfq4uEF7ODQjWQHCujamwVgKOSim7DOeQZydemQqhvnKqhyf8sWf7F wzYXCKGTbGMw88bKVhKmaC2ywqNNraBXIM+t6tjL+VvNYXuzfsgYZUvQVrMMPXh/yaQ2 QylMXNIqh3CzpE8A7h0uP+Q97XI22iHzyrzDe0ZfXCplfC2/u/P30Tf+eIP03DZb34UQ AwtNpsptfpiFmO/lUou+7E7JQXUO/JdijXYVNxh+BL4U44VQ5v1N9zs45PF0ZDFH4qbD klbA== 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 :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:ironport-sdr :ironport-sdr; bh=qkcBE6Fwa95ceXeeQ3xZJLpofoCmwPz1D1OiAD1PG78=; b=ScStpSBbIl+bUy8wZCKYWJ2avk+Ji0LDuGIx6up3IZSYAhNbksOs2JXibjBnslB0jT fq/wkZZO4gFyKZS6jhOGLYGCzermdcsEkWaiEqeXZSEFUCSgDxvCr3pA/zzraOc41Dn1 ZUx+NVxGE63RYKnBIm5TF+CXp5ey/mHt3jDIAOgqfeOXSv38H81uh7i/HhzQb27bRPwj xlFpkYAwffa/YPuV5lImvHtgHHWUTzD5nbmpcHHpe5XU+3pIGvIYrJEfCvWQdds/xU5V s6rl9hrXqs6D5/hM9UVPb3ypIwTQ/UxAfuDloJMNkK1u3vj4cx1wQ2Mr99T4Zi9oHSfu rpJQ== 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 b13si8615229pgr.460.2019.08.09.06.51.15; Fri, 09 Aug 2019 06:51:31 -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 S1726297AbfHINun convert rfc822-to-8bit (ORCPT + 99 others); Fri, 9 Aug 2019 09:50:43 -0400 Received: from esa1.mentor.iphmx.com ([68.232.129.153]:33310 "EHLO esa1.mentor.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406503AbfHINun (ORCPT ); Fri, 9 Aug 2019 09:50:43 -0400 IronPort-SDR: c7XBxt0w41laaEA18M61aI1hwu2HMMisTHs2woexRiDSJ0iPn8M294kN0RfZp9AZ5hoh1iwHvG 79Vt8rxj5PidvY4ICsQizKJLBz32t8/lpenNz4ZoXoVm2PlhamSIWhfIkeyVYxSrs9GdNn8HA6 aCJfdfDnAwQ8x5Y2NxEr1stKrbRxKzVBL/LhE/fAMMgylrYhckjkt0/DnPYh/0VJOubHTYnv8a Zr9AGW8t6rauyfoFZNO7GFro8ZEv6BwhL5U9e8ouvD4hgJgtN2UKDEciqQJDn9Cw9UlBAxgWRj 8io= X-IronPort-AV: E=Sophos;i="5.64,364,1559548800"; d="scan'208";a="42128753" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa1.mentor.iphmx.com with ESMTP; 09 Aug 2019 05:50:30 -0800 IronPort-SDR: +/kcSjdizrCDDTWTsoaJ8omvSmgV5tCBzXiYGZ8iOfCyXZ+nMExm1sz9B7ZEb+tbYkPlcXUNSC KlO8rsrEtNRR1sTmI7OTozxL7BR0bFazNPYBjHzpWRbqaJtddCmlE8+A8XP3tLppA2XC2J1dQL aMwtlVc+CWpuOZ81lB3H2v4YUBmH0m1f0l/V/guBXQgqWoKHY+pIhYcjRqJn9cRE76icjv4Mm7 vZXtoyxG4FbSFpslO4LFT/sorcynZp/rSZWNfI/YK83Mje1o3i9A1f78IjLZ1F7vXGY+DWO2bb 84E= From: "Schmid, Carsten" To: "bp@suse.de" , "dan.j.williams@intel.com" , "mingo@kernel.org" , "dave.hansen@linux.intel.com" , "linux-kernel@vger.kernel.org" CC: "bhelgaas@google.com" , "osalvador@suse.de" , "rdunlap@infradead.org" , "richardw.yang@linux.intel.com" , "gregkh@linuxfoundation.org" , "torvalds@linux-foundation.org" Subject: Resend [PATCH] kernel/resource.c: invalidate parent when freed resource has childs Thread-Topic: Resend [PATCH] kernel/resource.c: invalidate parent when freed resource has childs Thread-Index: AQHVTrktoAw70LsxJE6oaOICoAXoow== Date: Fri, 9 Aug 2019 13:50:24 +0000 Message-ID: <1565358624103.3694@mentor.com> References: <1565278859475.1962@mentor.com> In-Reply-To: <1565278859475.1962@mentor.com> Accept-Language: de-DE, en-IE, en-US Content-Language: de-DE X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [137.202.0.90] Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a resource is freed and has children, the childrens are left without any hint that their parent is no more valid. This caused at least one use-after-free in the xhci-hcd using ext-caps driver when platform code released platform devices. Fix this by setting child's parent to zero and warn. Signed-off-by: Carsten Schmid --- Rationale: When hunting for the root cause of a crash on a 4.14.86 kernel, i have found the root cause and checked it being still present upstream. Our case: Having xhci-hcd and intel_xhci_usb_sw active we can see in /proc/meminfo: (exceirpt) b3c00000-b3c0ffff : 0000:00:15.0 b3c00000-b3c0ffff : xhci-hcd b3c08070-b3c0846f : intel_xhci_usb_sw intel_xhci_usb_sw being a child of xhci-hcd. Doing an unbind command echo 0000:00:15.0 > /sys/bus/pci/drivers/xhci_hcd/unbind leads to xhci-hcd being freed in __release_region. The intel_xhci_usb_sw resource is accessed in platform code in platform_device_del with for (i = 0; i < pdev->num_resources; i++) { struct resource *r = &pdev->resource[i]; if (r->parent) release_resource(r); } as the resource's parent has not been updated, the release_resource uses the parent: p = &old->parent->child; which is now invalid. Fix this by marking the parent invalid in the child and give a warning in dmesg. --- Advised by Greg (thanks): Try resending it with at least the people who get_maintainer.pl says has touched that file last in it. [CS:done] Also, Linus is the unofficial resource.c maintainer. I think he has a set of userspace testing scripts for changes somewhere, so you should cc: him too. And might as well add me :) [CS:done] thanks, greg k-h --- kernel/resource.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kernel/resource.c b/kernel/resource.c index 158f04ec1d4f..95340cb0b1c2 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1200,6 +1200,15 @@ void __release_region(struct resource *parent, resource_size_t start, write_unlock(&resource_lock); if (res->flags & IORESOURCE_MUXED) wake_up(&muxed_resource_wait); + + write_lock(&resource_lock); + if (res->child) { + printk(KERN_WARNING "__release_region: %s has child %s," + "invalidating childs parent\n", + res->name, res->child->name); + res->child->parent = NULL; + } + write_unlock(&resource_lock); free_resource(res); return; } -- 2.17.1