Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2924865rwb; Mon, 15 Aug 2022 14:09:50 -0700 (PDT) X-Google-Smtp-Source: AA6agR586qDItaG3yCunVRwNlSFoMNRaTbVPefXNSyKnIjF4UiUTklO4XBuopz5byWDkon3KWm+3 X-Received: by 2002:a17:906:9be1:b0:730:957f:34a3 with SMTP id de33-20020a1709069be100b00730957f34a3mr11479753ejc.47.1660597790477; Mon, 15 Aug 2022 14:09:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660597790; cv=none; d=google.com; s=arc-20160816; b=MpZjXv8NyD4YBmu/SgnwAjepQuLj8UKCK7Dj7bk1wyineHgkvdTxwYn1j3Lo7je2sE T41x06/ScAVlHFkNxh06pEFBV6SWX6C42/36oWJHkMLA/B4Zo7Pwurz2Ihr7yKVJh/t7 35QIXuOa4k3t6RTLKn3G9or20NAzHoxf1zh6OiMjC3eICxlNUy9utCK+18QBWCl/BX5h WeppoNMx9l7frl7YbL6tvUOFimIA4vDZKqHP2tXtVmWAr6CZcgLKxH/efGPoNdXOt+IN nZzyWGVzlGjyhf5xGp8PvGZQsAfKi+7yDdoO1Fl/sy1FMKV6apZJgzKfkiNJSqqseA2P z0EQ== 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=W4lE3r3g/7DadhOvdQvL4MWTNZCzHpra3obM8/VvUWg=; b=Hdh1xYxBBr6KdkybO/oNe1RpKrO83oqzVlzZ+Yjdt2Upm/oFaO/bY7eXwwdq5VVqN8 Q1FWn1Nzx7RU6atOJUEXKVua3FQ2Krc8T3PNCDXZ41zgmot+JDyE2p2B9MV0ZGxf08gf mqpb/Mwdrn1NWoV5TbgwW5TuruC54Lg/T7Q9ExrdlTfNiJL59/D8bK9ciOwiLQ2YmYn5 gq2JFAyHiNzpcsg6zRM+Kt9VNLfba5CxAlqzG/IHRrF/Y9ZZuMjkYiivMouWNAwUm7qD svnhs7ROxScm3VlfHTGEuGqsceL5gDh9TlqY0s5E/qsLq913gISA1YDHfKEDFdj6XKtM KhZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cdnytPmz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f14-20020a170906494e00b00722e91c126bsi7241466ejt.55.2022.08.15.14.09.24; Mon, 15 Aug 2022 14:09:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cdnytPmz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1345931AbiHOUwC (ORCPT + 99 others); Mon, 15 Aug 2022 16:52:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346288AbiHOUuH (ORCPT ); Mon, 15 Aug 2022 16:50:07 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EC92B99FA; Mon, 15 Aug 2022 12:09:27 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id D72E0CE12C4; Mon, 15 Aug 2022 19:09:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C76F3C433C1; Mon, 15 Aug 2022 19:09:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590564; bh=3fm56voGlBMufrAF3Z9RjuxGbxgbRM8mf6liRp3l0UQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cdnytPmzWAriUYc1/gIGLM7jsV2nxFI6IVulP00y/iGCVBQhDYwiEO0LDpPMB+uXC cET0Pi+1ny2w6uViasEqrhXGjXf9AvCg6eTRljnYy/1Dt/q0EcTATdcDGejLa0wI0j 4wHw1th4BSE08h7fWHRje6uYOzITpqffCl43xVqQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yang Yingliang , Conor Dooley , Mark Brown , Sasha Levin Subject: [PATCH 5.18 0269/1095] spi: Fix simplification of devm_spi_register_controller Date: Mon, 15 Aug 2022 19:54:28 +0200 Message-Id: <20220815180440.876024149@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yang Yingliang [ Upstream commit 43cc5a0afe4184a7fafe1eba32b5a11bb69c9ce0 ] This reverts commit 59ebbe40fb51 ("spi: simplify devm_spi_register_controller"). If devm_add_action() fails in devm_add_action_or_reset(), devm_spi_unregister() will be called, it decreases the refcount of 'ctlr->dev' to 0, then it will cause uaf in the drivers that calling spi_put_controller() in error path. Fixes: 59ebbe40fb51 ("spi: simplify devm_spi_register_controller") Signed-off-by: Yang Yingliang Reviewed-by: Conor Dooley Link: https://lore.kernel.org/r/20220712135504.1055688-1-yangyingliang@huawei.com Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/spi/spi.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index e1065768c537..6a8850e62078 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -3068,9 +3068,9 @@ int spi_register_controller(struct spi_controller *ctlr) } EXPORT_SYMBOL_GPL(spi_register_controller); -static void devm_spi_unregister(void *ctlr) +static void devm_spi_unregister(struct device *dev, void *res) { - spi_unregister_controller(ctlr); + spi_unregister_controller(*(struct spi_controller **)res); } /** @@ -3089,13 +3089,22 @@ static void devm_spi_unregister(void *ctlr) int devm_spi_register_controller(struct device *dev, struct spi_controller *ctlr) { + struct spi_controller **ptr; int ret; + ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + ret = spi_register_controller(ctlr); - if (ret) - return ret; + if (!ret) { + *ptr = ctlr; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } - return devm_add_action_or_reset(dev, devm_spi_unregister, ctlr); + return ret; } EXPORT_SYMBOL_GPL(devm_spi_register_controller); -- 2.35.1