From 2081c5e7f73eadb2ec84640d4b03f4eb1881950e Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Fri, 15 Jun 2018 13:57:29 -0700 Subject: runsc: support /dev bind mount which does not conflict with default /dev mount. PiperOrigin-RevId: 200768923 Change-Id: I4b8da10bcac296e8171fe6754abec5aabfec5e65 --- runsc/specutils/specutils.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'runsc/specutils/specutils.go') diff --git a/runsc/specutils/specutils.go b/runsc/specutils/specutils.go index 3161360b4..0bb462eb5 100644 --- a/runsc/specutils/specutils.go +++ b/runsc/specutils/specutils.go @@ -195,7 +195,43 @@ func capsFromNames(names []string) (auth.CapabilitySet, error) { // Is9PMount returns true if the given mount can be mounted as an external gofer. func Is9PMount(m specs.Mount) bool { - return m.Type == "bind" && m.Source != "" && !strings.HasPrefix(m.Destination, "/dev") + return m.Type == "bind" && m.Source != "" && IsSupportedDevMount(m) +} + +// IsSupportedDevMount returns true if the mount is a supported /dev mount. +// Only mount that does not conflict with runsc default /dev mount is +// supported. +func IsSupportedDevMount(m specs.Mount) bool { + // These are devices exist inside sentry. See pkg/sentry/fs/dev/dev.go + var existingDevices = []string{ + "/dev/fd", "/dev/stdin", "/dev/stdout", "/dev/stderr", + "/dev/null", "/dev/zero", "/dev/full", "/dev/random", + "/dev/urandom", "/dev/shm", "/dev/pts", "/dev/ptmx", + } + dst := filepath.Clean(m.Destination) + if dst == "/dev" { + // OCI spec uses many different mounts for the things inside of '/dev'. We + // have a single mount at '/dev' that is always mounted, regardless of + // whether it was asked for, as the spec says we SHOULD. + return false + } + for _, dev := range existingDevices { + if dst == dev || strings.HasPrefix(dst, dev+"/") { + return false + } + } + return true +} + +// SupportedMounts filters out unsupported mounts. +func SupportedMounts(mounts []specs.Mount) []specs.Mount { + var newMounts []specs.Mount + for _, m := range mounts { + if IsSupportedDevMount(m) { + newMounts = append(newMounts, m) + } + } + return newMounts } // BinPath returns the real path to self, resolving symbolink links. This is done -- cgit v1.2.3