Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp275759pxu; Thu, 15 Oct 2020 03:49:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuEbrvdrJmHZp1sVG2WYmTEJSBtM8+w0xbKFKEvi9X9rT5PaXgoCbvvsLAHbsKJz8JD2Wv X-Received: by 2002:a05:6402:31af:: with SMTP id dj15mr3515500edb.275.1602758961880; Thu, 15 Oct 2020 03:49:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602758961; cv=none; d=google.com; s=arc-20160816; b=RSaVHaDhKmyQiGELi3ZSBoIyBL2EdBItQbwra2EoZJYxx4w+Yc3vpMQQjsJeiEbXY+ 5thOMF5QzzE2PEkeoa1k+J572BE95hvJ1IBfR4cqzaJahQrjxZrF+Fn/xYd1iK3rv7Jr EHxegTfIMFIEXZFeFquLb/wEE68vW1eGOwkYg3PdkM0A8rGjlmV3wYiUneN2nGv4WmUB K4IXoZIHMnND+t2VoPIhxRzTYP4wwYzxef2obyxS4BUzG5lXKx8AYSbcnTetwWZ5HxxZ R84futxd1KAleLN2F4LJpKW5UO8rlYEaDsZ4HIDdOtkQ9/YLKK/klsKhaiWP/qDPNobQ VSsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=BQBVnG15Kw8Umi73MhGUn+bjR1qFEnsjXl32lHpsQyM=; b=b9YvPeIh1CAsvv0oSLRsFe7fhDZ84fEFXcsz88jllTNtr2M3Ksgv95wbrjej+II8ZC dQ4Tm5YHi6kIrP4gzZz1NgbQgD60uV7UH+x/RP5cRitVgBqRDlP5vC5P71/fnnH6/Xsp q5Ra2pfbujgJEYGQYXj1HtotTDNCOd8/A4kG6mkNpqufi+DRzVsoPHTUoKCi41VP12TV TCViLVioJE1QGQg6bxDUJFAkbPjP1Wpj8yF9xsYkXRx3Fpp4Lqj23zkNVD0GUrbIywjr u68ZfHzBfplfus4lHNXmwTj5HGiBMAmt3V3tdCIOJSQQrzqDK/3r5E1bN1vDSSB1z7II Nu9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z8si1825209eju.267.2020.10.15.03.48.58; Thu, 15 Oct 2020 03:49:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728458AbgJOFid (ORCPT + 99 others); Thu, 15 Oct 2020 01:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728267AbgJOFid (ORCPT ); Thu, 15 Oct 2020 01:38:33 -0400 Received: from bmailout1.hostsharing.net (bmailout1.hostsharing.net [IPv6:2a01:37:1000::53df:5f64:0]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23E7CC061755 for ; Wed, 14 Oct 2020 22:38:33 -0700 (PDT) Received: from h08.hostsharing.net (h08.hostsharing.net [83.223.95.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.hostsharing.net", Issuer "COMODO RSA Domain Validation Secure Server CA" (not verified)) by bmailout1.hostsharing.net (Postfix) with ESMTPS id ED8363000C9B0; Thu, 15 Oct 2020 07:38:29 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id D141C1C023C; Thu, 15 Oct 2020 07:38:29 +0200 (CEST) Date: Thu, 15 Oct 2020 07:38:29 +0200 From: Lukas Wunner To: Mark Brown Cc: Vladimir Oltean , Florian Fainelli , "linux-kernel@vger.kernel.org" , linux-spi , Sascha Hauer Subject: Re: Use after free in bcm2835_spi_remove() Message-ID: <20201015053829.GA2461@wunner.de> References: <20201014140912.GB24850@wunner.de> <20201014194035.ukduovokggu37uba@skbuf> <20201014202505.GF4580@sirena.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201014202505.GF4580@sirena.org.uk> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [cc += Sascha] On Wed, Oct 14, 2020 at 09:25:05PM +0100, Mark Brown wrote: > > On Wed, Oct 14, 2020 at 04:09:12PM +0200, Lukas Wunner wrote: > > > Apparently the problem is that spi_unregister_controller() drops the > > > last ref on the controller, causing it to be freed, and afterwards we > > > access the controller's private data, which is part of the same > > > allocation as struct spi_controller: > > Right, the proposed patch is yet another way to fix the issue - it all > comes back to the fact that you shouldn't be using the driver data after > unregistering if it was allocated as part of allocating the controller. > This framework feature is unfortunately quite error prone. How about holding a ref on the controller as long as the SPI driver is bound to the controller's parent device? See below for a patch, compile-tested only for lack of an SPI-equipped machine. Makes sense or dumb idea? If this approach is deemed to be a case of "midlayer fallacy", we could alternatively do this in a library function which drivers opt-in to. Or, given that the majority of drivers seems to be affected, make it the default and allow drivers to opt-out. -- >8 -- diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 0cab239..5afa275 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2399,6 +2399,11 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr, extern struct class spi_slave_class; /* dummy */ #endif +static void __spi_controller_put(void *ctlr) +{ + spi_controller_put(ctlr); +} + /** * __spi_alloc_controller - allocate an SPI master or slave controller * @dev: the controller, possibly using the platform_bus @@ -2414,6 +2419,7 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr, * This call is used only by SPI controller drivers, which are the * only ones directly touching chip registers. It's how they allocate * an spi_controller structure, prior to calling spi_register_controller(). + * The structure is accessible as long as the SPI driver is bound to @dev. * * This must be called from context that can sleep. * @@ -2429,6 +2435,7 @@ struct spi_controller *__spi_alloc_controller(struct device *dev, { struct spi_controller *ctlr; size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); + int ret; if (!dev) return NULL; @@ -2449,6 +2456,13 @@ struct spi_controller *__spi_alloc_controller(struct device *dev, pm_suspend_ignore_children(&ctlr->dev, true); spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); + spi_controller_get(ctlr); + ret = devm_add_action(dev, __spi_controller_put, ctlr); + if (ret) { + kfree(ctlr); + return NULL; + } + return ctlr; } EXPORT_SYMBOL_GPL(__spi_alloc_controller);