* [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
@ 2025-04-12 18:38 Malaya Kumar Rout
2025-04-15 18:11 ` Martin KaFai Lau
0 siblings, 1 reply; 10+ messages in thread
From: Malaya Kumar Rout @ 2025-04-12 18:38 UTC (permalink / raw)
To: andrii.nakryiko, alexei.starovoitov; +Cc: bpf, Malaya Kumar Rout
Static analysis found an issue in bench_htab_mem.c and sk_assign.c
cppcheck output before this patch:
tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3: error: Resource leak: fd [resourceLeak]
tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3: error: Resource leak: tc [resourceLeak]
cppcheck output after this patch:
No resource leaks found
Fix the issue by closing the file descriptors fd and tc.
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 3 +--
tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
index 926ee822143e..297e32390cd1 100644
--- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
+++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
@@ -279,6 +279,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
}
got = read(fd, buf, sizeof(buf) - 1);
+ close(fd);
if (got <= 0) {
*value = 0;
return;
@@ -286,8 +287,6 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
buf[got] = 0;
*value = strtoull(buf, NULL, 0);
-
- close(fd);
}
static void htab_mem_measure(struct bench_res *res)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 0b9bd1d6f7cc..05cf66265cf1 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -37,8 +37,10 @@ configure_stack(void)
tc = popen("tc -V", "r");
if (CHECK_FAIL(!tc))
return false;
- if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
+ if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
+ close(tc);
return false;
+ }
if (strstr(tc_version, ", libbpf "))
prog = "test_sk_assign_libbpf.bpf.o";
else
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-04-12 18:38 [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks Malaya Kumar Rout
@ 2025-04-15 18:11 ` Martin KaFai Lau
0 siblings, 0 replies; 10+ messages in thread
From: Martin KaFai Lau @ 2025-04-15 18:11 UTC (permalink / raw)
To: Malaya Kumar Rout; +Cc: bpf, andrii.nakryiko, alexei.starovoitov
On 4/12/25 11:38 AM, Malaya Kumar Rout wrote:
> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> index 0b9bd1d6f7cc..05cf66265cf1 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> @@ -37,8 +37,10 @@ configure_stack(void)
> tc = popen("tc -V", "r");
> if (CHECK_FAIL(!tc))
> return false;
> - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
> + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
> + close(tc);
It is not even compiler tested.
pw-bot: cr
> return false;
> + }
> if (strstr(tc_version, ", libbpf "))
> prog = "test_sk_assign_libbpf.bpf.o";
> else
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
@ 2025-04-21 17:44 Malaya Kumar Rout
2025-04-22 21:40 ` patchwork-bot+netdevbpf
0 siblings, 1 reply; 10+ messages in thread
From: Malaya Kumar Rout @ 2025-04-21 17:44 UTC (permalink / raw)
To: andrii.nakryiko, alexei.starovoitov; +Cc: bpf, Malaya Kumar Rout
Static analysis found an issue in bench_htab_mem.c and sk_assign.c
cppcheck output before this patch:
tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3: error: Resource leak: fd [resourceLeak]
tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3: error: Resource leak: tc [resourceLeak]
cppcheck output after this patch:
No resource leaks found
Fix the issue by closing the file descriptors fd and tc.
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 3 +--
tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
index 926ee822143e..297e32390cd1 100644
--- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
+++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
@@ -279,6 +279,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
}
got = read(fd, buf, sizeof(buf) - 1);
+ close(fd);
if (got <= 0) {
*value = 0;
return;
@@ -286,8 +287,6 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
buf[got] = 0;
*value = strtoull(buf, NULL, 0);
-
- close(fd);
}
static void htab_mem_measure(struct bench_res *res)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 0b9bd1d6f7cc..10a0ab954b8a 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -37,8 +37,10 @@ configure_stack(void)
tc = popen("tc -V", "r");
if (CHECK_FAIL(!tc))
return false;
- if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
+ if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
+ pclose(tc);
return false;
+ }
if (strstr(tc_version, ", libbpf "))
prog = "test_sk_assign_libbpf.bpf.o";
else
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-04-21 17:44 Malaya Kumar Rout
@ 2025-04-22 21:40 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 10+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-04-22 21:40 UTC (permalink / raw)
To: malaya kumar rout; +Cc: andrii.nakryiko, alexei.starovoitov, bpf
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Mon, 21 Apr 2025 23:14:05 +0530 you wrote:
> Static analysis found an issue in bench_htab_mem.c and sk_assign.c
>
> cppcheck output before this patch:
> tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3: error: Resource leak: fd [resourceLeak]
> tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3: error: Resource leak: tc [resourceLeak]
>
> cppcheck output after this patch:
> No resource leaks found
>
> [...]
Here is the summary with links:
- selftests/bpf: close the file descriptor to avoid resource leaks
https://git.kernel.org/bpf/bpf-next/c/be2fea9c07d4
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
@ 2025-03-24 6:42 Malaya Kumar Rout
2025-04-01 6:46 ` Hou Tao
2025-04-04 15:52 ` Andrii Nakryiko
0 siblings, 2 replies; 10+ messages in thread
From: Malaya Kumar Rout @ 2025-03-24 6:42 UTC (permalink / raw)
To: malayarout91
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Mykola Lysenko, Shuah Khan, Geliang Tang, bpf, linux-kselftest,
linux-kernel
Static Analyis for bench_htab_mem.c with cppcheck:error
tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
error: Resource leak: fd [resourceLeak]
tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
error: Resource leak: tc [resourceLeak]
fix the issue by closing the file descriptor (fd & tc) when
read & fgets operation fails.
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
index 926ee822143e..59746fd2c23a 100644
--- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
+++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
@@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
got = read(fd, buf, sizeof(buf) - 1);
if (got <= 0) {
*value = 0;
+ close(fd);
return;
}
buf[got] = 0;
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 0b9bd1d6f7cc..10a0ab954b8a 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -37,8 +37,10 @@ configure_stack(void)
tc = popen("tc -V", "r");
if (CHECK_FAIL(!tc))
return false;
- if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
+ if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
+ pclose(tc);
return false;
+ }
if (strstr(tc_version, ", libbpf "))
prog = "test_sk_assign_libbpf.bpf.o";
else
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-03-24 6:42 Malaya Kumar Rout
@ 2025-04-01 6:46 ` Hou Tao
2025-04-04 15:52 ` Andrii Nakryiko
1 sibling, 0 replies; 10+ messages in thread
From: Hou Tao @ 2025-04-01 6:46 UTC (permalink / raw)
To: Malaya Kumar Rout
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Mykola Lysenko, Shuah Khan, Geliang Tang, bpf, linux-kselftest,
linux-kernel
On 3/24/2025 2:42 PM, Malaya Kumar Rout wrote:
> Static Analyis for bench_htab_mem.c with cppcheck:error
> tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
> error: Resource leak: fd [resourceLeak]
> tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
> error: Resource leak: tc [resourceLeak]
>
> fix the issue by closing the file descriptor (fd & tc) when
> read & fgets operation fails.
>
> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
Acked-by: Hou Tao <houtao1@huawei.com>
The right subject prefix for the patch should be "[PATCH bpf-next]",
however, it seems there is no need or no reason to respin the patch.
> ---
> tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
> tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
> 2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> index 926ee822143e..59746fd2c23a 100644
> --- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> +++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> @@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
> got = read(fd, buf, sizeof(buf) - 1);
> if (got <= 0) {
> *value = 0;
> + close(fd);
> return;
> }
> buf[got] = 0;
> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> index 0b9bd1d6f7cc..10a0ab954b8a 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> @@ -37,8 +37,10 @@ configure_stack(void)
> tc = popen("tc -V", "r");
> if (CHECK_FAIL(!tc))
> return false;
> - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
> + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
> + pclose(tc);
> return false;
> + }
> if (strstr(tc_version, ", libbpf "))
> prog = "test_sk_assign_libbpf.bpf.o";
> else
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-03-24 6:42 Malaya Kumar Rout
2025-04-01 6:46 ` Hou Tao
@ 2025-04-04 15:52 ` Andrii Nakryiko
2025-04-05 5:59 ` malaya kumar rout
1 sibling, 1 reply; 10+ messages in thread
From: Andrii Nakryiko @ 2025-04-04 15:52 UTC (permalink / raw)
To: Malaya Kumar Rout
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Mykola Lysenko, Shuah Khan, Geliang Tang, bpf, linux-kselftest,
linux-kernel
On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
<malayarout91@gmail.com> wrote:
>
> Static Analyis for bench_htab_mem.c with cppcheck:error
> tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
> error: Resource leak: fd [resourceLeak]
> tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
> error: Resource leak: tc [resourceLeak]
>
> fix the issue by closing the file descriptor (fd & tc) when
> read & fgets operation fails.
>
> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> ---
> tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
> tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
> 2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> index 926ee822143e..59746fd2c23a 100644
> --- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> +++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> @@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
> got = read(fd, buf, sizeof(buf) - 1);
It could be a bit cleaner to add close(fd) here and drop the one we
have at the end of the function.
pw-bot: cr
> if (got <= 0) {
> *value = 0;
> + close(fd);
> return;
> }
> buf[got] = 0;
> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> index 0b9bd1d6f7cc..10a0ab954b8a 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> @@ -37,8 +37,10 @@ configure_stack(void)
> tc = popen("tc -V", "r");
> if (CHECK_FAIL(!tc))
> return false;
> - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
> + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
> + pclose(tc);
this one looks good
> return false;
> + }
> if (strstr(tc_version, ", libbpf "))
> prog = "test_sk_assign_libbpf.bpf.o";
> else
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-04-04 15:52 ` Andrii Nakryiko
@ 2025-04-05 5:59 ` malaya kumar rout
2025-04-07 1:36 ` Hou Tao
0 siblings, 1 reply; 10+ messages in thread
From: malaya kumar rout @ 2025-04-05 5:59 UTC (permalink / raw)
To: Andrii Nakryiko
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Mykola Lysenko, Shuah Khan, Geliang Tang, bpf, linux-kselftest,
linux-kernel
On Fri, Apr 4, 2025 at 9:22 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
> <malayarout91@gmail.com> wrote:
> >
> > Static Analyis for bench_htab_mem.c with cppcheck:error
> > tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
> > error: Resource leak: fd [resourceLeak]
> > tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
> > error: Resource leak: tc [resourceLeak]
> >
> > fix the issue by closing the file descriptor (fd & tc) when
> > read & fgets operation fails.
> >
> > Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> > ---
> > tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
> > tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
> > 2 files changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> > index 926ee822143e..59746fd2c23a 100644
> > --- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> > +++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> > @@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
> > got = read(fd, buf, sizeof(buf) - 1);
>
> It could be a bit cleaner to add close(fd) here and drop the one we
> have at the end of the function.
>
Here, close(fd) is now positioned within the error handling block,
guaranteeing that
the file descriptor will be closed prior to returning from the
function in the event of a read error.
Meanwhile, the final close(fd) at the end of the function is retained
for successful execution,
thereby avoiding any potential resource leaks.
Hence, It is essential to add the close(fd) in both locations to
prevent resource leakage.
> pw-bot: cr
>
> > if (got <= 0) {
> > *value = 0;
> > + close(fd);
> > return;
> > }
> > buf[got] = 0;
> > diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> > index 0b9bd1d6f7cc..10a0ab954b8a 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> > @@ -37,8 +37,10 @@ configure_stack(void)
> > tc = popen("tc -V", "r");
> > if (CHECK_FAIL(!tc))
> > return false;
> > - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
> > + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
> > + pclose(tc);
>
> this one looks good
>
> > return false;
> > + }
> > if (strstr(tc_version, ", libbpf "))
> > prog = "test_sk_assign_libbpf.bpf.o";
> > else
> > --
> > 2.43.0
> >
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-04-05 5:59 ` malaya kumar rout
@ 2025-04-07 1:36 ` Hou Tao
2025-04-07 3:40 ` malaya kumar rout
0 siblings, 1 reply; 10+ messages in thread
From: Hou Tao @ 2025-04-07 1:36 UTC (permalink / raw)
To: malaya kumar rout, Andrii Nakryiko
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Mykola Lysenko, Shuah Khan, Geliang Tang, bpf, linux-kselftest,
linux-kernel
Hi,
On 4/5/2025 1:59 PM, malaya kumar rout wrote:
> On Fri, Apr 4, 2025 at 9:22 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
>> On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
>> <malayarout91@gmail.com> wrote:
>>> Static Analyis for bench_htab_mem.c with cppcheck:error
>>> tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
>>> error: Resource leak: fd [resourceLeak]
>>> tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
>>> error: Resource leak: tc [resourceLeak]
>>>
>>> fix the issue by closing the file descriptor (fd & tc) when
>>> read & fgets operation fails.
>>>
>>> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
>>> ---
>>> tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
>>> tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
>>> 2 files changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
>>> index 926ee822143e..59746fd2c23a 100644
>>> --- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
>>> +++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
>>> @@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
>>> got = read(fd, buf, sizeof(buf) - 1);
>> It could be a bit cleaner to add close(fd) here and drop the one we
>> have at the end of the function.
>>
> Here, close(fd) is now positioned within the error handling block,
> guaranteeing that
> the file descriptor will be closed prior to returning from the
> function in the event of a read error.
> Meanwhile, the final close(fd) at the end of the function is retained
> for successful execution,
> thereby avoiding any potential resource leaks.
> Hence, It is essential to add the close(fd) in both locations to
> prevent resource leakage.
I think Andrii was proposing the following solution:
{
/* ...... */
got = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (got <= 0) {
*value = 0;
return;
}
buf[got] = 0;
*value = strtoull(buf, NULL, 0);
}
It only invokes close(fd) once to handle both the failed case and the
successful case.
>
>> pw-bot: cr
>>
>>> if (got <= 0) {
>>> *value = 0;
>>> + close(fd);
>>> return;
>>> }
>>> buf[got] = 0;
>>> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
>>> index 0b9bd1d6f7cc..10a0ab954b8a 100644
>>> --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
>>> +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
>>> @@ -37,8 +37,10 @@ configure_stack(void)
>>> tc = popen("tc -V", "r");
>>> if (CHECK_FAIL(!tc))
>>> return false;
>>> - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
>>> + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
>>> + pclose(tc);
>> this one looks good
>>
>>> return false;
>>> + }
>>> if (strstr(tc_version, ", libbpf "))
>>> prog = "test_sk_assign_libbpf.bpf.o";
>>> else
>>> --
>>> 2.43.0
>>>
> .
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
2025-04-07 1:36 ` Hou Tao
@ 2025-04-07 3:40 ` malaya kumar rout
0 siblings, 0 replies; 10+ messages in thread
From: malaya kumar rout @ 2025-04-07 3:40 UTC (permalink / raw)
To: Hou Tao
Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
Hao Luo, Jiri Olsa, Mykola Lysenko, Shuah Khan, Geliang Tang, bpf,
linux-kselftest, linux-kernel
On Mon, Apr 7, 2025 at 7:07 AM Hou Tao <houtao@huaweicloud.com> wrote:
>
> Hi,
>
> On 4/5/2025 1:59 PM, malaya kumar rout wrote:
> > On Fri, Apr 4, 2025 at 9:22 PM Andrii Nakryiko
> > <andrii.nakryiko@gmail.com> wrote:
> >> On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
> >> <malayarout91@gmail.com> wrote:
> >>> Static Analyis for bench_htab_mem.c with cppcheck:error
> >>> tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
> >>> error: Resource leak: fd [resourceLeak]
> >>> tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
> >>> error: Resource leak: tc [resourceLeak]
> >>>
> >>> fix the issue by closing the file descriptor (fd & tc) when
> >>> read & fgets operation fails.
> >>>
> >>> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> >>> ---
> >>> tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
> >>> tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
> >>> 2 files changed, 4 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> >>> index 926ee822143e..59746fd2c23a 100644
> >>> --- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> >>> +++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> >>> @@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
> >>> got = read(fd, buf, sizeof(buf) - 1);
> >> It could be a bit cleaner to add close(fd) here and drop the one we
> >> have at the end of the function.
> >>
> > Here, close(fd) is now positioned within the error handling block,
> > guaranteeing that
> > the file descriptor will be closed prior to returning from the
> > function in the event of a read error.
> > Meanwhile, the final close(fd) at the end of the function is retained
> > for successful execution,
> > thereby avoiding any potential resource leaks.
> > Hence, It is essential to add the close(fd) in both locations to
> > prevent resource leakage.
>
> I think Andrii was proposing the following solution:
>
> {
> /* ...... */
> got = read(fd, buf, sizeof(buf) - 1);
> close(fd);
> if (got <= 0) {
> *value = 0;
> return;
> }
> buf[got] = 0;
>
> *value = strtoull(buf, NULL, 0);
> }
>
> It only invokes close(fd) once to handle both the failed case and the
> successful case.
> >
I greatly appreciate your insightful feedback on the review.
I will proceed to share the updated patch that includes the modifications.
> >> pw-bot: cr
> >>
> >>> if (got <= 0) {
> >>> *value = 0;
> >>> + close(fd);
> >>> return;
> >>> }
> >>> buf[got] = 0;
> >>> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> >>> index 0b9bd1d6f7cc..10a0ab954b8a 100644
> >>> --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> >>> +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> >>> @@ -37,8 +37,10 @@ configure_stack(void)
> >>> tc = popen("tc -V", "r");
> >>> if (CHECK_FAIL(!tc))
> >>> return false;
> >>> - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
> >>> + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
> >>> + pclose(tc);
> >> this one looks good
> >>
> >>> return false;
> >>> + }
> >>> if (strstr(tc_version, ", libbpf "))
> >>> prog = "test_sk_assign_libbpf.bpf.o";
> >>> else
> >>> --
> >>> 2.43.0
> >>>
> > .
>
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2025-04-22 21:39 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-12 18:38 [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks Malaya Kumar Rout
2025-04-15 18:11 ` Martin KaFai Lau
-- strict thread matches above, loose matches on Subject: below --
2025-04-21 17:44 Malaya Kumar Rout
2025-04-22 21:40 ` patchwork-bot+netdevbpf
2025-03-24 6:42 Malaya Kumar Rout
2025-04-01 6:46 ` Hou Tao
2025-04-04 15:52 ` Andrii Nakryiko
2025-04-05 5:59 ` malaya kumar rout
2025-04-07 1:36 ` Hou Tao
2025-04-07 3:40 ` malaya kumar rout
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).