Skip to content

Commit 4ec570d

Browse files
authored
Merge pull request #1138 from gaocegege/fix-config-validator
moby/moby#27484-check if sysctls are used in host network mode.
2 parents c7ed224 + 41c3581 commit 4ec570d

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

libcontainer/configs/validate/validator.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,36 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error {
125125
}
126126
}
127127
if strings.HasPrefix(s, "net.") {
128-
if config.Namespaces.Contains(configs.NEWNET) {
129-
continue
130-
} else {
128+
if !config.Namespaces.Contains(configs.NEWNET) {
131129
return fmt.Errorf("sysctl %q is not allowed in the hosts network namespace", s)
132130
}
131+
if path := config.Namespaces.PathOf(configs.NEWNET); path != "" {
132+
if err := checkHostNs(s, path); err != nil {
133+
return err
134+
}
135+
}
133136
}
134137
return fmt.Errorf("sysctl %q is not in a separate kernel namespace", s)
135138
}
136139

137140
return nil
138141
}
142+
143+
// checkHostNs checks whether network sysctl is used in host namespace.
144+
func checkHostNs(sysctlConfig string, path string) error {
145+
var currentProcessNetns = "/proc/self/ns/net"
146+
// readlink on the current processes network namespace
147+
destOfCurrentProcess, err := os.Readlink(currentProcessNetns)
148+
if err != nil {
149+
return fmt.Errorf("read soft link %q error", currentProcessNetns)
150+
}
151+
// readlink on the path provided in the struct
152+
destOfContainer, err := os.Readlink(path)
153+
if err != nil {
154+
return fmt.Errorf("read soft link %q error", path)
155+
}
156+
if destOfContainer == destOfCurrentProcess {
157+
return fmt.Errorf("sysctl %q is not allowed in the hosts network namespace", sysctlConfig)
158+
}
159+
return nil
160+
}

libcontainer/configs/validate/validator_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,38 @@ func TestValidateSysctl(t *testing.T) {
201201
}
202202
}
203203
}
204+
205+
func TestValidateSysctlWithSameNs(t *testing.T) {
206+
config := &configs.Config{
207+
Rootfs: "/var",
208+
Sysctl: map[string]string{"net.ctl": "ctl"},
209+
Namespaces: configs.Namespaces(
210+
[]configs.Namespace{
211+
{
212+
Type: configs.NEWNET,
213+
Path: "/proc/self/ns/net",
214+
},
215+
},
216+
),
217+
}
218+
219+
validator := validate.New()
220+
err := validator.Validate(config)
221+
if err == nil {
222+
t.Error("Expected error to occur but it was nil")
223+
}
224+
}
225+
226+
func TestValidateSysctlWithoutNETNamespace(t *testing.T) {
227+
config := &configs.Config{
228+
Rootfs: "/var",
229+
Sysctl: map[string]string{"net.ctl": "ctl"},
230+
Namespaces: []configs.Namespace{},
231+
}
232+
233+
validator := validate.New()
234+
err := validator.Validate(config)
235+
if err == nil {
236+
t.Error("Expected error to occur but it was nil")
237+
}
238+
}

0 commit comments

Comments
 (0)