libbpf: always NULL out pobj in bpf_prog_load_xattr
Subsystems (6)
PATCH 1/1 libbpf: always NULL out pobj in bpf_prog_load_xattr
Currently, code like the following segfaults if bpf_prog_load_xattr returns an error:
struct bpf_object *obj;
err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
bpf_object__close(obj);
if (err)
...Unconditionally reset pobj to NULL at the start of the function to fix this.
--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
@@ -3363,6 +3363,8 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,struct bpf_map *map;int err; + *pobj =NULL ; +if (!attr)return -EINVAL ;if (!attr->file)
Signature · 1 line
2.19.1
Quoted 7 lines · Lorenz Bauer
Currently, code like the following segfaults if
bpf_prog_load_xattrreturns an error:struct bpf_object *obj; err = bpf_prog_load_xattr(&attr, &obj, &prog_fd); bpf_object__close(obj);
This is a bug. err should always be checked first before using obj or prog_fd. If there is err, calling bpf_object__close(NULL) is another redundancy.
Quoted 5 lines
if (err) ...Unconditionally reset pobj to NULL at the start of the function to fix this.
Hence, this change is unnecessary.
Quoted 22 lines
Signed-off-by Lorenz Bauer <[email protected]>--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)tools/lib/bpf/libbpf.c +2
-0@@ -3363,6 +3363,8 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,struct bpf_map *map;int err; + *pobj =NULL ; +if (!attr)return -EINVAL ;if (!attr->file)Signature · 1 line
2.19.1
Quoted 5 lines · Martin Lau
Quoted 3 lines · Lorenz Bauer
Currently, code like the following segfaults if
bpf_prog_load_xattrreturns an error:
Quoted 7 lines
Quoted 4 lines
struct bpf_object *obj; err = bpf_prog_load_xattr(&attr, &obj, &prog_fd); bpf_object__close(obj);This is a bug. err should always be checked first before using obj or
prog_fd. If there is err, callingbpf_object__close(NULL)is another redundancy.
Ack, I'll send another patch. The bug is in a couple of places, so I figured that this is something people do naturally.
Quoted 30 lines
Quoted 6 lines
if (err) ...Unconditionally reset pobj to NULL at the start of the function to fix this.
Hence, this change is unnecessary.
Quoted 22 lines
Signed-off-by Lorenz Bauer <[email protected]>--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)tools/lib/bpf/libbpf.c +2
-1@@ -3363,6 +3363,8 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,struct bpf_map *map;int err; + *pobj =NULL ; +if (!attr)return -EINVAL ;if (!attr->file) --2.19.1
Signature · 4 lines
Lorenz Bauer | Systems Engineer 25 Lavington St., London SE1 0NZ www.cloudflare.com
PATCH v2 1/1 selftests: bpf: initialize bpf_object pointers where needed
In reply to Lorenz BauerThere are a few tests which call bpf_object__close on uninitialized bpf_object*, which may segfault. Explicitly zero-initialise these pointers to avoid this.
--- tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c | 2 +- tools/testing/selftests/bpf/prog_tests/task_fd_query_tp.c | 2 +- tools/testing/selftests/bpf/prog_tests/tp_attach_query.c | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1 -1
@@ -15,7 +15,7 @@ static int libbpf_debug_print(enum libbpf_print_level level,static int check_load (const char *file) {struct bpf_prog_load_attr attr; -struct bpf_object *obj; +struct bpf_object *obj =NULL ;int err, prog_fd;memset (&attr,0 ,sizeof (struct bpf_prog_load_attr ));
tools/testing/selftests/bpf/prog_tests/task_fd_query_tp.c +1 -1
@@ -9,7 +9,7 @@ static void test_task_fd_query_tp_core(const char *probe_name,struct perf_event_attr attr = {};__u64 probe_offset, probe_addr;__u32 len, prog_id, fd_type; -struct bpf_object *obj; +struct bpf_object *obj =NULL ;__u32 duration =0 ;char buf[256 ];
tools/testing/selftests/bpf/prog_tests/tp_attach_query.c +3 -0
@@ -13,6 +13,9 @@ void test_tp_attach_query(void)struct bpf_prog_info prog_info;char buf[256 ]; +for (i =0 ; i < num_progs; i++) + obj[i] =NULL ; +snprintf (buf,sizeof (buf),"/sys/kernel/debug/tracing/events/sched/sched_switch/id" ); efd =open (buf,O_RDONLY ,0 );
Signature · 1 line
2.19.1
Re: [PATCH bpf v2] selftests: bpf: initialize bpf_object pointers where needed
Quoted 3 lines · Lorenz Bauer
There are a few tests which call
bpf_object__closeon uninitialized bpf_object*, which may segfault. Explicitly zero-initialise these pointers to avoid this.
Alexei Starovoitov
AppliedRe: [PATCH bpf v2] selftests: bpf: initialize bpf_object pointers where needed
Quoted 6 lines · Martin Lau
Quoted 3 lines · Lorenz Bauer
There are a few tests which call
bpf_object__closeon uninitialized bpf_object*, which may segfault. Explicitly zero-initialise these pointers to avoid this.Acked-by Martin KaFai Lau <[email protected]>
Applied. Thanks