nullb: Adjust device size calculation in null_alloc_dev()
PATCH 1/1 nullb: Adjust device size calculation in null_alloc_dev()
In null_alloc_dev() device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.
Cast 'g_gb' to unsigned long to prevent overflow.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
--- drivers/block/null_blk/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/null_blk/main.c +1 -1
@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; } - dev->size = g_gb *1024 ; + dev->size = (unsigned long )g_gb *1024 ; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
Signature · 1 line
2.30.2
Quoted 19 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>--- drivers/block/null_blk/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/null_blk/main.c +0
-0
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 765
Quoted 5 lines
drivers/block/null_blk/main.c +1
-1@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; } - dev->size = g_gb *1024 ; + dev->size = (unsigned long )g_gb *1024 ;
This still does not prevent overflows... So what about doing a proper check ?
Quoted 3 lines
drivers/block/null_blk/main.c +0
-0dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 23 lines · Damien Le Moal
Quoted 22 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>--- drivers/block/null_blk/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/null_blk/main.c +0
-0@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; }
Quoted 4 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 765
Quoted 2 lines
drivers/block/null_blk/main.c +1
-1- dev->size = g_gb *1024 ; + dev->size = (unsigned long )g_gb *1024 ;This still does not prevent overflows... So what about doing a proper check ?
This still does not prevent overflows on 32-bits architectures.
Quoted 5 lines
Quoted 4 lines
drivers/block/null_blk/main.c +0
-0dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 24 lines · Damien Le Moal
Quoted 23 lines · Damien Le Moal
Quoted 22 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>--- drivers/block/null_blk/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/null_blk/main.c +0
-0@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; }
Quoted 4 lines
Quoted 3 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 765
Quoted 2 lines
drivers/block/null_blk/main.c +1
-1- dev->size = g_gb *1024 ; + dev->size = (unsigned long )g_gb *1024 ;This still does not prevent overflows... So what about doing a proper check ?
This still does not prevent overflows on 32-bits architectures.
The max value of "unsigned long" is 2^64 - 1 while the max value of int is 2^31 -1.
(2^64 - 1) / (2^31-1) is about 2^33 while 1024 is 2^10.
2^33 is greater than 2^10.
So in the above, it seems that it is difficult to overflow.
If I am missing something, please let me know.
Thanks,
Zhu Yanjun
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/null_blk/main.c +0
-0dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
Signature · 2 lines
Best Regards, Yanjun.Zhu
Quoted 24 lines · Damien Le Moal
Quoted 23 lines · Damien Le Moal
Quoted 22 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>--- drivers/block/null_blk/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/null_blk/main.c +0
-0@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; }
Quoted 6 lines
Quoted 4 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 765
Quoted 2 lines
drivers/block/null_blk/main.c +1
-1- dev->size = g_gb *1024 ; + dev->size = (unsigned long )g_gb *1024 ;This still does not prevent overflows... So what about doing a proper check ?
This still does not prevent overflows on 32-bits architectures.
Because "unsigned long" on 32-bits architectures is 32 bit, so solution 1 is to change the type "unsigned long" to u64, and the diff is as below:
drivers/block/null_blk/main.c +1 -1
@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; } - dev->size = g_gb *1024 ; + dev->size = (u64 )g_gb *1024 ; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
drivers/block/null_blk/null_blk.h +1 -1
@@ -74,7 +74,7 @@ struct nullb_device {bool need_zone_res_mgmt;spinlock_t zone_res_lock; -unsigned long size;/* device size in MB */ +u64 size;/* device size in MB */ unsigned long completion_nsec;/* time in ns to complete a
request */
unsigned long cache_size; /* disk cache size in MB */ unsigned long zone_size; /* zone size in MB if device is zoned */
I just built it and did not make tests.
Zhu Yanjun
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
drivers/block/null_blk/main.c +0
-0dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
Quoted 51 lines · Zhu Yanjun
Quoted 28 lines · Damien Le Moal
Quoted 15 lines · Damien Le Moal
Quoted 14 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>---
drivers/block/null_blk/main.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 11 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 764
Quoted 9 lines
drivers/block/null_blk/main.c +1
-1@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void) return NULL; } - dev->size = g_gb * 1024; + dev->size = (unsigned long)g_gb * 1024;This still does not prevent overflows... So what about doing a proper check ?
Quoted 1 line
This still does not prevent overflows on 32-bits architectures.
Because "unsigned long" on 32-bits architectures is 32 bit, so solution 1 is to change the type "unsigned long" to u64, and the diff is as below:
drivers/block/null_blk/main.c +1
-1@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void) return NULL; } - dev->size = g_gb * 1024; + dev->size = (u64)g_gb * 1024; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;drivers/block/null_blk/null_blk.h +0
-0
drivers/block/null_blk/null_blk.h struct nullb_device { · line 77
Quoted 5 lines
drivers/block/null_blk/null_blk.h +1
-1@@ -74,7 +74,7 @@ struct nullb_device { bool need_zone_res_mgmt; spinlock_t zone_res_lock; - unsigned long size; /* device size in MB */ + u64 size; /* device size in MB */
There is more, g_gb is GB, dev->size is MB, and dev->size will be used later for inode size in bytes, and bdev size in sectors.
The max inode size is LONG_MAX, this is still more than UINT_MAX GB, so it's right that set the device size by module params won't overflow.
However, take a look at setting the size through configfs, the max value is ULONG_MAX MB, this will still overflow.
Thanks,
Kuai
Quoted 20 lines
drivers/block/null_blk/null_blk.h +0
-0unsigned long completion_nsec; /* time in ns to complete arequest */
unsigned longcache_size; /* disk cache size in MB */ unsigned longzone_size; /* zone size in MB if device is zoned */I just built it and did not make tests.
Zhu Yanjun
Quoted 7 lines
Quoted 6 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 768
Quoted 5 lines
drivers/block/null_blk/main.c +0
-0dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;.
Quoted 63 lines · Yu Kuai
Quoted 51 lines · Zhu Yanjun
Quoted 28 lines · Damien Le Moal
Quoted 15 lines · Damien Le Moal
Quoted 14 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>---
drivers/block/null_blk/main.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 11 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 764
Quoted 9 lines
drivers/block/null_blk/main.c +1
-1@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void) return NULL; } - dev->size = g_gb * 1024; + dev->size = (unsigned long)g_gb * 1024;This still does not prevent overflows... So what about doing a proper check ?
Quoted 1 line
This still does not prevent overflows on 32-bits architectures.
Because "unsigned long" on 32-bits architectures is 32 bit, so solution 1 is to change the type "unsigned long" to u64, and the diff is as below:
drivers/block/null_blk/main.c +1
-1@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void) return NULL; } - dev->size = g_gb * 1024; + dev->size = (u64)g_gb * 1024; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;drivers/block/null_blk/null_blk.h +0
-0drivers/block/null_blk/null_blk.h struct nullb_device { · line 77
Quoted 5 lines
drivers/block/null_blk/null_blk.h +1
-1@@ -74,7 +74,7 @@ struct nullb_device { bool need_zone_res_mgmt; spinlock_t zone_res_lock; - unsigned long size; /* device size in MB */ + u64 size; /* device size in MB */There is more,
g_gbis GB,dev->sizeis MB, anddev->sizewill be used later for inode size in bytes, and bdev size in sectors.
Quoted 5 lines
The max inode size is
LONG_MAX, this is still more thanUINT_MAXGB, so it's right that set the device size by module params won't overflow.However, take a look at setting the size through configfs, the max value is
ULONG_MAXMB, this will still overflow.
Thanks a lot.
Add nullb_device_u64_attr_show and nullb_device_u64_attr_store functions.
So configfs should work well. Just a draft patch to try to fix this overflow problem.
I just built it and did not make tests.
drivers/block/null_blk/main.c +0 -0
@@ -289,6 +289,12 @@ static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
return snprintf(page, PAGE_SIZE, "%lu\n", val); }
+static inline ssize_t nullb_device_u64_attr_show(u64 val,
+ char *page)
+{
+ return snprintf(page, PAGE_SIZE, "%llu\n", val);
+}
+
static inline ssize_t nullb_device_bool_attr_show(bool val, char *page) {
return snprintf(page, PAGE_SIZE, "%u\n", val);
@@ -322,6 +328,20 @@ static ssize_t nullb_device_ulong_attr_store(unsigned long *val, return count;
}
+static ssize_t nullb_device_u64_attr_store(u64 *val,
+ const char *page, size_t count)
+{
+ int result;
+ u64 tmp;
+
+ result = kstrtou64(page, 0, &tmp);
+ if (result < 0)
+ return result;
+
+ *val = tmp;
+ return count;
+}
+
static ssize_t nullb_device_bool_attr_store(bool *val, const char *page, size_t count)
{
@@ -438,7 +458,7 @@ static int nullb_apply_poll_queues(struct nullb_device *dev, return ret;
}
-NULLB_DEVICE_ATTR(size, ulong, NULL); +NULLB_DEVICE_ATTR(size, u64, NULL); NULLB_DEVICE_ATTR(completion_nsec, ulong, NULL); NULLB_DEVICE_ATTR(submit_queues, uint, nullb_apply_submit_queues); NULLB_DEVICE_ATTR(poll_queues, uint, nullb_apply_poll_queues); @@ -762,7 +782,7 @@ static struct nullb_device *null_alloc_dev(void) return NULL;
}
- dev->size = g_gb * 1024;
+ dev->size = (u64)g_gb * 1024; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
drivers/block/null_blk/null_blk.h +0 -0
@@ -74,7 +74,7 @@ struct nullb_device {
bool need_zone_res_mgmt;
spinlock_t zone_res_lock;
- unsigned long size; /* device size in MB */
+ u64 size; /* device size in MB */
unsigned long completion_nsec; /* time in ns to complete a request */ unsigned long cache_size; /* disk cache size in MB */ unsigned long zone_size; /* zone size in MB if device is zoned */
Zhu Yanjun
Quoted 24 lines
Thanks,
KuaiQuoted 19 lines
unsigned long
completion_nsec; /* time in ns to complete a request */ unsigned longcache_size; /* disk cache size in MB */ unsigned longzone_size; /* zone size in MB if device is zoned */I just built it and did not make tests.
Zhu Yanjun
Quoted 7 lines
Quoted 6 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 767
Quoted 5 lines
drivers/block/null_blk/main.c +0
-0dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;.
Signature · 2 lines
Best Regards, Yanjun.Zhu
PATCH 1/1 null_blk: Use u64 to avoid overflow in null_alloc_dev()
The member variable size in struct nullb_device is the type unsigned long, and the module parameter g_gb is the type int. In 32 bit architecture, unsigned long has 32 bit. This introduces overflow risks.
Use the type u64 in struct nullb_device and configfs. This can avoid overflow risks.
--- drivers/block/null_blk/main.c | 23 +++++++++++++++++++++-- drivers/block/null_blk/null_blk.h | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-)
drivers/block/null_blk/main.c +21 -2
@@ -289,6 +289,11 @@ static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,return snprintf (page,PAGE_SIZE ,"%lu\n" , val); } +static inline ssize_t nullb_device_u64_attr_show (u64 val,char *page) +{ +return snprintf (page,PAGE_SIZE ,"%llu\n" , val); +} +static inline ssize_t nullb_device_bool_attr_show (bool val,char *page) {return snprintf (page,PAGE_SIZE ,"%u\n" , val);
@@ -322,6 +327,20 @@ static ssize_t nullb_device_ulong_attr_store(unsigned long *val,return count; } +static ssize_t nullb_device_u64_attr_store (u64 *val,const char *page, +size_t count) +{ +int result; +u64 tmp; + + result =kstrtou64 (page,0 , &tmp); +if (result <0 ) +return result; + + *val = tmp; +return count; +} +static ssize_t nullb_device_bool_attr_store (bool *val,const char *page,size_t count) {
@@ -438,7 +457,7 @@ static int nullb_apply_poll_queues(struct nullb_device *dev,return ret; } -NULLB_DEVICE_ATTR (size, ulong,NULL ); +NULLB_DEVICE_ATTR (size, u64,NULL );NULLB_DEVICE_ATTR (completion_nsec, ulong,NULL );NULLB_DEVICE_ATTR (submit_queues, uint, nullb_apply_submit_queues);NULLB_DEVICE_ATTR (poll_queues, uint, nullb_apply_poll_queues);
@@ -762,7 +781,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; } - dev->size = g_gb *1024 ; + dev->size = (u64 )g_gb *1024 ; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
drivers/block/null_blk/null_blk.h +1 -1
@@ -74,7 +74,7 @@ struct nullb_device {bool need_zone_res_mgmt;spinlock_t zone_res_lock; -unsigned long size;/* device size in MB */ +u64 size;/* device size in MB */ unsigned long completion_nsec;/* time in ns to complete a request */ unsigned long cache_size;/* disk cache size in MB */ unsigned long zone_size;/* zone size in MB if device is zoned */
Signature · 1 line
2.39.2
Re: [PATCH 1/1] null_blk: Use u64 to avoid overflow in null_alloc_dev()
Quoted 62 lines · Zhu Yanjun
The member variable size in struct
nullb_deviceis the type unsigned long, and the module parameterg_gbis the type int. In 32 bit architecture, unsigned long has 32 bit. This introduces overflow risks.Use the type u64 in struct
nullb_deviceand configfs. This can avoid overflow risks.Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Zhu Yanjun <[email protected]>--- drivers/block/null_blk/main.c | 23 +++++++++++++++++++++-- drivers/block/null_blk/null_blk.h | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-)drivers/block/null_blk/main.c +20
-1@@ -289,6 +289,11 @@ static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,return snprintf (page,PAGE_SIZE ,"%lu\n" , val); } +static inline ssize_t nullb_device_u64_attr_show (u64 val,char *page) +{ +return snprintf (page,PAGE_SIZE ,"%llu\n" , val); +} +static inline ssize_t nullb_device_bool_attr_show (bool val,char *page) {return snprintf (page,PAGE_SIZE ,"%u\n" , val);@@ -322,6 +327,20 @@ static ssize_t nullb_device_ulong_attr_store(unsigned long *val,return count; } +static ssize_t nullb_device_u64_attr_store (u64 *val,const char *page, +size_t count) +{ +int result; +u64 tmp; + + result =kstrtou64 (page,0 , &tmp); +if (result <0 ) +return result; + + *val = tmp; +return count; +} +static ssize_t nullb_device_bool_attr_store (bool *val,const char *page,size_t count) {@@ -438,7 +457,7 @@ static int nullb_apply_poll_queues(struct nullb_device *dev,return ret; } -NULLB_DEVICE_ATTR (size, ulong,NULL ); +NULLB_DEVICE_ATTR (size, u64,NULL );NULLB_DEVICE_ATTR (completion_nsec, ulong,NULL );NULLB_DEVICE_ATTR (submit_queues, uint, nullb_apply_submit_queues);NULLB_DEVICE_ATTR (poll_queues, uint, nullb_apply_poll_queues);
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 784
Quoted 5 lines
drivers/block/null_blk/main.c +1
-1@@ -762,7 +781,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; } - dev->size = g_gb *1024 ; + dev->size = (u64 )g_gb *1024 ;
As already commented on your previous version that was casting to an unsigned long, this is *not* avoiding an overflow. It is only changing the overflow value to a bigger one. So as suggested before, if you really want to fix this, fix it properly using check_mul_overflow().
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 1/1] null_blk: Use u64 to avoid overflow in null_alloc_dev()
Hi, Damien
Quoted 66 lines · Damien Le Moal
Quoted 65 lines · Zhu Yanjun
The member variable size in struct
nullb_deviceis the type unsigned long, and the module parameterg_gbis the type int. In 32 bit architecture, unsigned long has 32 bit. This introduces overflow risks.Use the type u64 in struct
nullb_deviceand configfs. This can avoid overflow risks.Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Zhu Yanjun <[email protected]>--- drivers/block/null_blk/main.c | 23 +++++++++++++++++++++-- drivers/block/null_blk/null_blk.h | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-)drivers/block/null_blk/main.c +20
-1@@ -289,6 +289,11 @@ static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,return snprintf (page,PAGE_SIZE ,"%lu\n" , val); } +static inline ssize_t nullb_device_u64_attr_show (u64 val,char *page) +{ +return snprintf (page,PAGE_SIZE ,"%llu\n" , val); +} +static inline ssize_t nullb_device_bool_attr_show (bool val,char *page) {return snprintf (page,PAGE_SIZE ,"%u\n" , val);@@ -322,6 +327,20 @@ static ssize_t nullb_device_ulong_attr_store(unsigned long *val,return count; } +static ssize_t nullb_device_u64_attr_store (u64 *val,const char *page, +size_t count) +{ +int result; +u64 tmp; + + result =kstrtou64 (page,0 , &tmp); +if (result <0 ) +return result; + + *val = tmp; +return count; +} +static ssize_t nullb_device_bool_attr_store (bool *val,const char *page,size_t count) {@@ -438,7 +457,7 @@ static int nullb_apply_poll_queues(struct nullb_device *dev,return ret; } -NULLB_DEVICE_ATTR (size, ulong,NULL ); +NULLB_DEVICE_ATTR (size, u64,NULL );NULLB_DEVICE_ATTR (completion_nsec, ulong,NULL );NULLB_DEVICE_ATTR (submit_queues, uint, nullb_apply_submit_queues);NULLB_DEVICE_ATTR (poll_queues, uint, nullb_apply_poll_queues);@@ -762,7 +781,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; }
Quoted 7 lines
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 784
Quoted 2 lines
drivers/block/null_blk/main.c +1
-1- dev->size = g_gb *1024 ; + dev->size = (u64 )g_gb *1024 ;As already commented on your previous version that was casting to an unsigned long, this is *not* avoiding an overflow. It is only changing the overflow value to a bigger one. So as suggested before, if you really want to fix this, fix it properly using
check_mul_overflow().
g_gb is 32-bit value in GB, hence the result won't overflow if size is changed to 64-bit value now.
And please also use a specific store function instead of nullb_device_u64_attr_store(), and using check_mul_overflow() to check overflow, because dev->size will used later for inode size, and 64-bit value in MB can overflow after switching to 64-bit value in sectors.
Thanks,
Kuai
Quoted 2 lines
Quoted 19 lines · Aleksandr Mishin
In
null_alloc_dev()device size is a subject to overflow because 'g_gb' (which is module parameter, may have any value and is not validated anywhere) is not cast to a larger data type before performing arithmetic.Cast '
g_gb' to unsigned long to prevent overflow.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 2984c8684f96 ("nullb: factor disk parameters")Signed-off-by Aleksandr Mishin <[email protected]>--- drivers/block/null_blk/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/null_blk/main.c +0
-0
drivers/block/null_blk/main.c static struct nullb_device *null_alloc_dev(void) · line 765
Quoted 4 lines
drivers/block/null_blk/main.c +0
-1@@ -762,7 +762,7 @@ static struct nullb_device *null_alloc_dev(void)return NULL ; } - dev->size = g_gb *1024 ;
The member variable size in struct nullb_device is type "unsigned long".
As such, changing g_gb from int to "unsigned long" seems reasonable.
So I am fine with this.
Thanks a lot.
Zhu Yanjun
Quoted 4 lines
drivers/block/null_blk/main.c +1
-0+ dev->size = (unsigned long )g_gb *1024 ; dev->completion_nsec = g_completion_nsec; dev->submit_queues = g_submit_queues; dev->prev_submit_queues = g_submit_queues;
Signature · 2 lines
Best Regards, Yanjun.Zhu