From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A123AC63705 for ; Wed, 4 Jan 2023 16:14:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239671AbjADQOH (ORCPT ); Wed, 4 Jan 2023 11:14:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239887AbjADQNj (ORCPT ); Wed, 4 Jan 2023 11:13:39 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 311792BFF for ; Wed, 4 Jan 2023 08:13:38 -0800 (PST) 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 CDA81B81730 for ; Wed, 4 Jan 2023 16:13:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2413EC433D2; Wed, 4 Jan 2023 16:13:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1672848815; bh=y1JgZVdLGISUSnF32rEMF2xE8v/iEEK0D7Rn+iY1srI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lj9z861vafPyDrFODNet+UhMWdkRHBhm4k1U5z3w6l9AsK+oUsJIX3ubbDkOVgNG/ bhveJRy3GB0YRyb7rQDPswPCdYZa+5NvTRoebClyaElLHmxs6FAHX3PBygJwGaWayy nqkcXEC793SkeEimXmpNonHGGCcUAIi4Fecaw6bE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, eriri <1527030098@qq.com>, void0red , David Sterba Subject: [PATCH 6.0 019/177] btrfs: fix extent map use-after-free when handling missing device in read_one_chunk Date: Wed, 4 Jan 2023 17:05:10 +0100 Message-Id: <20230104160508.243184168@linuxfoundation.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230104160507.635888536@linuxfoundation.org> References: <20230104160507.635888536@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: void0red commit 1742e1c90c3da344f3bb9b1f1309b3f47482756a upstream. Store the error code before freeing the extent_map. Though it's reference counted structure, in that function it's the first and last allocation so this would lead to a potential use-after-free. The error can happen eg. when chunk is stored on a missing device and the degraded mount option is missing. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216721 Reported-by: eriri <1527030098@qq.com> Fixes: adfb69af7d8c ("btrfs: add_missing_dev() should return the actual error") CC: stable@vger.kernel.org # 4.9+ Signed-off-by: void0red Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/volumes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -7128,8 +7128,9 @@ static int read_one_chunk(struct btrfs_k map->stripes[i].dev = handle_missing_device(fs_info, devid, uuid); if (IS_ERR(map->stripes[i].dev)) { + ret = PTR_ERR(map->stripes[i].dev); free_extent_map(em); - return PTR_ERR(map->stripes[i].dev); + return ret; } }