Skip to content

Commit

Permalink
security entitlement support
Browse files Browse the repository at this point in the history
Signed-off-by: Kunal Kushwaha <[email protected]>
  • Loading branch information
kunalkushwaha committed Aug 27, 2018
1 parent 5ea2f71 commit 19a2961
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 14 deletions.
4 changes: 0 additions & 4 deletions api/services/control/control.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ message SolveRequest {
string Frontend = 6;
map<string, string> FrontendAttrs = 7;
CacheOptions Cache = 8 [(gogoproto.nullable) = false];
<<<<<<< af46188e9b3e8c78cfee8a223919498680276122
repeated string Entitlements = 9 [(gogoproto.customtype) = "github.com/moby/buildkit/util/entitlements.Entitlement" ];
=======
repeated string Entitlements = 9 [(gogoproto.customtype) = "github.com/moby/buildkit/util/entitlements.Entitlement"];
>>>>>>> proto defination
}

message CacheOptions {
Expand Down
6 changes: 6 additions & 0 deletions client/llb/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Meta struct {
ProxyEnv *ProxyEnv
ExtraHosts []HostIP
Network pb.NetMode
Security pb.SecMode
}

func NewExecOp(root Output, meta Meta, readOnly bool, c Constraints) *ExecOp {
Expand Down Expand Up @@ -526,3 +527,8 @@ const (
NetModeHost = pb.NetMode_HOST
NetModeNone = pb.NetMode_NONE
)

const (
SecurityModeUnconfined = pb.SecMode_UNCONFINED
SecurityModeConfined = pb.SecMode_CONFINED
)
16 changes: 15 additions & 1 deletion client/llb/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
keyExtraHost = contextKeyT("llb.exec.extrahost")
keyPlatform = contextKeyT("llb.platform")
keyNetwork = contextKeyT("llb.network")
keySecurity = contextKeyT("llb.security")
)

func addEnv(key, value string) StateOption {
Expand Down Expand Up @@ -152,7 +153,6 @@ func network(v pb.NetMode) StateOption {
return s.WithValue(keyNetwork, v)
}
}

func getNetwork(s State) pb.NetMode {
v := s.Value(keyNetwork)
if v != nil {
Expand All @@ -162,6 +162,20 @@ func getNetwork(s State) pb.NetMode {
return NetModeSandbox
}

func security(v pb.SecMode) StateOption {
return func(s State) State {
return s.WithValue(keySecurity, v)
}
}
func getSecurity(s State) pb.SecMode {
v := s.Value(keySecurity)
if v != nil {
n := v.(pb.SecMode)
return n
}
return SecurityModeConfined
}

type EnvList []KeyValue

type KeyValue struct {
Expand Down
8 changes: 8 additions & 0 deletions client/llb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (s State) Run(ro ...RunOption) ExecState {
ProxyEnv: ei.ProxyEnv,
ExtraHosts: getExtraHosts(ei.State),
Network: getNetwork(ei.State),
Security: getSecurity(ei.State),
}

exec := NewExecOp(s.Output(), meta, ei.ReadonlyRootFS, ei.Constraints)
Expand Down Expand Up @@ -257,6 +258,13 @@ func (s State) Network(n pb.NetMode) State {
func (s State) GetNetwork() pb.NetMode {
return getNetwork(s)
}
func (s State) Security(n pb.SecMode) State {
return security(n)(s)
}

func (s State) GetSecurity() pb.SecMode {
return getSecurity(s)
}

func (s State) With(so ...StateOption) State {
for _, o := range so {
Expand Down
3 changes: 2 additions & 1 deletion executor/containerdexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ func (w containerdExecutor) Exec(ctx context.Context, meta executor.Meta, root c
if meta.ReadonlyRootFS {
opts = append(opts, containerdoci.WithRootFSReadonly())
}
if system.SeccompSupported() {
if system.SeccompSupported() && meta.SecMode == pb.SecMode_UNCONFINED {
opts = append(opts, seccomp.WithDefaultProfile())
}

spec, cleanup, err := oci.GenerateSpec(ctx, meta, mounts, id, resolvConf, hostsFile, meta.NetMode == pb.NetMode_HOST, opts...)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Meta struct {
ReadonlyRootFS bool
ExtraHosts []HostIP
NetMode pb.NetMode
SecMode pb.SecMode
}

type Mount struct {
Expand Down
15 changes: 9 additions & 6 deletions executor/oci/spec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/mitchellh/hashstructure"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/util/entitlements"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand All @@ -40,11 +41,13 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
if err != nil {
return nil, nil, err
}
s.Process.Args = meta.Args
s.Process.Env = meta.Env
s.Process.Cwd = meta.Cwd

s.Mounts = GetMounts(ctx,
ociProfile := entitlements.NewOCIProfile(s, meta.SecMode.String())
ociProfile.OCI.Process.Args = meta.Args
ociProfile.OCI.Process.Env = meta.Env
ociProfile.OCI.Process.Cwd = meta.Cwd

ociProfile.OCI.Mounts = GetMounts(ctx,
withROBind(resolvConf, "/etc/resolv.conf"),
withROBind(hostsFile, "/etc/hosts"),
)
Expand Down Expand Up @@ -81,7 +84,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
releaseAll()
return nil, nil, err
}
s.Mounts = append(s.Mounts, specs.Mount{
ociProfile.OCI.Mounts = append(ociProfile.OCI.Mounts, specs.Mount{
Destination: m.Dest,
Type: mount.Type,
Source: mount.Source,
Expand All @@ -90,7 +93,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
}
}

return s, releaseAll, nil
return ociProfile.OCI, releaseAll, nil
}

type mountRef struct {
Expand Down
4 changes: 4 additions & 0 deletions executor/runcexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (w *runcExecutor) Exec(ctx context.Context, meta executor.Meta, root cache.
}
}()

if meta.SecMode == pb.SecMode_UNCONFINED {
logrus.Info("enabling security.unconfined")
}

resolvConf, err := oci.GetResolvConf(ctx, w.root)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
PrefixPlatform: exportMap,
ExtraHosts: extraHosts,
ForceNetMode: defaultNetMode,
ForceSecMode: defaultSecMode,
})

if err != nil {
Expand Down Expand Up @@ -471,3 +472,17 @@ func parseNetMode(v string) (pb.NetMode, error) {
return 0, errors.Errorf("invalid netmode %s", v)
}
}

func parseSecMode(v string) (pb.SecMode, error) {
if v == "" {
return llb.SecurityModeConfined, nil
}
switch v {
case "confined":
return llb.SecurityModeConfined, nil
case "unconfined":
return llb.SecurityModeUnconfined, nil
default:
return 0, errors.Errorf("invalid secmode %s", v)
}
}
1 change: 1 addition & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ConvertOpt struct {
PrefixPlatform bool
ExtraHosts []llb.HostIP
ForceNetMode pb.NetMode
ForceSecMode pb.SecMode
}

func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, *Image, error) {
Expand Down
1 change: 0 additions & 1 deletion solver/llbsolver/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (b *llbBridge) Solve(ctx context.Context, req frontend.SolveRequest) (res *
if err != nil {
return nil, err
}

edge, err := Load(req.Definition, ValidateEntitlements(ent), WithCacheSources(cms), RuntimePlatforms(b.platforms), WithValidateCaps())
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions solver/llbsolver/ops/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func (e *execOp) Exec(ctx context.Context, inputs []solver.Result) ([]solver.Res
ReadonlyRootFS: readonlyRootFS,
ExtraHosts: extraHosts,
NetMode: e.op.Network,
SecMode: e.op.Security,
}

if e.op.Meta.ProxyEnv != nil {
Expand Down
6 changes: 5 additions & 1 deletion solver/llbsolver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,17 @@ func notifyCompleted(ctx context.Context, v *client.Vertex, err error, cached bo
pw.Write(v.Digest.String(), *v)
}

var AllowNetworkHostUnstable = false // TODO: enable in constructor
var AllowNetworkHostUnstable = false // TODO: enable in constructor
var AllowSecurityUnconfinedUnstable = false // TODO: enable in constructor

func supportedEntitlements() []entitlements.Entitlement {
out := []entitlements.Entitlement{} // nil means no filter
if AllowNetworkHostUnstable {
out = append(out, entitlements.EntitlementNetworkHost)
}
if AllowSecurityUnconfinedUnstable {
out = append(out, entitlements.EntitlementSecurityUnconfined)
}
return out
}

Expand Down
10 changes: 10 additions & 0 deletions solver/llbsolver/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func ValidateEntitlements(ent entitlements.Set) LoadOpt {
return errors.Errorf("%s is not allowed", entitlements.EntitlementNetworkNone)
}
}
if op.Exec.Security == pb.SecMode_CONFINED {
if !ent.Allowed(entitlements.EntitlementSecurityConfined) {
return errors.Errorf("%s is not allowed", entitlements.EntitlementSecurityConfined)
}
}
if op.Exec.Security == pb.SecMode_UNCONFINED {
if !ent.Allowed(entitlements.EntitlementSecurityUnconfined) {
return errors.Errorf("%s is not allowed", entitlements.EntitlementSecurityUnconfined)
}
}
}
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions solver/pb/caps.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
CapExecMetaBase apicaps.CapID = "exec.meta.base"
CapExecMetaProxy apicaps.CapID = "exec.meta.proxyenv"
CapExecMetaNetwork apicaps.CapID = "exec.meta.network"
CapExecMetaSecurity apicaps.CapID = "exec.meta.security"
CapExecMountBind apicaps.CapID = "exec.mount.bind"
CapExecMountCache apicaps.CapID = "exec.mount.cache"
CapExecMountCacheSharing apicaps.CapID = "exec.mount.cache.sharing"
Expand Down Expand Up @@ -169,6 +170,12 @@ func init() {
Status: apicaps.CapStatusExperimental,
})

Caps.Init(apicaps.Cap{
ID: CapExecMetaSecurity,
Enabled: true,
Status: apicaps.CapStatusExperimental,
})

Caps.Init(apicaps.Cap{
ID: CapExecMountBind,
Enabled: true,
Expand Down

0 comments on commit 19a2961

Please sign in to comment.