linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] loop: mask loop_control_ioctl parameter only as minor
@ 2021-11-18  2:36 wangyangbo
  2021-11-18 14:15 ` Tetsuo Handa
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: wangyangbo @ 2021-11-18  2:36 UTC (permalink / raw)
  To: axboe; +Cc: linux-block, linux-kernel, wangyangbo

From: wangyangbo <wangyangbo@uniontech.com>

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(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a154cab6cd98..0d8240a7e66d 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -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;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] loop: mask loop_control_ioctl parameter only as minor
  2021-11-18  2:36 [PATCH] loop: mask loop_control_ioctl parameter only as minor wangyangbo
@ 2021-11-18 14:15 ` Tetsuo Handa
  2021-11-19  1:50   ` wangyangbo
  2021-11-19  2:24 ` [PATCH] loop: check loop_control_ioctl parameter in range of minor wangyangbo
  2021-11-19  2:32 ` [PATCH v2] " wangyangbo
  2 siblings, 1 reply; 5+ messages in thread
From: Tetsuo Handa @ 2021-11-18 14:15 UTC (permalink / raw)
  To: wangyangbo, axboe; +Cc: linux-block, linux-kernel

On 2021/11/18 11:36, wangyangbo wrote:
> @@ -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?

>  	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.

>  	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?

>  	default:
>  		return -ENOSYS;
>  	}
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] loop: mask loop_control_ioctl parameter only as minor
  2021-11-18 14:15 ` Tetsuo Handa
@ 2021-11-19  1:50   ` wangyangbo
  0 siblings, 0 replies; 5+ messages in thread
From: wangyangbo @ 2021-11-19  1:50 UTC (permalink / raw)
  To: Tetsuo Handa; +Cc: axboe, linux-block, linux-kernel



> On Nov 18, 2021, at 22:15, Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp> wrote:
> 
> On 2021/11/18 11:36, wangyangbo wrote:
>> @@ -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?

> 
>>    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.

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

>>    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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] loop: check loop_control_ioctl parameter in range of minor
  2021-11-18  2:36 [PATCH] loop: mask loop_control_ioctl parameter only as minor wangyangbo
  2021-11-18 14:15 ` Tetsuo Handa
@ 2021-11-19  2:24 ` wangyangbo
  2021-11-19  2:32 ` [PATCH v2] " wangyangbo
  2 siblings, 0 replies; 5+ messages in thread
From: wangyangbo @ 2021-11-19  2:24 UTC (permalink / raw)
  To: axboe; +Cc: penguin-kernel, linux-block, linux-kernel, 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(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a154cab6cd98..3b19ffaa63e9 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -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;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2] loop: check loop_control_ioctl parameter in range of minor
  2021-11-18  2:36 [PATCH] loop: mask loop_control_ioctl parameter only as minor wangyangbo
  2021-11-18 14:15 ` Tetsuo Handa
  2021-11-19  2:24 ` [PATCH] loop: check loop_control_ioctl parameter in range of minor wangyangbo
@ 2021-11-19  2:32 ` wangyangbo
  2 siblings, 0 replies; 5+ messages in thread
From: wangyangbo @ 2021-11-19  2:32 UTC (permalink / raw)
  To: axboe; +Cc: penguin-kernel, linux-block, linux-kernel, 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(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a154cab6cd98..9e9d164b2a65 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -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;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-11-19  2:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-18  2:36 [PATCH] loop: mask loop_control_ioctl parameter only as minor wangyangbo
2021-11-18 14:15 ` Tetsuo Handa
2021-11-19  1:50   ` wangyangbo
2021-11-19  2:24 ` [PATCH] loop: check loop_control_ioctl parameter in range of minor wangyangbo
2021-11-19  2:32 ` [PATCH v2] " wangyangbo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).