diff mbox series

[bpf] libbpf: Prevent overriding errno when logging errors

Message ID 20200813142905.160381-1-toke@redhat.com
State Accepted
Delegated to: BPF Maintainers
Headers show
Series [bpf] libbpf: Prevent overriding errno when logging errors | expand

Commit Message

Toke Høiland-Jørgensen Aug. 13, 2020, 2:29 p.m. UTC
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.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
 tools/lib/bpf/libbpf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Comments

Andrii Nakryiko Aug. 13, 2020, 4:41 p.m. UTC | #1
On Thu, Aug 13, 2020 at 7:29 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> 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.
>
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> ---

Acked-by: Andrii Nakryiko <andriin@fb.com>

>  tools/lib/bpf/libbpf.c | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 0a06124f7999..fd256440e233 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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.

>                 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
>
Toke Høiland-Jørgensen Aug. 13, 2020, 7:52 p.m. UTC | #2
Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:

> On Thu, Aug 13, 2020 at 7:29 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>>
>> 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.
>>
>> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
>> ---
>
> Acked-by: Andrii Nakryiko <andriin@fb.com>
>
>>  tools/lib/bpf/libbpf.c | 12 +++++++-----
>>  1 file changed, 7 insertions(+), 5 deletions(-)
>>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 0a06124f7999..fd256440e233 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -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.

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

-Toke
Daniel Borkmann Aug. 13, 2020, 8:35 p.m. UTC | #3
On 8/13/20 9:52 PM, Toke Høiland-Jørgensen wrote:
> Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:
>> On Thu, Aug 13, 2020 at 7:29 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>>>
>>> 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.
>>>
>>> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
>>> ---
>>
>> Acked-by: Andrii Nakryiko <andriin@fb.com>
>>
>>>   tools/lib/bpf/libbpf.c | 12 +++++++-----
>>>   1 file changed, 7 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>>> index 0a06124f7999..fd256440e233 100644
>>> --- a/tools/lib/bpf/libbpf.c
>>> +++ b/tools/lib/bpf/libbpf.c
>>> @@ -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.
> 
> 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!
Toke Høiland-Jørgensen Aug. 13, 2020, 8:52 p.m. UTC | #4
Daniel Borkmann <daniel@iogearbox.net> writes:

> On 8/13/20 9:52 PM, Toke Høiland-Jørgensen wrote:
>> Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:
>>> On Thu, Aug 13, 2020 at 7:29 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>>>>
>>>> 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.
>>>>
>>>> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
>>>> ---
>>>
>>> Acked-by: Andrii Nakryiko <andriin@fb.com>
>>>
>>>>   tools/lib/bpf/libbpf.c | 12 +++++++-----
>>>>   1 file changed, 7 insertions(+), 5 deletions(-)
>>>>
>>>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>>>> index 0a06124f7999..fd256440e233 100644
>>>> --- a/tools/lib/bpf/libbpf.c
>>>> +++ b/tools/lib/bpf/libbpf.c
>>>> @@ -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.
>> 
>> 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
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 0a06124f7999..fd256440e233 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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);