Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libcontainer/cgroups/fs/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (s *DevicesGroup) Apply(d *data) error {
}

func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.IsHostUnprivileged {
return nil
}
if !cgroup.AllowAllDevices {
if err := writeFile(path, "devices.deny", "a"); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions libcontainer/configs/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Cgroup struct {

DeniedDevices []*Device `json:"denied_devices"`

IsHostUnprivileged bool `json:"isHostUnprivileged"`
// Memory limit (in bytes)
Memory int64 `json:"memory"`

Expand Down
3 changes: 3 additions & 0 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type Syscall struct {

// Config defines configuration options for executing a process inside a contained environment.
type Config struct {
// Indicates if the host is unprivileged
// Used by libcontainer to check whether to mknod and write to devices cgroup
IsHostUnprivileged bool `json:"isHostUnprivileged"`
// NoPivotRoot will use MS_MOVE and a chroot to jail the process into the container's rootfs
// This is a common option when the container is running in ramdisk
NoPivotRoot bool `json:"no_pivot_root"`
Expand Down
8 changes: 7 additions & 1 deletion libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,13 @@ func createDevices(config *configs.Config) error {
for _, node := range config.Devices {
// containers running in a user namespace are not allowed to mknod
// devices so we can just bind mount it from the host.
if err := createDeviceNode(config.Rootfs, node, config.Namespaces.Contains(configs.NEWUSER)); err != nil {
isHostUserns := config.IsHostUnprivileged

isContainerUserns := config.Namespaces.Contains(configs.NEWUSER)
if err := createDeviceNode(
config.Rootfs,
node,
isContainerUserns || isHostUserns); err != nil {
syscall.Umask(oldMask)
return err
}
Expand Down
11 changes: 6 additions & 5 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,12 @@ func createLibcontainerConfig(cgroupName string, spec *specs.LinuxSpec, rspec *s
rootfsPath = filepath.Join(cwd, rootfsPath)
}
config := &configs.Config{
Rootfs: rootfsPath,
Capabilities: spec.Linux.Capabilities,
Readonlyfs: spec.Root.Readonly,
Hostname: spec.Hostname,
Rootfs: rootfsPath,
Capabilities: spec.Linux.Capabilities,
Readonlyfs: spec.Root.Readonly,
Hostname: spec.Hostname,
IsHostUnprivileged: rspec.Linux.IsHostUnprivileged,
}

exists := false
if config.RootPropagation, exists = mountPropagationMapping[rspec.Linux.RootfsPropagation]; !exists {
return nil, fmt.Errorf("rootfsPropagation=%v is not supported", rspec.Linux.RootfsPropagation)
Expand Down Expand Up @@ -444,6 +444,7 @@ func createCgroupConfig(name string, spec *specs.LinuxRuntimeSpec, devices []*co
AllowedDevices: append(devices, allowedDevices...),
}
r := spec.Linux.Resources
c.IsHostUnprivileged = spec.Linux.IsHostUnprivileged
c.Memory = r.Memory.Limit
c.MemoryReservation = r.Memory.Reservation
c.MemorySwap = r.Memory.Swap
Expand Down