main: Use image_alloc_from_fd() to fix race condition between calling stat and opening file

This commit is contained in:
Pali Rohár committed 2017-06-17 23:48:01 +02:00
1 parent 5cbd5e92b8
commit cda5dc6116
1 file changed
+15 -7
+15 -7
View File
@@ -155,16 +155,24 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) {
char * version; char * version;
char * layout; char * layout;
char * layout_file; char * layout_file;
int fd;
/* First check if arg is file, then try to parse arg format */ /* First check if arg is file, then try to parse arg format */
if ( stat(arg, &st) == 0 ) { fd = open(arg, O_RDONLY);
image = image_alloc_from_file(arg, NULL, NULL, NULL, NULL, NULL); if ( fd >= 0 ) {
if ( ! image ) { if ( fstat(fd, &st) == 0 && !S_ISDIR(st.st_mode) ) {
ERROR("Cannot load image file %s", arg); image = image_alloc_from_fd(fd, arg, NULL, NULL, NULL, NULL, NULL);
exit(1); if ( ! image ) {
ERROR("Cannot load image file %s", arg);
exit(1);
}
image_list_add(image_first, image);
return;
} }
image_list_add(image_first, image); close(fd);
return; } else if ( errno != ENOENT ) {
ERROR("Cannot load image file %s", arg);
exit(1);
} }
layout_file = strchr(arg, '%'); layout_file = strchr(arg, '%');