Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2872314rwb; Mon, 15 Aug 2022 13:03:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR6Vu9Ss6MzK4jxmtHjF7iBr2du6f2u6aWXjDU2s15TJKc7H84wAgCFh5Hg4JpOoZcXsy2an X-Received: by 2002:a05:6402:d06:b0:440:3e9d:77d with SMTP id eb6-20020a0564020d0600b004403e9d077dmr15115083edb.286.1660593780197; Mon, 15 Aug 2022 13:03:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660593780; cv=none; d=google.com; s=arc-20160816; b=WnD7COldWzxrW8CnBtibSEAskJ+bxFgW9YvdA1F3kylod2aad1Tm3qMde6/olxUqmy /zsIBQbD+Xa3wNbNhOOVEDw4Xq6LYUnjimuqYll6kYHnq27CbVz86Z59BjKZiiFcruws ZODMx/oooagfzq9aPn29N9p3nVkDJN4izK86R6x+ThAfrUDPOF7tQs0SYGgW6kEL96b5 7yNbjAEvLAMfEVccQyWw5eVh15leB9YdeTLOLXc+C9bFfN0VaEHYI/6mndNgBr5VBWlG 16DMNKju0ZJjwwFxoLjcIChcIFDnh3cZtK8Zhg08xoSsutmywbaBvv7y8D4sRgi266G5 Eiww== 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=9dlCiXcgyJDMApYfi20mQ/r+55roX3uUaAdIPB17i4I=; b=U1Wfdn41ECSVOGIStAs7uvnb1yT2g+G7GJKdhUZcb6qH/F9rFg5oU6WfxbXpG55yj+ KjVYYDfwbKj5z5h9d5uwZyxNo99kfueVcOsO2BESr+lcKjtuldxZt2ZVkH1LtjZYi1if 6eelNauXDga97AbZuR5j6AiLJAkQ5ngzFn9c8cVX5gfpT0yruAKoM+lVRNMHXfvFoGTV GaNFwGQiCUlSw308Pu0aCV/jCihV4GH5dpOO1j1CFmjrUaQtH22v/fPOJBx+yDySCpbd l1/jqFyiNvSlKvrr0wP+Pm9ArHZYelLV5y4GvnfNeOJJYZtNPhLxCwndScSbbynh8G9L XjXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yz1HGcAk; 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 y14-20020a170906448e00b007309e910729si6969976ejo.580.2022.08.15.13.02.33; Mon, 15 Aug 2022 13:03:00 -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=yz1HGcAk; 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 S243982AbiHOSwQ (ORCPT + 99 others); Mon, 15 Aug 2022 14:52:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244231AbiHOSqt (ORCPT ); Mon, 15 Aug 2022 14:46:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BED7841D01; Mon, 15 Aug 2022 11:28:23 -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 C8F1160FBA; Mon, 15 Aug 2022 18:28:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D203AC433D7; Mon, 15 Aug 2022 18:28:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588102; bh=9fFU841hpe5Pyc79KUrqRFYYT/dlO83nB9lU2JYsXWQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yz1HGcAky8HTi81egvG5nfYaXEuU94j7M5D5n3sgBGifs/ksVk8PWo875GZPYWeuY HH3NLl5pl0qUqVio7p90pU/ZSUnfNt+/ybUTYV5yR2yC8ECuNwaeWcqKiafNz+DTCT sZuXDgEKQGigFCHYTNGp/OMizU3+E6bBbCBytj8Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xu Wang , Wolfram Sang , Sasha Levin Subject: [PATCH 5.15 264/779] i2c: Fix a potential use after free Date: Mon, 15 Aug 2022 19:58:28 +0200 Message-Id: <20220815180348.650294076@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: Xu Wang [ Upstream commit e4c72c06c367758a14f227c847f9d623f1994ecf ] Free the adap structure only after we are done using it. This patch just moves the put_device() down a bit to avoid the use after free. Fixes: 611e12ea0f12 ("i2c: core: manage i2c bus device refcount in i2c_[get|put]_adapter") Signed-off-by: Xu Wang [wsa: added comment to the code, added Fixes tag] Signed-off-by: Wolfram Sang Signed-off-by: Sasha Levin --- drivers/i2c/i2c-core-base.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index cfbef70e8ba7..8fb065caf30b 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -2464,8 +2464,9 @@ void i2c_put_adapter(struct i2c_adapter *adap) if (!adap) return; - put_device(&adap->dev); module_put(adap->owner); + /* Should be last, otherwise we risk use-after-free with 'adap' */ + put_device(&adap->dev); } EXPORT_SYMBOL(i2c_put_adapter); -- 2.35.1