Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3028856rwb; Mon, 15 Aug 2022 16:27:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR41GSioa8zLFQVigLrn15M9nGJgP8C4DNUGfk4f1TSh1bdTW6iULFy94n2JB5JbbRQuX96j X-Received: by 2002:aa7:c78e:0:b0:441:c311:9dcd with SMTP id n14-20020aa7c78e000000b00441c3119dcdmr15990188eds.155.1660605961862; Mon, 15 Aug 2022 16:26:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605961; cv=none; d=google.com; s=arc-20160816; b=o0VeAXDFsbJ3UJeFqrQ0r0EojchCjSOIo1ijNpk9/6ijSiNeOZyL29pVZMSO0LtGYu xkBoLVUW6Oq1a/HT2VXMTU09hH9GXe6pJV8t6usAgyFkCgrNSoA6yWiNvgTRDCsILzyu /Zlwf+Vz+9ZH1+YATQG0HDXpu0VOLKEXarJdl4ismnLN5j++hD6ZW0BUK/It4ch8PVOx +TuEubLAr12YJOcmBiCGmjy7fsJ2pc9rBE4m7u/P0ohWgxcdM7FrBas+c/cvNtlC2EZ8 na+o8JebRM+nicPLPolhHWsm7s/5So7zmt6hSp0o3jXuxQ9/XqO8gBZkr0SWPiZaJs4U lrEA== 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=E2waHy34ZrMLhnBGW7ggq8uppIdyVThk5gBJAqkMwp8=; b=atxw34od2hYh+KWmp/zhZppzORgji5UMo2In8rWEVZ6yVjjbbVcJdGNVBSxv3dydTV N+zUcWjVhC2L5zQjblkzOKZSjnRzAQkW6TiSQmkH/g7yE3duKZybHwisLKwuet+onLxS 0PvvqmMzg0Pg3IvWtbMDRFJsCSfoGKuisXMz3JMJyPdA3jBKO1Qw9cwGJRXFBJ2lYBoS cEnqZrWHBtfSOo6qvCLCDNj5mOBXJejkxQCpVWidGb8lv16ppA8WHJOrpyrW/QbSExL4 WQg9NoZoZ4B5FGoIjv+mAlN/jC9uXpwx8CkzXoquiN7K5GSDz9n21O+vC2Au/quKdbje hH6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZTvEBX6X; 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 dz10-20020a0564021d4a00b0043585bc1c54si9494921edb.567.2022.08.15.16.25.36; Mon, 15 Aug 2022 16:26:01 -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=ZTvEBX6X; 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 S245471AbiHOXCR (ORCPT + 99 others); Mon, 15 Aug 2022 19:02:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233204AbiHOXAm (ORCPT ); Mon, 15 Aug 2022 19:00:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD345D10D; Mon, 15 Aug 2022 12:57:37 -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 dfw.source.kernel.org (Postfix) with ESMTPS id B1640612A3; Mon, 15 Aug 2022 19:57:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8DC3C433D6; Mon, 15 Aug 2022 19:57:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660593456; bh=EHTqRDxAE6XI7DhbKyIDIwdWSDYQCC2Vet31YKcW/kI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZTvEBX6X8PGIDMgE2NOGWR7zq1aLqZVULSDjDXqWuFMDRDF043aqgtOVxccKiLo2n nfzaGrEKXeCjFqJhVIS8L/PN+E4pH0iw9vZSYa/uY/LnC+6g02SUh6QpEauMkMkXiF Uc40sXIrcfTA0NUMIxlhYKDSRlW1+35rOaPgeDYI= 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.19 0270/1157] spi: Fix simplification of devm_spi_register_controller Date: Mon, 15 Aug 2022 19:53:46 +0200 Message-Id: <20220815180450.412826860@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@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 62aa3f062f3d..2c616024f7c0 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -3050,9 +3050,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); } /** @@ -3071,13 +3071,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