From patchwork Wed Feb 28 22:24:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bethany Jamison X-Patchwork-Id: 1906028 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TlTRs2sFDz23fC for ; Thu, 29 Feb 2024 09:24:40 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1rfSLh-0001mb-Du; Wed, 28 Feb 2024 22:24:25 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1rfSLX-0001m0-JN for kernel-team@lists.ubuntu.com; Wed, 28 Feb 2024 22:24:15 +0000 Received: from mail-il1-f197.google.com (mail-il1-f197.google.com [209.85.166.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 37D1D3F32D for ; Wed, 28 Feb 2024 22:24:14 +0000 (UTC) Received: by mail-il1-f197.google.com with SMTP id e9e14a558f8ab-3657dff68d6so4262905ab.0 for ; Wed, 28 Feb 2024 14:24:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709159053; x=1709763853; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DfJx1RF5aYmGqm4b6inL2LzPJmoPXzY9j4Ve4xvxdJE=; b=KDb7uO1PHoogWAZNvGe/YjbLj5BI1XLbn+JtATO270AlPP4FICILBQoI9FFJdUUNd2 hUJHRZ3ZLFXbsLCmZl7YQMGddtLY/efyhYZEhCBYCN5RcuaeujBZVuDGdXtV8/bPSNIY Y2qAh8A6rpMXxQ5L+HRATLy2nyCDk7f3CBcL1mFrBD4+VADzlsr2qIU8Re40RRS0PcFf hDvsJI4nSSo8ciy+72YXRS6cekHqiLYHWzJ3ZQ7VH7Lk1b4AEqTx1Q1FjxK6qi36DLFq /NGL/4xufUne9+92KXMczGADqgHxmgK2LGRGZuqaSH/feerzQsJyoOWMLNwaO01tQYDd 8oEg== X-Gm-Message-State: AOJu0Yy4dF/X0kttyNchW082h5R4jY9nBtpRyk4gvjMVlb0JD3cd8UEO UeoZ2HzgNmIPWSrCDir/Udjs8/VTFvhJ2gPCc5YgW7iZKRpfQhMWhQ6AuhmFHwAUsRPcrS8wK7b nX0FP6iFoPgYE87nM91IFTq2omv/qdIZL4dHcVsnGX9oWcx36pmZgcyoRZrgJWyrx1H3BaKxsWF G4qgqXQLOEOg== X-Received: by 2002:a92:d481:0:b0:365:3010:14b1 with SMTP id p1-20020a92d481000000b00365301014b1mr658342ilg.10.1709159052872; Wed, 28 Feb 2024 14:24:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IHL55io8Wb88cFhYs6B6jbCMir0dkZQr+/QNI06g2xmsuSW2Y0Vj3Lq84fOdKCmMG07c5P7Ug== X-Received: by 2002:a92:d481:0:b0:365:3010:14b1 with SMTP id p1-20020a92d481000000b00365301014b1mr658332ilg.10.1709159052535; Wed, 28 Feb 2024 14:24:12 -0800 (PST) Received: from smtp.gmail.com (104-218-69-129.dynamic.lnk.ne.allofiber.net. [104.218.69.129]) by smtp.gmail.com with ESMTPSA id t7-20020a92c0c7000000b0036576c9ea6esm4452ilf.42.2024.02.28.14.24.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 14:24:12 -0800 (PST) From: Bethany Jamison To: kernel-team@lists.ubuntu.com Subject: [SRU][Mantic][Jammy][PATCH 1/1] btrfs: do not ASSERT() if the newly created subvolume already got read Date: Wed, 28 Feb 2024 16:24:10 -0600 Message-Id: <20240228222410.43705-2-bethany.jamison@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240228222410.43705-1-bethany.jamison@canonical.com> References: <20240228222410.43705-1-bethany.jamison@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Qu Wenruo [BUG] There is a syzbot crash, triggered by the ASSERT() during subvolume creation: assertion failed: !anon_dev, in fs/btrfs/disk-io.c:1319 ------------[ cut here ]------------ kernel BUG at fs/btrfs/disk-io.c:1319! invalid opcode: 0000 [#1] PREEMPT SMP KASAN RIP: 0010:btrfs_get_root_ref.part.0+0x9aa/0xa60 btrfs_get_new_fs_root+0xd3/0xf0 create_subvol+0xd02/0x1650 btrfs_mksubvol+0xe95/0x12b0 __btrfs_ioctl_snap_create+0x2f9/0x4f0 btrfs_ioctl_snap_create+0x16b/0x200 btrfs_ioctl+0x35f0/0x5cf0 __x64_sys_ioctl+0x19d/0x210 do_syscall_64+0x3f/0xe0 entry_SYSCALL_64_after_hwframe+0x63/0x6b ---[ end trace 0000000000000000 ]--- [CAUSE] During create_subvol(), after inserting root item for the newly created subvolume, we would trigger btrfs_get_new_fs_root() to get the btrfs_root of that subvolume. The idea here is, we have preallocated an anonymous device number for the subvolume, thus we can assign it to the new subvolume. But there is really nothing preventing things like backref walk to read the new subvolume. If that happens before we call btrfs_get_new_fs_root(), the subvolume would be read out, with a new anonymous device number assigned already. In that case, we would trigger ASSERT(), as we really expect no one to read out that subvolume (which is not yet accessible from the fs). But things like backref walk is still possible to trigger the read on the subvolume. Thus our assumption on the ASSERT() is not correct in the first place. [FIX] Fix it by removing the ASSERT(), and just free the @anon_dev, reset it to 0, and continue. If the subvolume tree is read out by something else, it should have already get a new anon_dev assigned thus we only need to free the preallocated one. Reported-by: Chenyuan Yang Fixes: 2dfb1e43f57d ("btrfs: preallocate anon block device at first phase of snapshot creation") CC: stable@vger.kernel.org # 5.15+ Reviewed-by: Filipe Manana Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba (cherry picked from commit e03ee2fe873eb68c1f9ba5112fee70303ebf9dfb) CVE-2024-23850 Signed-off-by: Bethany Jamison --- fs/btrfs/disk-io.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index d5e51c08e37e0..aadf3fb38e2ce 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1316,8 +1316,17 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, again: root = btrfs_lookup_fs_root(fs_info, objectid); if (root) { - /* Shouldn't get preallocated anon_dev for cached roots */ - ASSERT(!anon_dev); + /* + * Some other caller may have read out the newly inserted + * subvolume already (for things like backref walk etc). Not + * that common but still possible. In that case, we just need + * to free the anon_dev. + */ + if (unlikely(anon_dev)) { + free_anon_bdev(anon_dev); + anon_dev = 0; + } + if (check_ref && btrfs_root_refs(&root->root_item) == 0) { btrfs_put_root(root); return ERR_PTR(-ENOENT);