Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2929428rwb; Mon, 15 Aug 2022 14:15:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR4h2jfy2duD47/UqsQjDxIyU3oHH7VI99TMifV8wKqui2MMwUlulVKaglPZ5Igpm7H1jW69 X-Received: by 2002:a05:6402:4489:b0:43d:7f7d:7d88 with SMTP id er9-20020a056402448900b0043d7f7d7d88mr15897192edb.159.1660598108915; Mon, 15 Aug 2022 14:15:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660598108; cv=none; d=google.com; s=arc-20160816; b=y8uQtlXdipOGeqDvPjyPuUlVeEmWgTd1fB27y6Fn6gUkLzLal+7AOr/jUV1vu/1gq+ xPsLsAJpaROvsajJy+KhxC/mQXjGIPCSTypq1uRlCZWIUjJkjnfN8KIoAG6ywqcxhcIK nRABtw1dIFdpuoYMR8v2BU8AYw+3Pze+/RNXZ7VQ7DyJxSXSUQygVrIUUs9fPgieU1PG mAGcAWLqGX7HPxuhPQTS7PqdCdZGtbzHCWZcpyElpJyLo9n5v8qZxgCoJ+QgbXHhz/ht tOhkCvDpizIi3aN6V3p1wqB6WtfsxfJXppaKDQdRNO/Og3Qp9XZYfYap90g3E/M+tcJP Wpcg== 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=z0aMrfsOjvrOfTyL88DhFkM+n6xoQpG/JPxajTnZhgE=; b=JOZgAfhMwELrmvD+lFulhLMSWj531CU57LxJtG8urWdX2v1g3hv+QMV5X3uXBfYDUl Z5bVFjOP2lmpUsPDbKiV0iEDcarwcsFdB2lqTNoR1M4ZZquxokFI+XwLUccFm27G/A6i H60lKB84lRGzEaLfd2TXo59A8GNL4AryThVxeT6+uzodIxwefTxFnHMcsyeS/rm7pNqR 3+bdKMGKGQo1aUcbGLU/q4i4d9+DR5TK3if9LG07JVD6oa55S9+VACcpQihZg8en6Y4w 7GfDRRpHZuq+373glJJMGgb1DLSUUxMuAfc6hVlxoaCFlpvVqxhuqBOKMeQMftEnpECd epqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tXBigGMn; 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 hr5-20020a1709073f8500b007312789a036si9818258ejc.682.2022.08.15.14.14.41; Mon, 15 Aug 2022 14:15:08 -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=tXBigGMn; 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 S233161AbiHOVEc (ORCPT + 99 others); Mon, 15 Aug 2022 17:04:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346899AbiHOU4A (ORCPT ); Mon, 15 Aug 2022 16:56:00 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1120042AF7; Mon, 15 Aug 2022 12:11:29 -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 AC8496069E; Mon, 15 Aug 2022 19:11:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B82CC433D7; Mon, 15 Aug 2022 19:11:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590688; bh=gCasWjXfyVMddZoYuo5MwQA3iAWTIDwFsCXYGZBFCo8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tXBigGMnYSr8eHb0xvt2C8V6c/8ezIcc668vLHfypVomXulpSei7TK469IOq6J18s Qbdb1BORGKDEHxax5HEpQ26VDDrm1rEryYsbRjZU8oTngbHY+0z9TYBppO126Cuh11 h4N2Y3ZC3mAXfJDyQRydWjkt0j6Rt63jWyzGmPHo= 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.18 0340/1095] i2c: Fix a potential use after free Date: Mon, 15 Aug 2022 19:55:39 +0200 Message-Id: <20220815180443.862632134@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: 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 d43db2c3876e..19a317fdcf5b 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -2467,8 +2467,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