Skip to content

Commit 6328410

Browse files
Merge pull request #1149 from cyphar/fix-sysctl-validation
validator: unbreak sysctl net.* validation
2 parents a08733b + 1ab3c03 commit 6328410

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

libcontainer/configs/validate/validator.go

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

libcontainer/configs/validate/validator_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,35 @@ func TestValidateSysctl(t *testing.T) {
202202
}
203203
}
204204

205+
func TestValidateValidSysctl(t *testing.T) {
206+
sysctl := map[string]string{
207+
"fs.mqueue.ctl": "ctl",
208+
"net.ctl": "ctl",
209+
"kernel.msgmax": "ctl",
210+
}
211+
212+
for k, v := range sysctl {
213+
config := &configs.Config{
214+
Rootfs: "/var",
215+
Sysctl: map[string]string{k: v},
216+
Namespaces: []configs.Namespace{
217+
{
218+
Type: configs.NEWNET,
219+
},
220+
{
221+
Type: configs.NEWIPC,
222+
},
223+
},
224+
}
225+
226+
validator := validate.New()
227+
err := validator.Validate(config)
228+
if err != nil {
229+
t.Errorf("Expected error to not occur with {%s=%s} but got: %q", k, v, err)
230+
}
231+
}
232+
}
233+
205234
func TestValidateSysctlWithSameNs(t *testing.T) {
206235
config := &configs.Config{
207236
Rootfs: "/var",

0 commit comments

Comments
 (0)