Skip to content

Commit 629e356

Browse files
authored
Merge pull request #925 from cyphar/fix-path-checks
rootfs: clean up
2 parents 9123ce6 + c29695a commit 629e356

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

libcontainer/rootfs_linux.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NOD
2828
// needsSetupDev returns true if /dev needs to be set up.
2929
func needsSetupDev(config *configs.Config) bool {
3030
for _, m := range config.Mounts {
31-
if m.Device == "bind" && (m.Destination == "/dev" || m.Destination == "/dev/") {
31+
if m.Device == "bind" && libcontainerUtils.CleanPath(m.Destination) == "/dev" {
3232
return false
3333
}
3434
}
@@ -95,7 +95,7 @@ func setupRootfs(config *configs.Config, console *linuxConsole, pipe io.ReadWrit
9595
}
9696
// remount dev as ro if specifed
9797
for _, m := range config.Mounts {
98-
if m.Destination == "/dev" {
98+
if libcontainerUtils.CleanPath(m.Destination) == "/dev" {
9999
if m.Flags&syscall.MS_RDONLY != 0 {
100100
if err := remountReadonly(m.Destination); err != nil {
101101
return newSystemErrorWithCausef(err, "remounting %q as readonly", m.Destination)
@@ -238,29 +238,16 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error {
238238
return err
239239
}
240240
}
241-
// create symlinks for merged cgroups
242-
cwd, err := os.Getwd()
243-
if err != nil {
244-
return err
245-
}
246-
if err := os.Chdir(filepath.Join(rootfs, m.Destination)); err != nil {
247-
return err
248-
}
249241
for _, mc := range merged {
250242
for _, ss := range strings.Split(mc, ",") {
251-
if err := os.Symlink(mc, ss); err != nil {
252-
// if cgroup already exists, then okay(it could have been created before)
253-
if os.IsExist(err) {
254-
continue
255-
}
256-
os.Chdir(cwd)
243+
// symlink(2) is very dumb, it will just shove the path into
244+
// the link and doesn't do any checks or relative path
245+
// conversion. Also, don't error out if the cgroup already exists.
246+
if err := os.Symlink(mc, filepath.Join(rootfs, m.Destination, ss)); err != nil && !os.IsExist(err) {
257247
return err
258248
}
259249
}
260250
}
261-
if err := os.Chdir(cwd); err != nil {
262-
return err
263-
}
264251
if m.Flags&syscall.MS_RDONLY != 0 {
265252
// remount cgroup root as readonly
266253
mcgrouproot := &configs.Mount{
@@ -713,7 +700,7 @@ func mountPropagate(m *configs.Mount, rootfs string, mountLabel string) error {
713700
data = label.FormatMountLabel(m.Data, mountLabel)
714701
flags = m.Flags
715702
)
716-
if dest == "/dev" {
703+
if libcontainerUtils.CleanPath(dest) == "/dev" {
717704
flags &= ^syscall.MS_RDONLY
718705
}
719706
if !strings.HasPrefix(dest, rootfs) {

0 commit comments

Comments
 (0)