* [PATCH] genksyms: fix file pointer leaks on error paths
@ 2026-01-17 10:26 Weigang He
2026-01-20 11:48 ` Petr Pavlu
0 siblings, 1 reply; 2+ messages in thread
From: Weigang He @ 2026-01-17 10:26 UTC (permalink / raw)
To: masahiroy; +Cc: petr.pavlu, sam, linux-kernel, Weigang He
The option parsing loop processes multiple command-line options
sequentially. When -r or -T options successfully open their files,
but a later option causes an early return (e.g., -h for help, an
unknown option, or a failed fopen), the previously opened file
pointers are leaked.
Fix this by closing ref_file and dumpfile before each early return
in the option parsing switch statement.
Fixes: 64e6c1e12372 ("genksyms: track symbol checksum changes")
Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
---
scripts/genksyms/genksyms.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c
index 83e48670c2fcf..dee9216797935 100644
--- a/scripts/genksyms/genksyms.c
+++ b/scripts/genksyms/genksyms.c
@@ -776,6 +776,8 @@ int main(int argc, char **argv)
ref_file = fopen(optarg, "r");
if (!ref_file) {
perror(optarg);
+ if (dumpfile)
+ fclose(dumpfile);
return 1;
}
break;
@@ -784,6 +786,8 @@ int main(int argc, char **argv)
dumpfile = fopen(optarg, "w");
if (!dumpfile) {
perror(optarg);
+ if (ref_file)
+ fclose(ref_file);
return 1;
}
break;
@@ -792,9 +796,17 @@ int main(int argc, char **argv)
break;
case 'h':
genksyms_usage();
+ if (ref_file)
+ fclose(ref_file);
+ if (dumpfile)
+ fclose(dumpfile);
return 0;
default:
genksyms_usage();
+ if (ref_file)
+ fclose(ref_file);
+ if (dumpfile)
+ fclose(dumpfile);
return 1;
}
{
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] genksyms: fix file pointer leaks on error paths
2026-01-17 10:26 [PATCH] genksyms: fix file pointer leaks on error paths Weigang He
@ 2026-01-20 11:48 ` Petr Pavlu
0 siblings, 0 replies; 2+ messages in thread
From: Petr Pavlu @ 2026-01-20 11:48 UTC (permalink / raw)
To: Weigang He; +Cc: masahiroy, sam, linux-kernel
On 1/17/26 11:26 AM, Weigang He wrote:
> The option parsing loop processes multiple command-line options
> sequentially. When -r or -T options successfully open their files,
> but a later option causes an early return (e.g., -h for help, an
> unknown option, or a failed fopen), the previously opened file
> pointers are leaked.
>
> Fix this by closing ref_file and dumpfile before each early return
> in the option parsing switch statement.
>
> Fixes: 64e6c1e12372 ("genksyms: track symbol checksum changes")
> Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
> ---
> scripts/genksyms/genksyms.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c
> index 83e48670c2fcf..dee9216797935 100644
> --- a/scripts/genksyms/genksyms.c
> +++ b/scripts/genksyms/genksyms.c
> @@ -776,6 +776,8 @@ int main(int argc, char **argv)
> ref_file = fopen(optarg, "r");
> if (!ref_file) {
> perror(optarg);
> + if (dumpfile)
> + fclose(dumpfile);
> return 1;
> }
> break;
> @@ -784,6 +786,8 @@ int main(int argc, char **argv)
> dumpfile = fopen(optarg, "w");
> if (!dumpfile) {
> perror(optarg);
> + if (ref_file)
> + fclose(ref_file);
> return 1;
> }
> break;
> @@ -792,9 +796,17 @@ int main(int argc, char **argv)
> break;
> case 'h':
> genksyms_usage();
> + if (ref_file)
> + fclose(ref_file);
> + if (dumpfile)
> + fclose(dumpfile);
> return 0;
> default:
> genksyms_usage();
> + if (ref_file)
> + fclose(ref_file);
> + if (dumpfile)
> + fclose(dumpfile);
> return 1;
> }
> {
Genksyms is a utility used exclusively during the kernel and module
build process. My understanding is that it is designed to exit
immediately upon encountering an error and leaves the cleanup of memory
and open files to the kernel. Notice the use of xmalloc(), xstrdup() and
exit(1). This is intentional to keep the code simple.
--
Cheers,
Petr
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-01-20 11:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-17 10:26 [PATCH] genksyms: fix file pointer leaks on error paths Weigang He
2026-01-20 11:48 ` Petr Pavlu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®