setns_init_linux.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // +build linux
  2. package libcontainer
  3. import (
  4. "fmt"
  5. "os"
  6. "github.com/opencontainers/runc/libcontainer/apparmor"
  7. "github.com/opencontainers/runc/libcontainer/keys"
  8. "github.com/opencontainers/runc/libcontainer/label"
  9. "github.com/opencontainers/runc/libcontainer/seccomp"
  10. "github.com/opencontainers/runc/libcontainer/system"
  11. )
  12. // linuxSetnsInit performs the container's initialization for running a new process
  13. // inside an existing container.
  14. type linuxSetnsInit struct {
  15. config *initConfig
  16. }
  17. func (l *linuxSetnsInit) getSessionRingName() string {
  18. return fmt.Sprintf("_ses.%s", l.config.ContainerId)
  19. }
  20. func (l *linuxSetnsInit) Init() error {
  21. // do not inherit the parent's session keyring
  22. if _, err := keyctl.JoinSessionKeyring(l.getSessionRingName()); err != nil {
  23. return err
  24. }
  25. if l.config.NoNewPrivileges {
  26. if err := system.Prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil {
  27. return err
  28. }
  29. }
  30. if l.config.Config.Seccomp != nil {
  31. if err := seccomp.InitSeccomp(l.config.Config.Seccomp); err != nil {
  32. return err
  33. }
  34. }
  35. if err := finalizeNamespace(l.config); err != nil {
  36. return err
  37. }
  38. if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil {
  39. return err
  40. }
  41. if l.config.ProcessLabel != "" {
  42. if err := label.SetProcessLabel(l.config.ProcessLabel); err != nil {
  43. return err
  44. }
  45. }
  46. return system.Execv(l.config.Args[0], l.config.Args[0:], os.Environ())
  47. }