netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] samples: bpf: fix: change the buffer size for read()
@ 2019-05-19  9:05 Chang-Hsien Tsai
  2019-05-21 14:48 ` Daniel Borkmann
  2019-05-21 15:05 ` David Laight
  0 siblings, 2 replies; 4+ messages in thread
From: Chang-Hsien Tsai @ 2019-05-19  9:05 UTC (permalink / raw)
  To: netdev; +Cc: ast, daniel, kafai, songliubraving, yhs, luke.tw

If the trace for read is larger than 4096,
the return value sz will be 4096.
This results in off-by-one error on buf.

    static char buf[4096];
    ssize_t sz;

    sz = read(trace_fd, buf, sizeof(buf));
    if (sz > 0) {
        buf[sz] = 0;
        puts(buf);
    }

Signed-off-by: Chang-Hsien Tsai <luke.tw@gmail.com>
---
 samples/bpf/bpf_load.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index eae7b635343d..d4da90070b58 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -678,7 +678,7 @@ void read_trace_pipe(void)
 		static char buf[4096];
 		ssize_t sz;
 
-		sz = read(trace_fd, buf, sizeof(buf));
+		sz = read(trace_fd, buf, sizeof(buf)-1);
 		if (sz > 0) {
 			buf[sz] = 0;
 			puts(buf);
-- 
2.17.1


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

* Re: [PATCH] samples: bpf: fix: change the buffer size for read()
  2019-05-19  9:05 [PATCH] samples: bpf: fix: change the buffer size for read() Chang-Hsien Tsai
@ 2019-05-21 14:48 ` Daniel Borkmann
  2019-05-21 15:05 ` David Laight
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2019-05-21 14:48 UTC (permalink / raw)
  To: Chang-Hsien Tsai, netdev; +Cc: ast, kafai, songliubraving, yhs

On 05/19/2019 11:05 AM, Chang-Hsien Tsai wrote:
> If the trace for read is larger than 4096,
> the return value sz will be 4096.
> This results in off-by-one error on buf.
> 
>     static char buf[4096];
>     ssize_t sz;
> 
>     sz = read(trace_fd, buf, sizeof(buf));
>     if (sz > 0) {
>         buf[sz] = 0;
>         puts(buf);
>     }
> 
> Signed-off-by: Chang-Hsien Tsai <luke.tw@gmail.com>

Applied, thanks!

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

* RE: [PATCH] samples: bpf: fix: change the buffer size for read()
  2019-05-19  9:05 [PATCH] samples: bpf: fix: change the buffer size for read() Chang-Hsien Tsai
  2019-05-21 14:48 ` Daniel Borkmann
@ 2019-05-21 15:05 ` David Laight
  2019-05-22  1:59   ` luke
  1 sibling, 1 reply; 4+ messages in thread
From: David Laight @ 2019-05-21 15:05 UTC (permalink / raw)
  To: 'Chang-Hsien Tsai', netdev@vger.kernel.org
  Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com,
	songliubraving@fb.com, yhs@fb.com

From: Chang-Hsien Tsai
> Sent: 19 May 2019 10:06
> If the trace for read is larger than 4096,
> the return value sz will be 4096.
> This results in off-by-one error on buf.
> 
>     static char buf[4096];
>     ssize_t sz;
> 
>     sz = read(trace_fd, buf, sizeof(buf));
>     if (sz > 0) {
>         buf[sz] = 0;
>         puts(buf);
>     }
> 
> Signed-off-by: Chang-Hsien Tsai <luke.tw@gmail.com>
> ---
>  samples/bpf/bpf_load.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
> index eae7b635343d..d4da90070b58 100644
> --- a/samples/bpf/bpf_load.c
> +++ b/samples/bpf/bpf_load.c
> @@ -678,7 +678,7 @@ void read_trace_pipe(void)
>  		static char buf[4096];
>  		ssize_t sz;
> 
> -		sz = read(trace_fd, buf, sizeof(buf));
> +		sz = read(trace_fd, buf, sizeof(buf)-1);
>  		if (sz > 0) {
>  			buf[sz] = 0;
>  			puts(buf);

Why not change the puts() to fwrite(buf, sz, 1, stdout) ?
Then you don't need the '\0' terminator.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

* Re: [PATCH] samples: bpf: fix: change the buffer size for read()
  2019-05-21 15:05 ` David Laight
@ 2019-05-22  1:59   ` luke
  0 siblings, 0 replies; 4+ messages in thread
From: luke @ 2019-05-22  1:59 UTC (permalink / raw)
  To: David Laight
  Cc: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	kafai@fb.com, songliubraving@fb.com, yhs@fb.com

On Tue, May 21, 2019 at 11:05 PM David Laight <David.Laight@aculab.com> wrote:
>
> From: Chang-Hsien Tsai
> > @@ -678,7 +678,7 @@ void read_trace_pipe(void)
> >               static char buf[4096];
> >               ssize_t sz;
> >
> > -             sz = read(trace_fd, buf, sizeof(buf));
> > +             sz = read(trace_fd, buf, sizeof(buf)-1);
> >               if (sz > 0) {
> >                       buf[sz] = 0;
> >                       puts(buf);
>
> Why not change the puts() to fwrite(buf, sz, 1, stdout) ?
> Then you don't need the '\0' terminator.
>
>         David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>

Yes.
But, this will be different with the original code.
puts(buf) prints buf and a terminating newline character.

--
Chang-Hsien Tsai

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

end of thread, other threads:[~2019-05-22  2:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-05-19  9:05 [PATCH] samples: bpf: fix: change the buffer size for read() Chang-Hsien Tsai
2019-05-21 14:48 ` Daniel Borkmann
2019-05-21 15:05 ` David Laight
2019-05-22  1:59   ` luke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).