Skip to content

Commit f15e356

Browse files
AkihiroSudaruncom
authored andcommitted
BACKPORT: validate mount path for tmpfs
Upstream reference: moby#30182 Fix https://bugzilla.redhat.com/show_bug.cgi?id=1389545 There was no validation for `docker run --tmpfs foo`. In this PR, only two obvious rules are implemented: - path must be absolute - path must not be "/" We should add more rules carefully. Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
1 parent 4df4090 commit f15e356

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

daemon/daemon_unix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/docker/docker/pkg/sysinfo"
3030
"github.com/docker/docker/runconfig"
3131
runconfigopts "github.com/docker/docker/runconfig/opts"
32+
"github.com/docker/docker/volume"
3233
"github.com/docker/libnetwork"
3334
nwconfig "github.com/docker/libnetwork/config"
3435
"github.com/docker/libnetwork/drivers/bridge"
@@ -553,6 +554,12 @@ func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.
553554
return warnings, fmt.Errorf("Unknown runtime specified %s", hostConfig.Runtime)
554555
}
555556

557+
for dest := range hostConfig.Tmpfs {
558+
if err := volume.ValidateTmpfsMountDestination(dest); err != nil {
559+
return warnings, err
560+
}
561+
}
562+
556563
return warnings, nil
557564
}
558565

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// +build !windows
2+
3+
package main
4+
5+
import (
6+
"strings"
7+
8+
"github.com/go-check/check"
9+
)
10+
11+
// Test case for #30166 (target was not validated)
12+
func (s *DockerSuite) TestCreateTmpfsMountsTarget(c *check.C) {
13+
testRequires(c, DaemonIsLinux)
14+
type testCase struct {
15+
target string
16+
expectedError string
17+
}
18+
cases := []testCase{
19+
{
20+
target: ".",
21+
expectedError: "mount path must be absolute",
22+
},
23+
{
24+
target: "foo",
25+
expectedError: "mount path must be absolute",
26+
},
27+
{
28+
target: "/",
29+
expectedError: "destination can't be '/'",
30+
},
31+
{
32+
target: "//",
33+
expectedError: "destination can't be '/'",
34+
},
35+
}
36+
for _, x := range cases {
37+
out, _, _ := dockerCmdWithError("create", "--tmpfs", x.target, "busybox", "sh")
38+
if x.expectedError != "" && !strings.Contains(out, x.expectedError) {
39+
c.Fatalf("mounting tmpfs over %q should fail with %q, but got %q",
40+
x.target, x.expectedError, out)
41+
}
42+
}
43+
}

volume/validate.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func validateMountConfig(mnt *mount.Mount, options ...func(*validateOpts)) error
9191
if len(mnt.Source) != 0 {
9292
return &errMountConfig{mnt, errExtraField("Source")}
9393
}
94+
if err := ValidateTmpfsMountDestination(mnt.Target); err != nil {
95+
return &errMountConfig{mnt, err}
96+
}
9497
if _, err := ConvertTmpfsOptions(mnt.TmpfsOptions, mnt.ReadOnly); err != nil {
9598
return &errMountConfig{mnt, err}
9699
}
@@ -123,3 +126,15 @@ func validateAbsolute(p string) error {
123126
}
124127
return fmt.Errorf("invalid mount path: '%s' mount path must be absolute", p)
125128
}
129+
130+
// ValidateTmpfsMountDestination validates the destination of tmpfs mount.
131+
// Currently, we have only two obvious rule for validation:
132+
// - path must not be "/"
133+
// - path must be absolute
134+
// We should add more rules carefully (#30166)
135+
func ValidateTmpfsMountDestination(dest string) error {
136+
if err := validateNotRoot(dest); err != nil {
137+
return err
138+
}
139+
return validateAbsolute(dest)
140+
}

0 commit comments

Comments
 (0)