switch_root: check if mount point to move even exists

Signed-off-by: Thomas Deutschmann <whissi@gentoo.org>
This commit is contained in:
Thomas Deutschmann 2021-02-08 15:30:25 +01:00
parent 56fc4adac9
commit 037c7816ce
No known key found for this signature in database
GPG Key ID: 44E6EBDC9BF60559
1 changed files with 11 additions and 1 deletions

View File

@ -128,7 +128,12 @@ static int switchroot(const char *newroot)
const char *umounts[] = { "/dev", "/proc", "/sys", "/run", NULL };
int i;
int cfd = -1;
struct stat newroot_stat, sb;
struct stat newroot_stat, oldroot_stat, sb;
if (stat("/", &oldroot_stat) != 0) {
warn(_("stat of %s failed"), "/");
return -1;
}
if (stat(newroot, &newroot_stat) != 0) {
warn(_("stat of %s failed"), newroot);
@ -140,6 +145,11 @@ static int switchroot(const char *newroot)
snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
if ((stat(umounts[i], &sb) == 0) && sb.st_dev == oldroot_stat.st_dev) {
/* mount point to move seems to be a normal directory or stat failed */
continue;
}
if ((stat(newmount, &sb) != 0) || (sb.st_dev != newroot_stat.st_dev)) {
/* mount point seems to be mounted already or stat failed */
umount2(umounts[i], MNT_DETACH);