]> git.hungrycats.org Git - linux/commitdiff
btrfs: prevent deleting the wrong subvolume by ID
authorguolingxing <guolingxing@tju.edu.cn>
Sun, 18 Oct 2026 15:38:10 +0000 (15:38 +0000)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:17 +0000 (21:36 +0100)
When deleting a subvolume by ID, btrfs_ioctl_snap_destroy() first
resolves the requested ID to a name, then looks up that name again
under the parent directory lock.

A concurrent rename can change which subvolume is associated with
the name between these operations. Since the deletion path does not
verify the ID of the resolved subvolume, it can delete a different
subvolume while leaving the requested one intact.

This was reproduced by repeatedly exchanging the names of two
subvolumes using renameat2(RENAME_EXCHANGE) while deleting one of
them by ID. The wrong subvolume was deleted in 9 out of 14 independent
tests.

Fix this by checking that the resolved subvolume ID matches the
requested ID while holding the parent directory lock. Return -EAGAIN
on a mismatch so that userspace can retry the operation instead of
being told that the requested subvolume does not exist.

Fixes: 949964c92843 ("btrfs: add new BTRFS_IOC_SNAP_DESTROY_V2 ioctl")
CC: stable@vger.kernel.org
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: guolingxing <guolingxing@tju.edu.cn>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c

index 088dd6713405a113ffa4ff2dc0d404f04d81cdad..1b617e4a7e2b6f13940279658c45440fe1ce0596 100644 (file)
@@ -2393,6 +2393,17 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
 
        inode = d_inode(dentry);
        dest = BTRFS_I(inode)->root;
+
+       /*
+        * A concurrent rename may have changed which subvolume the name
+        * refers to. Verify that the resolved ID still matches the requested ID.
+        */
+       if (destroy_v2 && (vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID) &&
+           btrfs_root_id(dest) != vol_args2->subvolid) {
+               ret = -EAGAIN;
+               goto out_end_removing;
+       }
+
        if (!capable(CAP_SYS_ADMIN)) {
                /*
                 * Regular user.  Only allow this with a special mount