Skip to content

Commit

Permalink
Merge pull request containerd#9172 from lengrongfu/feat/add-validate-…
Browse files Browse the repository at this point in the history
…unprivileged

add verify kernel version when enable unprivileged
  • Loading branch information
samuelkarp committed Nov 8, 2023
2 parents c3101bd + e099717 commit 5149050
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/cri/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,8 @@ func ValidatePluginConfig(ctx context.Context, c *PluginConfig) ([]deprecation.W
return warnings, fmt.Errorf("invalid `drain_exec_sync_io_timeout`: %w", err)
}
}
if err := ValidateEnableUnprivileged(ctx, c); err != nil {
return warnings, err
}
return warnings, nil
}
43 changes: 43 additions & 0 deletions pkg/cri/config/config_kernel_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build linux

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"context"
"errors"
"fmt"

kernel "github.com/containerd/containerd/v2/contrib/seccomp/kernelversion"
)

var kernelGreaterEqualThan = kernel.GreaterEqualThan

func ValidateEnableUnprivileged(ctx context.Context, c *PluginConfig) error {
if c.EnableUnprivilegedICMP || c.EnableUnprivilegedPorts {
fourDotEleven := kernel.KernelVersion{Kernel: 4, Major: 11}
ok, err := kernelGreaterEqualThan(fourDotEleven)
if err != nil {
return fmt.Errorf("check current system kernel version error: %w", err)
}
if !ok {
return errors.New("unprivileged_icmp and unprivileged_port require kernel version greater than or equal to 4.11")
}
}
return nil
}
104 changes: 104 additions & 0 deletions pkg/cri/config/config_kernel_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"context"
"testing"

kernel "github.com/containerd/containerd/v2/contrib/seccomp/kernelversion"
"github.com/stretchr/testify/assert"
)

func TestValidateEnableUnprivileged(t *testing.T) {
origKernelGreaterEqualThan := kernelGreaterEqualThan
t.Cleanup(func() {
kernelGreaterEqualThan = origKernelGreaterEqualThan
})

tests := []struct {
name string
config *PluginConfig
kernelGreater bool
expectedErr string
}{
{
name: "disable unprivileged_icmp and unprivileged_port",
config: &PluginConfig{
ContainerdConfig: ContainerdConfig{
DefaultRuntimeName: RuntimeDefault,
Runtimes: map[string]Runtime{
RuntimeDefault: {
Type: "default",
},
},
},
EnableUnprivilegedICMP: false,
EnableUnprivilegedPorts: false,
},
expectedErr: "",
},
{
name: "enable unprivileged_icmp or unprivileged_port, but kernel version is smaller than 4.11",
config: &PluginConfig{
ContainerdConfig: ContainerdConfig{
DefaultRuntimeName: RuntimeDefault,
Runtimes: map[string]Runtime{
RuntimeDefault: {
Type: "default",
},
},
},
EnableUnprivilegedICMP: true,
EnableUnprivilegedPorts: true,
},
kernelGreater: false,
expectedErr: "unprivileged_icmp and unprivileged_port require kernel version greater than or equal to 4.11",
},
{
name: "enable unprivileged_icmp or unprivileged_port, but kernel version is greater than or equal 4.11",
config: &PluginConfig{
ContainerdConfig: ContainerdConfig{
DefaultRuntimeName: RuntimeDefault,
Runtimes: map[string]Runtime{
RuntimeDefault: {
Type: "default",
},
},
},
EnableUnprivilegedICMP: true,
EnableUnprivilegedPorts: true,
},
kernelGreater: true,
expectedErr: "",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
kernelGreaterEqualThan = func(minVersion kernel.KernelVersion) (bool, error) {
return test.kernelGreater, nil
}
err := ValidateEnableUnprivileged(context.Background(), test.config)
if test.expectedErr != "" {
assert.Equal(t, err.Error(), test.expectedErr)
} else {
assert.NoError(t, err)
}
})
}
}
27 changes: 27 additions & 0 deletions pkg/cri/config/config_kernel_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build !linux

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"context"
)

func ValidateEnableUnprivileged(ctx context.Context, c *PluginConfig) error {
return nil
}

0 comments on commit 5149050

Please sign in to comment.