Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions libcontainer/configs/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (v *ConfigValidator) Validate(config *configs.Config) error {
if err := v.rootfs(config); err != nil {
return err
}
if err := v.mounts(config); err != nil {
return err
}
if err := v.network(config); err != nil {
return err
}
Expand Down Expand Up @@ -65,6 +68,29 @@ func (v *ConfigValidator) rootfs(config *configs.Config) error {
return nil
}

// mounts validates that the config actually has all of the required
// mountpoints as specified by the runtime-spec (config-linux.md#default-filesystems).
func (v *ConfigValidator) mounts(config *configs.Config) error {
if !config.Namespaces.Contains(configs.NEWNS) {
// No mount namespace, nothing to check.
return nil
}

dests := map[string]bool{}
for _, mount := range config.Mounts {
dests[filepath.Clean(mount.Destination)] = true
}

// From the spec.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i think this comment is redundant.

for _, mountpoint := range []string{"/proc", "/sys", "/dev/shm", "/dev/pts"} {
if ok := dests[mountpoint]; !ok {
return fmt.Errorf("mount config is missing required filesystem: %s", mountpoint)
}
}

return nil
}

func (v *ConfigValidator) network(config *configs.Config) error {
if !config.Namespaces.Contains(configs.NEWNET) {
if len(config.Networks) > 0 || len(config.Routes) > 0 {
Expand Down
14 changes: 14 additions & 0 deletions libcontainer/configs/validate/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func TestValidateSecurityWithMaskPaths(t *testing.T) {
{Type: configs.NEWNS},
},
),
// Required to pass mount validation.
Mounts: []*configs.Mount{
{Destination: "/proc"},
{Destination: "/sys"},
{Destination: "/dev/shm"},
{Destination: "/dev/pts"},
},
}

validator := validate.New()
Expand All @@ -124,6 +131,13 @@ func TestValidateSecurityWithROPaths(t *testing.T) {
{Type: configs.NEWNS},
},
),
// Required to pass mount validation.
Mounts: []*configs.Mount{
{Destination: "/proc"},
{Destination: "/sys"},
{Destination: "/dev/shm"},
{Destination: "/dev/pts"},
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more test to make it failed with invalid mount info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll do it tomorrow (have an exam tomorrow morning).

}

validator := validate.New()
Expand Down