Skip to content

Commit

Permalink
'/' is allowed as a separator in sysctl name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengjiao Liu committed Nov 1, 2021
1 parent a7ccc02 commit 2818710
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
37 changes: 36 additions & 1 deletion libcontainer/configs/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,41 @@ func (v *ConfigValidator) cgroupnamespace(config *configs.Config) error {
return nil
}

// convertSysctlVariableToDotsSeparator can return sysctl variables in dots separator format.
// The '/' separator is also accepted in place of a '.'.
// Convert the sysctl variables to dots separator format for validation.
// More info:
// https://man7.org/linux/man-pages/man8/sysctl.8.html
// https://man7.org/linux/man-pages/man5/sysctl.d.5.html
func convertSysctlVariableToDotsSeparator(val string) string {
if val == "" {
return val
}
sepFunc := func(r rune) bool {
return r == '.' || r == '/'
}
firsSepIndex := strings.IndexFunc(val, sepFunc)
if firsSepIndex != -1 {
if val[firsSepIndex] == '.' {
return val
} else {
f := func(r rune) rune {
switch r {
case '.':
return '/'
case '/':
return '.'
default:
return r
}
}
convertedSysctlVal := strings.Map(f, val)
return convertedSysctlVal
}
}
return val
}

// sysctl validates that the specified sysctl keys are valid or not.
// /proc/sys isn't completely namespaced and depending on which namespaces
// are specified, a subset of sysctls are permitted.
Expand All @@ -150,7 +185,7 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error {
)

for s := range config.Sysctl {
s := strings.Replace(s, "/", ".", -1)
s := convertSysctlVariableToDotsSeparator(s)
if validSysctlMap[s] || strings.HasPrefix(s, "fs.mqueue.") {
if config.Namespaces.Contains(configs.NEWIPC) {
continue
Expand Down
26 changes: 14 additions & 12 deletions libcontainer/configs/validate/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ func TestValidateUsernamespaceWithoutUserNS(t *testing.T) {

func TestValidateSysctl(t *testing.T) {
sysctl := map[string]string{
"fs.mqueue.ctl": "ctl",
"fs/mqueue/ctl": "ctl",
"net.ctl": "ctl",
"net/ctl": "ctl",
"kernel.ctl": "ctl",
"kernel/ctl": "ctl",
"fs.mqueue.ctl": "ctl",
"fs/mqueue/ctl": "ctl",
"net.ctl": "ctl",
"net/ctl": "ctl",
"net.ipv4.conf.eno2/100.rp_filter": "ctl",
"kernel.ctl": "ctl",
"kernel/ctl": "ctl",
}

for k, v := range sysctl {
Expand All @@ -211,12 +212,13 @@ func TestValidateSysctl(t *testing.T) {

func TestValidateValidSysctl(t *testing.T) {
sysctl := map[string]string{
"fs.mqueue.ctl": "ctl",
"fs/mqueue/ctl": "ctl",
"net.ctl": "ctl",
"net/ctl": "ctl",
"kernel.msgmax": "ctl",
"kernel/msgmax": "ctl",
"fs.mqueue.ctl": "ctl",
"fs/mqueue/ctl": "ctl",
"net.ctl": "ctl",
"net/ctl": "ctl",
"net.ipv4.conf.eno2/100.rp_filter": "ctl",
"kernel.msgmax": "ctl",
"kernel/msgmax": "ctl",
}

for k, v := range sysctl {
Expand Down

0 comments on commit 2818710

Please sign in to comment.