All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] random: fix syzkaller fuzzer test int overflow
@ 2017-10-24  7:44 Chen Feng
  2017-10-24  9:09 ` Greg KH
  0 siblings, 1 reply; 15+ messages in thread
From: Chen Feng @ 2017-10-24  7:44 UTC (permalink / raw)
  To: puck.chen, zhaoyukun, tytso, arnd, linux-kernel
  Cc: gregkh, suzhuangluan, dan.zhao

[pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
Restart is not permit
=================================================================
UBSAN: Undefined behaviour in
kernel/linux-4.4/drivers/char/random.c:676:19
signed integer overflow:
2147483645 + 268435455 cannot be represented in type 'int'
CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
TGID: 11928 Comm: syz-executor
Hardware name: hi3660 (DT)
Call trace:
[<ffffffa661090378>] dump_backtrace+0x0/0x314
[<ffffffa66109077c>] show_stack+0x1c/0x24
[<ffffffa66180cc54>] dump_stack+0xdc/0x130
[<ffffffa6618833d0>] ubsan_epilogue+0x18/0x6c
[<ffffffa661883c5c>] handle_overflow+0x180/0x1d4
[<ffffffa661883cdc>] __ubsan_handle_add_overflow+0x2c/0x34
[<ffffffa661ab75a0>] credit_entropy_bits+0x358/0x9a8
[<ffffffa661ab85bc>] random_ioctl+0x338/0x384
[<ffffffa661399c74>] do_vfs_ioctl+0x60c/0xa4c
[<ffffffa66139a150>] SyS_ioctl+0x9c/0xc0
[<ffffffa6610838b0>] el0_svc_naked+0x24/0x28
=================================================================

Signed-off-by: Chen Feng <puck.chen@hisilicon.com>
Signed-off-by: Yukun Zhao <zhaoyukun@huawei.com>
---
 drivers/char/random.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 1ef2640..6f2bd6a 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -699,6 +699,11 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
 	if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
 		goto retry;
 
+	if (INT_MAX - nbits < r->entropy_total) {
+		WARN_ON(1);
+		return;
+	}
+
 	r->entropy_total += nbits;
 	if (!r->initialized && r->entropy_total > 128) {
 		r->initialized = 1;
-- 
1.9.1

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-24  7:44 [PATCH RFC] random: fix syzkaller fuzzer test int overflow Chen Feng
@ 2017-10-24  9:09 ` Greg KH
  2017-10-24  9:24   ` Chen Feng
  2017-10-24 10:25   ` Theodore Ts'o
  0 siblings, 2 replies; 15+ messages in thread
From: Greg KH @ 2017-10-24  9:09 UTC (permalink / raw)
  To: Chen Feng; +Cc: zhaoyukun, tytso, arnd, linux-kernel, suzhuangluan, dan.zhao

On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
> [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
> Restart is not permit
> =================================================================
> UBSAN: Undefined behaviour in
> kernel/linux-4.4/drivers/char/random.c:676:19
> signed integer overflow:
> 2147483645 + 268435455 cannot be represented in type 'int'
> CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2

Does this also happen on 4.14-rc6?

> TGID: 11928 Comm: syz-executor
> Hardware name: hi3660 (DT)
> Call trace:
> [<ffffffa661090378>] dump_backtrace+0x0/0x314
> [<ffffffa66109077c>] show_stack+0x1c/0x24
> [<ffffffa66180cc54>] dump_stack+0xdc/0x130
> [<ffffffa6618833d0>] ubsan_epilogue+0x18/0x6c
> [<ffffffa661883c5c>] handle_overflow+0x180/0x1d4
> [<ffffffa661883cdc>] __ubsan_handle_add_overflow+0x2c/0x34
> [<ffffffa661ab75a0>] credit_entropy_bits+0x358/0x9a8
> [<ffffffa661ab85bc>] random_ioctl+0x338/0x384
> [<ffffffa661399c74>] do_vfs_ioctl+0x60c/0xa4c
> [<ffffffa66139a150>] SyS_ioctl+0x9c/0xc0
> [<ffffffa6610838b0>] el0_svc_naked+0x24/0x28
> =================================================================
> 
> Signed-off-by: Chen Feng <puck.chen@hisilicon.com>
> Signed-off-by: Yukun Zhao <zhaoyukun@huawei.com>
> ---
>  drivers/char/random.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index 1ef2640..6f2bd6a 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -699,6 +699,11 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
>  	if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
>  		goto retry;
>  
> +	if (INT_MAX - nbits < r->entropy_total) {
> +		WARN_ON(1);

Why WARN_ON()?  What is that going to help with?

thanks,

greg k-h

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-24  9:09 ` Greg KH
@ 2017-10-24  9:24   ` Chen Feng
  2017-10-24  9:57     ` Greg KH
  2017-10-24 10:25   ` Theodore Ts'o
  1 sibling, 1 reply; 15+ messages in thread
From: Chen Feng @ 2017-10-24  9:24 UTC (permalink / raw)
  To: Greg KH; +Cc: zhaoyukun, tytso, arnd, linux-kernel, suzhuangluan, dan.zhao



On 2017/10/24 17:09, Greg KH wrote:
> On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
>> [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
>> Restart is not permit
>> =================================================================
>> UBSAN: Undefined behaviour in
>> kernel/linux-4.4/drivers/char/random.c:676:19
>> signed integer overflow:
>> 2147483645 + 268435455 cannot be represented in type 'int'
>> CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
> 
> Does this also happen on 4.14-rc6?

No, mainline also has this issue.
> 
>> TGID: 11928 Comm: syz-executor
>> Hardware name: hi3660 (DT)
>> Call trace:
>> [<ffffffa661090378>] dump_backtrace+0x0/0x314
>> [<ffffffa66109077c>] show_stack+0x1c/0x24
>> [<ffffffa66180cc54>] dump_stack+0xdc/0x130
>> [<ffffffa6618833d0>] ubsan_epilogue+0x18/0x6c
>> [<ffffffa661883c5c>] handle_overflow+0x180/0x1d4
>> [<ffffffa661883cdc>] __ubsan_handle_add_overflow+0x2c/0x34
>> [<ffffffa661ab75a0>] credit_entropy_bits+0x358/0x9a8
>> [<ffffffa661ab85bc>] random_ioctl+0x338/0x384
>> [<ffffffa661399c74>] do_vfs_ioctl+0x60c/0xa4c
>> [<ffffffa66139a150>] SyS_ioctl+0x9c/0xc0
>> [<ffffffa6610838b0>] el0_svc_naked+0x24/0x28
>> =================================================================
>>
>> Signed-off-by: Chen Feng <puck.chen@hisilicon.com>
>> Signed-off-by: Yukun Zhao <zhaoyukun@huawei.com>
>> ---
>>  drivers/char/random.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/drivers/char/random.c b/drivers/char/random.c
>> index 1ef2640..6f2bd6a 100644
>> --- a/drivers/char/random.c
>> +++ b/drivers/char/random.c
>> @@ -699,6 +699,11 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
>>  	if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
>>  		goto retry;
>>  
>> +	if (INT_MAX - nbits < r->entropy_total) {
>> +		WARN_ON(1);
> 
> Why WARN_ON()?  What is that going to help with?
Actually, I am not familiar with the random module....

This patch is RFC to see if some one has better idea.

-feng

> 
> thanks,
> 
> greg k-h
> 
> .
> 

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-24  9:24   ` Chen Feng
@ 2017-10-24  9:57     ` Greg KH
  0 siblings, 0 replies; 15+ messages in thread
From: Greg KH @ 2017-10-24  9:57 UTC (permalink / raw)
  To: Chen Feng; +Cc: zhaoyukun, tytso, arnd, linux-kernel, suzhuangluan, dan.zhao

On Tue, Oct 24, 2017 at 05:24:01PM +0800, Chen Feng wrote:
> 
> 
> On 2017/10/24 17:09, Greg KH wrote:
> > On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
> >> [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
> >> Restart is not permit
> >> =================================================================
> >> UBSAN: Undefined behaviour in
> >> kernel/linux-4.4/drivers/char/random.c:676:19
> >> signed integer overflow:
> >> 2147483645 + 268435455 cannot be represented in type 'int'
> >> CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
> > 
> > Does this also happen on 4.14-rc6?
> 
> No, mainline also has this issue.
> > 
> >> TGID: 11928 Comm: syz-executor
> >> Hardware name: hi3660 (DT)
> >> Call trace:
> >> [<ffffffa661090378>] dump_backtrace+0x0/0x314
> >> [<ffffffa66109077c>] show_stack+0x1c/0x24
> >> [<ffffffa66180cc54>] dump_stack+0xdc/0x130
> >> [<ffffffa6618833d0>] ubsan_epilogue+0x18/0x6c
> >> [<ffffffa661883c5c>] handle_overflow+0x180/0x1d4
> >> [<ffffffa661883cdc>] __ubsan_handle_add_overflow+0x2c/0x34
> >> [<ffffffa661ab75a0>] credit_entropy_bits+0x358/0x9a8
> >> [<ffffffa661ab85bc>] random_ioctl+0x338/0x384
> >> [<ffffffa661399c74>] do_vfs_ioctl+0x60c/0xa4c
> >> [<ffffffa66139a150>] SyS_ioctl+0x9c/0xc0
> >> [<ffffffa6610838b0>] el0_svc_naked+0x24/0x28
> >> =================================================================
> >>
> >> Signed-off-by: Chen Feng <puck.chen@hisilicon.com>
> >> Signed-off-by: Yukun Zhao <zhaoyukun@huawei.com>
> >> ---
> >>  drivers/char/random.c | 5 +++++
> >>  1 file changed, 5 insertions(+)
> >>
> >> diff --git a/drivers/char/random.c b/drivers/char/random.c
> >> index 1ef2640..6f2bd6a 100644
> >> --- a/drivers/char/random.c
> >> +++ b/drivers/char/random.c
> >> @@ -699,6 +699,11 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
> >>  	if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
> >>  		goto retry;
> >>  
> >> +	if (INT_MAX - nbits < r->entropy_total) {
> >> +		WARN_ON(1);
> > 
> > Why WARN_ON()?  What is that going to help with?
> Actually, I am not familiar with the random module....
> 
> This patch is RFC to see if some one has better idea.

Well, not spamming the kernel log for something that userspace can
trigger is a good start to modifying your patch :)

thanks,

greg k-h

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-24  9:09 ` Greg KH
  2017-10-24  9:24   ` Chen Feng
@ 2017-10-24 10:25   ` Theodore Ts'o
  2017-10-25  6:30     ` Chen Feng
  1 sibling, 1 reply; 15+ messages in thread
From: Theodore Ts'o @ 2017-10-24 10:25 UTC (permalink / raw)
  To: Greg KH; +Cc: Chen Feng, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao

On Tue, Oct 24, 2017 at 11:09:27AM +0200, Greg KH wrote:
> On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
> > [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
> > Restart is not permit
> > =================================================================
> > UBSAN: Undefined behaviour in
> > kernel/linux-4.4/drivers/char/random.c:676:19
> > signed integer overflow:
> > 2147483645 + 268435455 cannot be represented in type 'int'
> > CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
> 
> Does this also happen on 4.14-rc6?

No.  It was fixed in 4.8, by commit 86a574de4590: "random: strengthen
input validation for RNDADDTOENTCNT".

						- Ted

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-24 10:25   ` Theodore Ts'o
@ 2017-10-25  6:30     ` Chen Feng
  2017-10-25  6:56       ` Greg KH
  0 siblings, 1 reply; 15+ messages in thread
From: Chen Feng @ 2017-10-25  6:30 UTC (permalink / raw)
  To: Theodore Ts'o, Greg KH, zhaoyukun, arnd, linux-kernel,
	suzhuangluan, dan.zhao

Hi Ted,

On 2017/10/24 18:25, Theodore Ts'o wrote:
> On Tue, Oct 24, 2017 at 11:09:27AM +0200, Greg KH wrote:
>> On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
>>> [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
>>> Restart is not permit
>>> =================================================================
>>> UBSAN: Undefined behaviour in
>>> kernel/linux-4.4/drivers/char/random.c:676:19
>>> signed integer overflow:
>>> 2147483645 + 268435455 cannot be represented in type 'int'
>>> CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
>>
>> Does this also happen on 4.14-rc6?
> 
> No.  It was fixed in 4.8, by commit 86a574de4590: "random: strengthen
> input validation for RNDADDTOENTCNT".


I see my kernel has already merged this patch. So I don't think this patch
can resolve the issue above.

	-feng
> 
> 						- Ted
> 
> .
> 

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-25  6:30     ` Chen Feng
@ 2017-10-25  6:56       ` Greg KH
  2017-10-25  7:08         ` Chen Feng
  0 siblings, 1 reply; 15+ messages in thread
From: Greg KH @ 2017-10-25  6:56 UTC (permalink / raw)
  To: Chen Feng
  Cc: Theodore Ts'o, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao

On Wed, Oct 25, 2017 at 02:30:56PM +0800, Chen Feng wrote:
> Hi Ted,
> 
> On 2017/10/24 18:25, Theodore Ts'o wrote:
> > On Tue, Oct 24, 2017 at 11:09:27AM +0200, Greg KH wrote:
> >> On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
> >>> [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
> >>> Restart is not permit
> >>> =================================================================
> >>> UBSAN: Undefined behaviour in
> >>> kernel/linux-4.4/drivers/char/random.c:676:19
> >>> signed integer overflow:
> >>> 2147483645 + 268435455 cannot be represented in type 'int'
> >>> CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
> >>
> >> Does this also happen on 4.14-rc6?
> > 
> > No.  It was fixed in 4.8, by commit 86a574de4590: "random: strengthen
> > input validation for RNDADDTOENTCNT".
> 
> 
> I see my kernel has already merged this patch. So I don't think this patch
> can resolve the issue above.

Do you have a reproducer for this issue that we can use to test?

thanks,

greg k-h

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-25  6:56       ` Greg KH
@ 2017-10-25  7:08         ` Chen Feng
  2017-10-25  8:49           ` Theodore Ts'o
  0 siblings, 1 reply; 15+ messages in thread
From: Chen Feng @ 2017-10-25  7:08 UTC (permalink / raw)
  To: Greg KH
  Cc: Theodore Ts'o, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao



On 2017/10/25 14:56, Greg KH wrote:
> On Wed, Oct 25, 2017 at 02:30:56PM +0800, Chen Feng wrote:
>> Hi Ted,
>>
>> On 2017/10/24 18:25, Theodore Ts'o wrote:
>>> On Tue, Oct 24, 2017 at 11:09:27AM +0200, Greg KH wrote:
>>>> On Tue, Oct 24, 2017 at 03:44:17PM +0800, Chen Feng wrote:
>>>>> [pid:11940,cpu6,syz-executor][flp_ioctl]cmd[0x1]
>>>>> Restart is not permit
>>>>> =================================================================
>>>>> UBSAN: Undefined behaviour in
>>>>> kernel/linux-4.4/drivers/char/random.c:676:19
>>>>> signed integer overflow:
>>>>> 2147483645 + 268435455 cannot be represented in type 'int'
>>>>> CPU: 4 PID: 11941 Comm: syz-executor Not tainted 4.4.76+ #2
>>>>
>>>> Does this also happen on 4.14-rc6?
>>>
>>> No.  It was fixed in 4.8, by commit 86a574de4590: "random: strengthen
>>> input validation for RNDADDTOENTCNT".
>>
>>
>> I see my kernel has already merged this patch. So I don't think this patch
>> can resolve the issue above.
> 
> Do you have a reproducer for this issue that we can use to test?
> 

It's hard to reproduce, we found this on stress test with syzkaller test.

r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
syz_open_dev$random(&(0x7f0000005000-0xc)="2f6465762f72616e646f6d00", 0x0, 0x10100)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
UBSAN: Undefined behaviour in kernel/linux-4.4/drivers/char/random.c:676:19
[<ffffffa661ab85bc>] random_ioctl+0x338/0x384


git log --oneline drivers/char/random.c

3991576 random: properly align get_random_int_hash
5f87c47 Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
f48dd2d random: add interrupt callback to VMBus IRQ handler
529025b random: print a warning for the first ten uninitialized random users
f41fc0b random: initialize the non-blocking pool via add_hwgenerator_randomness()
fdd8543 Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
93f84c8 random: strengthen input validation for RNDADDTOENTCNT
06bfe14 FROMLIST: drivers: char: random: add get_random_long()
c271950 random: Remove kernel blocking API
205a525 random: Add callback API for random pool readiness
16b369a random: Blocking API for accessing nonblocking_pool
1d9de44 random: Wake up all getrandom(2) callers when pool is ready
19acc77 random: Fix fast_mix() function


> thanks,
> 
> greg k-h
> 
> .
> 

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-25  7:08         ` Chen Feng
@ 2017-10-25  8:49           ` Theodore Ts'o
  2017-10-26  8:25             ` Chen Feng
  0 siblings, 1 reply; 15+ messages in thread
From: Theodore Ts'o @ 2017-10-25  8:49 UTC (permalink / raw)
  To: Chen Feng; +Cc: Greg KH, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao

Other people who have sent me fuzzer test reproducers are able to
reproduce syzkaller logs into a simple C program.  Can you explain to
me what the heck:

> r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)

means?

					- Ted

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-25  8:49           ` Theodore Ts'o
@ 2017-10-26  8:25             ` Chen Feng
  2017-10-26 15:04               ` Theodore Ts'o
  0 siblings, 1 reply; 15+ messages in thread
From: Chen Feng @ 2017-10-26  8:25 UTC (permalink / raw)
  To: Theodore Ts'o, Greg KH, zhaoyukun, arnd, linux-kernel,
	suzhuangluan, dan.zhao



On 2017/10/25 16:49, Theodore Ts'o wrote:
> Other people who have sent me fuzzer test reproducers are able to
> reproduce syzkaller logs into a simple C program.  Can you explain to
> me what the heck:
> 
>> r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
> 
> means?

Take a look at this:

https://github.com/google/syzkaller/blob/master/sys/linux/random.txt

> 
> 					- Ted
> 
> 

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-26  8:25             ` Chen Feng
@ 2017-10-26 15:04               ` Theodore Ts'o
  2017-10-28  3:22                 ` Chen Feng
  0 siblings, 1 reply; 15+ messages in thread
From: Theodore Ts'o @ 2017-10-26 15:04 UTC (permalink / raw)
  To: Chen Feng; +Cc: Greg KH, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao

On Thu, Oct 26, 2017 at 04:25:15PM +0800, Chen Feng wrote:
> 
> 
> On 2017/10/25 16:49, Theodore Ts'o wrote:
> > Other people who have sent me fuzzer test reproducers are able to
> > reproduce syzkaller logs into a simple C program.  Can you explain to
> > me what the heck:
> > 
> >> r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
> > 
> > means?
> 
> Take a look at this:
> 
> https://github.com/google/syzkaller/blob/master/sys/linux/random.txt

Sorry, this *still* looks like gobbledygook.

What ioctls are you executing, and with what arguments?

*Please*, give me a C program I can compile.

					 -Ted

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-26 15:04               ` Theodore Ts'o
@ 2017-10-28  3:22                 ` Chen Feng
  2017-10-29 18:25                   ` Theodore Ts'o
  0 siblings, 1 reply; 15+ messages in thread
From: Chen Feng @ 2017-10-28  3:22 UTC (permalink / raw)
  To: Theodore Ts'o, Greg KH, zhaoyukun, arnd, linux-kernel,
	suzhuangluan, dan.zhao

Hi ted,

On 2017/10/26 23:04, Theodore Ts'o wrote:
> On Thu, Oct 26, 2017 at 04:25:15PM +0800, Chen Feng wrote:
>>
>>
>> On 2017/10/25 16:49, Theodore Ts'o wrote:
>>> Other people who have sent me fuzzer test reproducers are able to
>>> reproduce syzkaller logs into a simple C program.  Can you explain to
>>> me what the heck:
>>>
>>>> r3 = syz_open_dev$urandom(&(0x7f000000a000)="2f6465762f7572616e646f6d00", 0x0, 0x0)
>>>
>>> means?
>>
>> Take a look at this:
>>
>> https://github.com/google/syzkaller/blob/master/sys/linux/random.txt
> 
> Sorry, this *still* looks like gobbledygook.
> 
> What ioctls are you executing, and with what arguments?
> 
> *Please*, give me a C program I can compile.

I checked the ioctl. What's the purpose of RNDADDTOENTCNT ioctl to userspace?

We need to checked the user-input at credit_entropy_bits_safe.

+	if (INT_MAX - nbits < r->entropy_total)
+		return -EINVAL;
+


The test-code below:

void *random_ioctl_test(void *args)
{
	int fd = -1;
	int ret = -1;
	int test_arg = 0x7fffffff;

	fd = open("dev/urandom", 0x0, 0x0);
	if (fd < 0) {
		printf("open dev/urandom failed!\n");
		return NULL;
	}

	ret = ioctl(fd, 0x40045201, &test_arg);

	printf("random_ioctl ret=%d\n", ret);
	close(fd);
	return NULL;
}

int main(int argc, char *argv[])
{
	int ret, i;
	pthread_t thread[100];

	for (i = 0; i < 100; i++) {
		ret = pthread_create(&thread[i], NULL, random_ioctl_test, &i);
		if (ret) {
			printf("create thread %d fail with ret=%d\n", i, ret);
			return -1;
		}
	}

	for (i = 0; i < 100; i++) {
		pthread_join(thread[i], NULL);
	}
	return 0;
}


> 
> 					 -Ted
> 
> .
> 

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-28  3:22                 ` Chen Feng
@ 2017-10-29 18:25                   ` Theodore Ts'o
  2017-10-30  7:39                     ` Greg KH
  0 siblings, 1 reply; 15+ messages in thread
From: Theodore Ts'o @ 2017-10-29 18:25 UTC (permalink / raw)
  To: Chen Feng; +Cc: Greg KH, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao

On Sat, Oct 28, 2017 at 11:22:00AM +0800, Chen Feng wrote:
> 
> I checked the ioctl. What's the purpose of RNDADDTOENTCNT ioctl to
> userspace?

It's a legacy ioctl which is probably not used anywhere; it's been
replaced by RNDADDENTROPY.  It previously allows root to bump the
entropy estimate, but the right way to do this by rngd is to
atomically add entropy to the pool land and bump the entropy estimate
at the same time.

The UBSAN is harmless.  The ioctl requires root, and the entropy_total
field, which is involved in the UBSAN, is only used in the first few
seconds of boot, to determine when the entropy pool has been
initialized.  In general on desktop and servers this happens before
userspace has a chance to run.

In any case, here's a fix for this.

					- Ted

commit 6f7034d0c52e21f30002b95126b6b98e4618dc57
Author: Theodore Ts'o <tytso@mit.edu>
Date:   Sun Oct 29 14:17:26 2017 -0400

    random: use a tighter cap in credit_entropy_bits_safe()
    
    This fixes a harmless UBSAN where root could potentially end up
    causing an overflow while bumping the entropy_total field (which is
    ignored once the entropy pool has been initialized, and this generally
    is completed during the boot sequence).
    
    This is marginal for the stable kernel series, but it's a really
    trivial patch, and it UBSAN warning that might cause security folks to
    get overly excited for no reason.
    
    Signed-off-by: Theodore Ts'o <tytso@mit.edu>
    Cc: stable@vger.kernel.org

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 8ad92707e45f..ae8a2f829890 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -733,7 +733,7 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
 
 static int credit_entropy_bits_safe(struct entropy_store *r, int nbits)
 {
-	const int nbits_max = (int)(~0U >> (ENTROPY_SHIFT + 1));
+	const int nbits_max = r->poolinfo->poolwords * 32;
 
 	if (nbits < 0)
 		return -EINVAL;

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-29 18:25                   ` Theodore Ts'o
@ 2017-10-30  7:39                     ` Greg KH
  2017-10-30  9:11                       ` Theodore Ts'o
  0 siblings, 1 reply; 15+ messages in thread
From: Greg KH @ 2017-10-30  7:39 UTC (permalink / raw)
  To: Theodore Ts'o, Chen Feng, zhaoyukun, arnd, linux-kernel,
	suzhuangluan, dan.zhao

On Sun, Oct 29, 2017 at 02:25:29PM -0400, Theodore Ts'o wrote:
> On Sat, Oct 28, 2017 at 11:22:00AM +0800, Chen Feng wrote:
> > 
> > I checked the ioctl. What's the purpose of RNDADDTOENTCNT ioctl to
> > userspace?
> 
> It's a legacy ioctl which is probably not used anywhere; it's been
> replaced by RNDADDENTROPY.  It previously allows root to bump the
> entropy estimate, but the right way to do this by rngd is to
> atomically add entropy to the pool land and bump the entropy estimate
> at the same time.
> 
> The UBSAN is harmless.  The ioctl requires root, and the entropy_total
> field, which is involved in the UBSAN, is only used in the first few
> seconds of boot, to determine when the entropy pool has been
> initialized.  In general on desktop and servers this happens before
> userspace has a chance to run.
> 
> In any case, here's a fix for this.
> 
> 					- Ted
> 
> commit 6f7034d0c52e21f30002b95126b6b98e4618dc57
> Author: Theodore Ts'o <tytso@mit.edu>
> Date:   Sun Oct 29 14:17:26 2017 -0400
> 
>     random: use a tighter cap in credit_entropy_bits_safe()
>     
>     This fixes a harmless UBSAN where root could potentially end up
>     causing an overflow while bumping the entropy_total field (which is
>     ignored once the entropy pool has been initialized, and this generally
>     is completed during the boot sequence).
>     
>     This is marginal for the stable kernel series, but it's a really
>     trivial patch, and it UBSAN warning that might cause security folks to
>     get overly excited for no reason.
>     
>     Signed-off-by: Theodore Ts'o <tytso@mit.edu>
>     Cc: stable@vger.kernel.org

No "Reported-by:"?

thanks,

greg k-h

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

* Re: [PATCH RFC] random: fix syzkaller fuzzer test int overflow
  2017-10-30  7:39                     ` Greg KH
@ 2017-10-30  9:11                       ` Theodore Ts'o
  0 siblings, 0 replies; 15+ messages in thread
From: Theodore Ts'o @ 2017-10-30  9:11 UTC (permalink / raw)
  To: Greg KH; +Cc: Chen Feng, zhaoyukun, arnd, linux-kernel, suzhuangluan, dan.zhao

On Mon, Oct 30, 2017 at 08:39:56AM +0100, Greg KH wrote:
> 
> No "Reported-by:"?

Good point, fixed in my tree.

				- Ted

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

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

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-24  7:44 [PATCH RFC] random: fix syzkaller fuzzer test int overflow Chen Feng
2017-10-24  9:09 ` Greg KH
2017-10-24  9:24   ` Chen Feng
2017-10-24  9:57     ` Greg KH
2017-10-24 10:25   ` Theodore Ts'o
2017-10-25  6:30     ` Chen Feng
2017-10-25  6:56       ` Greg KH
2017-10-25  7:08         ` Chen Feng
2017-10-25  8:49           ` Theodore Ts'o
2017-10-26  8:25             ` Chen Feng
2017-10-26 15:04               ` Theodore Ts'o
2017-10-28  3:22                 ` Chen Feng
2017-10-29 18:25                   ` Theodore Ts'o
2017-10-30  7:39                     ` Greg KH
2017-10-30  9:11                       ` Theodore Ts'o

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.