Skip to contentSkip to conversation

btrfs: fix match incorrectly in dev_args_match_device

PATCH In review Liu Shixin 3 participants 3 messages Nov 3, 2022 Btrfs file system

lore
Share & tools
lore

Liu Shixin

author
#0 lore raw

PATCH 1/1 btrfs: fix match incorrectly in dev_args_match_device

syzkaller found an assert failed:

assertion failed: (args->devid != (u64)-1) || args->missing, in fs/btrfs/volumes.c:6921

This can be trigger when we set devid to (u64)-1) by ioctl. In this case, the match of devid will be skipped and the match of device may be succeed incorrectly.

Patch 562d7b1512f7 introduced this function which is used to match device. This function contaions two matching scenarios, we can distinguish them by checking the value of args->missing rather than check whether args->devid and args->uuid is default value.

Reported-by [email protected]
Fixes 562d7b1512f7 ("btrfs: handle device lookup with btrfs_dev_lookup_args")
---
 fs/btrfs/volumes.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
fs/btrfs/volumes.c +8 -8
index 94ba46d57920..bf2d886cfb4b 100644
@@ -6918,18 +6918,18 @@ static bool dev_args_match_fs_devices(const struct btrfs_dev_lookup_args *args,
 static bool dev_args_match_device(const struct btrfs_dev_lookup_args *args,
 				  const struct btrfs_device *device)
 {
-	ASSERT((args->devid != (u64)-1) || args->missing);
+	if (args->missing) {
+		if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) &&
+		    !device->bdev)
+			return true;
+		return false;
+	}
 
-	if ((args->devid != (u64)-1) && device->devid != args->devid)
+	if (device->devid != args->devid)
 		return false;
 	if (args->uuid && memcmp(device->uuid, args->uuid, BTRFS_UUID_SIZE) != 0)
 		return false;
-	if (!args->missing)
-		return true;
-	if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) &&
-	    !device->bdev)
-		return true;
-	return false;
+	return true;
 }
 
 /*
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Liu Shixin

syzkaller found an assert failed:

  assertion failed: (args->devid != (u64)-1) || args->missing, in fs/btrfs/volumes.c:6921

This can be trigger when we set devid to (u64)-1) by ioctl. In this case, the match of devid will be skipped and the match of device may be succeed incorrectly.

Quoted 8 lines

Patch 562d7b1512f7 introduced this function which is used to match device. This function contaions two matching scenarios, we can distinguish them by checking the value of args->missing rather than check whether args->devid and args->uuid is default value.

Reported-by [email protected]
Fixes 562d7b1512f7 ("btrfs: handle device lookup with btrfs_dev_lookup_args")
Quoted 35 lines
---
  fs/btrfs/volumes.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)
fs/btrfs/volumes.c +8 -8
index 94ba46d57920..bf2d886cfb4b 100644
@@ -6918,18 +6918,18 @@ static bool dev_args_match_fs_devices(const struct btrfs_dev_lookup_args *args,
  static bool dev_args_match_device(const struct btrfs_dev_lookup_args *args,
  				  const struct btrfs_device *device)
  {
-	ASSERT((args->devid != (u64)-1) || args->missing);
+	if (args->missing) {
+		if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) &&
+		    !device->bdev)
+			return true;
+		return false;
+	}
 
-	if ((args->devid != (u64)-1) && device->devid != args->devid)
+	if (device->devid != args->devid)
  		return false;
  	if (args->uuid && memcmp(device->uuid, args->uuid, BTRFS_UUID_SIZE) != 0)
  		return false;
-	if (!args->missing)
-		return true;
-	if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) &&
-	    !device->bdev)
-		return true;
-	return false;
+	return true;
  }
 
  /*
#2 lore raw
In reply to Liu Shixin
Quoted 8 lines · Liu Shixin

syzkaller found an assert failed:

assertion failed: (args->devid != (u64)-1) || args->missing, in fs/btrfs/volumes.c:6921

This can be trigger when we set devid to (u64)-1) by ioctl. In this case, the match of devid will be skipped and the match of device may be succeed incorrectly.

Quoted 8 lines

Patch 562d7b1512f7 introduced this function which is used to match device. This function contaions two matching scenarios, we can distinguish them by checking the value of args->missing rather than check whether args->devid and args->uuid is default value.

Reported-by [email protected]
Fixes 562d7b1512f7 ("btrfs: handle device lookup with btrfs_dev_lookup_args")

Added to misc-next, thanks.