From 4233a39933277b0d715d59d72942a8fd65bcec4a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 24 Jan 2020 23:18:18 +0100 Subject: mtd-utils: Fix various TOCTOU issues This patch restructures various code parts that follow the pattern of "stat(x, &sb) ... makes_sense(&sb) ... open(x)". Signed-off-by: David Oberhollenzer --- misc-utils/ftl_format.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'misc-utils/ftl_format.c') diff --git a/misc-utils/ftl_format.c b/misc-utils/ftl_format.c index bf3c8f2..34d436c 100644 --- a/misc-utils/ftl_format.c +++ b/misc-utils/ftl_format.c @@ -312,18 +312,20 @@ int main(int argc, char *argv[]) exit(errflg > 0 ? EXIT_SUCCESS : EXIT_FAILURE); } - if (stat(argv[optind], &buf) != 0) { + fd = open(argv[optind], O_RDWR); + if (fd == -1) { + perror("open failed"); + exit(EXIT_FAILURE); + } + if (fstat(fd, &buf) != 0) { perror("status check failed"); + close(fd); exit(EXIT_FAILURE); } if (!(buf.st_mode & S_IFCHR)) { fprintf(stderr, "%s is not a character special device\n", argv[optind]); - exit(EXIT_FAILURE); - } - fd = open(argv[optind], O_RDWR); - if (fd == -1) { - perror("open failed"); + close(fd); exit(EXIT_FAILURE); } -- cgit v1.2.3