aboutsummaryrefslogtreecommitdiff
path: root/misc-utils
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-01-24 23:18:18 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-09 22:13:18 +0100
commit4233a39933277b0d715d59d72942a8fd65bcec4a (patch)
treea7ef2db87b697d54c19ee03a115508b60f27f623 /misc-utils
parentb636250e211198210ab996671bccc2983300c6f5 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'misc-utils')
-rw-r--r--misc-utils/ftl_check.c14
-rw-r--r--misc-utils/ftl_format.c14
2 files changed, 16 insertions, 12 deletions
diff --git a/misc-utils/ftl_check.c b/misc-utils/ftl_check.c
index 5a04155..5b2dae5 100644
--- a/misc-utils/ftl_check.c
+++ b/misc-utils/ftl_check.c
@@ -206,18 +206,20 @@ int main(int argc, char *argv[])
exit(errflg > 0 ? 0 : EXIT_FAILURE);
}
- if (stat(argv[optind], &buf) != 0) {
+ fd = open(argv[optind], O_RDONLY);
+ 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_RDONLY);
- if (fd == -1) {
- perror("open failed");
+ close(fd);
exit(EXIT_FAILURE);
}
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);
}