Skip to content

Commit a6eccc3

Browse files
committed
Do not raise in switch root if paths are too long
If we encounter the (unlikely) situation where the combined path to the new root and a path to a mount to be moved together exceed maximum path length, we shouldn't crash, but fail this path instead.
1 parent 75ead2b commit a6eccc3

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/shared/switch-root.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,29 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot,
7575
NULSTR_FOREACH(i, move_mounts) {
7676
char new_mount[PATH_MAX];
7777
struct stat sb;
78+
size_t n;
7879

79-
xsprintf(new_mount, "%s%s", new_root, i);
80+
n = snprintf(new_mount, sizeof new_mount, "%s%s", new_root, i);
81+
if (n >= sizeof new_mount) {
82+
bool move = mountflags & MS_MOVE;
83+
84+
log_warning("New path is too long, %s: %s%s",
85+
move ? "forcing unmount instead" : "ignoring",
86+
new_root, i);
87+
88+
if (move)
89+
if (umount2(i, MNT_FORCE) < 0)
90+
log_warning_errno(errno, "Failed to unmount %s: %m", i);
91+
continue;
92+
}
8093

8194
mkdir_p_label(new_mount, 0755);
8295

83-
if ((stat(new_mount, &sb) < 0) ||
96+
if (stat(new_mount, &sb) < 0 ||
8497
sb.st_dev != new_root_stat.st_dev) {
8598

8699
/* Mount point seems to be mounted already or
87-
* stat failed. Unmount the old mount
88-
* point. */
100+
* stat failed. Unmount the old mount point. */
89101
if (umount2(i, MNT_DETACH) < 0)
90102
log_warning_errno(errno, "Failed to unmount %s: %m", i);
91103
continue;
@@ -97,10 +109,9 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot,
97109

98110
if (umount2(i, MNT_FORCE) < 0)
99111
log_warning_errno(errno, "Failed to unmount %s: %m", i);
100-
}
101-
if (mountflags & MS_BIND)
102-
log_error_errno(errno, "Failed to bind mount %s to %s: %m", i, new_mount);
103112

113+
} else if (mountflags & MS_BIND)
114+
log_error_errno(errno, "Failed to bind mount %s to %s: %m", i, new_mount);
104115
}
105116
}
106117

0 commit comments

Comments
 (0)