lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAE2+fR992BprqDXkZTqi3Mgtq9WErSFvOtxvc16ZT9ufiBLNNQ@mail.gmail.com>
Date: Mon, 7 Apr 2025 09:10:18 +0530
From: malaya kumar rout <malayarout91@...il.com>
To: Hou Tao <houtao@...weicloud.com>
Cc: Andrii Nakryiko <andrii.nakryiko@...il.com>, Alexei Starovoitov <ast@...nel.org>, 
	Daniel Borkmann <daniel@...earbox.net>, Andrii Nakryiko <andrii@...nel.org>, 
	Martin KaFai Lau <martin.lau@...ux.dev>, Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>, 
	Yonghong Song <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>, 
	KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>, 
	Jiri Olsa <jolsa@...nel.org>, Mykola Lysenko <mykolal@...com>, Shuah Khan <shuah@...nel.org>, 
	Geliang Tang <geliang@...nel.org>, bpf@...r.kernel.org, linux-kselftest@...r.kernel.org, 
	linux-kernel@...r.kernel.org
Subject: Re: [PATCH] selftests/bpf: close the file descriptor to avoid
 resource leaks

On Mon, Apr 7, 2025 at 7:07 AM Hou Tao <houtao@...weicloud.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@...il.com> wrote:
> >> On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
> >> <malayarout91@...il.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@...il.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
> >>>
> > .
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ