Merge pull request #5252 from thaJeztah/separate_userns

Move RunningInUserNS() to its own package
This commit is contained in:
Phil Estes 2021-03-23 08:56:04 -04:00 committed by GitHub
commit 717dde3c27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 13 deletions

View File

@ -24,7 +24,7 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/pkg/userns"
"github.com/containerd/continuity/fs" "github.com/containerd/continuity/fs"
"github.com/containerd/continuity/sysx" "github.com/containerd/continuity/sysx"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -87,7 +87,7 @@ func skipFile(hdr *tar.Header) bool {
switch hdr.Typeflag { switch hdr.Typeflag {
case tar.TypeBlock, tar.TypeChar: case tar.TypeBlock, tar.TypeChar:
// cannot create a device if running in user namespace // cannot create a device if running in user namespace
return sys.RunningInUserNS() return userns.RunningInUserNS()
default: default:
return false return false
} }

View File

@ -26,7 +26,7 @@ import (
"github.com/containerd/containerd/archive" "github.com/containerd/containerd/archive"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/pkg/userns"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -35,7 +35,7 @@ func apply(ctx context.Context, mounts []mount.Mount, r io.Reader) error {
case len(mounts) == 1 && mounts[0].Type == "overlay": case len(mounts) == 1 && mounts[0].Type == "overlay":
// OverlayConvertWhiteout (mknod c 0 0) doesn't work in userns. // OverlayConvertWhiteout (mknod c 0 0) doesn't work in userns.
// https://github.com/containerd/containerd/issues/3762 // https://github.com/containerd/containerd/issues/3762
if sys.RunningInUserNS() { if userns.RunningInUserNS() {
break break
} }
path, parents, err := getOverlayPath(mounts[0].Options) path, parents, err := getOverlayPath(mounts[0].Options)

View File

@ -18,7 +18,7 @@ package server
import ( import (
"github.com/containerd/containerd/pkg/cap" "github.com/containerd/containerd/pkg/cap"
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/pkg/userns"
cni "github.com/containerd/go-cni" cni "github.com/containerd/go-cni"
"github.com/opencontainers/selinux/go-selinux" "github.com/opencontainers/selinux/go-selinux"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -33,7 +33,7 @@ const networkAttachCount = 2
func (c *criService) initPlatform() error { func (c *criService) initPlatform() error {
var err error var err error
if sys.RunningInUserNS() { if userns.RunningInUserNS() {
if !(c.config.DisableCgroup && !c.apparmorEnabled() && c.config.RestrictOOMScoreAdj) { if !(c.config.DisableCgroup && !c.apparmorEnabled() && c.config.RestrictOOMScoreAdj) {
logrus.Warn("Running containerd in a user namespace typically requires disable_cgroup, disable_apparmor, restrict_oom_score_adj set to be true") logrus.Warn("Running containerd in a user namespace typically requires disable_cgroup, disable_apparmor, restrict_oom_score_adj set to be true")
} }

View File

@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
*/ */
package sys package userns
import ( import (
"bufio" "bufio"

View File

@ -16,7 +16,7 @@
limitations under the License. limitations under the License.
*/ */
package sys package userns
// RunningInUserNS is a stub for non-Linux systems // RunningInUserNS is a stub for non-Linux systems
// Always returns false // Always returns false

View File

@ -41,11 +41,11 @@ import (
oomv2 "github.com/containerd/containerd/pkg/oom/v2" oomv2 "github.com/containerd/containerd/pkg/oom/v2"
"github.com/containerd/containerd/pkg/process" "github.com/containerd/containerd/pkg/process"
"github.com/containerd/containerd/pkg/stdio" "github.com/containerd/containerd/pkg/stdio"
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/runtime/v2/runc" "github.com/containerd/containerd/runtime/v2/runc"
"github.com/containerd/containerd/runtime/v2/runc/options" "github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/containerd/runtime/v2/shim" "github.com/containerd/containerd/runtime/v2/shim"
taskAPI "github.com/containerd/containerd/runtime/v2/task" taskAPI "github.com/containerd/containerd/runtime/v2/task"
"github.com/containerd/containerd/sys"
"github.com/containerd/containerd/sys/reaper" "github.com/containerd/containerd/sys/reaper"
runcC "github.com/containerd/go-runc" runcC "github.com/containerd/go-runc"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
@ -386,7 +386,7 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI.
logrus.WithError(err).Error("failed to get root controllers") logrus.WithError(err).Error("failed to get root controllers")
} else { } else {
if err := cg.ToggleControllers(allControllers, cgroupsv2.Enable); err != nil { if err := cg.ToggleControllers(allControllers, cgroupsv2.Enable); err != nil {
if sys.RunningInUserNS() { if userns.RunningInUserNS() {
logrus.WithError(err).Debugf("failed to enable controllers (%v)", allControllers) logrus.WithError(err).Debugf("failed to enable controllers (%v)", allControllers)
} else { } else {
logrus.WithError(err).Errorf("failed to enable controllers (%v)", allControllers) logrus.WithError(err).Errorf("failed to enable controllers (%v)", allControllers)

View File

@ -26,7 +26,7 @@ import (
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/pkg/userns"
"github.com/containerd/continuity/fs" "github.com/containerd/continuity/fs"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -108,7 +108,7 @@ func Supported(root string) error {
// //
// The "userxattr" support is not exposed in "/sys/module/overlay/parameters". // The "userxattr" support is not exposed in "/sys/module/overlay/parameters".
func NeedsUserXAttr(d string) (bool, error) { func NeedsUserXAttr(d string) (bool, error) {
if !sys.RunningInUserNS() { if !userns.RunningInUserNS() {
// we are the real root (i.e., the root in the initial user NS), // we are the real root (i.e., the root in the initial user NS),
// so we do never need "userxattr" opt. // so we do never need "userxattr" opt.
return false, nil return false, nil

View File

@ -24,6 +24,8 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/containerd/containerd/pkg/userns"
) )
const ( const (
@ -42,7 +44,7 @@ func SetOOMScore(pid, score int) error {
} }
defer f.Close() defer f.Close()
if _, err = f.WriteString(strconv.Itoa(score)); err != nil { if _, err = f.WriteString(strconv.Itoa(score)); err != nil {
if os.IsPermission(err) && (RunningInUserNS() || RunningUnprivileged()) { if os.IsPermission(err) && (userns.RunningInUserNS() || RunningUnprivileged()) {
return nil return nil
} }
return err return err

23
sys/userns_deprecated.go Normal file
View File

@ -0,0 +1,23 @@
/*
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 sys
import "github.com/containerd/containerd/pkg/userns"
// RunningInUserNS detects whether we are currently running in a user namespace.
// Deprecated: use github.com/containerd/containerd/pkg/userns.RunningInUserNS instead.
var RunningInUserNS = userns.RunningInUserNS