Skip to contentSkip to conversation

bpftool: Clear errno after libcap's checks

PATCH Superseded Quentin Monnet 2 participants 3 messages Aug 12, 2022 – Aug 15, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: Clear errno after libcap's checks

When bpftool is linked against libcap, the library runs a "constructor" function to compute the number of capabilities of the running kernel [0], at the beginning of the execution of the program. As part of this, it performs multiple calls to prctl(). Some of these may fail, and set errno to a non-zero value:

    # strace -e prctl ./bpftool version
    prctl(PR_CAPBSET_READ, CAP_MAC_OVERRIDE) = 1
    prctl(PR_CAPBSET_READ, 0x30 /* CAP_??? */) = -1 EINVAL (Invalid argument)
    prctl(PR_CAPBSET_READ, CAP_CHECKPOINT_RESTORE) = 1
    prctl(PR_CAPBSET_READ, 0x2c /* CAP_??? */) = -1 EINVAL (Invalid argument)
    prctl(PR_CAPBSET_READ, 0x2a /* CAP_??? */) = -1 EINVAL (Invalid argument)
    prctl(PR_CAPBSET_READ, 0x29 /* CAP_??? */) = -1 EINVAL (Invalid argument)
    ** fprintf added at the top of main(): we have errno == 1
    ./bpftool v7.0.0
    using libbpf v1.0
    features: libbfd, libbpf_strict, skeletons
    +++ exited with 0 +++

Let's clean errno at the beginning of the main() function, to make sure that these checks do not interfere with the batch mode, where we error out if errno is set after a bpftool command.

[0] https://git.kernel.org/pub/scm/libs/libcap/libcap.git/tree/libcap/cap_alloc.c?h=v1.2.65#n20

---
 tools/bpf/bpftool/main.c | 3 +++
 1 file changed, 3 insertions(+)
tools/bpf/bpftool/main.c +3 -0
index 451cefc2d0da..c0e2e4fedbe8 100644
@@ -435,6 +435,9 @@ int main(int argc, char **argv)
 
 	setlinebuf(stdout);
 
+	/* Libcap */
+	errno = 0;
+
 	last_do_help = do_help;
 	pretty_output = false;
 	json_output = false;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 35 lines · Quentin Monnet

When bpftool is linked against libcap, the library runs a "constructor" function to compute the number of capabilities of the running kernel [0], at the beginning of the execution of the program. As part of this, it performs multiple calls to prctl(). Some of these may fail, and set errno to a non-zero value:

     # strace -e prctl ./bpftool version
     prctl(PR_CAPBSET_READ, CAP_MAC_OVERRIDE) = 1
     prctl(PR_CAPBSET_READ, 0x30 /* CAP_??? */) = -1 EINVAL (Invalid argument)
     prctl(PR_CAPBSET_READ, CAP_CHECKPOINT_RESTORE) = 1
     prctl(PR_CAPBSET_READ, 0x2c /* CAP_??? */) = -1 EINVAL (Invalid argument)
     prctl(PR_CAPBSET_READ, 0x2a /* CAP_??? */) = -1 EINVAL (Invalid argument)
     prctl(PR_CAPBSET_READ, 0x29 /* CAP_??? */) = -1 EINVAL (Invalid argument)
     ** fprintf added at the top of main(): we have errno == 1
     ./bpftool v7.0.0
     using libbpf v1.0
     features: libbfd, libbpf_strict, skeletons
     +++ exited with 0 +++

Let's clean errno at the beginning of the main() function, to make sure that these checks do not interfere with the batch mode, where we error out if errno is set after a bpftool command.

[0] https://git.kernel.org/pub/scm/libs/libcap/libcap.git/tree/libcap/cap_alloc.c?h=v1.2.65#n20

---
  tools/bpf/bpftool/main.c | 3 +++
  1 file changed, 3 insertions(+)
tools/bpf/bpftool/main.c +0 -0
index 451cefc2d0da..c0e2e4fedbe8 100644

tools/bpf/bpftool/main.c int main(int argc, char **argv) · line 438

Quoted 4 lines
tools/bpf/bpftool/main.c +1 -0
@@ -435,6 +435,9 @@ int main(int argc, char **argv)
 
  	setlinebuf(stdout);
 
+	/* Libcap */

Good catch! The comment is a bit too terse, could you improve it, so that it's clear from reading code (w/o digging through git log) why we need to reset errno in this location? Thx

Quoted 6 lines
tools/bpf/bpftool/main.c +2 -0
+	errno = 0;
+
  	last_do_help = do_help;
  	pretty_output = false;
  	json_output = false;
#2 lore raw
Quoted 36 lines · Daniel Borkmann
Quoted 35 lines · Quentin Monnet

When bpftool is linked against libcap, the library runs a "constructor" function to compute the number of capabilities of the running kernel [0], at the beginning of the execution of the program. As part of this, it performs multiple calls to prctl(). Some of these may fail, and set errno to a non-zero value:

     # strace -e prctl ./bpftool version
     prctl(PR_CAPBSET_READ, CAP_MAC_OVERRIDE) = 1
     prctl(PR_CAPBSET_READ, 0x30 /* CAP_??? */) = -1 EINVAL (Invalid
argument)
     prctl(PR_CAPBSET_READ, CAP_CHECKPOINT_RESTORE) = 1
     prctl(PR_CAPBSET_READ, 0x2c /* CAP_??? */) = -1 EINVAL (Invalid
argument)
     prctl(PR_CAPBSET_READ, 0x2a /* CAP_??? */) = -1 EINVAL (Invalid
argument)
     prctl(PR_CAPBSET_READ, 0x29 /* CAP_??? */) = -1 EINVAL (Invalid
argument)
     ** fprintf added at the top of main(): we have errno == 1
     ./bpftool v7.0.0
     using libbpf v1.0
     features: libbfd, libbpf_strict, skeletons
     +++ exited with 0 +++

Let's clean errno at the beginning of the main() function, to make sure that these checks do not interfere with the batch mode, where we error out if errno is set after a bpftool command.

[0]
https://git.kernel.org/pub/scm/libs/libcap/libcap.git/tree/libcap/cap_alloc.c?h=v1.2.65#n20

---
  tools/bpf/bpftool/main.c | 3 +++

  1 file changed, 3 insertions(+)
Quoted 13 lines
Quoted 7 lines
tools/bpf/bpftool/main.c +0 -0
index 451cefc2d0da..c0e2e4fedbe8 100644
@@ -435,6 +435,9 @@ int main(int argc, char **argv)
         setlinebuf(stdout);

  +    /* Libcap */

Good catch! The comment is a bit too terse, could you improve it, so that it's
clear from reading code (w/o digging through git log) why we need to reset errno
in this location? Thx

Right, I'll work on the comment and repost, thank you for the review Quentin