setns_init_linux.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package libcontainer
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "github.com/opencontainers/selinux/go-selinux"
  8. "github.com/sirupsen/logrus"
  9. "golang.org/x/sys/unix"
  10. "github.com/opencontainers/runc/libcontainer/apparmor"
  11. "github.com/opencontainers/runc/libcontainer/keys"
  12. "github.com/opencontainers/runc/libcontainer/seccomp"
  13. "github.com/opencontainers/runc/libcontainer/system"
  14. )
  15. // linuxSetnsInit performs the container's initialization for running a new process
  16. // inside an existing container.
  17. type linuxSetnsInit struct {
  18. pipe *os.File
  19. consoleSocket *os.File
  20. config *initConfig
  21. logFd int
  22. }
  23. func (l *linuxSetnsInit) getSessionRingName() string {
  24. return "_ses." + l.config.ContainerId
  25. }
  26. func (l *linuxSetnsInit) Init() error {
  27. if !l.config.Config.NoNewKeyring {
  28. if err := selinux.SetKeyLabel(l.config.ProcessLabel); err != nil {
  29. return err
  30. }
  31. defer selinux.SetKeyLabel("") //nolint: errcheck
  32. // Do not inherit the parent's session keyring.
  33. if _, err := keys.JoinSessionKeyring(l.getSessionRingName()); err != nil {
  34. // Same justification as in standart_init_linux.go as to why we
  35. // don't bail on ENOSYS.
  36. //
  37. // TODO(cyphar): And we should have logging here too.
  38. if !errors.Is(err, unix.ENOSYS) {
  39. return fmt.Errorf("unable to join session keyring: %w", err)
  40. }
  41. }
  42. }
  43. if l.config.CreateConsole {
  44. if err := setupConsole(l.consoleSocket, l.config, false); err != nil {
  45. return err
  46. }
  47. if err := system.Setctty(); err != nil {
  48. return err
  49. }
  50. }
  51. if l.config.NoNewPrivileges {
  52. if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil {
  53. return err
  54. }
  55. }
  56. if err := selinux.SetExecLabel(l.config.ProcessLabel); err != nil {
  57. return err
  58. }
  59. defer selinux.SetExecLabel("") //nolint: errcheck
  60. // Without NoNewPrivileges seccomp is a privileged operation, so we need to
  61. // do this before dropping capabilities; otherwise do it as late as possible
  62. // just before execve so as few syscalls take place after it as possible.
  63. if l.config.Config.Seccomp != nil && !l.config.NoNewPrivileges {
  64. seccompFd, err := seccomp.InitSeccomp(l.config.Config.Seccomp)
  65. if err != nil {
  66. return err
  67. }
  68. if err := syncParentSeccomp(l.pipe, seccompFd); err != nil {
  69. return err
  70. }
  71. }
  72. if err := finalizeNamespace(l.config); err != nil {
  73. return err
  74. }
  75. if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil {
  76. return err
  77. }
  78. // Set seccomp as close to execve as possible, so as few syscalls take
  79. // place afterward (reducing the amount of syscalls that users need to
  80. // enable in their seccomp profiles).
  81. if l.config.Config.Seccomp != nil && l.config.NoNewPrivileges {
  82. seccompFd, err := seccomp.InitSeccomp(l.config.Config.Seccomp)
  83. if err != nil {
  84. return fmt.Errorf("unable to init seccomp: %w", err)
  85. }
  86. if err := syncParentSeccomp(l.pipe, seccompFd); err != nil {
  87. return err
  88. }
  89. }
  90. logrus.Debugf("setns_init: about to exec")
  91. // Close the log pipe fd so the parent's ForwardLogs can exit.
  92. if err := unix.Close(l.logFd); err != nil {
  93. return &os.PathError{Op: "close log pipe", Path: "fd " + strconv.Itoa(l.logFd), Err: err}
  94. }
  95. return system.Execv(l.config.Args[0], l.config.Args[0:], os.Environ())
  96. }