2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-03-18 18:50:19 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-10 18:45:14 +00:00
|
|
|
"context"
|
2016-03-18 18:50:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-04-10 18:45:14 +00:00
|
|
|
"github.com/containerd/containerd/containers"
|
|
|
|
coci "github.com/containerd/containerd/oci"
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2016-03-18 18:50:19 +00:00
|
|
|
"github.com/docker/docker/container"
|
2021-06-07 11:44:32 +00:00
|
|
|
dconfig "github.com/docker/docker/daemon/config"
|
2016-03-19 21:07:17 +00:00
|
|
|
"github.com/docker/docker/profiles/seccomp"
|
2023-06-06 16:57:38 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-03-18 18:50:19 +00:00
|
|
|
)
|
|
|
|
|
2019-10-12 22:39:34 +00:00
|
|
|
const supportsSeccomp = true
|
2016-07-08 22:54:48 +00:00
|
|
|
|
2019-04-10 18:45:14 +00:00
|
|
|
// WithSeccomp sets the seccomp profile
|
|
|
|
func WithSeccomp(daemon *Daemon, c *container.Container) coci.SpecOpts {
|
|
|
|
return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
|
2021-06-07 11:44:32 +00:00
|
|
|
if c.SeccompProfile == dconfig.SeccompProfileUnconfined {
|
2020-09-09 17:23:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-10 18:45:14 +00:00
|
|
|
if c.HostConfig.Privileged {
|
|
|
|
return nil
|
|
|
|
}
|
daemon: load and cache sysInfo on initialization
The `daemon.RawSysInfo()` function can be a heavy operation, as it collects
information about all cgroups on the host, networking, AppArmor, Seccomp, etc.
While looking at our code, I noticed that various parts in the code call this
function, potentially even _multiple times_ per container, for example, it is
called from:
- `verifyPlatformContainerSettings()`
- `oci.WithCgroups()` if the daemon has `cpu-rt-period` or `cpu-rt-runtime` configured
- in `ContainerDecoder.DecodeConfig()`, which is called on boith `container create` and `container commit`
Given that this information is not expected to change during the daemon's
lifecycle, and various information coming from this (such as seccomp and
apparmor status) was already cached, we may as well load it once, and cache
the results in the daemon instance.
This patch updates `daemon.RawSysInfo()` to use a `sync.Once()` so that
it's only executed once for the daemon's lifecycle.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-01-07 11:54:47 +00:00
|
|
|
if !daemon.RawSysInfo().Seccomp {
|
2021-06-07 12:25:52 +00:00
|
|
|
if c.SeccompProfile != "" && c.SeccompProfile != dconfig.SeccompProfileDefault {
|
2019-08-05 13:13:46 +00:00
|
|
|
return fmt.Errorf("seccomp is not enabled in your kernel, cannot run a custom seccomp profile")
|
2019-04-10 18:45:14 +00:00
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Warn("seccomp is not enabled in your kernel, running container without default profile")
|
2021-06-07 11:44:32 +00:00
|
|
|
c.SeccompProfile = dconfig.SeccompProfileUnconfined
|
2019-04-10 18:45:14 +00:00
|
|
|
return nil
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2023-06-06 16:57:38 +00:00
|
|
|
if s.Linux == nil {
|
|
|
|
s.Linux = &specs.Linux{}
|
|
|
|
}
|
2020-09-09 17:23:27 +00:00
|
|
|
var err error
|
|
|
|
switch {
|
2021-06-07 12:25:52 +00:00
|
|
|
case c.SeccompProfile == dconfig.SeccompProfileDefault:
|
|
|
|
s.Linux.Seccomp, err = seccomp.GetDefaultProfile(s)
|
2020-09-09 17:23:27 +00:00
|
|
|
case c.SeccompProfile != "":
|
|
|
|
s.Linux.Seccomp, err = seccomp.LoadProfile(c.SeccompProfile, s)
|
|
|
|
case daemon.seccompProfile != nil:
|
|
|
|
s.Linux.Seccomp, err = seccomp.LoadProfile(string(daemon.seccompProfile), s)
|
2021-06-07 11:50:00 +00:00
|
|
|
case daemon.seccompProfilePath == dconfig.SeccompProfileUnconfined:
|
|
|
|
c.SeccompProfile = dconfig.SeccompProfileUnconfined
|
2020-09-09 17:23:27 +00:00
|
|
|
default:
|
|
|
|
s.Linux.Seccomp, err = seccomp.GetDefaultProfile(s)
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2020-09-09 17:23:27 +00:00
|
|
|
return err
|
2019-04-10 18:45:14 +00:00
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|