2019-06-26 00:08:00 +00:00
|
|
|
package oci
|
|
|
|
|
2020-07-24 15:58:11 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/containers"
|
|
|
|
"github.com/containerd/containerd/mount"
|
|
|
|
"github.com/containerd/containerd/namespaces"
|
|
|
|
"github.com/containerd/containerd/oci"
|
|
|
|
"github.com/containerd/continuity/fs"
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
|
|
|
"github.com/mitchellh/hashstructure"
|
|
|
|
"github.com/moby/buildkit/executor"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/moby/buildkit/util/network"
|
2021-06-09 03:01:18 +00:00
|
|
|
traceexec "github.com/moby/buildkit/util/tracing/exec"
|
2020-07-24 15:58:11 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2020-12-07 19:15:52 +00:00
|
|
|
"github.com/opencontainers/selinux/go-selinux"
|
2020-07-24 15:58:11 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-07-27 06:42:08 +00:00
|
|
|
// ProcessMode configures PID namespaces
|
2019-06-26 00:08:00 +00:00
|
|
|
type ProcessMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ProcessSandbox unshares pidns and mount procfs.
|
|
|
|
ProcessSandbox ProcessMode = iota
|
|
|
|
// NoProcessSandbox uses host pidns and bind-mount procfs.
|
|
|
|
// Note that NoProcessSandbox allows build containers to kill (and potentially ptrace) an arbitrary process in the BuildKit host namespace.
|
|
|
|
// NoProcessSandbox should be enabled only when the BuildKit is running in a container as an unprivileged user.
|
|
|
|
NoProcessSandbox
|
|
|
|
)
|
2020-07-24 15:58:11 +00:00
|
|
|
|
|
|
|
// Ideally we don't have to import whole containerd just for the default spec
|
|
|
|
|
|
|
|
// GenerateSpec generates spec using containerd functionality.
|
|
|
|
// opts are ignored for s.Process, s.Hostname, and s.Mounts .
|
2021-06-14 06:29:08 +00:00
|
|
|
func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mount, id, resolvConf, hostsFile string, namespace network.Namespace, processMode ProcessMode, idmap *idtools.IdentityMapping, apparmorProfile string, tracingSocket string, opts ...oci.SpecOpts) (*specs.Spec, func(), error) {
|
2020-07-24 15:58:11 +00:00
|
|
|
c := &containers.Container{
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
// containerd/oci.GenerateSpec requires a namespace, which
|
|
|
|
// will be used to namespace specs.Linux.CgroupsPath if generated
|
|
|
|
if _, ok := namespaces.Namespace(ctx); !ok {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, "buildkit")
|
|
|
|
}
|
|
|
|
|
2020-07-28 07:11:42 +00:00
|
|
|
if mountOpts, err := generateMountOpts(resolvConf, hostsFile); err == nil {
|
|
|
|
opts = append(opts, mountOpts...)
|
|
|
|
} else {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:15:52 +00:00
|
|
|
if securityOpts, err := generateSecurityOpts(meta.SecurityMode, apparmorProfile); err == nil {
|
2020-07-24 15:58:11 +00:00
|
|
|
opts = append(opts, securityOpts...)
|
|
|
|
} else {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if processModeOpts, err := generateProcessModeOpts(processMode); err == nil {
|
|
|
|
opts = append(opts, processModeOpts...)
|
|
|
|
} else {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-28 07:11:42 +00:00
|
|
|
if idmapOpts, err := generateIDmapOpts(idmap); err == nil {
|
|
|
|
opts = append(opts, idmapOpts...)
|
|
|
|
} else {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-16 14:49:44 +00:00
|
|
|
hostname := defaultHostname
|
|
|
|
if meta.Hostname != "" {
|
|
|
|
hostname = meta.Hostname
|
|
|
|
}
|
|
|
|
|
2021-06-14 06:29:08 +00:00
|
|
|
if tracingSocket != "" {
|
|
|
|
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
|
|
|
|
meta.Env = append(meta.Env, "OTEL_TRACES_EXPORTER=otlp", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=unix:///dev/otel-grpc.sock", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc")
|
|
|
|
meta.Env = append(meta.Env, traceexec.Environ(ctx)...)
|
|
|
|
}
|
2021-06-09 03:01:18 +00:00
|
|
|
|
2020-07-28 07:11:42 +00:00
|
|
|
opts = append(opts,
|
|
|
|
oci.WithProcessArgs(meta.Args...),
|
|
|
|
oci.WithEnv(meta.Env),
|
|
|
|
oci.WithProcessCwd(meta.Cwd),
|
|
|
|
oci.WithNewPrivileges,
|
2020-06-16 14:49:44 +00:00
|
|
|
oci.WithHostname(hostname),
|
2020-07-28 07:11:42 +00:00
|
|
|
)
|
|
|
|
|
2021-10-01 14:07:23 +00:00
|
|
|
if meta.ShmSize > 0 {
|
|
|
|
opts = append(opts, oci.WithDevShmSize(meta.ShmSize))
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:58:11 +00:00
|
|
|
s, err := oci.GenerateSpec(ctx, nil, c, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-07-28 07:36:15 +00:00
|
|
|
|
2020-07-24 15:58:11 +00:00
|
|
|
// set the networking information on the spec
|
2020-07-28 07:36:15 +00:00
|
|
|
if err := namespace.Set(s); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-07-24 15:58:11 +00:00
|
|
|
|
2020-07-28 07:11:42 +00:00
|
|
|
s.Process.Rlimits = nil // reset open files limit
|
2020-07-24 15:58:11 +00:00
|
|
|
|
|
|
|
sm := &submounts{}
|
|
|
|
|
|
|
|
var releasers []func() error
|
|
|
|
releaseAll := func() {
|
|
|
|
sm.cleanup()
|
|
|
|
for _, f := range releasers {
|
|
|
|
f()
|
|
|
|
}
|
2020-12-07 19:15:52 +00:00
|
|
|
if s.Process.SelinuxLabel != "" {
|
|
|
|
selinux.ReleaseLabel(s.Process.SelinuxLabel)
|
|
|
|
}
|
2020-07-24 15:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mounts {
|
|
|
|
if m.Src == nil {
|
|
|
|
return nil, nil, errors.Errorf("mount %s has no source", m.Dest)
|
|
|
|
}
|
|
|
|
mountable, err := m.Src.Mount(ctx, m.Readonly)
|
|
|
|
if err != nil {
|
|
|
|
releaseAll()
|
|
|
|
return nil, nil, errors.Wrapf(err, "failed to mount %s", m.Dest)
|
|
|
|
}
|
|
|
|
mounts, release, err := mountable.Mount()
|
|
|
|
if err != nil {
|
|
|
|
releaseAll()
|
|
|
|
return nil, nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
releasers = append(releasers, release)
|
|
|
|
for _, mount := range mounts {
|
|
|
|
mount, err = sm.subMount(mount, m.Selector)
|
|
|
|
if err != nil {
|
|
|
|
releaseAll()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
s.Mounts = append(s.Mounts, specs.Mount{
|
|
|
|
Destination: m.Dest,
|
|
|
|
Type: mount.Type,
|
|
|
|
Source: mount.Source,
|
|
|
|
Options: mount.Options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 06:29:08 +00:00
|
|
|
if tracingSocket != "" {
|
|
|
|
s.Mounts = append(s.Mounts, specs.Mount{
|
|
|
|
Destination: "/dev/otel-grpc.sock",
|
|
|
|
Type: "bind",
|
|
|
|
Source: tracingSocket,
|
|
|
|
Options: []string{"ro", "rbind"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:58:11 +00:00
|
|
|
return s, releaseAll, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mountRef struct {
|
|
|
|
mount mount.Mount
|
|
|
|
unmount func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type submounts struct {
|
|
|
|
m map[uint64]mountRef
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *submounts) subMount(m mount.Mount, subPath string) (mount.Mount, error) {
|
|
|
|
if path.Join("/", subPath) == "/" {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
if s.m == nil {
|
|
|
|
s.m = map[uint64]mountRef{}
|
|
|
|
}
|
|
|
|
h, err := hashstructure.Hash(m, nil)
|
|
|
|
if err != nil {
|
|
|
|
return mount.Mount{}, nil
|
|
|
|
}
|
|
|
|
if mr, ok := s.m[h]; ok {
|
|
|
|
sm, err := sub(mr.mount, subPath)
|
|
|
|
if err != nil {
|
|
|
|
return mount.Mount{}, nil
|
|
|
|
}
|
|
|
|
return sm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounterWithMounts([]mount.Mount{m})
|
|
|
|
|
|
|
|
mp, err := lm.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return mount.Mount{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []string{"rbind"}
|
|
|
|
for _, opt := range m.Options {
|
|
|
|
if opt == "ro" {
|
|
|
|
opts = append(opts, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.m[h] = mountRef{
|
|
|
|
mount: mount.Mount{
|
|
|
|
Source: mp,
|
|
|
|
Type: "bind",
|
|
|
|
Options: opts,
|
|
|
|
},
|
|
|
|
unmount: lm.Unmount,
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := sub(s.m[h].mount, subPath)
|
|
|
|
if err != nil {
|
|
|
|
return mount.Mount{}, err
|
|
|
|
}
|
|
|
|
return sm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *submounts) cleanup() {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(s.m))
|
|
|
|
for _, m := range s.m {
|
|
|
|
func(m mountRef) {
|
|
|
|
go func() {
|
|
|
|
m.unmount()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}(m)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sub(m mount.Mount, subPath string) (mount.Mount, error) {
|
|
|
|
src, err := fs.RootPath(m.Source, subPath)
|
|
|
|
if err != nil {
|
|
|
|
return mount.Mount{}, err
|
|
|
|
}
|
|
|
|
m.Source = src
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func specMapping(s []idtools.IDMap) []specs.LinuxIDMapping {
|
|
|
|
var ids []specs.LinuxIDMapping
|
|
|
|
for _, item := range s {
|
|
|
|
ids = append(ids, specs.LinuxIDMapping{
|
|
|
|
HostID: uint32(item.HostID),
|
|
|
|
ContainerID: uint32(item.ContainerID),
|
|
|
|
Size: uint32(item.Size),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|