exec_linux.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "github.com/containerd/containerd"
  5. coci "github.com/containerd/containerd/oci"
  6. "github.com/containerd/containerd/pkg/apparmor"
  7. "github.com/docker/docker/container"
  8. "github.com/docker/docker/daemon/config"
  9. "github.com/docker/docker/oci/caps"
  10. specs "github.com/opencontainers/runtime-spec/specs-go"
  11. )
  12. func getUserFromContainerd(ctx context.Context, containerdCli *containerd.Client, ec *container.ExecConfig) (specs.User, error) {
  13. ctr, err := containerdCli.LoadContainer(ctx, ec.Container.ID)
  14. if err != nil {
  15. return specs.User{}, err
  16. }
  17. cinfo, err := ctr.Info(ctx)
  18. if err != nil {
  19. return specs.User{}, err
  20. }
  21. spec, err := ctr.Spec(ctx)
  22. if err != nil {
  23. return specs.User{}, err
  24. }
  25. opts := []coci.SpecOpts{
  26. coci.WithUser(ec.User),
  27. coci.WithAdditionalGIDs(ec.User),
  28. coci.WithAppendAdditionalGroups(ec.Container.HostConfig.GroupAdd...),
  29. }
  30. for _, opt := range opts {
  31. if err := opt(ctx, containerdCli, &cinfo, spec); err != nil {
  32. return specs.User{}, err
  33. }
  34. }
  35. return spec.Process.User, nil
  36. }
  37. func (daemon *Daemon) execSetPlatformOpt(ctx context.Context, daemonCfg *config.Config, ec *container.ExecConfig, p *specs.Process) error {
  38. if len(ec.User) > 0 {
  39. var err error
  40. if daemon.UsesSnapshotter() {
  41. p.User, err = getUserFromContainerd(ctx, daemon.containerdClient, ec)
  42. if err != nil {
  43. return err
  44. }
  45. } else {
  46. p.User, err = getUser(ec.Container, ec.User)
  47. if err != nil {
  48. return err
  49. }
  50. }
  51. }
  52. if ec.Privileged {
  53. p.Capabilities = &specs.LinuxCapabilities{
  54. Bounding: caps.GetAllCapabilities(),
  55. Permitted: caps.GetAllCapabilities(),
  56. Effective: caps.GetAllCapabilities(),
  57. }
  58. }
  59. if apparmor.HostSupports() {
  60. var appArmorProfile string
  61. if ec.Container.AppArmorProfile != "" {
  62. appArmorProfile = ec.Container.AppArmorProfile
  63. } else if ec.Container.HostConfig.Privileged {
  64. // `docker exec --privileged` does not currently disable AppArmor
  65. // profiles. Privileged configuration of the container is inherited
  66. appArmorProfile = unconfinedAppArmorProfile
  67. } else {
  68. appArmorProfile = defaultAppArmorProfile
  69. }
  70. if appArmorProfile == defaultAppArmorProfile {
  71. // Unattended upgrades and other fun services can unload AppArmor
  72. // profiles inadvertently. Since we cannot store our profile in
  73. // /etc/apparmor.d, nor can we practically add other ways of
  74. // telling the system to keep our profile loaded, in order to make
  75. // sure that we keep the default profile enabled we dynamically
  76. // reload it if necessary.
  77. if err := ensureDefaultAppArmorProfile(); err != nil {
  78. return err
  79. }
  80. }
  81. p.ApparmorProfile = appArmorProfile
  82. }
  83. s := &specs.Spec{Process: p}
  84. return withRlimits(daemon, daemonCfg, ec.Container)(ctx, nil, nil, s)
  85. }