All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] NTB: ntb_tool: uninitialized heap data in tool_fn_write()
@ 2022-07-20 18:28 Dan Carpenter
  2022-07-28 11:44 ` Serge Semin
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2022-07-20 18:28 UTC (permalink / raw)
  To: Jon Mason, Allen Hubbe; +Cc: Dave Jiang, ntb, kernel-janitors

The call to:

	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);

will return success if it is able to write even one byte to "buf".
The value of "*offp" controls which byte.  This could result in
reading uninitialized data when we do the sscanf() on the next line.

This code is not really desigined to handle partial writes where
*offp is non-zero and the "buf" is preserved and re-used between writes.
Just ban partial writes and replace the simple_write_to_buffer() with
copy_from_user().

Fixes: 578b881ba9c4 ("NTB: Add tool test client")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/ntb/test/ntb_tool.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/ntb/test/ntb_tool.c b/drivers/ntb/test/ntb_tool.c
index b7bf3f863d79..5ee0afa621a9 100644
--- a/drivers/ntb/test/ntb_tool.c
+++ b/drivers/ntb/test/ntb_tool.c
@@ -367,14 +367,16 @@ static ssize_t tool_fn_write(struct tool_ctx *tc,
 	u64 bits;
 	int n;
 
+	if (*offp)
+		return 0;
+
 	buf = kmalloc(size + 1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
-	if (ret < 0) {
+	if (copy_from_user(buf, ubuf, size)) {
 		kfree(buf);
-		return ret;
+		return -EFAULT;
 	}
 
 	buf[size] = 0;
-- 
2.35.1


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

* Re: [PATCH] NTB: ntb_tool: uninitialized heap data in tool_fn_write()
  2022-07-20 18:28 [PATCH] NTB: ntb_tool: uninitialized heap data in tool_fn_write() Dan Carpenter
@ 2022-07-28 11:44 ` Serge Semin
  2022-08-12 14:15   ` Jon Mason
  0 siblings, 1 reply; 3+ messages in thread
From: Serge Semin @ 2022-07-28 11:44 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: Jon Mason, Allen Hubbe, Dave Jiang, ntb, kernel-janitors

On Wed, Jul 20, 2022 at 09:28:18PM +0300, Dan Carpenter wrote:
> The call to:
> 
> 	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
> 
> will return success if it is able to write even one byte to "buf".
> The value of "*offp" controls which byte.  This could result in
> reading uninitialized data when we do the sscanf() on the next line.
> 
> This code is not really desigined to handle partial writes where
> *offp is non-zero and the "buf" is preserved and re-used between writes.
> Just ban partial writes and replace the simple_write_to_buffer() with
> copy_from_user().
> 
> Fixes: 578b881ba9c4 ("NTB: Add tool test client")

Looks good. Thanks.
Reviewed-by: Serge Semin <fancer.lancer@gmail.com>

-Sergey

> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/ntb/test/ntb_tool.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/ntb/test/ntb_tool.c b/drivers/ntb/test/ntb_tool.c
> index b7bf3f863d79..5ee0afa621a9 100644
> --- a/drivers/ntb/test/ntb_tool.c
> +++ b/drivers/ntb/test/ntb_tool.c
> @@ -367,14 +367,16 @@ static ssize_t tool_fn_write(struct tool_ctx *tc,
>  	u64 bits;
>  	int n;
>  
> +	if (*offp)
> +		return 0;
> +
>  	buf = kmalloc(size + 1, GFP_KERNEL);
>  	if (!buf)
>  		return -ENOMEM;
>  
> -	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
> -	if (ret < 0) {
> +	if (copy_from_user(buf, ubuf, size)) {
>  		kfree(buf);
> -		return ret;
> +		return -EFAULT;
>  	}
>  
>  	buf[size] = 0;
> -- 
> 2.35.1
> 
> 

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

* Re: [PATCH] NTB: ntb_tool: uninitialized heap data in tool_fn_write()
  2022-07-28 11:44 ` Serge Semin
@ 2022-08-12 14:15   ` Jon Mason
  0 siblings, 0 replies; 3+ messages in thread
From: Jon Mason @ 2022-08-12 14:15 UTC (permalink / raw)
  To: Serge Semin; +Cc: Dan Carpenter, Allen Hubbe, Dave Jiang, ntb, kernel-janitors

On Thu, Jul 28, 2022 at 02:44:17PM +0300, Serge Semin wrote:
> On Wed, Jul 20, 2022 at 09:28:18PM +0300, Dan Carpenter wrote:
> > The call to:
> > 
> > 	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
> > 
> > will return success if it is able to write even one byte to "buf".
> > The value of "*offp" controls which byte.  This could result in
> > reading uninitialized data when we do the sscanf() on the next line.
> > 
> > This code is not really desigined to handle partial writes where
> > *offp is non-zero and the "buf" is preserved and re-used between writes.
> > Just ban partial writes and replace the simple_write_to_buffer() with
> > copy_from_user().
> > 
> > Fixes: 578b881ba9c4 ("NTB: Add tool test client")
> 
> Looks good. Thanks.
> Reviewed-by: Serge Semin <fancer.lancer@gmail.com>
> 
> -Sergey

Sorry for the extremely long delay in response.  This series is in my
ntb branch and will be in my pull request for v5.20 which should be
going out later today.

Thanks,
Jon

> 
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/ntb/test/ntb_tool.c | 8 +++++---
> >  1 file changed, 5 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/ntb/test/ntb_tool.c b/drivers/ntb/test/ntb_tool.c
> > index b7bf3f863d79..5ee0afa621a9 100644
> > --- a/drivers/ntb/test/ntb_tool.c
> > +++ b/drivers/ntb/test/ntb_tool.c
> > @@ -367,14 +367,16 @@ static ssize_t tool_fn_write(struct tool_ctx *tc,
> >  	u64 bits;
> >  	int n;
> >  
> > +	if (*offp)
> > +		return 0;
> > +
> >  	buf = kmalloc(size + 1, GFP_KERNEL);
> >  	if (!buf)
> >  		return -ENOMEM;
> >  
> > -	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
> > -	if (ret < 0) {
> > +	if (copy_from_user(buf, ubuf, size)) {
> >  		kfree(buf);
> > -		return ret;
> > +		return -EFAULT;
> >  	}
> >  
> >  	buf[size] = 0;
> > -- 
> > 2.35.1
> > 
> > 

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

end of thread, other threads:[~2022-08-12 14:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-20 18:28 [PATCH] NTB: ntb_tool: uninitialized heap data in tool_fn_write() Dan Carpenter
2022-07-28 11:44 ` Serge Semin
2022-08-12 14:15   ` Jon Mason

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.