samples, bpf: fix to change the buffer size for read() [Linux 5.2]

samples, bpf: fix to change the buffer size for read() [Linux 5.2]

This Linux kernel change "samples, bpf: fix to change the buffer size for read()" is included in the Linux 5.2 release. This change is authored by Chang-Hsien Tsai <luke.tw [at] gmail.com> on Sun May 19 09:05:44 2019 +0000. The commit for this change in Linux stable tree is f7c2d64 (patch).

samples, bpf: fix to change the buffer size for read()

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>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

There are 2 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 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 eae7b63..6e87cc8 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);

Leave a Reply

Your email address will not be published. Required fields are marked *