Skip to content

Commit

Permalink
runroot: add check that it is on volatile storage
Browse files Browse the repository at this point in the history
Make sure the runroot won't persist after a reboot, if it happens then
we can carry wrong information on the current active mounts.

Closes: containers/podman#2150

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Apr 24, 2019
1 parent f00b842 commit 20b8545
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ var (
ErrDigestUnknown = errors.New("could not compute digest of item")
// ErrLayerNotMounted is returned when the requested information can only be computed for a mounted layer, and the layer is not mounted.
ErrLayerNotMounted = errors.New("layer is not mounted")
// ErrTargetNotVolatile is returned when a path must be on volatile storage.
ErrTargetNotVolatile = errors.New("the target is not on tmpfs")
)
5 changes: 5 additions & 0 deletions pkg/mount/mountinfo_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ func parseMountTable() ([]*Info, error) {
}
return out, nil
}

// IsOnVolatileStorage returns whether the specified target is on tmpfs.
func IsOnVolatileStorage(target string) (bool, error) {
return true, nil
}
23 changes: 23 additions & 0 deletions pkg/mount/mountinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

const (
Expand All @@ -26,6 +30,8 @@ const (
(10) mount source: filesystem specific information or "none"
(11) super options: per super block options*/
mountinfoFormat = "%d %d %d:%d %s %s %s %s"

TMPFS_MAGIC = 0x1021994
)

// Parse /proc/self/mountinfo because comparing Dev and ino does not work from
Expand Down Expand Up @@ -93,3 +99,20 @@ func PidMountInfo(pid int) ([]*Info, error) {

return parseInfoFile(f)
}

// IsOnVolatileStorage returns whether the specified target is on tmpfs.
func IsOnVolatileStorage(target string) (bool, error) {
var fs unix.Statfs_t
// Make sure it's read-only.
for {
err := unix.Statfs(target, &fs)
if err == nil {
break
}
if !os.IsNotExist(err) {
return false, errors.Wrapf(err, "error statfs %s", target)
}
target = filepath.Dir(target)
}
return fs.Type == TMPFS_MAGIC, nil
}
5 changes: 5 additions & 0 deletions pkg/mount/mountinfo_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ func parseMountTable() ([]*Info, error) {
C.fclose(mnttab)
return out, nil
}

// IsOnVolatileStorage returns whether the specified target is on tmpfs.
func IsOnVolatileStorage(target string) (bool, error) {
return true, nil
}
5 changes: 5 additions & 0 deletions pkg/mount/mountinfo_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ import (
func parseMountTable() ([]*Info, error) {
return nil, fmt.Errorf("mount.parseMountTable is not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

// IsOnVolatileStorage returns whether the specified target is on tmpfs.
func IsOnVolatileStorage(target string) (bool, error) {
return true, nil
}
5 changes: 5 additions & 0 deletions pkg/mount/mountinfo_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ func parseMountTable() ([]*Info, error) {
// Do NOT return an error!
return nil, nil
}

// IsOnVolatileStorage returns whether the specified target is on tmpfs.
func IsOnVolatileStorage(target string) (bool, error) {
return true, nil
}
26 changes: 26 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/containers/storage/pkg/directory"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/pkg/mount"
"github.com/containers/storage/pkg/parsers"
"github.com/containers/storage/pkg/stringid"
"github.com/containers/storage/pkg/stringutils"
Expand Down Expand Up @@ -148,6 +149,9 @@ type StoreOptions struct {
// for use inside of a user namespace where UID mapping is being used.
UIDMap []idtools.IDMap `json:"uidmap,omitempty"`
GIDMap []idtools.IDMap `json:"gidmap,omitempty"`
// SkipRunRootCheck disables the check for the RunRoot to be on a volatile
// storage.
SkipRunRootCheck bool
}

// Store wraps up the various types of file-based stores that we use into a
Expand Down Expand Up @@ -535,6 +539,16 @@ type store struct {
digestLockRoot string
}

func validateRunRoot(runRoot string) error {
if onTmpfs, err := mount.IsOnVolatileStorage(runRoot); err != nil || !onTmpfs {
if err != nil {
return errors.Wrapf(err, "cannot check if %s is on tmpfs", runRoot)
}
return errors.Wrapf(ErrTargetNotVolatile, "%s must be on tmpfs", runRoot)
}
return nil
}

// GetStore attempts to find an already-created Store object matching the
// specified location and graph driver, and if it can't, it creates and
// initializes a new Store object, and the underlying storage that it controls.
Expand Down Expand Up @@ -600,6 +614,12 @@ func GetStore(options StoreOptions) (Store, error) {
}
}

if !options.SkipRunRootCheck {
if err := validateRunRoot(options.RunRoot); err != nil {
return nil, err
}
}

graphLock, err := GetLockfile(filepath.Join(options.GraphRoot, "storage.lock"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -3278,6 +3298,12 @@ func ReloadConfigurationFile(configFile string, storeOptions *StoreOptions) {
storeOptions.GraphDriverName = config.Storage.Driver
}
if config.Storage.RunRoot != "" {
if !storeOptions.SkipRunRootCheck {
if err := validateRunRoot(config.Storage.RunRoot); err != nil {
fmt.Printf("Failed to set runroot to %s %v\n", config.Storage.RunRoot, err.Error())
return
}
}
storeOptions.RunRoot = config.Storage.RunRoot
}
if config.Storage.GraphRoot != "" {
Expand Down

0 comments on commit 20b8545

Please sign in to comment.