This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from Netflix/fuse
Lock Down FUSE
- Loading branch information
Showing
10 changed files
with
949 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package docker | ||
|
||
import ( | ||
"github.com/Netflix/titus-executor/executor/runtime/docker/seccomp" | ||
runtimeTypes "github.com/Netflix/titus-executor/executor/runtime/types" | ||
"github.com/docker/docker/api/types/container" | ||
|
||
"fmt" | ||
) | ||
|
||
const ( | ||
SYS_ADMIN = "SYS_ADMIN" // nolint: golint | ||
) | ||
|
||
func addAdditionalCapabilities(c *runtimeTypes.Container, hostCfg *container.HostConfig) map[string]struct{} { | ||
addedCapabilities := make(map[string]struct{}) | ||
|
||
// Set any additional capabilities for this container | ||
if cap := c.TitusInfo.GetCapabilities(); cap != nil { | ||
for _, add := range cap.GetAdd() { | ||
addedCapabilities[add.String()] = struct{}{} | ||
hostCfg.CapAdd = append(hostCfg.CapAdd, add.String()) | ||
} | ||
for _, drop := range cap.GetDrop() { | ||
hostCfg.CapDrop = append(hostCfg.CapDrop, drop.String()) | ||
} | ||
} | ||
return addedCapabilities | ||
} | ||
|
||
func setupAdditionalCapabilities(c *runtimeTypes.Container, hostCfg *container.HostConfig) error { | ||
addedCapabilities := addAdditionalCapabilities(c, hostCfg) | ||
|
||
// Privileged containers automaticaly deactivate seccomp and friends, no need to do this | ||
fuseEnabled, err := c.GetFuseEnabled() | ||
if err != nil { | ||
return err | ||
} | ||
if fuseEnabled || c.TitusInfo.GetAllowNestedContainers() { | ||
if _, ok := addedCapabilities[SYS_ADMIN]; !ok { | ||
hostCfg.CapAdd = append(hostCfg.CapAdd, SYS_ADMIN) | ||
} | ||
} | ||
seccompProfile := "default.json" | ||
apparmorProfile := "" | ||
|
||
if fuseEnabled { | ||
hostCfg.Resources.Devices = append(hostCfg.Resources.Devices, container.DeviceMapping{ | ||
PathOnHost: fuseDev, | ||
PathInContainer: fuseDev, | ||
CgroupPermissions: "rmw", | ||
}) | ||
apparmorProfile = "docker-fuse" | ||
seccompProfile = "fuse-container.json" | ||
|
||
} | ||
// We can do this here because nested containers can do everything fuse containers can | ||
if c.TitusInfo.GetAllowNestedContainers() { | ||
apparmorProfile = "docker-nested" | ||
seccompProfile = "nested-container.json" | ||
|
||
c.Env["TINI_HANDOFF"] = trueString | ||
c.Env["TINI_UNSHARE"] = trueString | ||
} | ||
|
||
if apparmorProfile != "" { | ||
hostCfg.SecurityOpt = append(hostCfg.SecurityOpt, "apparmor:"+apparmorProfile) | ||
} | ||
hostCfg.SecurityOpt = append(hostCfg.SecurityOpt, fmt.Sprintf("seccomp=%s", string(seccomp.MustAsset(seccompProfile)))) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package docker | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Netflix/titus-executor/api/netflix/titus" | ||
runtimeTypes "github.com/Netflix/titus-executor/executor/runtime/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDefaultProfile(t *testing.T) { | ||
c := runtimeTypes.Container{ | ||
TitusInfo: &titus.ContainerInfo{}, | ||
} | ||
hostConfig := container.HostConfig{} | ||
|
||
assert.NoError(t, setupAdditionalCapabilities(&c, &hostConfig)) | ||
|
||
assert.Len(t, hostConfig.CapAdd, 0) | ||
assert.Len(t, hostConfig.CapDrop, 0) | ||
assert.Len(t, hostConfig.SecurityOpt, 1) | ||
} | ||
|
||
func TestFuseProfile(t *testing.T) { | ||
c := runtimeTypes.Container{ | ||
TitusInfo: &titus.ContainerInfo{ | ||
PassthroughAttributes: map[string]string{ | ||
runtimeTypes.FuseEnabledParam: "true", | ||
}, | ||
}, | ||
} | ||
hostConfig := container.HostConfig{} | ||
|
||
assert.NoError(t, setupAdditionalCapabilities(&c, &hostConfig)) | ||
|
||
assert.Contains(t, hostConfig.CapAdd, "SYS_ADMIN") | ||
assert.Len(t, hostConfig.CapDrop, 0) | ||
assert.Len(t, hostConfig.SecurityOpt, 2) | ||
assert.Contains(t, hostConfig.SecurityOpt, "apparmor:docker-fuse") | ||
} | ||
|
||
func TestNestedContainerProfile(t *testing.T) { | ||
c := runtimeTypes.Container{ | ||
Env: map[string]string{}, | ||
TitusInfo: &titus.ContainerInfo{ | ||
AllowNestedContainers: proto.Bool(true), | ||
}, | ||
} | ||
hostConfig := container.HostConfig{} | ||
|
||
assert.NoError(t, setupAdditionalCapabilities(&c, &hostConfig)) | ||
|
||
assert.Contains(t, hostConfig.CapAdd, "SYS_ADMIN") | ||
assert.Len(t, hostConfig.CapDrop, 0) | ||
assert.Len(t, hostConfig.SecurityOpt, 2) | ||
assert.Contains(t, hostConfig.SecurityOpt, "apparmor:docker-nested") | ||
|
||
} | ||
|
||
func TestFuseAndNestedContainerProfileProfile(t *testing.T) { | ||
c := runtimeTypes.Container{ | ||
Env: map[string]string{}, | ||
TitusInfo: &titus.ContainerInfo{ | ||
AllowNestedContainers: proto.Bool(true), | ||
PassthroughAttributes: map[string]string{ | ||
runtimeTypes.FuseEnabledParam: "true", | ||
}, | ||
}, | ||
} | ||
hostConfig := container.HostConfig{} | ||
|
||
assert.NoError(t, setupAdditionalCapabilities(&c, &hostConfig)) | ||
|
||
assert.Contains(t, hostConfig.CapAdd, "SYS_ADMIN") | ||
assert.Len(t, hostConfig.CapDrop, 0) | ||
assert.Len(t, hostConfig.SecurityOpt, 2) | ||
assert.Contains(t, hostConfig.SecurityOpt, "apparmor:docker-nested") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.