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"
|
|
|
|
|
2023-04-04 13:12:28 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
coci "github.com/containerd/containerd/oci"
|
2021-04-08 13:37:13 +00:00
|
|
|
"github.com/containerd/containerd/pkg/apparmor"
|
2016-03-18 18:50:19 +00:00
|
|
|
"github.com/docker/docker/container"
|
2022-08-17 21:13:49 +00:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2018-12-10 20:40:40 +00:00
|
|
|
"github.com/docker/docker/oci/caps"
|
2019-08-05 14:37:47 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-03-18 18:50:19 +00:00
|
|
|
)
|
|
|
|
|
2023-04-04 13:12:28 +00:00
|
|
|
func getUserFromContainerd(ctx context.Context, containerdCli *containerd.Client, ec *container.ExecConfig) (specs.User, error) {
|
|
|
|
ctr, err := containerdCli.LoadContainer(ctx, ec.Container.ID)
|
|
|
|
if err != nil {
|
|
|
|
return specs.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cinfo, err := ctr.Info(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return specs.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
spec, err := ctr.Spec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return specs.User{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-25 15:18:32 +00:00
|
|
|
opts := []coci.SpecOpts{
|
2023-04-04 13:12:28 +00:00
|
|
|
coci.WithUser(ec.User),
|
|
|
|
coci.WithAdditionalGIDs(ec.User),
|
2023-10-25 01:28:39 +00:00
|
|
|
coci.WithAppendAdditionalGroups(ec.Container.HostConfig.GroupAdd...),
|
2023-04-04 13:12:28 +00:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(ctx, containerdCli, &cinfo, spec); err != nil {
|
|
|
|
return specs.User{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec.Process.User, nil
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:13:49 +00:00
|
|
|
func (daemon *Daemon) execSetPlatformOpt(ctx context.Context, daemonCfg *config.Config, ec *container.ExecConfig, p *specs.Process) error {
|
2016-03-18 18:50:19 +00:00
|
|
|
if len(ec.User) > 0 {
|
2020-07-29 12:26:05 +00:00
|
|
|
var err error
|
2023-04-04 13:12:28 +00:00
|
|
|
if daemon.UsesSnapshotter() {
|
2023-07-18 11:57:27 +00:00
|
|
|
p.User, err = getUserFromContainerd(ctx, daemon.containerdClient, ec)
|
2023-04-04 13:12:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.User, err = getUser(ec.Container, ec.User)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:12:28 +00:00
|
|
|
|
2016-03-18 18:50:19 +00:00
|
|
|
if ec.Privileged {
|
2022-01-31 20:08:01 +00:00
|
|
|
p.Capabilities = &specs.LinuxCapabilities{
|
|
|
|
Bounding: caps.GetAllCapabilities(),
|
|
|
|
Permitted: caps.GetAllCapabilities(),
|
|
|
|
Effective: caps.GetAllCapabilities(),
|
2017-09-22 13:52:41 +00:00
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2023-04-04 13:12:28 +00:00
|
|
|
|
2021-04-08 13:37:13 +00:00
|
|
|
if apparmor.HostSupports() {
|
2017-03-13 03:57:35 +00:00
|
|
|
var appArmorProfile string
|
2022-05-10 19:59:00 +00:00
|
|
|
if ec.Container.AppArmorProfile != "" {
|
|
|
|
appArmorProfile = ec.Container.AppArmorProfile
|
|
|
|
} else if ec.Container.HostConfig.Privileged {
|
2018-03-02 12:17:56 +00:00
|
|
|
// `docker exec --privileged` does not currently disable AppArmor
|
|
|
|
// profiles. Privileged configuration of the container is inherited
|
2019-10-12 22:04:44 +00:00
|
|
|
appArmorProfile = unconfinedAppArmorProfile
|
2017-03-13 03:57:35 +00:00
|
|
|
} else {
|
2019-08-09 10:33:15 +00:00
|
|
|
appArmorProfile = defaultAppArmorProfile
|
2017-03-13 03:57:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 10:33:15 +00:00
|
|
|
if appArmorProfile == defaultAppArmorProfile {
|
2017-03-13 03:57:35 +00:00
|
|
|
// Unattended upgrades and other fun services can unload AppArmor
|
|
|
|
// profiles inadvertently. Since we cannot store our profile in
|
|
|
|
// /etc/apparmor.d, nor can we practically add other ways of
|
|
|
|
// telling the system to keep our profile loaded, in order to make
|
|
|
|
// sure that we keep the default profile enabled we dynamically
|
|
|
|
// reload it if necessary.
|
|
|
|
if err := ensureDefaultAppArmorProfile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 12:17:56 +00:00
|
|
|
p.ApparmorProfile = appArmorProfile
|
2017-03-13 03:57:35 +00:00
|
|
|
}
|
2019-04-10 18:45:14 +00:00
|
|
|
s := &specs.Spec{Process: p}
|
2022-08-17 21:13:49 +00:00
|
|
|
return withRlimits(daemon, daemonCfg, ec.Container)(ctx, nil, nil, s)
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|