* [PATCH v2 bpf] tools: bpftool: Add missing close before bpftool net attach exit
@ 2020-11-10 1:46 Wang Hai
2020-11-10 4:33 ` John Fastabend
0 siblings, 1 reply; 4+ messages in thread
From: Wang Hai @ 2020-11-10 1:46 UTC (permalink / raw)
To: quentin, mrostecki, john.fastabend
Cc: ast, daniel, kafai, songliubraving, yhs, andrii, kpsingh, toke,
danieltimlee, bpf, netdev, linux-kernel
progfd is created by prog_parse_fd(), before 'bpftool net attach' exit,
it should be closed.
Fixes: 04949ccc273e ("tools: bpftool: add net attach command to attach XDP on interface")
Signed-off-by: Wang Hai <wanghai38@huawei.com>
---
v1->v2: use cleanup tag instead of repeated closes
tools/bpf/bpftool/net.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 910e7bac6e9e..1ac7228167e6 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -578,8 +578,8 @@ static int do_attach(int argc, char **argv)
ifindex = net_parse_dev(&argc, &argv);
if (ifindex < 1) {
- close(progfd);
- return -EINVAL;
+ err = -EINVAL;
+ goto cleanup;
}
if (argc) {
@@ -587,8 +587,8 @@ static int do_attach(int argc, char **argv)
overwrite = true;
} else {
p_err("expected 'overwrite', got: '%s'?", *argv);
- close(progfd);
- return -EINVAL;
+ err = -EINVAL;
+ goto cleanup;
}
}
@@ -600,13 +600,15 @@ static int do_attach(int argc, char **argv)
if (err < 0) {
p_err("interface %s attach failed: %s",
attach_type_strings[attach_type], strerror(-err));
- return err;
+ goto cleanup;
}
if (json_output)
jsonw_null(json_wtr);
- return 0;
+cleanup:
+ close(progfd);
+ return err;
}
static int do_detach(int argc, char **argv)
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* RE: [PATCH v2 bpf] tools: bpftool: Add missing close before bpftool net attach exit
2020-11-10 1:46 [PATCH v2 bpf] tools: bpftool: Add missing close before bpftool net attach exit Wang Hai
@ 2020-11-10 4:33 ` John Fastabend
2020-11-10 8:40 ` wanghai (M)
0 siblings, 1 reply; 4+ messages in thread
From: John Fastabend @ 2020-11-10 4:33 UTC (permalink / raw)
To: Wang Hai, quentin, mrostecki, john.fastabend
Cc: ast, daniel, kafai, songliubraving, yhs, andrii, kpsingh, toke,
danieltimlee, bpf, netdev, linux-kernel
Wang Hai wrote:
> progfd is created by prog_parse_fd(), before 'bpftool net attach' exit,
> it should be closed.
>
> Fixes: 04949ccc273e ("tools: bpftool: add net attach command to attach XDP on interface")
> Signed-off-by: Wang Hai <wanghai38@huawei.com>
> ---
> v1->v2: use cleanup tag instead of repeated closes
> tools/bpf/bpftool/net.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> index 910e7bac6e9e..1ac7228167e6 100644
> --- a/tools/bpf/bpftool/net.c
> +++ b/tools/bpf/bpftool/net.c
> @@ -578,8 +578,8 @@ static int do_attach(int argc, char **argv)
>
> ifindex = net_parse_dev(&argc, &argv);
> if (ifindex < 1) {
> - close(progfd);
> - return -EINVAL;
> + err = -EINVAL;
> + goto cleanup;
> }
>
> if (argc) {
> @@ -587,8 +587,8 @@ static int do_attach(int argc, char **argv)
> overwrite = true;
> } else {
> p_err("expected 'overwrite', got: '%s'?", *argv);
> - close(progfd);
> - return -EINVAL;
> + err = -EINVAL;
> + goto cleanup;
> }
> }
>
> @@ -600,13 +600,15 @@ static int do_attach(int argc, char **argv)
I think now that return value depends on this err it should be 'if (err)'
otherwise we risk retunring non-zero error code from do_attach which
will cause programs to fail.
> if (err < 0) {
^^^^^^^^^^^^
if (err) {
> p_err("interface %s attach failed: %s",
> attach_type_strings[attach_type], strerror(-err));
> - return err;
> + goto cleanup;
> }
>
> if (json_output)
> jsonw_null(json_wtr);
>
> - return 0;
Alternatively we could add an 'err = 0' here, but above should never
return a value >0 as far as I can see.
Thanks,
John
> +cleanup:
> + close(progfd);
> + return err;
> }
>
> static int do_detach(int argc, char **argv)
> --
> 2.17.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 bpf] tools: bpftool: Add missing close before bpftool net attach exit
2020-11-10 4:33 ` John Fastabend
@ 2020-11-10 8:40 ` wanghai (M)
2020-11-11 8:44 ` John Fastabend
0 siblings, 1 reply; 4+ messages in thread
From: wanghai (M) @ 2020-11-10 8:40 UTC (permalink / raw)
To: John Fastabend
Cc: quentin, mrostecki, ast, daniel, kafai, songliubraving, yhs,
andrii, kpsingh, toke, danieltimlee, bpf, netdev, linux-kernel
在 2020/11/10 12:33, John Fastabend 写道:
> Wang Hai wrote:
>> progfd is created by prog_parse_fd(), before 'bpftool net attach' exit,
>> it should be closed.
>>
>> Fixes: 04949ccc273e ("tools: bpftool: add net attach command to attach XDP on interface")
>> Signed-off-by: Wang Hai <wanghai38@huawei.com>
>> ---
>> v1->v2: use cleanup tag instead of repeated closes
>> tools/bpf/bpftool/net.c | 14 ++++++++------
>> 1 file changed, 8 insertions(+), 6 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
>> index 910e7bac6e9e..1ac7228167e6 100644
>> --- a/tools/bpf/bpftool/net.c
>> +++ b/tools/bpf/bpftool/net.c
>> @@ -578,8 +578,8 @@ static int do_attach(int argc, char **argv)
>>
>> ifindex = net_parse_dev(&argc, &argv);
>> if (ifindex < 1) {
>> - close(progfd);
>> - return -EINVAL;
>> + err = -EINVAL;
>> + goto cleanup;
>> }
>>
>> if (argc) {
>> @@ -587,8 +587,8 @@ static int do_attach(int argc, char **argv)
>> overwrite = true;
>> } else {
>> p_err("expected 'overwrite', got: '%s'?", *argv);
>> - close(progfd);
>> - return -EINVAL;
>> + err = -EINVAL;
>> + goto cleanup;
>> }
>> }
>>
>> @@ -600,13 +600,15 @@ static int do_attach(int argc, char **argv)
> I think now that return value depends on this err it should be 'if (err)'
> otherwise we risk retunring non-zero error code from do_attach which
> will cause programs to fail.
I agree with you. Thanks.
>> if (err < 0) {
> ^^^^^^^^^^^^
> if (err) {
>
>> p_err("interface %s attach failed: %s",
>> attach_type_strings[attach_type], strerror(-err));
>> - return err;
>> + goto cleanup;
>> }
>>
>> if (json_output)
>> jsonw_null(json_wtr);
>>
>> - return 0;
>
> Alternatively we could add an 'err = 0' here, but above should never
> return a value >0 as far as I can see.
It's true that 'err > 0' doesn't exist currently , but adding 'err = 0'
would make the code clearer. Thanks for your advice.
>> +cleanup:
>> + close(progfd);
>> + return err;
>> }
>>
>> static int do_detach(int argc, char **argv)
>> --
>> 2.17.1
>>
Can it be fixed like this?
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -578,8 +578,8 @@ static int do_attach(int argc, char **argv)
ifindex = net_parse_dev(&argc, &argv);
if (ifindex < 1) {
- close(progfd);
- return -EINVAL;
+ err = -EINVAL;
+ goto cleanup;
}
if (argc) {
@@ -587,8 +587,8 @@ static int do_attach(int argc, char **argv)
overwrite = true;
} else {
p_err("expected 'overwrite', got: '%s'?", *argv);
- close(progfd);
- return -EINVAL;
+ err = -EINVAL;
+ goto cleanup;
}
}
@@ -597,16 +597,19 @@ static int do_attach(int argc, char **argv)
err = do_attach_detach_xdp(progfd, attach_type, ifindex,
overwrite);
- if (err < 0) {
+ if (err) {
p_err("interface %s attach failed: %s",
attach_type_strings[attach_type], strerror(-err));
- return err;
+ goto cleanup;
}
if (json_output)
jsonw_null(json_wtr);
- return 0;
+ ret = 0;
+cleanup:
+ close(progfd);
+ return err;
}
>
> .
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 bpf] tools: bpftool: Add missing close before bpftool net attach exit
2020-11-10 8:40 ` wanghai (M)
@ 2020-11-11 8:44 ` John Fastabend
0 siblings, 0 replies; 4+ messages in thread
From: John Fastabend @ 2020-11-11 8:44 UTC (permalink / raw)
To: wanghai (M), John Fastabend
Cc: quentin, mrostecki, ast, daniel, kafai, songliubraving, yhs,
andrii, kpsingh, toke, danieltimlee, bpf, netdev, linux-kernel
wanghai (M) wrote:
>
> 在 2020/11/10 12:33, John Fastabend 写道:
> > Wang Hai wrote:
> >> progfd is created by prog_parse_fd(), before 'bpftool net attach' exit,
> >> it should be closed.
> >>
> >> Fixes: 04949ccc273e ("tools: bpftool: add net attach command to attach XDP on interface")
> >> Signed-off-by: Wang Hai <wanghai38@huawei.com>
> >> ---
[...]
> > Alternatively we could add an 'err = 0' here, but above should never
> > return a value >0 as far as I can see.
> It's true that 'err > 0' doesn't exist currently , but adding 'err = 0'
> would make the code clearer. Thanks for your advice.
> >> +cleanup:
> >> + close(progfd);
> >> + return err;
> >> }
> >>
> >> static int do_detach(int argc, char **argv)
> >> --
> >> 2.17.1
> >>
> Can it be fixed like this?
>
> --- a/tools/bpf/bpftool/net.c
> +++ b/tools/bpf/bpftool/net.c
> @@ -578,8 +578,8 @@ static int do_attach(int argc, char **argv)
>
> ifindex = net_parse_dev(&argc, &argv);
> if (ifindex < 1) {
> - close(progfd);
> - return -EINVAL;
> + err = -EINVAL;
> + goto cleanup;
> }
>
> if (argc) {
> @@ -587,8 +587,8 @@ static int do_attach(int argc, char **argv)
> overwrite = true;
> } else {
> p_err("expected 'overwrite', got: '%s'?", *argv);
> - close(progfd);
> - return -EINVAL;
> + err = -EINVAL;
> + goto cleanup;
> }
> }
>
> @@ -597,16 +597,19 @@ static int do_attach(int argc, char **argv)
> err = do_attach_detach_xdp(progfd, attach_type, ifindex,
> overwrite);
>
> - if (err < 0) {
> + if (err) {
> p_err("interface %s attach failed: %s",
> attach_type_strings[attach_type], strerror(-err));
> - return err;
> + goto cleanup;
> }
>
> if (json_output)
> jsonw_null(json_wtr);
>
> - return 0;
> + ret = 0;
> +cleanup:
> + close(progfd);
> + return err;
> }
>
LGTM. Send a v3.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-11-11 8:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-10 1:46 [PATCH v2 bpf] tools: bpftool: Add missing close before bpftool net attach exit Wang Hai
2020-11-10 4:33 ` John Fastabend
2020-11-10 8:40 ` wanghai (M)
2020-11-11 8:44 ` John Fastabend
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).