1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

cov: avoid TOCTOU

Use just opendir() call and produce error message from errno state.
This commit is contained in:
Zdenek Kabelac 2024-05-07 14:48:55 +02:00
parent 5f1c799e6a
commit 09f70dbb87
2 changed files with 8 additions and 17 deletions

View File

@ -722,7 +722,6 @@ const char *dev_mpath_component_wwid(struct cmd_context *cmd, struct device *dev
char sysbuf[PATH_MAX] = { 0 }; char sysbuf[PATH_MAX] = { 0 };
char *slave_name; char *slave_name;
const char *wwid = NULL; const char *wwid = NULL;
struct stat info;
DIR *dr; DIR *dr;
struct dirent *de; struct dirent *de;
@ -734,18 +733,13 @@ const char *dev_mpath_component_wwid(struct cmd_context *cmd, struct device *dev
return NULL; return NULL;
} }
if (stat(slaves_path, &info))
return NULL;
if (!S_ISDIR(info.st_mode)) {
log_warn("Path %s is not a directory.", slaves_path);
return NULL;
}
/* Get wwid from first component */ /* Get wwid from first component */
if (!(dr = opendir(slaves_path))) { if (!(dr = opendir(slaves_path))) {
log_debug("Device %s has no slaves dir", dev_name(dev)); if (errno == ENOTDIR)
log_warn("WARNING: Path %s is not a directory.", slaves_path);
else if (errno != ENOENT)
log_sys_debug("opendir", slaves_path);
return NULL; return NULL;
} }

View File

@ -45,7 +45,6 @@ static int _get_crypt_path(dev_t lv_devt, char *lv_path, char *crypt_path)
char holders_path[PATH_MAX]; char holders_path[PATH_MAX];
char *holder_name; char *holder_name;
DIR *dr; DIR *dr;
struct stat st;
struct dirent *de; struct dirent *de;
int ret = 0; int ret = 0;
@ -56,13 +55,11 @@ static int _get_crypt_path(dev_t lv_devt, char *lv_path, char *crypt_path)
} }
/* If the crypt dev is not active, there will be no LV holder. */ /* If the crypt dev is not active, there will be no LV holder. */
if (stat(holders_path, &st)) {
log_error("Missing %s for %s", crypt_path, lv_path);
return 0;
}
if (!(dr = opendir(holders_path))) { if (!(dr = opendir(holders_path))) {
log_error("Cannot open %s", holders_path); if (errno == ENOENT)
log_error("Missing %s for %s.", crypt_path, lv_path);
else
log_error("Cannot open %s.", holders_path);
return 0; return 0;
} }