Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp340228pxj; Thu, 20 May 2021 10:30:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPkvjZfq+LGM4yGWDnjXBjH6/N2SVu2pl/XQV3phDWm/izC9SBqtEID09jFtbR0PTHdTJs X-Received: by 2002:aa7:cd8b:: with SMTP id x11mr6230983edv.87.1621531816511; Thu, 20 May 2021 10:30:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621531816; cv=none; d=google.com; s=arc-20160816; b=aAbWn0IMSdjdJXjUiQpQyhCQ55f7NXYfpIvGr7rKQ96RQesgE5+MoPe6p7+fvNUnDM +BkY69GSMklyXs1zWbMl5NPiVh4yjavW4hjUqxG0z+vAwsVdV9IoBtS53ahqO94K+AFs kieBvIFL0cflgXiiGOLLQzHBOvZl6+qQqCQESOIDrF0Y7pUn5OTyIG57nGs3/QzQcOmo EZPRAhdeYfWKL2EbAsQ/wvvX1lKNFhZPRXsMSgP6mSQ3nSnmJn6qdk2KWyzIGJBhqWgS BSaRHrSPLmqSlfUAloACnrReE/phaRcf4NzQuZvmxbJsd/jrkBF64ipmHGD+Uvu7ij7/ m6SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ty3sKcBmTR3B6fTL7o5W/Fh6CGb3PycpbIMNVEleO3c=; b=HVJmDLn7BfTcOKb3S7Sp1KKHQNsaOLotG/Hcv2WMZAC39z8GyRgQ4fzbAcs0So3Xm7 Bht2Sma364V9EWEwnWgxxO6AVAlKHHirWC49SEYQ7aScKv+fOqX3tvgZs9Y8t7RCWwqg E3O3SMsaROTAQmr7BHozq0IXz8FmsZ8/JItVbMGhqG6Isj8vIO/Ny+jOyfnBhZTsBTwD YmvAQrBSlpGUDsr8U+edJ/TwVTLuYU1Ppu9cu3q7J3TcAAU+vqUskF+udK4cuzKYubuV h2RVPSfa09zRM2A6Cu1wGDIcQU/zizHQLNmWuljBLt5MzmFPmCT2TuXN4tBwL+UYm3Op piKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=g61t9SHH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ds10si3820730ejc.559.2021.05.20.10.29.52; Thu, 20 May 2021 10:30:16 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=g61t9SHH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234350AbhETJ5I (ORCPT + 99 others); Thu, 20 May 2021 05:57:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:53062 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234240AbhETJx3 (ORCPT ); Thu, 20 May 2021 05:53:29 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C9D7E60241; Thu, 20 May 2021 09:36:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621503394; bh=F+1/Fe1GW30JKkImHUOHdOdDblEDnq9xFvjDfX7NlYA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g61t9SHHDwlCzPDAK4HvyljzyI6D26gts3CguhF4D+tfWr2hq4z8NaPCqBNN0MvRu VxkeKVKAjw30QjgrrpdJXeLWfbOSRDegHGZXsrcj0vTt1dBTojzEQYqXZuR58bh4YT mRKa169mxewy0BhGZ9xd6aVrVLuX2qS5KbBoFISg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "William A. Kennington III" , Mark Brown , Sasha Levin Subject: [PATCH 4.19 201/425] spi: Fix use-after-free with devm_spi_alloc_* Date: Thu, 20 May 2021 11:19:30 +0200 Message-Id: <20210520092138.019920715@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092131.308959589@linuxfoundation.org> References: <20210520092131.308959589@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: William A. Kennington III [ Upstream commit 794aaf01444d4e765e2b067cba01cc69c1c68ed9 ] We can't rely on the contents of the devres list during spi_unregister_controller(), as the list is already torn down at the time we perform devres_find() for devm_spi_release_controller. This causes devices registered with devm_spi_alloc_{master,slave}() to be mistakenly identified as legacy, non-devm managed devices and have their reference counters decremented below 0. ------------[ cut here ]------------ WARNING: CPU: 1 PID: 660 at lib/refcount.c:28 refcount_warn_saturate+0x108/0x174 [] (refcount_warn_saturate) from [] (kobject_put+0x90/0x98) [] (kobject_put) from [] (put_device+0x20/0x24) r4:b6700140 [] (put_device) from [] (devm_spi_release_controller+0x3c/0x40) [] (devm_spi_release_controller) from [] (release_nodes+0x84/0xc4) r5:b6700180 r4:b6700100 [] (release_nodes) from [] (devres_release_all+0x5c/0x60) r8:b1638c54 r7:b117ad94 r6:b1638c10 r5:b117ad94 r4:b163dc10 [] (devres_release_all) from [] (__device_release_driver+0x144/0x1ec) r5:b117ad94 r4:b163dc10 [] (__device_release_driver) from [] (device_driver_detach+0x84/0xa0) r9:00000000 r8:00000000 r7:b117ad94 r6:b163dc54 r5:b1638c10 r4:b163dc10 [] (device_driver_detach) from [] (unbind_store+0xe4/0xf8) Instead, determine the devm allocation state as a flag on the controller which is guaranteed to be stable during cleanup. Fixes: 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation") Signed-off-by: William A. Kennington III Link: https://lore.kernel.org/r/20210407095527.2771582-1-wak@google.com Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/spi/spi.c | 9 ++------- include/linux/spi/spi.h | 3 +++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 7dabbc82b646..bbe33016d371 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2084,6 +2084,7 @@ struct spi_controller *__devm_spi_alloc_controller(struct device *dev, ctlr = __spi_alloc_controller(dev, size, slave); if (ctlr) { + ctlr->devm_allocated = true; *ptr = ctlr; devres_add(dev, ptr); } else { @@ -2344,11 +2345,6 @@ int devm_spi_register_controller(struct device *dev, } EXPORT_SYMBOL_GPL(devm_spi_register_controller); -static int devm_spi_match_controller(struct device *dev, void *res, void *ctlr) -{ - return *(struct spi_controller **)res == ctlr; -} - static int __unregister(struct device *dev, void *null) { spi_unregister_device(to_spi_device(dev)); @@ -2395,8 +2391,7 @@ void spi_unregister_controller(struct spi_controller *ctlr) /* Release the last reference on the controller if its driver * has not yet been converted to devm_spi_alloc_master/slave(). */ - if (!devres_find(ctlr->dev.parent, devm_spi_release_controller, - devm_spi_match_controller, ctlr)) + if (!ctlr->devm_allocated) put_device(&ctlr->dev); /* free bus id */ diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 8ceba9b8e51e..16158fe097a8 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -450,6 +450,9 @@ struct spi_controller { #define SPI_MASTER_GPIO_SS BIT(5) /* GPIO CS must select slave */ + /* flag indicating this is a non-devres managed controller */ + bool devm_allocated; + /* flag indicating this is an SPI slave controller */ bool slave; -- 2.30.2