Commit 88cb927b authored by npzacs's avatar npzacs
Browse files

Fix TOCTOU

parent c8d08a80
...@@ -114,22 +114,20 @@ static int _open_block_device(const char *path) ...@@ -114,22 +114,20 @@ static int _open_block_device(const char *path)
struct stat st; struct stat st;
int fd; int fd;
if (stat(path, &st)) { fd = open(path, O_RDONLY | O_NONBLOCK);
BD_DEBUG(DBG_MMC | DBG_CRIT, "stat(%s) failed\n", path); if (fd < 0) {
return -1; BD_DEBUG(DBG_MMC | DBG_CRIT, "Error opening %s\n", path);
}
if (!S_ISBLK(st.st_mode)) {
return -1; return -1;
} }
BD_DEBUG(DBG_MMC, "Opening block device %s\n", path); if (!fstat(fd, &st) && S_ISBLK(st.st_mode)) {
fd = open(path, O_RDONLY | O_NONBLOCK); BD_DEBUG(DBG_MMC, "Opened block device %s\n", path);
if (fd < 0) { return fd;
BD_DEBUG(DBG_MMC | DBG_CRIT, "Error opening block device %s\n", path);
} }
return fd; BD_DEBUG(DBG_MMC, "%s is not a block device\n", path);
close(fd);
return -1;
} }
MMCDEV *device_open(const char *path) MMCDEV *device_open(const char *path)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment