Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp869928pxf; Wed, 7 Apr 2021 13:46:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdP27MXe/mkOBSSghxEJKpaqV2cvp/R9vvOKzBJmleeb1L7TQ+UpuavSAONfzDEBExUKu7 X-Received: by 2002:a17:906:2818:: with SMTP id r24mr6008332ejc.502.1617828365743; Wed, 07 Apr 2021 13:46:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617828365; cv=none; d=google.com; s=arc-20160816; b=PVVWw2+Al8TXILhCImbC/1SUd/AejcMVT9uxIXS6a2RlxahcNs3C6635fzplclqPzB eHt4c9Y8pU5CgGBIrYBhwvNdF6nO8u2Yrah/dU/jivJeY5FZ3O+CTin4Gk8z3nTHZjED cgnGnl7JgGDSZWZimozRMkAusvgW+UnrnNB3/Tqk4TluouXtb/Lu9ZfSUrIQ//hRyC5Z XzybDD/lITi1FU339Ou4Dbw1bincl1Y7cNStAQ9rb5oK6th5vBfKv0ck9XnWdkiEPeyb xXg223KvlCtmKzuy3O8RKWvPB2pdiBdwmoOX5lCxaZF147+kiHPFRPR2A8ymGHJgIOce SFIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=VWa5xUA+9GCbJ9DlX5ZS/vYDVMc9Ch1YyfdrjHd7dd4=; b=sXsnQ2C7e6/RKFywBS2hfbMuNOoC9O7JvQTFN0w7VKjLvst0epDH0wutUEpKVqz39x x9qiBgHR/87eR0KLZDIz6an3FX5eoTwMproJ9Yh8f6S6HWOJfXzEPMX0wzzEOD4Ktti5 7BpHptoWjXaapKKUAna0YM/Icx0axRp00EBqcobOs6N74f/pqSOtreA2dbaeu1ysaiL4 wfHZhZYnxqUTu8HnjV3t6QvVpnyibvZoo9DLcv469ch2v+II7C5WKOxuI8pVl3xLcLiY xO/PY865osyReizBhzpZ7/TAO3w6JGaIzy+qiShkfPN58hywlakCLgS0no1dN8SxBnn+ vtCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=l4ISsIo1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w22si18625945edd.295.2021.04.07.13.45.42; Wed, 07 Apr 2021 13:46:05 -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=@google.com header.s=20161025 header.b=l4ISsIo1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234496AbhDGJ4D (ORCPT + 99 others); Wed, 7 Apr 2021 05:56:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346426AbhDGJ4B (ORCPT ); Wed, 7 Apr 2021 05:56:01 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41561C06175F for ; Wed, 7 Apr 2021 02:55:52 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id d1so23204878ybj.15 for ; Wed, 07 Apr 2021 02:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=VWa5xUA+9GCbJ9DlX5ZS/vYDVMc9Ch1YyfdrjHd7dd4=; b=l4ISsIo1bCuQjUNIgt5hBLv04y2KMWkLr8ycFUvTmUS1L60UoCeg6r+xF2uXACr9G7 gtff5aMua90RcvRhqs0K6ZxyqBgkJLp/O8NIXvdzKp8NSYCQIAVKALwnZtmVzrnlTdqY pLkcrC9RQCmCdoXht41Ltsf1XOEA8n2es//viRP+JwcHmYZdpOdQZxnRB+lJbqEv16sJ 9sRqNu9M7dm/gaoNQLvhZAir9oSgytTkAkS8fgzEiQaaTmb5p9nZ+KHrc60CgtvlCvxJ 70LmqbFCnO4S53pFGa65A3NYsvKcDw1M9Q+5E2TJN+69oHiHX1MOidauxabSv9wWmBB8 eyPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=VWa5xUA+9GCbJ9DlX5ZS/vYDVMc9Ch1YyfdrjHd7dd4=; b=qoeeCxj2rK7NxfcBObRmgBUiy2WnzY0B64ibuaHULJSsKN0j/K9MmELPbUHU/yxcmS D9OwW+yTqKlyDCgHaHcq80jmStaV7uIijI5mSXJJyzhjxGGeLVMWug3qffqNXTs9PA/o VLKAfiILCTnw84NZhmmPmQuEp3nXWDL9riU54HDTnBYvjTKOisrZJd0Ds7NwZp1QtJZW 4KQ6ndQ6DSGZd0iKAUt3tuBpeFK+W5/LLXiSNwd5e0nHGGCQ823qYXZhs3IJF+NohWBX P34Fgikgf6Hamzb8qGUcvV6oX1UCKe+OnhZdTnXh5y+Trfyv7DAnZqr4fwEc8aibP9gW magQ== X-Gm-Message-State: AOAM533sCDoA8tqV5NkByscNNFhDSjHcIws9uqN8PKI+Fm4bMVETc5KZ S4y8kOdHxGAs0ScgnTKhEquj6F0= X-Received: from wak-linux.svl.corp.google.com ([2620:15c:2c5:3:d5a1:fccd:85fb:f380]) (user=wak job=sendgmr) by 2002:a25:d0c7:: with SMTP id h190mr3349871ybg.428.1617789351418; Wed, 07 Apr 2021 02:55:51 -0700 (PDT) Date: Wed, 7 Apr 2021 02:55:27 -0700 Message-Id: <20210407095527.2771582-1-wak@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.0.208.g409f899ff0-goog Subject: [PATCH] spi: Fix use-after-free with devm_spi_alloc_* From: "William A. Kennington III" To: broonie@kernel.org Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Joel Stanley , "William A. Kennington III" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 b08efe88ccd6..904a353798b6 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2496,6 +2496,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 { @@ -2842,11 +2843,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)); @@ -2893,8 +2889,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 592897fa4f03..643139b1eafe 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -510,6 +510,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.31.0.208.g409f899ff0-goog