Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2868289rwb; Mon, 15 Aug 2022 12:58:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR6YuUpVIlw1jB9pKWQKhIb5DdU0+i0rWYXsYWs3XfnGhe9dRjRBkCmus8UDajyfiq9NoIsU X-Received: by 2002:a17:906:8a74:b0:730:7aa8:69eb with SMTP id hy20-20020a1709068a7400b007307aa869ebmr10760837ejc.573.1660593526155; Mon, 15 Aug 2022 12:58:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660593526; cv=none; d=google.com; s=arc-20160816; b=PH2K4CLuRV/k30WFWJBrCeftsJd/f6LUzvBK22FvMprf2QArcKUCLVu2jRAZFmKXFk qmvAz916k9/qMpr/Bj/quS1uNYjvXoT6YCG5GJF2WtIoqaI/zaQeiEZcmlQpOoBFlbcc AIAKwZNVh0627d6e8bQ0bC5/kqABE7p5rDFMnWfO8gTad+NJaKMt2PjWGHLtjB5HVoTJ tN1M46Lc5dkhadmb1FJsWGZAUAb/dcdXGdx7HQAdJSVZVN0riUp9t1cCGzkUgroFYi6K KNIUOAHSRfEDugEJyuSW6xWTN69d/aZmxcAgW/+xYq3jXo6GeryJzwtkrCzekSPuxdAm M1KQ== 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=raZe4pVb4qzdE3zfw9p4jk/dLJj3oENV9pgfgzRTDHA=; b=NgzdvwfNYHo4mTuuGEyswkLmzmbRqN7afCMCPgtSQ//dt+L+d3FBk+g8rRSBFYMX+p nabv5QdIf/vuFIWNKDwJ3jpTpz85Ed+jVje4WF+4DXNaRgbuvHpGxAZXI8tCbWXCCZmd 08Tw8R/8KvQrqYiLKfik0fuE++fVWATIwxrhZ6CPMM1YRVRncJt5+Guo/SH6YrpDsfJN N6dqbHTYJWL23kNCJTzWFBHL5FMGKezGfRfjzLvIpbjE//9+0XdZYYVYCCOTXIHOuac2 0Xwoz2lJY+/Qp3z4Dx+o7lLFdyCMlNVLQzVkwF8d9Lp1Okmk2VMi+t7XV0UyWRtmq7YW 07kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jk5ECl1G; 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 c7-20020a05640227c700b0043be28d93adsi9381842ede.261.2022.08.15.12.58.13; Mon, 15 Aug 2022 12:58:46 -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=jk5ECl1G; 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 S241963AbiHOSkO (ORCPT + 99 others); Mon, 15 Aug 2022 14:40:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240822AbiHOSjO (ORCPT ); Mon, 15 Aug 2022 14:39:14 -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 9B9B82ED4B; Mon, 15 Aug 2022 11:23:55 -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 43F5360FA3; Mon, 15 Aug 2022 18:23:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E031C433C1; Mon, 15 Aug 2022 18:23:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587833; bh=G2wKVs2hxomJLfCHleVjEq03UftGpFfhnCPiTRRTtEU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jk5ECl1GYkZOgqQqeAqUb3Y+A5s5NgKRVcOvfHU0oRwQOGGcBnPiDMscXTqmbYBHP rDVJWxHC+gWLm0oeqcdAyJTcwcqDAcsAFFcVcHu8a5+BlS/XJp94wcGBZlc2Vjz76w zlhmg0+sbnEu30z44lXQ2mTUEOeW73dO6UNGLMas= 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.15 209/779] spi: Fix simplification of devm_spi_register_controller Date: Mon, 15 Aug 2022 19:57:33 +0200 Message-Id: <20220815180346.204631619@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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 d0bbf8f9414d..556d65af5e23 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2949,9 +2949,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); } /** @@ -2970,13 +2970,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