Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3123899rwb; Mon, 15 Aug 2022 18:43:12 -0700 (PDT) X-Google-Smtp-Source: AA6agR5cOIrO+e+8zWQVev/36o+9cXcCAVYrnXMlGbs6LNOiZi7igf23PZVTYJB5xBgaB6Uw8RAj X-Received: by 2002:a17:90a:9408:b0:1f7:6bc6:5ec with SMTP id r8-20020a17090a940800b001f76bc605ecmr21072499pjo.79.1660614192544; Mon, 15 Aug 2022 18:43:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660614192; cv=none; d=google.com; s=arc-20160816; b=mOIxG1uDq5i8oYmMmxvB6lGldSxfh2M99bwar43xH2OidSepqf7qXBMkWIXHMf6XTM fOaNiLf0lwi9KXC5yPF8I7E4ZwS/Yo5Pw4CDiyN/JxCJ44+uwKvakYA7Tq7/2tbjKwx5 qngig66zo5J8yzVo5dJOsbRSb6B/AMiVKuAz1i/lL13zBho9qfOzn827TtKhhtnJwas9 VhYSJJS5xLrZXjcsJHH2InE+AvCYdhnDk3iGvqX6GwaLpYVLozeOYspCpE3c5so5vwLf 0/XmdJ6LigdQHeoo4ENjkeBXc4BF8ZhRIcouFVW5TW4z1hl8RGJlKmUDB26gndQQ4x7T TQpg== 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=rHNN/YSTz04RErG3E2WYp4uuqHoGKcAhopADPt1LQ/JuUF007PceHgsAy8jt/X/uH/ Slsd8tH3OFTM+6J9ik07La6EE1I2CB60os2cLucHi/4/Bz0wA/wLHAWW1IqBFPRxSprK 95XRNnubLG6Y4Mvg7ZS/r9tjSkyQC0Oo4rVEYR5dwJWsfaojU4QgCo+SDSKQk55UAVMW ghliI9tppKnMrgZpjhaFtQmLxbtDJljoNZrEXmhYMjPXtyqxkyBMWPIPRS34hC2UNZaK 6JGGWeEmuIF7X9t4R9q04hSZzRKPkwrY/aZkSUUewuvg0G8yJZexRRRhSJwyKKXWOsCe ZLyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=T4tq7Xe+; 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 e11-20020a170902744b00b0016cd74e550fsi11320153plt.351.2022.08.15.18.43.02; Mon, 15 Aug 2022 18:43:12 -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=T4tq7Xe+; 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 S1344421AbiHOXdJ (ORCPT + 99 others); Mon, 15 Aug 2022 19:33:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344645AbiHOX00 (ORCPT ); Mon, 15 Aug 2022 19:26:26 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C19B480538; Mon, 15 Aug 2022 13:06:51 -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 ams.source.kernel.org (Postfix) with ESMTPS id 47A27B80EA8; Mon, 15 Aug 2022 20:06:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 788FFC433D7; Mon, 15 Aug 2022 20:06:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660594009; bh=gCasWjXfyVMddZoYuo5MwQA3iAWTIDwFsCXYGZBFCo8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T4tq7Xe+zfBeroVhNnJO8zpm70A8MgMeFICbrTHINPPFN5JOPcfnLmL5G4exxr3oN Hb5ZcpYyO4KznZL2jrJKxWiPta6i7VAOnj6v9AEI3rDDHGsLs75Gguffe0zfAdR0cK N6C5LBxVPaO5/tyHzcZ1z6giY2uVnOeqPM0PN4F0= 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.19 0361/1157] i2c: Fix a potential use after free Date: Mon, 15 Aug 2022 19:55:17 +0200 Message-Id: <20220815180454.153000538@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: 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