Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1185936pxf; Fri, 9 Apr 2021 02:06:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcLCMnjW/lOUzk2n5mh3wG1hatbN+6jKa2sL+TxxrVfS15bytZGLv2Vqx3UMncyXB27Gsh X-Received: by 2002:a65:6143:: with SMTP id o3mr2189691pgv.332.1617959206958; Fri, 09 Apr 2021 02:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617959206; cv=none; d=google.com; s=arc-20160816; b=ikPlakrd/jalW2NRwxgxfluvgObuilWkUgrgQdqPMZ/kyPb+a/ZoynhOyQHTgJpSwH 2W+PL7zRDmJ8wTlDv4KpXY7FaHU1kdGdlGzB8fHeloFbTv6lm0uDSjhmPkqj3nUfgAjM ZJLjfaYoD8qtm5ht8Em3DLnOQ7jFsVzWxWMdPK7StCC4dith2sXbEJBZOv94A/o/tB+F /ju5m++FSDQai2nummxUP7SJAUJDcd2FItYYcEFX6XVS997mTcDu30HgCYMFh9EXWjJS hJ77VMlWPBseFvkeJ8QXIB9aZ0Lq2iJH5aPKVfIEOq/wm1HXwosMBKYwAfSSE4Pv58bS 0lRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Diu26lQeyc9P30LZFZqyI13l/S31KxD5z4XWDOuH4g8=; b=g7DAbkfSI072nie1LB8jy9DAJ9XK5ai+zPLj0riT9mWBsJk3u4c76P/Wgo+KDEtzXM wpxdE3lSxmuV6KyKfgRKmVdREaJCe6TsrGe0lXhxCLJVtMFO3c1eBFLE+2EtpxxLCKXp x+LiKPiwFHION3tigjgzPS6kCfLmaZTjqX1g65hgHEnnGp3gyJUdEX8V6M5L4sueKJ93 zdKDRr0SvduYlBAo9QIuJDSKmB2slNOA8I2RboYvRwfoeAzMErD7PbRU4s8ttXdoRh8y PKvHXgUraFLx7VDf/LSJDECQakBrJjHPi9J+YMTki02EQeMX84aBRQEBfPJHy8OvtzmE AGFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qERsISDl; 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 h4si2276849pgk.363.2021.04.09.02.06.34; Fri, 09 Apr 2021 02:06:46 -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=qERsISDl; 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 S232332AbhDIJFd (ORCPT + 99 others); Fri, 9 Apr 2021 05:05:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231621AbhDIJFb (ORCPT ); Fri, 9 Apr 2021 05:05:31 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDC38C061760 for ; Fri, 9 Apr 2021 02:05:18 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id z8so5558500ljm.12 for ; Fri, 09 Apr 2021 02:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Diu26lQeyc9P30LZFZqyI13l/S31KxD5z4XWDOuH4g8=; b=qERsISDlAU/QOVO75hzThfBtSMeroZxxnjblbrgURP4R2TDm1x0A7b8Dc27BY0Jtyw UrbSmqFt4gZo47ulHn+1RV7RF7JYmm6OzbUpAIZf3X7SK341PuXhYT3It1fINRnlz6Wi HSpj5ivgzBbRfB9g+uP/d6IbtRfGOMFILLKMyAJ8843uEsVQR8GodsRNZTJ5Mww0aJYw o4+voc/hj6ou6zm8uP+gcCd7hfQ49QBW+Av5UPmc7QpQdoOIG56OZLxnkz34sNKaSy8e dZtN2n47F6NeAY+feM8GVHMmYCwSQiFV60IHITad2s9qs73k3ERomNerayWcRkTzki0v G69w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Diu26lQeyc9P30LZFZqyI13l/S31KxD5z4XWDOuH4g8=; b=HaHPXG2+K3n26eh8OPSEKLg5LiE7TIZpBIEiDBdkuQaqCInR/6029JQtCm15Lz89Ab Sge/R1f51741NEiZCgiYFhOywbeCv0jkrnZ9+ph7mRUS+QUsFYPd6768zw2fedkqhP+a hk4y6sEOy4z12iw7oNhnhKQsnVWJyTXPXg5stBt/Gvb+mp8U2UvakMbmSUWAlNuGjYFD gZCsNuAMa/Lde3tYOlMNBHnJ5gl5+Jw50RLYh3nvVwRScqivWftTPalqHogYTJDmXcia n+zV57+xazg6PJW+i0b6OwU/TbIzIoi7wM0KDUZGkFywbfng3Er1Y6m511TxFbiRXo6k FP6w== X-Gm-Message-State: AOAM533/szTNSMccHlv1nqfpZ+lOqy6aFcBohUVhrXCCmOcjF0KDv0w2 0i8MJBw709vjdenaoJMUQNQd2dfUsOnlnBj9qdSkug== X-Received: by 2002:a2e:b014:: with SMTP id y20mr8760575ljk.464.1617959117051; Fri, 09 Apr 2021 02:05:17 -0700 (PDT) MIME-Version: 1.0 References: <20210407095527.2771582-1-wak@google.com> In-Reply-To: From: William Kennington Date: Fri, 9 Apr 2021 02:05:05 -0700 Message-ID: Subject: Re: [PATCH] spi: Fix use-after-free with devm_spi_alloc_* To: Andy Shevchenko Cc: "broonie@kernel.org" , "linux-spi@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Joel Stanley Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For the header file comment? I think it's actually backwards since `devm_allocated =3D true` would indicate the device is managed with devres. Should I send a followup change or v2? On Fri, Apr 9, 2021 at 12:20 AM Andy Shevchenko wrote: > > > > On Wednesday, April 7, 2021, William A. Kennington III w= rote: >> >> 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+0x1= 08/0x174 >> [] (refcount_warn_saturate) from [] (kobject_put+0x9= 0/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_no= des+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_dr= iver+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 alloc= ation") >> 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 =3D __spi_alloc_controller(dev, size, slave); >> if (ctlr) { >> + ctlr->devm_allocated =3D true; >> *ptr =3D ctlr; >> devres_add(dev, ptr); >> } else { >> @@ -2842,11 +2843,6 @@ int devm_spi_register_controller(struct device *d= ev, >> } >> EXPORT_SYMBOL_GPL(devm_spi_register_controller); >> >> -static int devm_spi_match_controller(struct device *dev, void *res, voi= d *ctlr) >> -{ >> - return *(struct spi_controller **)res =3D=3D 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_controll= er *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 s= lave */ >> >> + /* flag indicating this is a non-devres managed controller */ > > > > Isn=E2=80=99t =E2=80=9Cnon-=E2=80=9C part confusing a lot? > >> >> + bool devm_allocated; >> + >> /* flag indicating this is an SPI slave controller */ >> bool slave; >> >> -- >> 2.31.0.208.g409f899ff0-goog >> > > > -- > With Best Regards, > Andy Shevchenko > >