Skip to contentSkip to conversation

loop: mask loop_control_ioctl parameter only as minor

PATCH In review wangyangbo 3 participants 8 messages Nov 18, 2021 – Nov 19, 2021 Block layer

lore
Share & tools
lore

wangyangbo

author
#0 lore raw

PATCH 1/1 loop: mask loop_control_ioctl parameter only as minor

From: wangyangbo <[email protected]>

loop_control_ioctl call add_disk of block layer, but may pass number beyond MAX MINOR. So mask loop_control_ioctl parameter only as minor.

Reproduce:
touch file
losetup /dev/loop1048576 file
losetup /dev/loop0 file

Problem:
sysfs: cannot create duplicate filename '/dev/block/7:0'

CPU: 0 PID: 529 Comm: losetup Not tainted 5.14.16-arch1-1 #1 ad87b876fa2ab6fdbd995dc1c9aab0ad8f767b2c
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
Call Trace:
 dump_stack_lvl+0x46/0x5a
 sysfs_warn_dup.cold+0x17/0x24
 sysfs_do_create_link_sd+0xbe/0xd0
 device_add+0x580/0x970
 ? dev_set_name+0x5b/0x80
 __device_add_disk+0xb5/0x2f0
 loop_add+0x236/0x290 [loop 2ed923fc8fbd84fdc093bf55ac085973636a3936]
 loop_control_ioctl+0x7f/0x1f0 [loop 2ed923fc8fbd84fdc093bf55ac085973636a3936]
 __x64_sys_ioctl+0x82/0xb0
 do_syscall_64+0x5c/0x80
 ? syscall_exit_to_user_mode+0x23/0x40
 ? do_syscall_64+0x69/0x80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
RIP: 0033:0x7ff3e8ba259b
Code: ff ff ff 85 c0 79 9b 49 c7 c4 ff ff ff ff 5b 5d 4c 89 e0 41 5c c3 66 0f 1f 84 00 00 00 00 00 f3 0f 1e fa b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d a5 a8 0c 00 f7 d8 64 89 01 48
RSP: 002b:00007ffe6d2eaa18 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffe6d2eb038 RCX: 00007ff3e8ba259b
RDX: 0000000000000000 RSI: 0000000000004c80 RDI: 0000000000000003
RBP: 00007ffe6d2eaae0 R08: 1999999999999999 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000003
R13: 00007ffe6d2eaa24 R14: 0000560b131bd200 R15: 0000560b131c44a0
---
 drivers/block/loop.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/block/loop.c +3 -3
index a154cab6cd98..0d8240a7e66d 100644
@@ -2170,11 +2170,11 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 {
 	switch (cmd) {
 	case LOOP_CTL_ADD:
-		return loop_add(parm);
+		return loop_add(MINOR(parm));
 	case LOOP_CTL_REMOVE:
-		return loop_control_remove(parm);
+		return loop_control_remove(MINOR(parm));
 	case LOOP_CTL_GET_FREE:
-		return loop_control_get_free(parm);
+		return loop_control_get_free(MINOR(parm));
 	default:
 		return -ENOSYS;
 	}
Signature · 1 line
2.20.1
#1 lore raw

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2173

Quoted 6 lines · wangyangbo
drivers/block/loop.c +1 -1
@@ -2170,11 +2170,11 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 {
 	switch (cmd) {
 	case LOOP_CTL_ADD:
-		return loop_add(parm);
+		return loop_add(MINOR(parm));

Better to return -EINVAL or something if out of minor range?

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2175

Quoted 3 lines
drivers/block/loop.c +1 -1
 	case LOOP_CTL_REMOVE:
-		return loop_control_remove(parm);
+		return loop_control_remove(MINOR(parm));

This is bad, for this change makes

	if (idx < 0) {
		pr_warn("deleting an unspecified loop device is not supported.\n");
		return -EINVAL;
	}

dead code by masking the argument to 0-1048575 range.

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2177

Quoted 3 lines
drivers/block/loop.c +1 -1
 	case LOOP_CTL_GET_FREE:
-		return loop_control_get_free(parm);
+		return loop_control_get_free(MINOR(parm));

This is pointless, for the passed argument is not used. By the way, didn't someone already propose removal of this argument?

Quoted 4 lines
drivers/block/loop.c +0 -0
 	default:
 		return -ENOSYS;
 	}
#2 lore raw
Quoted 9 lines · Tetsuo Handa

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2173

Quoted 6 lines · wangyangbo
drivers/block/loop.c +1 -1
@@ -2170,11 +2170,11 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 {
 	switch (cmd) {
 	case LOOP_CTL_ADD:
-		return loop_add(parm);
+		return loop_add(MINOR(parm));

Better to return -EINVAL or something if out of minor range?

Well, this is not specific to loop devices. Shouldn't the minor range be checked by device_add_disk() ?

#3 lore raw
Quoted 15 lines · Tetsuo Handa
Quoted 9 lines · Tetsuo Handa

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2173

Quoted 7 lines · wangyangbo
drivers/block/loop.c +1 -1
@@ -2170,11 +2170,11 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 {
   switch (cmd) {
   case LOOP_CTL_ADD:
-        return loop_add(parm);
+        return loop_add(MINOR(parm));

Better to return -EINVAL or something if out of minor range?

Quoted 2 lines

Well, this is not specific to loop devices. Shouldn't the minor range be checked by device_add_disk() ?

I just think ioctl paramter need to make sense.

Perhaps block layer need add/update/del check for consistency, but as inner interface caller check is also agreeable.

By the way, do other driver have similar problems?

#4 lore raw
In reply to Tetsuo Handa
Quoted 11 lines · Tetsuo Handa
Quoted 9 lines · Tetsuo Handa

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2173

Quoted 6 lines · wangyangbo
drivers/block/loop.c +1 -1
@@ -2170,11 +2170,11 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 {
 	switch (cmd) {
 	case LOOP_CTL_ADD:
-		return loop_add(parm);
+		return loop_add(MINOR(parm));

Better to return -EINVAL or something if out of minor range?

Quoted 2 lines

Well, this is not specific to loop devices. Shouldn't the minor range be checked by device_add_disk() ?

Yes, that probably makes sense.

author
#5 lore raw
In reply to Tetsuo Handa
Quoted 13 lines · Tetsuo Handa

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2173

Quoted 6 lines · wangyangbo
drivers/block/loop.c +1 -1
@@ -2170,11 +2170,11 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 {
   switch (cmd) {
   case LOOP_CTL_ADD:
-        return loop_add(parm);
+        return loop_add(MINOR(parm));

Better to return -EINVAL or something if out of minor range?

Definitely, EINVAL or EDOM, which do you think is better?

Quoted 5 lines

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2175

Quoted 4 lines
drivers/block/loop.c +1 -1
   case LOOP_CTL_REMOVE:
-        return loop_control_remove(parm);
+        return loop_control_remove(MINOR(parm));
Quoted 8 lines

This is bad, for this change makes

   if (idx < 0) {
       pr_warn("deleting an unspecified loop device is not supported.\n");
       return -EINVAL;
   }

dead code by masking the argument to 0-1048575 range.

But ioctl param is unsigned long, I think this need to sanitize.

Quoted 6 lines

drivers/block/loop.c static long loop_control_ioctl(struct file *file, unsigned int cmd, · line 2177

Quoted 3 lines
drivers/block/loop.c +1 -1
   case LOOP_CTL_GET_FREE:
-        return loop_control_get_free(parm);
+        return loop_control_get_free(MINOR(parm));

This is pointless, for the passed argument is not used. By the way, didn't someone already propose removal of this argument?

I don't find this in mail list, but I would like to sanitize that code.

author
#6 lore raw

PATCH 1/1 loop: check loop_control_ioctl parameter in range of minor

In reply to wangyangbo

loop_control_ioctl call add_disk of block layer, but may pass number beyond MAX MINOR. So check loop_control_ioctl parameter in range of minor, and delete redundant code.

Reproduce:
touch file
losetup /dev/loop1048576 file
losetup /dev/loop0 file

Problem:
sysfs: cannot create duplicate filename '/dev/block/7:0'

CPU: 0 PID: 529 Comm: losetup Not tainted 5.14.16-arch1-1 #1 ad87b876fa2ab6fdbd995dc1c9aab0ad8f767b2c
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
Call Trace:
 dump_stack_lvl+0x46/0x5a
 sysfs_warn_dup.cold+0x17/0x24
 sysfs_do_create_link_sd+0xbe/0xd0
 device_add+0x580/0x970
 ? dev_set_name+0x5b/0x80
 __device_add_disk+0xb5/0x2f0
 loop_add+0x236/0x290 [loop 2ed923fc8fbd84fdc093bf55ac085973636a3936]
 loop_control_ioctl+0x7f/0x1f0 [loop 2ed923fc8fbd84fdc093bf55ac085973636a3936]
 __x64_sys_ioctl+0x82/0xb0
 do_syscall_64+0x5c/0x80
 ? syscall_exit_to_user_mode+0x23/0x40
 ? do_syscall_64+0x69/0x80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
RIP: 0033:0x7ff3e8ba259b
Code: ff ff ff 85 c0 79 9b 49 c7 c4 ff ff ff ff 5b 5d 4c 89 e0 41 5c c3 66 0f 1f 84 00 00 00 00 00 f3 0f 1e fa b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d a5 a8 0c 00 f7 d8 64 89 01 48
RSP: 002b:00007ffe6d2eaa18 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffe6d2eb038 RCX: 00007ff3e8ba259b
RDX: 0000000000000000 RSI: 0000000000004c80 RDI: 0000000000000003
RBP: 00007ffe6d2eaae0 R08: 1999999999999999 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000003
R13: 00007ffe6d2eaa24 R14: 0000560b131bd200 R15: 0000560b131c44a0
---
 drivers/block/loop.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/block/loop.c +5 -7
index a154cab6cd98..3b19ffaa63e9 100644
@@ -2102,11 +2102,6 @@ static int loop_control_remove(int idx)
 	struct loop_device *lo;
 	int ret;
 
-	if (idx < 0) {
-		pr_warn("deleting an unspecified loop device is not supported.\n");
-		return -EINVAL;
-	}
-		
 	/* Hide this loop device for serialization. */
 	ret = mutex_lock_killable(&loop_ctl_mutex);
 	if (ret)
@@ -2145,7 +2140,7 @@ static int loop_control_remove(int idx)
 	return ret;
 }
 
-static int loop_control_get_free(int idx)
+static int loop_control_get_free(void)
 {
 	struct loop_device *lo;
 	int id, ret;
@@ -2168,13 +2163,16 @@ static int loop_control_get_free(int idx)
 static long loop_control_ioctl(struct file *file, unsigned int cmd,
 			       unsigned long parm)
 {
+	if (parm > MINORMASK)
+		pr_warn("ioctl parameter is out of max_minor.\n");
+		return -EINVAL;
 	switch (cmd) {
 	case LOOP_CTL_ADD:
 		return loop_add(parm);
 	case LOOP_CTL_REMOVE:
 		return loop_control_remove(parm);
 	case LOOP_CTL_GET_FREE:
-		return loop_control_get_free(parm);
+		return loop_control_get_free();
 	default:
 		return -ENOSYS;
 	}
Signature · 1 line
2.20.1
author
#7 lore raw

PATCH v2 1/1 loop: check loop_control_ioctl parameter in range of minor

In reply to wangyangbo

loop_control_ioctl call add_disk of block layer, but may pass number beyond MAX MINOR. So check loop_control_ioctl parameter in range of minor, and delete redundant code.

Reproduce:
touch file
losetup /dev/loop1048576 file
losetup /dev/loop0 file

Problem:
sysfs: cannot create duplicate filename '/dev/block/7:0'

CPU: 0 PID: 529 Comm: losetup Not tainted 5.14.16-arch1-1 #1 ad87b876fa2ab6fdbd995dc1c9aab0ad8f767b2c
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
Call Trace:
 dump_stack_lvl+0x46/0x5a
 sysfs_warn_dup.cold+0x17/0x24
 sysfs_do_create_link_sd+0xbe/0xd0
 device_add+0x580/0x970
 ? dev_set_name+0x5b/0x80
 __device_add_disk+0xb5/0x2f0
 loop_add+0x236/0x290 [loop 2ed923fc8fbd84fdc093bf55ac085973636a3936]
 loop_control_ioctl+0x7f/0x1f0 [loop 2ed923fc8fbd84fdc093bf55ac085973636a3936]
 __x64_sys_ioctl+0x82/0xb0
 do_syscall_64+0x5c/0x80
 ? syscall_exit_to_user_mode+0x23/0x40
 ? do_syscall_64+0x69/0x80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
RIP: 0033:0x7ff3e8ba259b
Code: ff ff ff 85 c0 79 9b 49 c7 c4 ff ff ff ff 5b 5d 4c 89 e0 41 5c c3 66 0f 1f 84 00 00 00 00 00 f3 0f 1e fa b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d a5 a8 0c 00 f7 d8 64 89 01 48
RSP: 002b:00007ffe6d2eaa18 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffe6d2eb038 RCX: 00007ff3e8ba259b
RDX: 0000000000000000 RSI: 0000000000004c80 RDI: 0000000000000003
RBP: 00007ffe6d2eaae0 R08: 1999999999999999 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000003
R13: 00007ffe6d2eaa24 R14: 0000560b131bd200 R15: 0000560b131c44a0
---
 drivers/block/loop.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/block/loop.c +6 -7
index a154cab6cd98..9e9d164b2a65 100644
@@ -2102,11 +2102,6 @@ static int loop_control_remove(int idx)
 	struct loop_device *lo;
 	int ret;
 
-	if (idx < 0) {
-		pr_warn("deleting an unspecified loop device is not supported.\n");
-		return -EINVAL;
-	}
-		
 	/* Hide this loop device for serialization. */
 	ret = mutex_lock_killable(&loop_ctl_mutex);
 	if (ret)
@@ -2145,7 +2140,7 @@ static int loop_control_remove(int idx)
 	return ret;
 }
 
-static int loop_control_get_free(int idx)
+static int loop_control_get_free(void)
 {
 	struct loop_device *lo;
 	int id, ret;
@@ -2168,13 +2163,17 @@ static int loop_control_get_free(int idx)
 static long loop_control_ioctl(struct file *file, unsigned int cmd,
 			       unsigned long parm)
 {
+	if (parm > MINORMASK) {
+		pr_warn("ioctl parameter is out of max_minor.\n");
+		return -EINVAL;
+	}
 	switch (cmd) {
 	case LOOP_CTL_ADD:
 		return loop_add(parm);
 	case LOOP_CTL_REMOVE:
 		return loop_control_remove(parm);
 	case LOOP_CTL_GET_FREE:
-		return loop_control_get_free(parm);
+		return loop_control_get_free();
 	default:
 		return -ENOSYS;
 	}
Signature · 1 line
2.20.1