Skip to contentSkip to conversation

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

PATCH Superseded Wang Hai 2 participants 4 messages Nov 10, 2020 – Nov 11, 2020

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wang Hai

author
#0 lore raw

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

---
v1->v2: use cleanup tag instead of repeated closes

 tools/bpf/bpftool/net.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/bpf/bpftool/net.c +8 -6
index 910e7bac6e9e..1ac7228167e6 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;
 		}
 	}
 
@@ -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)
Signature · 1 line
2.17.1
#1 lore raw

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

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

---
v1->v2: use cleanup tag instead of repeated closes

 tools/bpf/bpftool/net.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/bpf/bpftool/net.c +4 -4
index 910e7bac6e9e..1ac7228167e6 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;
 		}
 	}
 
@@ -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.

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

Quoted 1 line
tools/bpf/bpftool/net.c +0 -0
 	if (err < 0) {
        ^^^^^^^^^^^^
        if (err) {

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

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

Quoted 9 lines
tools/bpf/bpftool/net.c +3 -0
+cleanup:
+	close(progfd);
+	return err;
 }
 
 static int do_detach(int argc, char **argv)
Signature · 1 line
2.17.1
#2 lore raw
Quoted 37 lines · John Fastabend
Quoted 36 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")

---
v1->v2: use cleanup tag instead of repeated closes

  tools/bpf/bpftool/net.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
tools/bpf/bpftool/net.c +4 -4
index 910e7bac6e9e..1ac7228167e6 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;
  		}
  	}
 
Quoted 4 lines

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

Quoted 1 line
tools/bpf/bpftool/net.c +0 -0
@@ -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.

Quoted 10 lines

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

Quoted 1 line
tools/bpf/bpftool/net.c +0 -0
  	if (err < 0) {
         ^^^^^^^^^^^^
         if (err) {
Quoted 6 lines
tools/bpf/bpftool/net.c +1 -1
  		p_err("interface %s attach failed: %s",
  		      attach_type_strings[attach_type], strerror(-err));
-		return err;
+		goto cleanup;
  	}
 
Quoted 7 lines

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

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

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

Can it be fixed like this?

tools/bpf/bpftool/net.c +0 -0
@@ -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;
 }

Quoted 3 lines

.

#3 lore raw
Quoted 9 lines · wanghai (M)
Quoted 7 lines · John Fastabend
Quoted 6 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")

---

[...]

Quoted 51 lines
Quoted 2 lines

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.

Quoted 9 lines
Quoted 9 lines
(quoted diff) +3 -0
+cleanup:
+	close(progfd);
+	return err;
  }
 
  static int do_detach(int argc, char **argv)
Signature · 1 line
2.17.1

Can it be fixed like this?

tools/bpf/bpftool/net.c +6 -6
@@ -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;
         }
 

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

Quoted 10 lines
tools/bpf/bpftool/net.c +4 -1
         if (json_output)
                 jsonw_null(json_wtr);
 
-       return 0;
+       ret = 0;
+cleanup:
+       close(progfd);
+       return err;
  }
 

LGTM. Send a v3.