Skip to contentSkip to conversation

pmdinfogen: fix resource leak of FILE object

PATCH Applied Bruce Richardson 3 participants 6 messages Feb 2, 2018 – Feb 6, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pmdinfogen: fix resource leak of FILE object

Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case.

The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates.

As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls.

Coverity issue: 260399

Fixes 0d68533617e3 ("pmdinfogen: allow using stdin and stdout")
---
 buildtools/pmdinfogen/pmdinfogen.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
buildtools/pmdinfogen/pmdinfogen.c +10 -6
index 45b267346..0f35ca46b 100644
@@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size)
 		/* from stdin, use a temporary file to mmap */
 		FILE *infile;
 		char buffer[1024];
-		size_t n;
+		int n;
 
 		infile = tmpfile();
 		if (infile == NULL) {
 			perror("tmpfile");
 			return NULL;
 		}
-		while (!feof(stdin)) {
-			n = fread(buffer, 1, sizeof(buffer), stdin);
-			if (fwrite(buffer, 1, n, infile) != n)
+		fd = dup(fileno(infile));
+		fclose(infile);
+		if (fd < 0)
+			return NULL;
+
+		n = read(STDIN_FILENO, buffer, sizeof(buffer));
+		while (n > 0) {
+			if (write(fd, buffer, n) != n)
 				goto failed;
+			n = read(STDIN_FILENO, buffer, sizeof(buffer));
 		}
-		fflush(infile);
-		fd = fileno(infile);
 	}
 
 	if (fstat(fd, &st))
Signature · 1 line
2.14.3
#1 lore raw
Quoted 60 lines · Bruce Richardson

Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case.

The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates.

As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls.

Coverity issue: 260399

Fixes 0d68533617e3 ("pmdinfogen: allow using stdin and stdout")
---
 buildtools/pmdinfogen/pmdinfogen.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
buildtools/pmdinfogen/pmdinfogen.c +10 -6
index 45b267346..0f35ca46b 100644
@@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size)
 		/* from stdin, use a temporary file to mmap */
 		FILE *infile;
 		char buffer[1024];
-		size_t n;
+		int n;
 
 		infile = tmpfile();
 		if (infile == NULL) {
 			perror("tmpfile");
 			return NULL;
 		}
-		while (!feof(stdin)) {
-			n = fread(buffer, 1, sizeof(buffer), stdin);
-			if (fwrite(buffer, 1, n, infile) != n)
+		fd = dup(fileno(infile));
+		fclose(infile);
+		if (fd < 0)
+			return NULL;
+
+		n = read(STDIN_FILENO, buffer, sizeof(buffer));
+		while (n > 0) {
+			if (write(fd, buffer, n) != n)
 				goto failed;
+			n = read(STDIN_FILENO, buffer, sizeof(buffer));
 		}
-		fflush(infile);
-		fd = fileno(infile);
 	}
 
 	if (fstat(fd, &st))
Signature · 1 line
2.14.3

Wouldn't it be just as good, and easier to check fd for == -1 as a condition of calling close?

like
failed:

	if (fd >= 0)
		close(fd);
#2 lore raw
Quoted 65 lines · Neil Horman
Quoted 60 lines · Bruce Richardson

Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case.

The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates.

As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls.

Coverity issue: 260399

Fixes 0d68533617e3 ("pmdinfogen: allow using stdin and stdout")
---
 buildtools/pmdinfogen/pmdinfogen.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
buildtools/pmdinfogen/pmdinfogen.c +10 -6
index 45b267346..0f35ca46b 100644
@@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size)
 		/* from stdin, use a temporary file to mmap */
 		FILE *infile;
 		char buffer[1024];
-		size_t n;
+		int n;
 
 		infile = tmpfile();
 		if (infile == NULL) {
 			perror("tmpfile");
 			return NULL;
 		}
-		while (!feof(stdin)) {
-			n = fread(buffer, 1, sizeof(buffer), stdin);
-			if (fwrite(buffer, 1, n, infile) != n)
+		fd = dup(fileno(infile));
+		fclose(infile);
+		if (fd < 0)
+			return NULL;
+
+		n = read(STDIN_FILENO, buffer, sizeof(buffer));
+		while (n > 0) {
+			if (write(fd, buffer, n) != n)
 				goto failed;
+			n = read(STDIN_FILENO, buffer, sizeof(buffer));
 		}
-		fflush(infile);
-		fd = fileno(infile);
 	}
 
 	if (fstat(fd, &st))
Signature · 1 line
2.14.3

Wouldn't it be just as good, and easier to check fd for == -1 as a condition of calling close?

Quoted 5 lines

like
failed:

	if (fd >= 0)
		close(fd);

That would fix the problem of calling goto failed with fd set to -1, but would not fix the resource issue that coverity was complaining about. We were allocating a stdio FILE object, then taking just the fileno of it and letting the file number go out of scope. This cleans this that up, so that we just use file numbers and properly close the FILE * once it's outlived its usefulness.

BTW: I did investigate using open and O_TMPFILE in place of tmpfile() call, but while it would work great on Linux, it's not available elsewhere, so tmpfile looks the best option.

Regards,
/Bruce

#3 lore raw
Quoted 71 lines · Bruce Richardson
Quoted 65 lines · Neil Horman
Quoted 60 lines · Bruce Richardson

Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case.

The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates.

As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls.

Coverity issue: 260399

Fixes 0d68533617e3 ("pmdinfogen: allow using stdin and stdout")
---
 buildtools/pmdinfogen/pmdinfogen.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
buildtools/pmdinfogen/pmdinfogen.c +10 -6
index 45b267346..0f35ca46b 100644
@@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size)
 		/* from stdin, use a temporary file to mmap */
 		FILE *infile;
 		char buffer[1024];
-		size_t n;
+		int n;
 
 		infile = tmpfile();
 		if (infile == NULL) {
 			perror("tmpfile");
 			return NULL;
 		}
-		while (!feof(stdin)) {
-			n = fread(buffer, 1, sizeof(buffer), stdin);
-			if (fwrite(buffer, 1, n, infile) != n)
+		fd = dup(fileno(infile));
+		fclose(infile);
+		if (fd < 0)
+			return NULL;
+
+		n = read(STDIN_FILENO, buffer, sizeof(buffer));
+		while (n > 0) {
+			if (write(fd, buffer, n) != n)
 				goto failed;
+			n = read(STDIN_FILENO, buffer, sizeof(buffer));
 		}
-		fflush(infile);
-		fd = fileno(infile);
 	}
 
 	if (fstat(fd, &st))
Signature · 1 line
2.14.3

Wouldn't it be just as good, and easier to check fd for == -1 as a condition of calling close?

Quoted 5 lines

like
failed:

	if (fd >= 0)
		close(fd);
Quoted 4 lines

That would fix the problem of calling goto failed with fd set to -1, but would not fix the resource issue that coverity was complaining about. We were allocating a stdio FILE object, then taking just the fileno of it and letting the file number go out of scope. This cleans this that up,

s/file number/FILE object ptr/

Quoted 9 lines

so that we just use file numbers and properly close the FILE * once it's outlived its usefulness.

BTW: I did investigate using open and O_TMPFILE in place of tmpfile() call, but while it would work great on Linux, it's not available elsewhere, so tmpfile looks the best option.

Regards,
/Bruce

Acked-by
#4 lore raw
Quoted 72 lines · Bruce Richardson
Quoted 71 lines · Bruce Richardson
Quoted 70 lines · Neil Horman
Quoted 60 lines · Bruce Richardson

Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case.

The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates.

As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls.

Coverity issue: 260399

Fixes 0d68533617e3 ("pmdinfogen: allow using stdin and stdout")
---
 buildtools/pmdinfogen/pmdinfogen.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
buildtools/pmdinfogen/pmdinfogen.c +10 -6
index 45b267346..0f35ca46b 100644
@@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size)
 		/* from stdin, use a temporary file to mmap */
 		FILE *infile;
 		char buffer[1024];
-		size_t n;
+		int n;
 
 		infile = tmpfile();
 		if (infile == NULL) {
 			perror("tmpfile");
 			return NULL;
 		}
-		while (!feof(stdin)) {
-			n = fread(buffer, 1, sizeof(buffer), stdin);
-			if (fwrite(buffer, 1, n, infile) != n)
+		fd = dup(fileno(infile));
+		fclose(infile);
+		if (fd < 0)
+			return NULL;
+
+		n = read(STDIN_FILENO, buffer, sizeof(buffer));
+		while (n > 0) {
+			if (write(fd, buffer, n) != n)
 				goto failed;
+			n = read(STDIN_FILENO, buffer, sizeof(buffer));
 		}
-		fflush(infile);
-		fd = fileno(infile);
 	}
 
 	if (fstat(fd, &st))
Signature · 1 line
2.14.3

Wouldn't it be just as good, and easier to check fd for == -1 as a condition of calling close?

like
failed:

	if (fd >= 0)
		close(fd);
Quoted 6 lines
Quoted 4 lines

That would fix the problem of calling goto failed with fd set to -1, but would not fix the resource issue that coverity was complaining about. We were allocating a stdio FILE object, then taking just the fileno of it and letting the file number go out of scope. This cleans this that up,

s/file number/FILE object ptr/

Yeah, Ok, I can see that, though I still think its a bit of a false positive, since the definition of tmpfile says it will automatically unlink the file on process exit. No matter though, what you have is an improvement regardless.

Quoted 7 lines
Quoted 7 lines

so that we just use file numbers and properly close the FILE * once it's outlived its usefulness.

BTW: I did investigate using open and O_TMPFILE in place of tmpfile() call, but while it would work great on Linux, it's not available elsewhere, so tmpfile looks the best option.

yeah, thats both OS and filesystem specific, I wouldn't trust it too much.

Quoted 3 lines
Quoted 2 lines

Regards,
/Bruce

#5 lore raw

02/02/2018 19:47, Neil Horman:

Quoted 21 lines · Bruce Richardson
Quoted 20 lines · Bruce Richardson
Quoted 19 lines · Neil Horman
Quoted 18 lines · Bruce Richardson
Quoted 17 lines

Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case.

The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates.

As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls.

Coverity issue: 260399

Fixes 0d68533617e3 ("pmdinfogen: allow using stdin and stdout")

[...]

Quoted 1 line

Applied, thanks