Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1313204rdb; Wed, 6 Dec 2023 15:17:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEP+m4PjTzIwLjZrA6m5i6PV8nKm6e7rCoyqaGGSkBK+OxKLnE+CtjvtwsdyKIazpLi5+5V X-Received: by 2002:a05:6a20:35a1:b0:18f:97c:5b8c with SMTP id j33-20020a056a2035a100b0018f097c5b8cmr1002326pze.90.1701904674945; Wed, 06 Dec 2023 15:17:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701904674; cv=none; d=google.com; s=arc-20160816; b=l514Ksd1riqNyumPe0Xr7zEfasiNr3KnWl9i0+lRTOURoe/eXyen2LqC1TYy1XdwR3 RnXjj2G4lUDdvyfRwnzb9B7g422vVmtMpFLbn4FMjH62c7Dt3bk9sDLv0G4SYQgiwO2K HXDFiGIAOxFXr7FGXzCDoAS702S76acUPG5X5hkLTFHkXfq9FjnHQNA2abmHudFUtqSn axanvXgH6yI0Mg1HO2NFLlD+Q5pKGeHR6zjzJK1JasGYvJeguKHoo370uyH5oYFFBi1B PIT8e5RMcuKgjf1x4zatISxYMxqTlpJQh0cEuP6vdtEvpDL38R32pIqN7HlBOYJswp/x 5ceQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GHpn8i2i6PhKHssE/MepqgmgdsrNDF4ZmbyHHfW1KCY=; fh=23+4sTwLWDRCm6vm5eDuLNS4sSnEsE3GjKbynyYGEtE=; b=TN9RoDZLZhy0yWGg7r3qQKzhUJVfiOGd8AfKbs8Dw7N6P83PluAlzANBYeibz3rU45 3166zSZWkHrhP/dnmjZdjqd1/Y/9EcJ/cjSyNNDn7mf0a6Hkzz8NiOdfkP2sQ6zt2FPk 2LDAT9wtWal1n0P9XM70vylxTKpLa8Lv6sWIGzWlkfM5vvOIon9HcDnj1e4aNMn9925J Nqhjt3XcU427ECTo7HM4WYhGW4iLxf5pObIH1amj/H2LAtmcG/mXYD5UjvzUdcBqlCGP TM4DohhvEBaCXNK0KkUrqm13PuXKgpLY5ToCglQYGmkkZd/yFtmUQiT1xX8x6i3SFAtR 9a9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="o+I5/P8j"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id w8-20020a170902e88800b001cfc419e609si7809plg.67.2023.12.06.15.17.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 15:17:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="o+I5/P8j"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 77FAB82F2C52; Wed, 6 Dec 2023 15:17:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379646AbjLFXRe (ORCPT + 99 others); Wed, 6 Dec 2023 18:17:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377556AbjLFXR3 (ORCPT ); Wed, 6 Dec 2023 18:17:29 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90CE2181 for ; Wed, 6 Dec 2023 15:17:35 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC886C433CA; Wed, 6 Dec 2023 23:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701904655; bh=DE6LOODg8opqMxDQr9pajmg5bZcQ8RbpkX6Gz17Xsa4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o+I5/P8jMIg5fG9kLI7hWzQVkunc0BfPRII4N7CB72iq2sxL9q5BicDtkfVtb/T4U xEJ+g32CtkG1tRpnPX9W0eIh8I4RVFdtunftSv5dVygMuF3J2RsuM1Js99lnLQ9fIg d2tbrlGCduycPKs3smcm4etVDMtkATGm/dKfU5foU5UrpdxkkH+Tryud70/JhboP1l n3whXkHfCiCrw47Np+hHmLfnM3puM3HQShycc6ZBvZ3wdS7dX6aJURO+rCvj2XeeSd iQ56ZWE+SukHSBhgWnbjrFU85zUpvwa4YC569ArB4bA52iiwBooT3sLkFA4j1KSIR4 mxbPFVUHPtbXw== From: Stephen Boyd To: Greg Kroah-Hartman Cc: Yu-Che Cheng , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Fei Shao , Chen-Yu Tsai Subject: [PATCH 2/8] spmi: mediatek: Fix UAF on device remove Date: Wed, 6 Dec 2023 15:17:25 -0800 Message-ID: <20231206231733.4031901-3-sboyd@kernel.org> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog In-Reply-To: <20231206231733.4031901-1-sboyd@kernel.org> References: <20231206231733.4031901-1-sboyd@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 06 Dec 2023 15:17:52 -0800 (PST) From: Yu-Che Cheng The pmif driver data that contains the clocks is allocated along with spmi_controller. On device remove, spmi_controller will be freed first, and then devres , including the clocks, will be cleanup. This leads to UAF because putting the clocks will access the clocks in the pmif driver data, which is already freed along with spmi_controller. This can be reproduced by enabling DEBUG_TEST_DRIVER_REMOVE and building the kernel with KASAN. Fix the UAF issue by using unmanaged clk_bulk_get() and putting the clocks before freeing spmi_controller. Reported-by: Fei Shao Signed-off-by: Yu-Che Cheng Link: https://lore.kernel.org/r/20230717173934.1.If004a6e055a189c7f2d0724fa814422c26789839@changeid Tested-by: Fei Shao Reviewed-by: Fei Shao Reviewed-by: Chen-Yu Tsai Signed-off-by: Stephen Boyd --- drivers/spmi/spmi-mtk-pmif.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/spmi/spmi-mtk-pmif.c b/drivers/spmi/spmi-mtk-pmif.c index 54c35f5535cb..1261f381cae6 100644 --- a/drivers/spmi/spmi-mtk-pmif.c +++ b/drivers/spmi/spmi-mtk-pmif.c @@ -475,7 +475,7 @@ static int mtk_spmi_probe(struct platform_device *pdev) for (i = 0; i < arb->nclks; i++) arb->clks[i].id = pmif_clock_names[i]; - err = devm_clk_bulk_get(&pdev->dev, arb->nclks, arb->clks); + err = clk_bulk_get(&pdev->dev, arb->nclks, arb->clks); if (err) { dev_err(&pdev->dev, "Failed to get clocks: %d\n", err); goto err_put_ctrl; @@ -484,7 +484,7 @@ static int mtk_spmi_probe(struct platform_device *pdev) err = clk_bulk_prepare_enable(arb->nclks, arb->clks); if (err) { dev_err(&pdev->dev, "Failed to enable clocks: %d\n", err); - goto err_put_ctrl; + goto err_put_clks; } ctrl->cmd = pmif_arb_cmd; @@ -510,6 +510,8 @@ static int mtk_spmi_probe(struct platform_device *pdev) err_domain_remove: clk_bulk_disable_unprepare(arb->nclks, arb->clks); +err_put_clks: + clk_bulk_put(arb->nclks, arb->clks); err_put_ctrl: spmi_controller_put(ctrl); return err; @@ -521,6 +523,7 @@ static void mtk_spmi_remove(struct platform_device *pdev) struct pmif *arb = spmi_controller_get_drvdata(ctrl); clk_bulk_disable_unprepare(arb->nclks, arb->clks); + clk_bulk_put(arb->nclks, arb->clks); spmi_controller_remove(ctrl); spmi_controller_put(ctrl); } -- https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/ https://git.kernel.org/pub/scm/linux/kernel/git/sboyd/spmi.git