diff options
author | Pali Rohár <pali.rohar@gmail.com> | 2017-06-17 23:48:01 +0200 |
---|---|---|
committer | Pali Rohár <pali.rohar@gmail.com> | 2017-06-17 23:48:01 +0200 |
commit | cda5dc6116d607cbe72cfe9d799fab890aba7119 (patch) | |
tree | 753d5141b592a9987ed83fb580ad414a9ee814de | |
parent | 5cbd5e92b878dfe1aa6c9623b21c38030c23b5ff (diff) | |
download | 0xFFFF-cda5dc6116d607cbe72cfe9d799fab890aba7119.tar.bz2 |
main: Use image_alloc_from_fd() to fix race condition between calling stat and opening file
-rw-r--r-- | src/main.c | 22 |
1 files changed, 15 insertions, 7 deletions
@@ -155,16 +155,24 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { char * version; char * layout; char * layout_file; + int fd; /* First check if arg is file, then try to parse arg format */ - if ( stat(arg, &st) == 0 ) { - image = image_alloc_from_file(arg, NULL, NULL, NULL, NULL, NULL); - if ( ! image ) { - ERROR("Cannot load image file %s", arg); - exit(1); + fd = open(arg, O_RDONLY); + if ( fd >= 0 ) { + if ( fstat(fd, &st) == 0 && !S_ISDIR(st.st_mode) ) { + image = image_alloc_from_fd(fd, arg, NULL, NULL, NULL, NULL, NULL); + if ( ! image ) { + ERROR("Cannot load image file %s", arg); + exit(1); + } + image_list_add(image_first, image); + return; } - image_list_add(image_first, image); - return; + close(fd); + } else if ( errno != ENOENT ) { + ERROR("Cannot load image file %s", arg); + exit(1); } layout_file = strchr(arg, '%'); |