* [PATCH] tomoyo: Reject excessively long lines
2024-12-15 21:48 [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
@ 2024-12-16 2:14 ` Leo Stone
2024-12-16 7:19 ` [syzbot] Re: [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Leo Stone @ 2024-12-16 2:14 UTC (permalink / raw)
To: syzbot+7536f77535e5210a5c76
Cc: Leo Stone, jmorris, linux-kernel, linux-security-module, paul,
penguin-kernel, serge, syzkaller-bugs, takedakn, tomoyo-dev-en
syzbot creates an anonymous memory region, and then issues a
write syscall from the new memory region to a sysfs entry controlled by
tomoyo, specifying a buffer size of just under 2 GB (the actual size of
the buffer is ~32 MB). Because tomoyo_write_control will double the
size of head->write_buf every time it runs out of space for the current
line, and everything in the zero-initialized buffer is on the same line,
the function will eventually issue a kzalloc with a size that is too large,
triggering the warning.
Reject writes with excessively long lines.
Reported-by: syzbot+7536f77535e5210a5c76@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7536f77535e5210a5c76
Signed-off-by: Leo Stone <leocstone@gmail.com>
---
security/tomoyo/common.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
index 5c7b059a332a..0c75be949c9d 100644
--- a/security/tomoyo/common.c
+++ b/security/tomoyo/common.c
@@ -2665,6 +2665,10 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
if (head->w.avail >= head->writebuf_size - 1) {
const int len = head->writebuf_size * 2;
+ if (len > KMALLOC_MAX_SIZE) {
+ error = -EINVAL;
+ break;
+ }
char *cp = kzalloc(len, GFP_NOFS);
if (!cp) {
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [syzbot] Re: [syzbot] [tomoyo?] WARNING in tomoyo_write_control
2024-12-15 21:48 [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
2024-12-16 2:14 ` [PATCH] tomoyo: Reject excessively long lines Leo Stone
@ 2024-12-16 7:19 ` syzbot
2024-12-16 7:20 ` syzbot
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: syzbot @ 2024-12-16 7:19 UTC (permalink / raw)
To: linux-kernel
For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org.
***
Subject: Re: [syzbot] [tomoyo?] WARNING in tomoyo_write_control
Author: lizhi.xu@windriver.com
User input a too large avail length 0xfffffdeful.
#syz test
diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
index 5c7b059a332a..d25752eb8790 100644
--- a/security/tomoyo/common.c
+++ b/security/tomoyo/common.c
@@ -2656,6 +2656,9 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
return -EINVAL;
if (mutex_lock_interruptible(&head->io_sem))
return -EINTR;
+ if (avail_len << PAGE_SHIFT > MAX_PAGE_ORDER)
+ return -EINVAL;
+
cp0 = head->write_buf;
head->read_user_buf_avail = 0;
idx = tomoyo_read_lock();
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [syzbot] Re: [syzbot] [tomoyo?] WARNING in tomoyo_write_control
2024-12-15 21:48 [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
2024-12-16 2:14 ` [PATCH] tomoyo: Reject excessively long lines Leo Stone
2024-12-16 7:19 ` [syzbot] Re: [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
@ 2024-12-16 7:20 ` syzbot
2024-12-16 8:19 ` [PATCH] tomoyo: prevent bad buffer size in tracing_cpumask_write Lizhi Xu
2024-12-16 10:45 ` [PATCH] tomoyo: don't emit warning in tomoyo_write_control() Tetsuo Handa
4 siblings, 0 replies; 7+ messages in thread
From: syzbot @ 2024-12-16 7:20 UTC (permalink / raw)
To: linux-kernel
For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org.
***
Subject: Re: [syzbot] [tomoyo?] WARNING in tomoyo_write_control
Author: lizhi.xu@windriver.com
User input a too large avail length 0xfffffdeful.
#syz test
diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
index 5c7b059a332a..65458b4059ab 100644
--- a/security/tomoyo/common.c
+++ b/security/tomoyo/common.c
@@ -2654,6 +2654,8 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
if (!head->write)
return -EINVAL;
+ if (avail_len << PAGE_SHIFT > MAX_PAGE_ORDER)
+ return -EINVAL;
if (mutex_lock_interruptible(&head->io_sem))
return -EINTR;
cp0 = head->write_buf;
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH] tomoyo: prevent bad buffer size in tracing_cpumask_write
2024-12-15 21:48 [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
` (2 preceding siblings ...)
2024-12-16 7:20 ` syzbot
@ 2024-12-16 8:19 ` Lizhi Xu
2024-12-16 10:06 ` Tetsuo Handa
2024-12-16 10:45 ` [PATCH] tomoyo: don't emit warning in tomoyo_write_control() Tetsuo Handa
4 siblings, 1 reply; 7+ messages in thread
From: Lizhi Xu @ 2024-12-16 8:19 UTC (permalink / raw)
To: syzbot+7536f77535e5210a5c76
Cc: jmorris, linux-kernel, linux-security-module, paul,
penguin-kernel, serge, syzkaller-bugs, takedakn, tomoyo-dev-en
User input a too large buffer size 0xfffffdeful, although it is truncated to
MAX_RW_COUNT in vfs_write, its value is still too large, causing warning when
allocating memory in tomoyo_write_control.
Add a check for it to avoid this case.
Reported-by: syzbot+7536f77535e5210a5c76@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7536f77535e5210a5c76
Tested-by: syzbot+7536f77535e5210a5c76@syzkaller.appspotmail.com
Signed-off-by: Lizhi Xu <lizhi.xu@windriver.com>
---
security/tomoyo/common.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
index 5c7b059a332a..f63388c2fffd 100644
--- a/security/tomoyo/common.c
+++ b/security/tomoyo/common.c
@@ -2654,6 +2654,8 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
if (!head->write)
return -EINVAL;
+ if (avail_len > KMALLOC_MAX_SIZE)
+ return -EINVAL;
if (mutex_lock_interruptible(&head->io_sem))
return -EINTR;
cp0 = head->write_buf;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH] tomoyo: prevent bad buffer size in tracing_cpumask_write
2024-12-16 8:19 ` [PATCH] tomoyo: prevent bad buffer size in tracing_cpumask_write Lizhi Xu
@ 2024-12-16 10:06 ` Tetsuo Handa
0 siblings, 0 replies; 7+ messages in thread
From: Tetsuo Handa @ 2024-12-16 10:06 UTC (permalink / raw)
To: Lizhi Xu, syzbot+7536f77535e5210a5c76
Cc: jmorris, linux-kernel, linux-security-module, paul, serge,
syzkaller-bugs, takedakn
On 2024/12/16 17:19, Lizhi Xu wrote:
> User input a too large buffer size 0xfffffdeful, although it is truncated to
> MAX_RW_COUNT in vfs_write, its value is still too large, causing warning when
> allocating memory in tomoyo_write_control.
>
> Add a check for it to avoid this case.
Thank you for a patch. But I don't think this fix is correct, for one can make
head->writebuf_size too large by writing chunks without new line for many times.
>
> Reported-by: syzbot+7536f77535e5210a5c76@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=7536f77535e5210a5c76
> Tested-by: syzbot+7536f77535e5210a5c76@syzkaller.appspotmail.com
> Signed-off-by: Lizhi Xu <lizhi.xu@windriver.com>
> ---
> security/tomoyo/common.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
> index 5c7b059a332a..f63388c2fffd 100644
> --- a/security/tomoyo/common.c
> +++ b/security/tomoyo/common.c
> @@ -2654,6 +2654,8 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
>
> if (!head->write)
> return -EINVAL;
> + if (avail_len > KMALLOC_MAX_SIZE)
> + return -EINVAL;
> if (mutex_lock_interruptible(&head->io_sem))
> return -EINTR;
> cp0 = head->write_buf;
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] tomoyo: don't emit warning in tomoyo_write_control()
2024-12-15 21:48 [syzbot] [tomoyo?] WARNING in tomoyo_write_control syzbot
` (3 preceding siblings ...)
2024-12-16 8:19 ` [PATCH] tomoyo: prevent bad buffer size in tracing_cpumask_write Lizhi Xu
@ 2024-12-16 10:45 ` Tetsuo Handa
4 siblings, 0 replies; 7+ messages in thread
From: Tetsuo Handa @ 2024-12-16 10:45 UTC (permalink / raw)
To: syzbot, jmorris, linux-kernel, linux-security-module, paul, serge,
syzkaller-bugs, takedakn, Leo Stone
syzbot is reporting too large allocation warning at tomoyo_write_control(),
for one can write a very very long line without new line character. To fix
this warning, I use __GFP_NOWARN rather than checking for KMALLOC_MAX_SIZE,
for practically a valid line should be always shorter than 32KB where the
"too small to fail" memory-allocation rule applies.
One might try to write a valid line that is longer than 32KB, but such
request will likely fail with -ENOMEM. Therefore, I feel that separately
returning -EINVAL when a line is longer than KMALLOC_MAX_SIZE is redundant.
There is no need to distinguish over-32KB and over-KMALLOC_MAX_SIZE.
Reported-by: syzbot+7536f77535e5210a5c76@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7536f77535e5210a5c76
Reported-by: Leo Stone <leocstone@gmail.com>
Closes: https://lkml.kernel.org/r/20241216021459.178759-2-leocstone@gmail.com
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
security/tomoyo/common.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
index 5c7b059a332a..972664962e8f 100644
--- a/security/tomoyo/common.c
+++ b/security/tomoyo/common.c
@@ -2665,7 +2665,7 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
if (head->w.avail >= head->writebuf_size - 1) {
const int len = head->writebuf_size * 2;
- char *cp = kzalloc(len, GFP_NOFS);
+ char *cp = kzalloc(len, GFP_NOFS | __GFP_NOWARN);
if (!cp) {
error = -ENOMEM;
--
2.43.5
^ permalink raw reply related [flat|nested] 7+ messages in thread