Skip to contentSkip to conversation

x86: mkpiggy.c: fix resource leakage

PATCH In review Geyslan G. Bem 3 participants 5 messages Oct 7, 2013 – Oct 8, 2013 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: mkpiggy.c: fix resource leakage

Even though the resource is released when the application is closed or when returned from main function, modified to make it in accordance. Now FILE pointer is always verified.

---
 arch/x86/boot/compressed/mkpiggy.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/mkpiggy.c +10 -6
index 958a641..b669ab6 100644
@@ -36,11 +36,12 @@ int main(int argc, char *argv[])
 	uint32_t olen;
 	long ilen;
 	unsigned long offs;
-	FILE *f;
+	FILE *f = NULL;
+	int retval = 1;
 
 	if (argc < 2) {
 		fprintf(stderr, "Usage: %s compressed_file\n", argv[0]);
-		return 1;
+		goto bail;
 	}
 
 	/* Get the information for the compressed kernel image first */
@@ -48,7 +49,7 @@ int main(int argc, char *argv[])
 	f = fopen(argv[1], "r");
 	if (!f) {
 		perror(argv[1]);
-		return 1;
+		goto bail;
 	}
 
 
@@ -58,12 +59,11 @@ int main(int argc, char *argv[])
 
 	if (fread(&olen, sizeof(olen), 1, f) != 1) {
 		perror(argv[1]);
-		return 1;
+		goto bail;
 	}
 
 	ilen = ftell(f);
 	olen = get_unaligned_le32(&olen);
-	fclose(f);
 
 	/*
 	 * Now we have the input (compressed) and output (uncompressed)
@@ -91,5 +91,9 @@ int main(int argc, char *argv[])
 	printf(".incbin \"%s\"\n", argv[1]);
 	printf("input_data_end:\n");
 
-	return 0;
+	retval = 0;
+bail:
+	if (f)
+		fclose(f);
+	return retval;
 }
Signature · 1 line
1.8.4
#1 lore raw
Quoted 5 lines · Geyslan G. Bem

Even though the resource is released when the application is closed or when returned from main function, modified to make it in accordance. Now FILE pointer is always verified.

Perhaps you could explain what problem is solved by this patch?

Note that for correctness we really should check the return value from fclose().

-hpa

#2 lore raw
In reply to Geyslan G. Bem
Quoted 3 lines · Geyslan G. Bem

Even though the resource is released when the application is closed or when returned from main function, modified to make it in accordance. Now FILE pointer is always verified.

By the way, the subject is misleading - there is no resource leakage since the program is terminated anyway.

-hpa

#3 lore raw

Peter,

Yes, Really doesn't have leakage, I explained it. The changes are just to cleanup the code following a secure pattern.

Geyslan Gregório Bem
hackingbits.com

2013/10/7 H. Peter Anvin <[email protected]>:

Quoted 11 lines · Geyslan G. Bem
Quoted 3 lines

Even though the resource is released when the application is closed or when returned from main function, modified to make it in accordance. Now FILE pointer is always verified.

By the way, the subject is misleading - there is no resource leakage since the program is terminated anyway.

-hpa

#4 lore raw

[tip:x86/cleanups] x86: mkpiggy.c: Explicitly close the output file

In reply to Geyslan G. Bem
x86: mkpiggy.c: Explicitly close the output file
Commit-ID:  49449c30c4d1514486364d1e0dbea6938914b86f
Gitweb:     http://git.kernel.org/tip/49449c30c4d1514486364d1e0dbea6938914b86f
Author:     Geyslan G. Bem <[email protected]>
AuthorDate: Mon, 7 Oct 2013 19:16:59 -0300
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Tue, 8 Oct 2013 11:36:09 -0700

x86: mkpiggy.c: Explicitly close the output file

Even though the resource is released when the application is closed or when returned from main function, modify the code to make it obvious, and to keep static analysis tools from complaining.

---
 arch/x86/boot/compressed/mkpiggy.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/mkpiggy.c +10 -6
index 958a641..b669ab6 100644
@@ -36,11 +36,12 @@ int main(int argc, char *argv[])
 	uint32_t olen;
 	long ilen;
 	unsigned long offs;
-	FILE *f;
+	FILE *f = NULL;
+	int retval = 1;
 
 	if (argc < 2) {
 		fprintf(stderr, "Usage: %s compressed_file\n", argv[0]);
-		return 1;
+		goto bail;
 	}
 
 	/* Get the information for the compressed kernel image first */
@@ -48,7 +49,7 @@ int main(int argc, char *argv[])
 	f = fopen(argv[1], "r");
 	if (!f) {
 		perror(argv[1]);
-		return 1;
+		goto bail;
 	}
 
 
@@ -58,12 +59,11 @@ int main(int argc, char *argv[])
 
 	if (fread(&olen, sizeof(olen), 1, f) != 1) {
 		perror(argv[1]);
-		return 1;
+		goto bail;
 	}
 
 	ilen = ftell(f);
 	olen = get_unaligned_le32(&olen);
-	fclose(f);
 
 	/*
 	 * Now we have the input (compressed) and output (uncompressed)
@@ -91,5 +91,9 @@ int main(int argc, char *argv[])
 	printf(".incbin \"%s\"\n", argv[1]);
 	printf("input_data_end:\n");
 
-	return 0;
+	retval = 0;
+bail:
+	if (f)
+		fclose(f);
+	return retval;
 }