mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] block: Fix minor range check in device_add_disk()
@ 2023-10-25  8:46 Zhong Jinghua
  2023-10-25 10:06 ` Tetsuo Handa
  0 siblings, 1 reply; 5+ messages in thread
From: Zhong Jinghua @ 2023-10-25  8:46 UTC (permalink / raw)
  To: axboe, penguin-kernel
  Cc: linux-block, linux-kernel, zhongjinghua, yi.zhang, yukuai3

From: Zhong Jinghua <zhongjinghua@huawei.com>

Checks added in patch:
commit e338924bd05d ("block: check minor range in device_add_disk()")
ignore the problem of first_minore < 0 and disk->minors < 0.

Fix it by adding first_minore < 0 and disk->minors < 0 check.

Fixes: e338924bd05d ("block: check minor range in device_add_disk()")
Signed-off-by: Zhong Jinghua <zhongjinghua@huawei.com>
---
 block/genhd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/block/genhd.c b/block/genhd.c
index 736215e9ddc3..8292a1e265cf 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -432,7 +432,9 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 				DISK_MAX_PARTS);
 			disk->minors = DISK_MAX_PARTS;
 		}
-		if (disk->first_minor + disk->minors > MINORMASK + 1)
+		if (disk->first_minor > MINORMASK ||
+			disk->minors > (1U << MINORBITS) ||
+			disk->first_minor + disk->minors > MINORMASK + 1)
 			goto out_exit_elevator;
 	} else {
 		if (WARN_ON(disk->minors))
-- 
2.31.1


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

* Re: [PATCH] block: Fix minor range check in device_add_disk()
  2023-10-25  8:46 [PATCH] block: Fix minor range check in device_add_disk() Zhong Jinghua
@ 2023-10-25 10:06 ` Tetsuo Handa
  2023-10-26  8:52   ` zhongjinghua
  0 siblings, 1 reply; 5+ messages in thread
From: Tetsuo Handa @ 2023-10-25 10:06 UTC (permalink / raw)
  To: Zhong Jinghua, axboe
  Cc: linux-block, linux-kernel, zhongjinghua, yi.zhang, yukuai3

On 2023/10/25 17:46, Zhong Jinghua wrote:
> Checks added in patch:
> commit e338924bd05d ("block: check minor range in device_add_disk()")
> ignore the problem of first_minore < 0 and disk->minors < 0.

What is the problem of first_minor < 0 or disk->minors < 0 ?
Are negative values legal/illegal ?


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

* Re: [PATCH] block: Fix minor range check in device_add_disk()
  2023-10-25 10:06 ` Tetsuo Handa
@ 2023-10-26  8:52   ` zhongjinghua
  2023-10-30  9:26     ` Yu Kuai
  0 siblings, 1 reply; 5+ messages in thread
From: zhongjinghua @ 2023-10-26  8:52 UTC (permalink / raw)
  To: Tetsuo Handa, axboe
  Cc: linux-block, linux-kernel, zhongjinghua, yi.zhang, yukuai3


在 2023/10/25 18:06, Tetsuo Handa 写道:
> On 2023/10/25 17:46, Zhong Jinghua wrote:
>> Checks added in patch:
>> commit e338924bd05d ("block: check minor range in device_add_disk()")
>> ignore the problem of first_minore < 0 and disk->minors < 0.
> What is the problem of first_minor < 0 or disk->minors < 0 ?
> Are negative values legal/illegal ?

These two values are used as the secondary device number and the maximum number of partitions, which is illegal if negative. Then first_minore and disk->minors are signed numbers, and the sum may be less than MINORMASK to bypass the check.


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

* Re: [PATCH] block: Fix minor range check in device_add_disk()
  2023-10-26  8:52   ` zhongjinghua
@ 2023-10-30  9:26     ` Yu Kuai
  2023-10-30  9:32       ` zhongjinghua
  0 siblings, 1 reply; 5+ messages in thread
From: Yu Kuai @ 2023-10-30  9:26 UTC (permalink / raw)
  To: zhongjinghua, Tetsuo Handa, axboe
  Cc: linux-block, linux-kernel, zhongjinghua, yi.zhang, yukuai (C)

Hi,

在 2023/10/26 16:52, zhongjinghua 写道:
> 
> 在 2023/10/25 18:06, Tetsuo Handa 写道:
>> On 2023/10/25 17:46, Zhong Jinghua wrote:
>>> Checks added in patch:
>>> commit e338924bd05d ("block: check minor range in device_add_disk()")
>>> ignore the problem of first_minore < 0 and disk->minors < 0.
>> What is the problem of first_minor < 0 or disk->minors < 0 ?
>> Are negative values legal/illegal ?
> 
> These two values are used as the secondary device number and the maximum 
> number of partitions, which is illegal if negative. Then first_minore 
> and disk->minors are signed numbers, and the sum may be less than 
> MINORMASK to bypass the check.

Let me complement it, first_minor and minors can be set by driver, and
driver allow set them throuhh ioctl/sysfs from user parameters, for
example:

If user pass in -1, and each disk support 8 partitions, driver will
usually set:

disk->first_minor = -1 * 8 = -8;
disk->minors = 8;

Then first_minor + minors = 0, then the following condition can't detect
this case:

if (disk->first_minor + disk->minors > MINORMASK + 1)

By the way, we never limit how first_minor and minors is set by driver,
and it's illegal if driver set first_minor = -4, and minors = 8.

Thanks,
Kuai

> 
> .
> 


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

* Re: [PATCH] block: Fix minor range check in device_add_disk()
  2023-10-30  9:26     ` Yu Kuai
@ 2023-10-30  9:32       ` zhongjinghua
  0 siblings, 0 replies; 5+ messages in thread
From: zhongjinghua @ 2023-10-30  9:32 UTC (permalink / raw)
  To: Yu Kuai, zhongjinghua, Tetsuo Handa, axboe
  Cc: linux-block, linux-kernel, yi.zhang, yukuai (C)


在 2023/10/30 17:26, Yu Kuai 写道:
> Hi,
>
> 在 2023/10/26 16:52, zhongjinghua 写道:
>>
>> 在 2023/10/25 18:06, Tetsuo Handa 写道:
>>> On 2023/10/25 17:46, Zhong Jinghua wrote:
>>>> Checks added in patch:
>>>> commit e338924bd05d ("block: check minor range in device_add_disk()")
>>>> ignore the problem of first_minore < 0 and disk->minors < 0.
>>> What is the problem of first_minor < 0 or disk->minors < 0 ?
>>> Are negative values legal/illegal ?
>>
>> These two values are used as the secondary device number and the 
>> maximum number of partitions, which is illegal if negative. Then 
>> first_minore and disk->minors are signed numbers, and the sum may be 
>> less than MINORMASK to bypass the check.
>
> Let me complement it, first_minor and minors can be set by driver, and
> driver allow set them throuhh ioctl/sysfs from user parameters, for
> example:
>
> If user pass in -1, and each disk support 8 partitions, driver will
> usually set:
>
> disk->first_minor = -1 * 8 = -8;
> disk->minors = 8;
>
> Then first_minor + minors = 0, then the following condition can't detect
> this case:
>
> if (disk->first_minor + disk->minors > MINORMASK + 1)
>
> By the way, we never limit how first_minor and minors is set by driver,
> and it's illegal if driver set first_minor = -4, and minors = 8.
>
> Thanks,
> Kuai
>
>>
>> .
>>
>
Kuai, Thank for your explanation.

Jinghua


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

end of thread, other threads:[~2023-10-30  9:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-25  8:46 [PATCH] block: Fix minor range check in device_add_disk() Zhong Jinghua
2023-10-25 10:06 ` Tetsuo Handa
2023-10-26  8:52   ` zhongjinghua
2023-10-30  9:26     ` Yu Kuai
2023-10-30  9:32       ` zhongjinghua

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®