Skip to contentSkip to conversation

libbpf: Prevent overriding errno when logging errors

PATCH In review Toke Høiland-Jørgensen 3 participants 5 messages Aug 13, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Prevent overriding errno when logging errors

Turns out there were a few more instances where libbpf didn't save the errno before writing an error message, causing errno to be overridden by the printf() return and the error disappearing if logging is enabled.

---
 tools/lib/bpf/libbpf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +7 -5
index 0a06124f7999..fd256440e233 100644
@@ -3478,10 +3478,11 @@ bpf_object__probe_global_data(struct bpf_object *obj)
 
 	map = bpf_create_map_xattr(&map_attr);
 	if (map < 0) {
-		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+		ret = -errno;
+		cp = libbpf_strerror_r(-ret, errmsg, sizeof(errmsg));
 		pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
-			__func__, cp, errno);
-		return -errno;
+			__func__, cp, -ret);
+		return ret;
 	}
 
 	insns[0].imm = map;
@@ -6012,9 +6013,10 @@ int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
 	}
 
 	if (bpf_obj_pin(prog->instances.fds[instance], path)) {
-		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+		err = -errno;
+		cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
 		pr_warn("failed to pin program: %s\n", cp);
-		return -errno;
+		return err;
 	}
 	pr_debug("pinned program '%s'\n", path);
 
Signature · 1 line
2.28.0
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Toke Høiland-Jørgensen

Turns out there were a few more instances where libbpf didn't save the errno before writing an error message, causing errno to be overridden by the printf() return and the error disappearing if logging is enabled.

---

Quoted 8 lines
 tools/lib/bpf/libbpf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 0a06124f7999..fd256440e233 100644

tools/lib/bpf/libbpf.c bpf_object__probe_global_data(struct bpf_object *obj) · line 3482

Quoted 6 lines
tools/lib/bpf/libbpf.c +2 -1
@@ -3478,10 +3478,11 @@ bpf_object__probe_global_data(struct bpf_object *obj)
 
        map = bpf_create_map_xattr(&map_attr);
        if (map < 0) {
-               cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+               ret = -errno;
+               cp = libbpf_strerror_r(-ret, errmsg, sizeof(errmsg));

fyi, libbpf_strerror_r() is smart enough to work with both negative and positive error numbers (it basically takes abs(err)), so no need to ensure it's positive here and below.

Quoted 24 lines
tools/lib/bpf/libbpf.c +5 -5
                pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
-                       __func__, cp, errno);
-               return -errno;
+                       __func__, cp, -ret);
+               return ret;
        }
 
        insns[0].imm = map;
@@ -6012,9 +6013,10 @@ int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
        }
 
        if (bpf_obj_pin(prog->instances.fds[instance], path)) {
-               cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+               err = -errno;
+               cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
                pr_warn("failed to pin program: %s\n", cp);
-               return -errno;
+               return err;
        }
        pr_debug("pinned program '%s'\n", path);
 
--

2.28.0

#2 lore raw
Quoted 20 lines · Andrii Nakryiko
Quoted 7 lines · Toke Høiland-Jørgensen

Turns out there were a few more instances where libbpf didn't save the errno before writing an error message, causing errno to be overridden by the printf() return and the error disappearing if logging is enabled.

---

Quoted 9 lines
 tools/lib/bpf/libbpf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 0a06124f7999..fd256440e233 100644
@@ -3478,10 +3478,11 @@ bpf_object__probe_global_data(struct bpf_object *obj)
 
Quoted 9 lines

tools/lib/bpf/libbpf.c bpf_object__probe_global_data(struct bpf_object *obj) · line 3482

Quoted 5 lines
tools/lib/bpf/libbpf.c +2 -1
        map = bpf_create_map_xattr(&map_attr);
        if (map < 0) {
-               cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+               ret = -errno;
+               cp = libbpf_strerror_r(-ret, errmsg, sizeof(errmsg));

fyi, libbpf_strerror_r() is smart enough to work with both negative and positive error numbers (it basically takes abs(err)), so no need to ensure it's positive here and below.

Noted. Although that also means it doesn't hurt either, I suppose; so not going to bother respinning this unless someone insists :)

-Toke

maintainer
#3 lore raw
Quoted 31 lines · Toke Høiland-Jørgensen
Quoted 20 lines · Andrii Nakryiko
Quoted 7 lines · Toke Høiland-Jørgensen

Turns out there were a few more instances where libbpf didn't save the errno before writing an error message, causing errno to be overridden by the printf() return and the error disappearing if logging is enabled.

---

Quoted 9 lines
  tools/lib/bpf/libbpf.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 0a06124f7999..fd256440e233 100644
@@ -3478,10 +3478,11 @@ bpf_object__probe_global_data(struct bpf_object *obj)
 
Quoted 9 lines

tools/lib/bpf/libbpf.c bpf_object__probe_global_data(struct bpf_object *obj) · line 3482

Quoted 5 lines
tools/lib/bpf/libbpf.c +2 -1
         map = bpf_create_map_xattr(&map_attr);
         if (map < 0) {
-               cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+               ret = -errno;
+               cp = libbpf_strerror_r(-ret, errmsg, sizeof(errmsg));

fyi, libbpf_strerror_r() is smart enough to work with both negative and positive error numbers (it basically takes abs(err)), so no need to ensure it's positive here and below.

Quoted 2 lines

Noted. Although that also means it doesn't hurt either, I suppose; so not going to bother respinning this unless someone insists :)

Fixed up while applying, thanks!

#4 lore raw
Quoted 32 lines · Daniel Borkmann
Quoted 31 lines · Toke Høiland-Jørgensen
Quoted 29 lines · Andrii Nakryiko
Quoted 7 lines · Toke Høiland-Jørgensen

Turns out there were a few more instances where libbpf didn't save the errno before writing an error message, causing errno to be overridden by the printf() return and the error disappearing if logging is enabled.

---

Quoted 8 lines
  tools/lib/bpf/libbpf.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 0a06124f7999..fd256440e233 100644

tools/lib/bpf/libbpf.c bpf_object__probe_global_data(struct bpf_object *obj) · line 3482

Quoted 6 lines
tools/lib/bpf/libbpf.c +2 -1
@@ -3478,10 +3478,11 @@ bpf_object__probe_global_data(struct bpf_object *obj)
 
         map = bpf_create_map_xattr(&map_attr);
         if (map < 0) {
-               cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+               ret = -errno;
+               cp = libbpf_strerror_r(-ret, errmsg, sizeof(errmsg));

fyi, libbpf_strerror_r() is smart enough to work with both negative and positive error numbers (it basically takes abs(err)), so no need to ensure it's positive here and below.

Quoted 4 lines
Quoted 2 lines

Noted. Although that also means it doesn't hurt either, I suppose; so not going to bother respinning this unless someone insists :)

Fixed up while applying, thanks!

Great, thanks!

-Toke