Skip to contentSkip to conversation

tools: bpftool: Add missing close before bpftool net attach exit

PATCH Superseded Wang Hai 4 participants 5 messages Nov 11, 2020 – Nov 12, 2020

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wang Hai

author
#0 lore raw

PATCH v3 1/1 tools: bpftool: Add missing close before bpftool net attach exit

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

---
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(-)
tools/bpf/bpftool/net.c +11 -7
index 910e7bac6e9e..f927392271cc 100644
@@ -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)
Signature · 1 line
2.17.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 52 lines · Wang Hai

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

---
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(-)
tools/bpf/bpftool/net.c +6 -6
index 910e7bac6e9e..f927392271cc 100644
@@ -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);

tools/bpf/bpftool/net.c static int do_attach(int argc, char **argv) · line 616

Quoted 10 lines
tools/bpf/bpftool/net.c +5 -1
 
-	return 0;
+	err = 0;
+
+cleanup:
+	close(progfd);
+	return err;
  }
 
  static int do_detach(int argc, char **argv)

LGTM, thanks!

maintainer
#2 lore raw
In reply to Wang Hai
Quoted 44 lines · Wang Hai

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

---
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(-)
tools/bpf/bpftool/net.c +5 -5
index 910e7bac6e9e..f927392271cc 100644
@@ -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",

tools/bpf/bpftool/net.c static int do_attach(int argc, char **argv) · line 609

Quoted 10 lines
tools/bpf/bpftool/net.c +2 -2
  		      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.

Quoted 7 lines
tools/bpf/bpftool/net.c +3 -0
+cleanup:
+	close(progfd);
+	return err;
  }
 
  static int do_detach(int argc, char **argv)
#3 lore raw
Quoted 51 lines · Daniel Borkmann
Quoted 49 lines · Wang Hai

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

---
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(-)
tools/bpf/bpftool/net.c +6 -6
index 910e7bac6e9e..f927392271cc 100644
@@ -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;
      }
 
Quoted 8 lines

tools/bpf/bpftool/net.c static int do_attach(int argc, char **argv) · line 609

Quoted 5 lines
tools/bpf/bpftool/net.c +1 -1
      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?

Quoted 9 lines
Quoted 8 lines
tools/bpf/bpftool/net.c +3 -0
+cleanup:
+     close(progfd);
+     return err;
  }
 
  static int do_detach(int argc, char **argv)
#4 lore raw
Quoted 56 lines · Andrii Nakryiko
Quoted 54 lines · Daniel Borkmann
Quoted 52 lines · Wang Hai

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

---
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(-)
tools/bpf/bpftool/net.c +6 -6
index 910e7bac6e9e..f927392271cc 100644
@@ -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);
 
Quoted 8 lines
Quoted 5 lines

tools/bpf/bpftool/net.c static int do_attach(int argc, char **argv) · line 609

Quoted 2 lines
tools/bpf/bpftool/net.c +1 -1
-     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.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
tools/bpf/bpftool/net.c +3 -0
+cleanup:
+     close(progfd);
+     return err;
  }
 
  static int do_detach(int argc, char **argv)