bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit
@ 2020-11-11 13:54 Wang Hai
  2020-11-11 14:43 ` Michal Rostecki
  2020-11-11 21:24 ` Daniel Borkmann
  0 siblings, 2 replies; 5+ messages in thread
From: Wang Hai @ 2020-11-11 13:54 UTC (permalink / raw)
  To: john.fastabend, quentin, mrostecki
  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>
---
v2->v3: add 'err = 0' before successful return
v1->v2: use cleanup tag instead of repeated closes
 tools/bpf/bpftool/net.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 910e7bac6e9e..f927392271cc 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;
 		}
 	}
 
@@ -597,16 +597,20 @@ 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;
+	err = 0;
+
+cleanup:
+	close(progfd);
+	return err;
 }
 
 static int do_detach(int argc, char **argv)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit
  2020-11-11 13:54 [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit Wang Hai
@ 2020-11-11 14:43 ` Michal Rostecki
  2020-11-11 21:24 ` Daniel Borkmann
  1 sibling, 0 replies; 5+ messages in thread
From: Michal Rostecki @ 2020-11-11 14:43 UTC (permalink / raw)
  To: Wang Hai, john.fastabend, quentin
  Cc: ast, daniel, kafai, songliubraving, yhs, andrii, kpsingh, toke,
	danieltimlee, bpf, netdev, linux-kernel

On 11/11/20 2:54 PM, 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>
> ---
> v2->v3: add 'err = 0' before successful return
> v1->v2: use cleanup tag instead of repeated closes
>   tools/bpf/bpftool/net.c | 18 +++++++++++-------
>   1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> index 910e7bac6e9e..f927392271cc 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;
>   		}
>   	}
>   
> @@ -597,16 +597,20 @@ 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;
> +	err = 0;
> +
> +cleanup:
> +	close(progfd);
> +	return err;
>   }
>   
>   static int do_detach(int argc, char **argv)
> 

LGTM, thanks!

Reviewed-By: Michal Rostecki <mrostecki@opensuse.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit
  2020-11-11 13:54 [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit Wang Hai
  2020-11-11 14:43 ` Michal Rostecki
@ 2020-11-11 21:24 ` Daniel Borkmann
  2020-11-11 22:37   ` Andrii Nakryiko
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Borkmann @ 2020-11-11 21:24 UTC (permalink / raw)
  To: Wang Hai, john.fastabend, quentin, mrostecki
  Cc: ast, kafai, songliubraving, yhs, andrii, kpsingh, toke,
	danieltimlee, bpf, netdev, linux-kernel

On 11/11/20 2:54 PM, 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>
> ---
> v2->v3: add 'err = 0' before successful return
> v1->v2: use cleanup tag instead of repeated closes
>   tools/bpf/bpftool/net.c | 18 +++++++++++-------
>   1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> index 910e7bac6e9e..f927392271cc 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;
>   		}
>   	}
>   
> @@ -597,16 +597,20 @@ 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;
> +	err = 0;

Why is the 'err = 0' still needed here given we test for err != 0 earlier?
Would just remove it, otherwise looks good.

> +cleanup:
> +	close(progfd);
> +	return err;
>   }
>   
>   static int do_detach(int argc, char **argv)
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit
  2020-11-11 21:24 ` Daniel Borkmann
@ 2020-11-11 22:37   ` Andrii Nakryiko
  2020-11-11 22:49     ` Andrii Nakryiko
  0 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2020-11-11 22:37 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Wang Hai, john fastabend, Quentin Monnet, Michal Rostecki,
	Alexei Starovoitov, Martin Lau, Song Liu, Yonghong Song,
	Andrii Nakryiko, KP Singh, Toke Høiland-Jørgensen,
	Daniel T. Lee, bpf, Networking, open list

On Wed, Nov 11, 2020 at 1:24 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> On 11/11/20 2:54 PM, 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>
> > ---
> > v2->v3: add 'err = 0' before successful return
> > v1->v2: use cleanup tag instead of repeated closes
> >   tools/bpf/bpftool/net.c | 18 +++++++++++-------
> >   1 file changed, 11 insertions(+), 7 deletions(-)
> >
> > diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> > index 910e7bac6e9e..f927392271cc 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;
> >               }
> >       }
> >
> > @@ -597,16 +597,20 @@ 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;
> > +     err = 0;
>
> Why is the 'err = 0' still needed here given we test for err != 0 earlier?
> Would just remove it, otherwise looks good.

This patch was already applied. Wang, can you please follow up with
another patch to address Daniel's feedback?

>
> > +cleanup:
> > +     close(progfd);
> > +     return err;
> >   }
> >
> >   static int do_detach(int argc, char **argv)
> >
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit
  2020-11-11 22:37   ` Andrii Nakryiko
@ 2020-11-11 22:49     ` Andrii Nakryiko
  0 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2020-11-11 22:49 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Wang Hai, john fastabend, Quentin Monnet, Michal Rostecki,
	Alexei Starovoitov, Martin Lau, Song Liu, Yonghong Song,
	Andrii Nakryiko, KP Singh, Toke Høiland-Jørgensen,
	Daniel T. Lee, bpf, Networking, open list

On Wed, Nov 11, 2020 at 2:37 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Wed, Nov 11, 2020 at 1:24 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
> >
> > On 11/11/20 2:54 PM, 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>
> > > ---
> > > v2->v3: add 'err = 0' before successful return
> > > v1->v2: use cleanup tag instead of repeated closes
> > >   tools/bpf/bpftool/net.c | 18 +++++++++++-------
> > >   1 file changed, 11 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> > > index 910e7bac6e9e..f927392271cc 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;
> > >               }
> > >       }
> > >
> > > @@ -597,16 +597,20 @@ 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;
> > > +     err = 0;
> >
> > Why is the 'err = 0' still needed here given we test for err != 0 earlier?
> > Would just remove it, otherwise looks good.
>
> This patch was already applied. Wang, can you please follow up with
> another patch to address Daniel's feedback?

Actually, the patch hasn't been applied yet, so please just respin, thanks.

>
> >
> > > +cleanup:
> > > +     close(progfd);
> > > +     return err;
> > >   }
> > >
> > >   static int do_detach(int argc, char **argv)
> > >
> >

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-11-12  1:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-11 13:54 [PATCH v3 bpf] tools: bpftool: Add missing close before bpftool net attach exit Wang Hai
2020-11-11 14:43 ` Michal Rostecki
2020-11-11 21:24 ` Daniel Borkmann
2020-11-11 22:37   ` Andrii Nakryiko
2020-11-11 22:49     ` Andrii Nakryiko

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).