2018-02-05 16:05:59 -05:00
|
|
|
package chrootarchive // import "github.com/docker/docker/pkg/chrootarchive"
|
2016-05-04 13:32:51 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-05-10 10:18:16 -04:00
|
|
|
|
2021-06-18 05:01:24 -04:00
|
|
|
"github.com/containerd/containerd/pkg/userns"
|
2020-03-13 19:38:24 -04:00
|
|
|
"github.com/moby/sys/mount"
|
|
|
|
"github.com/moby/sys/mountinfo"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2016-05-04 13:32:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// chroot on linux uses pivot_root instead of chroot
|
|
|
|
// pivot_root takes a new root and an old root.
|
|
|
|
// Old root must be a sub-dir of new root, it is where the current rootfs will reside after the call to pivot_root.
|
|
|
|
// New root is where the new rootfs is set to.
|
|
|
|
// Old root is removed after the call to pivot_root so it is no longer available under the new root.
|
|
|
|
// This is similar to how libcontainer sets up a container's rootfs
|
|
|
|
func chroot(path string) (err error) {
|
2016-08-12 16:31:01 -04:00
|
|
|
// if the engine is running in a user namespace we need to use actual chroot
|
2021-06-18 05:01:24 -04:00
|
|
|
if userns.RunningInUserNS() {
|
2016-08-12 16:31:01 -04:00
|
|
|
return realChroot(path)
|
|
|
|
}
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Unshare(unix.CLONE_NEWNS); err != nil {
|
2016-05-04 13:32:51 -04:00
|
|
|
return fmt.Errorf("Error creating mount namespace before pivot: %v", err)
|
|
|
|
}
|
2016-05-10 10:18:16 -04:00
|
|
|
|
2017-10-16 13:49:31 -04:00
|
|
|
// Make everything in new ns slave.
|
|
|
|
// Don't use `private` here as this could race where the mountns gets a
|
|
|
|
// reference to a mount and an unmount from the host does not propagate,
|
|
|
|
// which could potentially cause transient errors for other operations,
|
|
|
|
// even though this should be relatively small window here `slave` should
|
|
|
|
// not cause any problems.
|
|
|
|
if err := mount.MakeRSlave("/"); err != nil {
|
2016-10-20 18:29:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-11-03 15:57:38 -04:00
|
|
|
|
2020-03-13 19:38:24 -04:00
|
|
|
if mounted, _ := mountinfo.Mounted(path); !mounted {
|
2016-11-03 15:57:38 -04:00
|
|
|
if err := mount.Mount(path, path, "bind", "rbind,rw"); err != nil {
|
|
|
|
return realChroot(path)
|
|
|
|
}
|
2016-05-04 13:32:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup oldRoot for pivot_root
|
2021-08-24 06:10:50 -04:00
|
|
|
pivotDir, err := os.MkdirTemp(path, ".pivot_root")
|
2016-05-04 13:32:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error setting up pivot dir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mounted bool
|
|
|
|
defer func() {
|
|
|
|
if mounted {
|
|
|
|
// make sure pivotDir is not mounted before we try to remove it
|
2017-05-23 10:22:32 -04:00
|
|
|
if errCleanup := unix.Unmount(pivotDir, unix.MNT_DETACH); errCleanup != nil {
|
2016-05-04 13:32:51 -04:00
|
|
|
if err == nil {
|
|
|
|
err = errCleanup
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errCleanup := os.Remove(pivotDir)
|
2016-05-09 11:06:46 -04:00
|
|
|
// pivotDir doesn't exist if pivot_root failed and chroot+chdir was successful
|
2016-05-10 10:18:16 -04:00
|
|
|
// because we already cleaned it up on failed pivot_root
|
2016-05-09 11:06:46 -04:00
|
|
|
if errCleanup != nil && !os.IsNotExist(errCleanup) {
|
2016-05-04 13:32:51 -04:00
|
|
|
errCleanup = fmt.Errorf("Error cleaning up after pivot: %v", errCleanup)
|
|
|
|
if err == nil {
|
|
|
|
err = errCleanup
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.PivotRoot(path, pivotDir); err != nil {
|
2016-05-10 10:18:16 -04:00
|
|
|
// If pivot fails, fall back to the normal chroot after cleaning up temp dir
|
2016-05-09 11:06:46 -04:00
|
|
|
if err := os.Remove(pivotDir); err != nil {
|
|
|
|
return fmt.Errorf("Error cleaning up after failed pivot: %v", err)
|
|
|
|
}
|
2016-05-04 13:32:51 -04:00
|
|
|
return realChroot(path)
|
|
|
|
}
|
|
|
|
mounted = true
|
|
|
|
|
|
|
|
// This is the new path for where the old root (prior to the pivot) has been moved to
|
|
|
|
// This dir contains the rootfs of the caller, which we need to remove so it is not visible during extraction
|
|
|
|
pivotDir = filepath.Join("/", filepath.Base(pivotDir))
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Chdir("/"); err != nil {
|
2016-05-04 13:32:51 -04:00
|
|
|
return fmt.Errorf("Error changing to new root: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the pivotDir (where the old root lives) private so it can be unmounted without propagating to the host
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Mount("", pivotDir, "", unix.MS_PRIVATE|unix.MS_REC, ""); err != nil {
|
2016-05-04 13:32:51 -04:00
|
|
|
return fmt.Errorf("Error making old root private after pivot: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now unmount the old root so it's no longer visible from the new root
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Unmount(pivotDir, unix.MNT_DETACH); err != nil {
|
2016-05-04 13:32:51 -04:00
|
|
|
return fmt.Errorf("Error while unmounting old root after pivot: %v", err)
|
|
|
|
}
|
|
|
|
mounted = false
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func realChroot(path string) error {
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Chroot(path); err != nil {
|
2016-05-04 13:32:51 -04:00
|
|
|
return fmt.Errorf("Error after fallback to chroot: %v", err)
|
|
|
|
}
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Chdir("/"); err != nil {
|
2016-05-09 11:06:46 -04:00
|
|
|
return fmt.Errorf("Error changing to new root after chroot: %v", err)
|
2016-05-04 13:32:51 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|