]> www.infradead.org Git - mtd-utils.git/commitdiff
mtd-utils: Fix potential negative arguments passed to close(2)
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Fri, 24 Jan 2020 22:01:57 +0000 (23:01 +0100)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Sun, 9 Feb 2020 21:13:18 +0000 (22:13 +0100)
Many tools open a file descriptor, close it a the end and have some
form of error path in between that jumps to the end.

In some cases, if opening the file fails the error path is taken and
the utility ends up closing one or more invalid file descriptors. It's
technically not a real issue but something that pretty much any static
analysis tool barks at.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
nand-utils/nanddump.c
nand-utils/nandwrite.c
nor-utils/rfddump.c
tests/fs-tests/stress/atoms/fwrite00.c

index 841ed67596cfc9982afa17cf795747c5ada6bdf1..62699e079678e256fd2b8d950834cd4f9a852d2f 100644 (file)
@@ -549,7 +549,8 @@ int main(int argc, char * const argv[])
 
 closeall:
        close(fd);
-       close(ofd);
+       if (ofd > 0 && ofd != STDOUT_FILENO)
+               close(ofd);
        free(oobbuf);
        free(readbuf);
        exit(EXIT_FAILURE);
index 8f21593253996c447e4642df550726ff829d56b9..e8a210c44c5e1deaf68187982b858af3b9ede1de 100644 (file)
@@ -605,7 +605,8 @@ int main(int argc, char * const argv[])
        failed = false;
 
 closeall:
-       close(ifd);
+       if (ifd > 0 && ifd != STDIN_FILENO)
+               close(ifd);
        libmtd_close(mtd_desc);
        free(filebuf);
        close(fd);
index 4ad2f91eb60985ed51f89af13cdb881d12204325..01ab4c2dcf0086bf82de459da52b157719afb982 100644 (file)
@@ -324,7 +324,7 @@ int main(int argc, char *argv[])
        return 0;
 
 err:
-       if (out_fd)
+       if (out_fd > 0)
                close(out_fd);
 
        close(fd);
index 3406bba44232b8a8fa497e34ffd8514e398cb045..877c63cafd91da69e9a603a25d992ab92db4576f 100644 (file)
@@ -138,7 +138,9 @@ static void filestress00(void)
                        deleted = 1;
                }
        }
-       CHECK(close(fd) != -1);
+       if (fd > 0) {
+               CHECK(close(fd) != -1);
+       }
        /* Sleep */
        if (tests_sleep_parameter > 0) {
                unsigned us = tests_sleep_parameter * 1000;