Skip to contentSkip to conversation

genksyms: fix file pointer leaks on error paths

PATCH In review Weigang He 2 participants 2 messages Jan 17 – Jan 20

lore
Share & tools
lore

Weigang He

author
#0 lore raw

PATCH 1/1 genksyms: fix file pointer leaks on error paths

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")
---
 scripts/genksyms/genksyms.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
scripts/genksyms/genksyms.c +12 -0
index 83e48670c2fcf..dee9216797935 100644
@@ -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;
 		}
 	{
Signature · 1 line
2.34.1
#1 lore raw
Quoted 45 lines · 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")
---
 scripts/genksyms/genksyms.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
scripts/genksyms/genksyms.c +8 -0
index 83e48670c2fcf..dee9216797935 100644
@@ -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);

scripts/genksyms/genksyms.c int main(int argc, char **argv) · line 812

Quoted 10 lines
scripts/genksyms/genksyms.c +4 -0
 			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.

Signature · 2 lines
Cheers,
Petr