2021-02-04 00:33:54

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH] net/qrtr: restrict user-controlled length in qrtr_tun_write_iter()

On Tue, 2 Feb 2021 15:20:59 +0600 Sabyrzhan Tasbolatov wrote:
> syzbot found WARNING in qrtr_tun_write_iter [1] when write_iter length
> exceeds KMALLOC_MAX_SIZE causing order >= MAX_ORDER condition.
>
> Additionally, there is no check for 0 length write.
>
> [1]
> WARNING: mm/page_alloc.c:5011
> [..]
> Call Trace:
> alloc_pages_current+0x18c/0x2a0 mm/mempolicy.c:2267
> alloc_pages include/linux/gfp.h:547 [inline]
> kmalloc_order+0x2e/0xb0 mm/slab_common.c:837
> kmalloc_order_trace+0x14/0x120 mm/slab_common.c:853
> kmalloc include/linux/slab.h:557 [inline]
> kzalloc include/linux/slab.h:682 [inline]
> qrtr_tun_write_iter+0x8a/0x180 net/qrtr/tun.c:83
> call_write_iter include/linux/fs.h:1901 [inline]
>
> Reported-by: [email protected]
> Signed-off-by: Sabyrzhan Tasbolatov <[email protected]>
> ---
> net/qrtr/tun.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/net/qrtr/tun.c b/net/qrtr/tun.c
> index 15ce9b642b25..b238c40a9984 100644
> --- a/net/qrtr/tun.c
> +++ b/net/qrtr/tun.c
> @@ -80,6 +80,12 @@ static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
> ssize_t ret;
> void *kbuf;
>
> + if (!len)
> + return -EINVAL;
> +
> + if (len > KMALLOC_MAX_SIZE)
> + return -ENOMEM;
> +
> kbuf = kzalloc(len, GFP_KERNEL);

For potential, separate clean up - this is followed
by copy_from_iter_full(len) kzalloc() can probably
be replaced by kmalloc()?

> if (!kbuf)
> return -ENOMEM;


2021-02-04 09:12:49

by Sabyrzhan Tasbolatov

[permalink] [raw]
Subject: [PATCH] net/qrtr: replaced useless kzalloc with kmalloc in qrtr_tun_write_iter()

Replaced kzalloc() with kmalloc(), there is no need for zeroed-out
memory for simple void *kbuf.

>For potential, separate clean up - this is followed
>by copy_from_iter_full(len) kzalloc() can probably
>be replaced by kmalloc()?
>
>> if (!kbuf)
>> return -ENOMEM;

Signed-off-by: Sabyrzhan Tasbolatov <[email protected]>
---
net/qrtr/tun.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/qrtr/tun.c b/net/qrtr/tun.c
index b238c40a9984..9b607c7614de 100644
--- a/net/qrtr/tun.c
+++ b/net/qrtr/tun.c
@@ -86,7 +86,7 @@ static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (len > KMALLOC_MAX_SIZE)
return -ENOMEM;

- kbuf = kzalloc(len, GFP_KERNEL);
+ kbuf = kmalloc(len, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;

--
2.25.1

2021-02-04 18:59:13

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH] net/qrtr: replaced useless kzalloc with kmalloc in qrtr_tun_write_iter()

On Thu, 4 Feb 2021 10:51:59 -0800 Jakub Kicinski wrote:
> On Thu, 4 Feb 2021 15:02:30 +0600 Sabyrzhan Tasbolatov wrote:
> > Replaced kzalloc() with kmalloc(), there is no need for zeroed-out
> > memory for simple void *kbuf.
>
> There is no need for zeroed-out memory because it's immediately
> overwritten by copy_from_iter...

Also if you don't mind please wait a week until the fixes get merged
back into net-next and then repost. Otherwise this patch will not apply
cleanly. (Fixes are merged into a different tree than cleanups)

> > Signed-off-by: Sabyrzhan Tasbolatov <[email protected]>
> > ---
> > net/qrtr/tun.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/net/qrtr/tun.c b/net/qrtr/tun.c
> > index b238c40a9984..9b607c7614de 100644
> > --- a/net/qrtr/tun.c
> > +++ b/net/qrtr/tun.c
> > @@ -86,7 +86,7 @@ static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
> > if (len > KMALLOC_MAX_SIZE)
> > return -ENOMEM;
> >
> > - kbuf = kzalloc(len, GFP_KERNEL);
> > + kbuf = kmalloc(len, GFP_KERNEL);
> > if (!kbuf)
> > return -ENOMEM;
> >
>

2021-02-05 01:36:07

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH] net/qrtr: replaced useless kzalloc with kmalloc in qrtr_tun_write_iter()

On Thu, 4 Feb 2021 15:02:30 +0600 Sabyrzhan Tasbolatov wrote:
> Replaced kzalloc() with kmalloc(), there is no need for zeroed-out
> memory for simple void *kbuf.

There is no need for zeroed-out memory because it's immediately
overwritten by copy_from_iter...

> >For potential, separate clean up - this is followed
> >by copy_from_iter_full(len) kzalloc() can probably
> >be replaced by kmalloc()?
> >
> >> if (!kbuf)
> >> return -ENOMEM;
>
> Signed-off-by: Sabyrzhan Tasbolatov <[email protected]>
> ---
> net/qrtr/tun.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/qrtr/tun.c b/net/qrtr/tun.c
> index b238c40a9984..9b607c7614de 100644
> --- a/net/qrtr/tun.c
> +++ b/net/qrtr/tun.c
> @@ -86,7 +86,7 @@ static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
> if (len > KMALLOC_MAX_SIZE)
> return -ENOMEM;
>
> - kbuf = kzalloc(len, GFP_KERNEL);
> + kbuf = kmalloc(len, GFP_KERNEL);
> if (!kbuf)
> return -ENOMEM;
>