loop: don't change loop device under exclusive opener in loop_set_status
PATCH 1/1 loop: don't change loop device under exclusive opener in loop_set_status
loop_set_status() is allowed to change the loop device while there are other openers of the device, even exclusive ones.
In this case, it causes a KASAN: slab-out-of-bounds Read in ext4_search_dir(), since when looking for an entry in an inlined directory, e_value_offs is changed underneath the filesystem by loop_set_status().
Fix the problem by forbidding loop_set_status() from modifying the loop device while there are exclusive openers of the device. This is similar to the fix in loop_configure() by commit 33ec3e53e7b1 ("loop: Don't change loop device under exclusive opener") alongside commit ecbe6bc0003b ("block: use bd_prepare_to_claim directly in the loop driver").
---
ML thread for previous, misguided patch idea:
https://lore.kernel.org/all/[email protected]/t/
drivers/block/loop.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)
drivers/block/loop.c +30 -11
@@ -1222,13 +1222,24 @@ static int loop_clr_fd(struct loop_device *lo) }static int -loop_set_status (struct loop_device *lo,const struct loop_info64 *info) +loop_set_status (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev,const struct loop_info64 *info) {int err;bool partscan = false;bool size_changed = false;unsigned int memflags; +/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } + err =mutex_lock_killable (&lo->lo_mutex);if (err)return err;
@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);
@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;
@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int
@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);
@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;
@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int
@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :
Signature · 1 line
2.43.0
Quoted 42 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").Reported-by [email protected]Tested-by [email protected]Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>---
ML thread for previous, misguided patch idea:
https://lore.kernel.org/all/[email protected]/t/drivers/block/loop.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)drivers/block/loop.c +2
-1@@ -1222,13 +1222,24 @@ static int loop_clr_fd(struct loop_device *lo) }static int -loop_set_status (struct loop_device *lo,const struct loop_info64 *info) +loop_set_status (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev,const struct loop_info64 *info) {int err;bool partscan = false;bool size_changed = false;unsigned int memflags;
drivers/block/loop.c static int loop_clr_fd(struct loop_device *lo) · line 1242
Quoted 10 lines
drivers/block/loop.c +10
-0+/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } +
+ if (mode & BLK_OPEN_EXCL) {
+ struct block_device *whole = bdev_whole(bdev);
+
+ BUG_ON(whole->bd_claiming == NULL);
+ }
I add the above code and do the following test:
# losetup -f data.1g # echo "0 `blockdev --getsz /dev/loop0` linear /dev/loop0 0" | dmsetup create my-linear # ./ioctl-test /dev/mapper/my-linear // trigger BUG_ON, ioctl-test.c is in attachment.
The root causes of BUG_ON:
1. When creating 'my-linear' device, the mode for opening /dev/loop0 does not include the BLK_OPEN_EXCL flag.table_load
- dm_table_create // get_mode() never assign BLK_OPEN_EXCL to {structdm_table *t}->mode
- populate_table
- dm_table_add_target
- linear_ctr
- dm_get_device // mode = {struct dm_table *t}->mode, never open
loop0 with BLK_OPEN_EXCL mode.
2. When 'my-linear' device is opened with the O_EXCL flag, and an ioctl is issued to it. The dm_blk_ioctl function calls bdev->bd_disk->fops-
Quoted 1 line
ioctl(bdev, mode, cmd, arg), which passes the mode withBLK_OPEN_EXCL
flag to lo_ioctl.
3. loop0 was not opened by dm_get_device() in BLK_OPEN_EXCL mode. As a result, whole->bd_claiming is NULL.
Thus, the BLK_OPEN_EXCL flag in the mode passed to lo_ioctl doesn't guarantee the loop device was opened with BLK_OPEN_EXCL.
How about use per-device rw_semaphore instead of 'bd_prepare_to_claim/ bd_abort_claiming'?
Yongpeng,
Quoted 86 lines
err = mutex_lock_killable(&lo->lo_mutex); if (err) return err;drivers/block/loop.c +17
-9@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int
drivers/block/loop.c static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode, · line 1707
Quoted 8 lines
drivers/block/loop.c +1
-1@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <linux/loop.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s [dev path]\n", argv[0]);
return 1;
}
const char *dev = argv[1];
struct loop_info64 info;
memset(&info, 0, sizeof(info));
int fd = open(dev, O_RDWR | O_EXCL);
if (fd < 0) {
fprintf(stderr, "Failed to open %s exclusively: %s\n",
dev, strerror(errno));
return 1;
}
if (ioctl(fd, LOOP_SET_STATUS64, &info) == -1) {
perror("ioctl error");
close(fd);
return 1;
}
close(fd);
return 0;
}Quoted 65 lines · Yongpeng Yang
Quoted 50 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").Reported-by [email protected]Tested-by [email protected]Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>---
ML thread for previous, misguided patch idea:
https://lore.kernel.org/all/[email protected]/t/drivers/block/loop.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)drivers/block/loop.c +12
-1@@ -1222,13 +1222,24 @@ static int loop_clr_fd(struct loop_device *lo) }static int -loop_set_status (struct loop_device *lo,const struct loop_info64 *info) +loop_set_status (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev,const struct loop_info64 *info) {int err;bool partscan = false;bool size_changed = false;unsigned int memflags; +/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } +(quoted diff) +5
-0+ if (mode & BLK_OPEN_EXCL) { + struct block_device *whole = bdev_whole(bdev); + + BUG_ON(whole->bd_claiming == NULL); + }I add the above code and do the following test:
# losetup -f data.1g # echo "0 `blockdev --getsz /dev/loop0` linear /dev/loop0 0" | dmsetup create my-linear # ./ioctl-test /dev/mapper/my-linear // trigger BUG_ON, ioctl-test.c is in attachment.
Quoted 11 lines
The root causes of
BUG_ON:
1. When creating 'my-linear' device, the mode for opening/dev/loop0does not include theBLK_OPEN_EXCLflag.table_load
-dm_table_create//get_mode()never assignBLK_OPEN_EXCLto {structdm_table*t}->mode
-populate_table
-dm_table_add_target
-linear_ctr
-dm_get_device// mode = {structdm_table*t}->mode, never open
loop0 withBLK_OPEN_EXCLmode.
BLK_OPEN_EXCL is added by bdev_open() whenever it is called with non-NULL holder. And DM code (open_table_device()) calls bdev_file_open_by_dev() with _dm_claim_ptr as the holder. So all opens from DM should be exclusive ones. The question obviously is what is broken in this that your reproducer still works...
Honza
Quoted 102 lines
2. When 'my-linear' device is opened with the
O_EXCLflag, and an ioctl is issued to it. Thedm_blk_ioctlfunction calls bdev->bd_disk->fops-Quoted 1 line
ioctl(bdev, mode, cmd, arg), which passes the mode withBLK_OPEN_EXCLflag to
lo_ioctl.3. loop0 was not opened by
dm_get_device()inBLK_OPEN_EXCLmode. As a result,whole->bd_claimingis NULL.Thus, the
BLK_OPEN_EXCLflag in the mode passed tolo_ioctldoesn't guarantee the loop device was opened withBLK_OPEN_EXCL.How about use per-device
rw_semaphoreinstead of 'bd_prepare_to_claim/bd_abort_claiming'?Yongpeng,
Quoted 86 lines
err = mutex_lock_killable(&lo->lo_mutex); if (err) return err;drivers/block/loop.c +18
-10@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Yongpeng Yang
Tested-byQuoted 84 lines · Jan Kara
Quoted 65 lines · Yongpeng Yang
Quoted 50 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").Reported-by [email protected]Tested-by [email protected]Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>---
ML thread for previous, misguided patch idea:
https://lore.kernel.org/all/[email protected]/t/drivers/block/loop.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)drivers/block/loop.c +12
-1@@ -1222,13 +1222,24 @@ static int loop_clr_fd(struct loop_device *lo) }static int -loop_set_status (struct loop_device *lo,const struct loop_info64 *info) +loop_set_status (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev,const struct loop_info64 *info) {int err;bool partscan = false;bool size_changed = false;unsigned int memflags; +/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } +(quoted diff) +5
-0+ if (mode & BLK_OPEN_EXCL) { + struct block_device *whole = bdev_whole(bdev); + + BUG_ON(whole->bd_claiming == NULL); + }I add the above code and do the following test:
# losetup -f data.1g # echo "0 `blockdev --getsz /dev/loop0` linear /dev/loop0 0" | dmsetup create my-linear # ./ioctl-test /dev/mapper/my-linear // trigger BUG_ON, ioctl-test.c is in attachment.Quoted 11 lines
The root causes of
BUG_ON:
1. When creating 'my-linear' device, the mode for opening/dev/loop0does not include theBLK_OPEN_EXCLflag.table_load
-dm_table_create//get_mode()never assignBLK_OPEN_EXCLto {structdm_table*t}->mode
-populate_table
-dm_table_add_target
-linear_ctr
-dm_get_device// mode = {structdm_table*t}->mode, never open
loop0 withBLK_OPEN_EXCLmode.
BLK_OPEN_EXCLis added bybdev_open()whenever it is called with non-NULL holder. And DM code (open_table_device()) callsbdev_file_open_by_dev()with_dm_claim_ptras the holder. So all opens from DM should be exclusive ones. The question obviously is what is broken in this that your reproducer still works...
Quoted 2 lines
Honza
Yes, I was mistaken. When loop0's whole->bd_claiming is NULL, bdev->bd_holder is set to _dm_claim_ptr by bd_finish_claiming. When the my-linear device is opened with BLK_OPEN_EXCL, its ioctls are handled normally. If the my-linear device is opened without BLK_OPEN_EXCL, the ioctl call returns -EBUSY, which is the expected behavior.
bdev_open
- bd_prepare_to_claim //whole->bd_claiming = _dm_claim_ptr;
- bd_finish_claiming // whole->bd_holder = bd_may_claim;bdev->bd_holder = _dm_claim_ptr;
- bd_clear_claiming // whole->bd_claiming = NULL;
Yongpeng,
Quoted 104 lines
Quoted 102 lines
2. When 'my-linear' device is opened with the
O_EXCLflag, and an ioctl is issued to it. Thedm_blk_ioctlfunction calls bdev->bd_disk->fops-Quoted 1 line
ioctl(bdev, mode, cmd, arg), which passes the mode withBLK_OPEN_EXCLflag to
lo_ioctl.3. loop0 was not opened by
dm_get_device()inBLK_OPEN_EXCLmode. As a result,whole->bd_claimingis NULL.Thus, the
BLK_OPEN_EXCLflag in the mode passed tolo_ioctldoesn't guarantee the loop device was opened withBLK_OPEN_EXCL.How about use per-device
rw_semaphoreinstead of 'bd_prepare_to_claim/bd_abort_claiming'?Yongpeng,
Quoted 86 lines
err = mutex_lock_killable(&lo->lo_mutex); if (err) return err;drivers/block/loop.c +18
-10@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :
Quoted 14 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").
Quoted 4 lines
Reported-by [email protected]Tested-by [email protected]Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>
This patch looks mostly good to me. Just one comment:
Quoted 9 lines
drivers/block/loop.c +2
-1-loop_set_status(struct loop_device *lo, const struct loop_info64 *info) +loop_set_status(struct loop_device *lo, blk_mode_t mode, + struct block_device *bdev, const struct loop_info64 *info) { int err;bool partscan = false;bool size_changed = false;unsigned int memflags;
drivers/block/loop.c static int loop_clr_fd(struct loop_device *lo) · line 1242
Quoted 10 lines
drivers/block/loop.c +10
-0+/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } +
So now any LOOP_SET_STATUS call will fail for device that is already exclusively open. There are some operations (like modifying the AUTOCLEAR flag or loop device name) that are safe even for a device with a mounted filesystem. I wouldn't probably bother with that now but I wanted to note that there may be valid uses of LOOP_SET_STATUS even for an exclusively open loop device and if there are users of that out there we might need to refine this a bit. Anyway for now feel free to add:
Honza
Quoted 97 lines
drivers/block/loop.c +18
-10err =mutex_lock_killable (&lo->lo_mutex);if (err)return err;@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 50 lines · Jan Kara
Quoted 14 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").Quoted 4 lines
Reported-by [email protected]Tested-by [email protected]Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>This patch looks mostly good to me. Just one comment:
Quoted 9 lines
drivers/block/loop.c +2
-1-loop_set_status(struct loop_device *lo, const struct loop_info64 *info) +loop_set_status(struct loop_device *lo, blk_mode_t mode, + struct block_device *bdev, const struct loop_info64 *info) { int err;bool partscan = false;bool size_changed = false;unsigned int memflags;drivers/block/loop.c static int loop_clr_fd(struct loop_device *lo) · line 1242
Quoted 10 lines
drivers/block/loop.c +10
-0+/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } +So now any
LOOP_SET_STATUScall will fail for device that is already exclusively open. There are some operations (like modifying the AUTOCLEAR flag or loop device name) that are safe even for a device with a mounted filesystem. I wouldn't probably bother with that now but I wanted to note that there may be valid uses ofLOOP_SET_STATUSeven for an exclusively open loop device and if there are users of that out there we might need to refine this a bit. Anyway for now feel free to add:
Quoted 1 line
Reviewed-by Jan Kara <[email protected]>
Raphael, this patch seems to have fallen through the cracks. Can you please resend it to Jens with by Reviewed-by tag so that he picks it up? Thanks!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 1/1 loop: don't change loop device under exclusive opener in loop_set_status
loop_set_status() is allowed to change the loop device while there are other openers of the device, even exclusive ones.
In this case, it causes a KASAN: slab-out-of-bounds Read in ext4_search_dir(), since when looking for an entry in an inlined directory, e_value_offs is changed underneath the filesystem by loop_set_status().
Fix the problem by forbidding loop_set_status() from modifying the loop device while there are exclusive openers of the device. This is similar to the fix in loop_configure() by commit 33ec3e53e7b1 ("loop: Don't change loop device under exclusive opener") alongside commit ecbe6bc0003b ("block: use bd_prepare_to_claim directly in the loop driver").
---
v2:
- added Tested-by and Reviewed-by tags for v1
drivers/block/loop.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)
drivers/block/loop.c +30 -11
@@ -1222,13 +1222,24 @@ static int loop_clr_fd(struct loop_device *lo) }static int -loop_set_status (struct loop_device *lo,const struct loop_info64 *info) +loop_set_status (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev,const struct loop_info64 *info) {int err;bool partscan = false;bool size_changed = false;unsigned int memflags; +/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } + err =mutex_lock_killable (&lo->lo_mutex);if (err)return err;
@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);
@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;
@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int
@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);
@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;
@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int
@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :
Signature · 1 line
2.43.0
Quoted 14 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").
Quoted 6 lines
Reported-by [email protected]Tested-by [email protected]Tested-by Yongpeng Yang <[email protected]>Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>Reviewed-by Jan Kara <[email protected]>
Jens, ping?
Honza
Quoted 131 lines
---
v2:
- added Tested-by and Reviewed-by tags for v1drivers/block/loop.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)drivers/block/loop.c +30
-11@@ -1222,13 +1222,24 @@ static int loop_clr_fd(struct loop_device *lo) }static int -loop_set_status (struct loop_device *lo,const struct loop_info64 *info) +loop_set_status (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev,const struct loop_info64 *info) {int err;bool partscan = false;bool size_changed = false;unsigned int memflags; +/* +* If we don't hold exclusive handle for the device, upgrade to it +* here to avoid changing device under exclusive owner. +*/ +if (!(mode &BLK_OPEN_EXCL )) { + err =bd_prepare_to_claim (bdev, loop_set_status,NULL ); +if (err) + goto out_reread_partitions; + } + err =mutex_lock_killable (&lo->lo_mutex);if (err)return err;@@ -1270,6 +1281,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } out_unlock:mutex_unlock (&lo->lo_mutex); +if (!(mode &BLK_OPEN_EXCL )) +bd_abort_claiming (bdev, loop_set_status); +out_reread_partitions:if (partscan)loop_reread_partitions (lo);@@ -1349,7 +1363,9 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) }static int -loop_set_status_old (struct loop_device *lo,const struct loop_info __user *arg) +loop_set_status_old (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info __user *arg) {struct loop_info info;struct loop_info64 info64;@@ -1357,17 +1373,19 @@ loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)if (copy_from_user (&info, arg,sizeof (struct loop_info )))return -EFAULT ;loop_info64_from_old (&info, &info64); -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); }static int -loop_set_status64 (struct loop_device *lo,const struct loop_info64 __user *arg) +loop_set_status64 (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct loop_info64 __user *arg) {struct loop_info64 info64;if (copy_from_user (&info64, arg,sizeof (struct loop_info64 )))return -EFAULT ; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1546,14 +1564,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,case LOOP_SET_STATUS : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status_old (lo, argp); + err =loop_set_status_old (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS :return loop_get_status_old (lo, argp);case LOOP_SET_STATUS64 : err = -EPERM ;if ((mode &BLK_OPEN_WRITE ) ||capable (CAP_SYS_ADMIN )) - err =loop_set_status64 (lo, argp); + err =loop_set_status64 (lo, mode, bdev, argp);break ;case LOOP_GET_STATUS64 :return loop_get_status64 (lo, argp);@@ -1647,8 +1665,9 @@ loop_info64_to_compat(const struct loop_info64 *info64, }static int -loop_set_status_compat (struct loop_device *lo, -const struct compat_loop_info __user *arg) +loop_set_status_compat (struct loop_device *lo,blk_mode_t mode, +struct block_device *bdev, +const struct compat_loop_info __user *arg) {struct loop_info64 info64;int ret;@@ -1656,7 +1675,7 @@ loop_set_status_compat(struct loop_device *lo, ret =loop_info64_from_compat (arg, &info64);if (ret <0 )return ret; -return loop_set_status (lo, &info64); +return loop_set_status (lo, mode, bdev, &info64); } static int@@ -1682,7 +1701,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case LOOP_SET_STATUS : - err =loop_set_status_compat (lo, + err =loop_set_status_compat (lo, mode, bdev, (const struct compat_loop_info __user *)arg);break ;case LOOP_GET_STATUS :Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jens Axboe
maintainerQuoted 15 lines · Jan Kara
Quoted 14 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().Fix the problem by forbidding
loop_set_status()from modifying the loop device while there are exclusive openers of the device. This is similar to the fix inloop_configure()by commit33ec3e53e7b1("loop: Don't change loop device under exclusive opener") alongside commitecbe6bc0003b("block: usebd_prepare_to_claimdirectly in the loop driver").
Quoted 8 lines
Quoted 6 lines
Reported-by [email protected]Tested-by [email protected]Tested-by Yongpeng Yang <[email protected]>Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>Reviewed-by Jan Kara <[email protected]>Jens, ping?
Now applied. Heads up in general, don't nest v2 or later inside the original thread. It just makes emails get lost, as it appears part of the original discussion.
Signature · 1 line
Jens Axboe
Quoted 9 lines · Raphael Pinsonneault-Thibeault
loop_set_status()is allowed to change the loop device while there are other openers of the device, even exclusive ones.In this case, it causes a KASAN: slab-out-of-bounds Read in
ext4_search_dir(), since when looking for an entry in an inlined directory,e_value_offsis changed underneath the filesystem byloop_set_status().[...]
Applied, thanks!
[1/1] loop: don't change loop device under exclusive opener in loop_set_status
commit: 08e136ebd193eae7d5eff4c66d576c4a2dabdc3f
Best regards,
Signature · 1 line
Jens Axboe