|
| 1 | +package libcontainer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/opencontainers/runc/libcontainer/configs" |
| 10 | + "github.com/opencontainers/runc/libcontainer/system" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/vishvananda/netlink/nl" |
| 13 | + "golang.org/x/sys/execabs" |
| 14 | + "golang.org/x/sys/unix" |
| 15 | +) |
| 16 | + |
| 17 | +// NsExecSyncMsg is used for communication between the parent and child during |
| 18 | +// container setup. |
| 19 | +type NsExecSyncMsg uint32 |
| 20 | + |
| 21 | +const ( |
| 22 | + SyncUsermapPls NsExecSyncMsg = iota + 0x40 |
| 23 | + SyncUsermapAck |
| 24 | + SyncRecvPidPls |
| 25 | + SyncRecvPidAck |
| 26 | + SyncTimeOffsetsPls |
| 27 | + SyncTimeOffsetsAck |
| 28 | +) |
| 29 | + |
| 30 | +const bufSize = 4 |
| 31 | + |
| 32 | +// setupNsExec is used to help nsexec to setup the container and wait the container's pid. |
| 33 | +func (s *containerProcess) setupNsExec(syncSock *os.File) error { |
| 34 | + logrus.Debugf("waiting nsexec to report the container's pid") |
| 35 | + err := ParseNsExecSync(syncSock, func(msg NsExecSyncMsg) error { |
| 36 | + switch msg { |
| 37 | + case SyncUsermapPls: |
| 38 | + logrus.Debugf("nsexec has requested userns mappings") |
| 39 | + if err := s.setupUsermap(); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + return AckNsExecSync(syncSock, SyncUsermapAck) |
| 43 | + case SyncTimeOffsetsPls: |
| 44 | + logrus.Debugf("nsexec has requested to configure timens offsets") |
| 45 | + if err := system.UpdateTimeNsOffsets(s.cmd.Process.Pid, s.container.config.TimeOffsets); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + return AckNsExecSync(syncSock, SyncTimeOffsetsAck) |
| 49 | + case SyncRecvPidPls: |
| 50 | + logrus.Debugf("nsexec has reported pid") |
| 51 | + var pid uint32 |
| 52 | + if err := binary.Read(syncSock, nl.NativeEndian(), &pid); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + s.childPid = int(pid) |
| 56 | + return AckNsExecSync(syncSock, SyncRecvPidAck) |
| 57 | + default: |
| 58 | + return fmt.Errorf("unexpected message %d", msg) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + return err |
| 63 | +} |
| 64 | + |
| 65 | +// ParseNsExecSync runs the given callback function on each message received |
| 66 | +// from the child. It will return once the child sends SYNC_RECVPID_PLS. |
| 67 | +func ParseNsExecSync(r io.Reader, fn func(NsExecSyncMsg) error) error { |
| 68 | + var ( |
| 69 | + msg NsExecSyncMsg |
| 70 | + buf [bufSize]byte |
| 71 | + ) |
| 72 | + |
| 73 | + native := nl.NativeEndian() |
| 74 | + |
| 75 | + for { |
| 76 | + if _, err := io.ReadAtLeast(r, buf[:], bufSize); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + msg = NsExecSyncMsg(native.Uint32(buf[:])) |
| 80 | + if err := fn(msg); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + if msg == SyncRecvPidPls { |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// AckNsExecSync is used to send a message to the child. |
| 91 | +func AckNsExecSync(f *os.File, msg NsExecSyncMsg) error { |
| 92 | + var buf [bufSize]byte |
| 93 | + native := nl.NativeEndian() |
| 94 | + native.PutUint32(buf[:], uint32(msg)) |
| 95 | + if _, err := unix.Write(int(f.Fd()), buf[:]); err != nil { |
| 96 | + logrus.Debugf("failed to write message to nsexec: %v", err) |
| 97 | + return err |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// setupUsermap is used to set up the user mappings. |
| 103 | +func (s *containerProcess) setupUsermap() error { |
| 104 | + var uidMapPath, gidMapPath string |
| 105 | + |
| 106 | + // Enable setgroups(2) if we've been asked to. But we also have to explicitly |
| 107 | + // disable setgroups(2) if we're creating a rootless container for single-entry |
| 108 | + // mapping. (this is required since Linux 3.19). |
| 109 | + // For rootless multi-entry mapping, we should use newuidmap/newgidmap |
| 110 | + // to do mapping user namespace. |
| 111 | + if s.config.Config.RootlessEUID && !requiresRootOrMappingTool(s.config.Config.GIDMappings) { |
| 112 | + _ = system.UpdateSetgroups(s.cmd.Process.Pid, system.SetgroupsDeny) |
| 113 | + } |
| 114 | + |
| 115 | + nsMaps := make(map[configs.NamespaceType]string) |
| 116 | + for _, ns := range s.container.config.Namespaces { |
| 117 | + if ns.Path != "" { |
| 118 | + nsMaps[ns.Type] = ns.Path |
| 119 | + } |
| 120 | + } |
| 121 | + _, joinExistingUser := nsMaps[configs.NEWUSER] |
| 122 | + if !joinExistingUser { |
| 123 | + // write uid mappings |
| 124 | + if len(s.container.config.UIDMappings) > 0 { |
| 125 | + if s.container.config.RootlessEUID { |
| 126 | + if path, err := execabs.LookPath("newuidmap"); err == nil { |
| 127 | + uidMapPath = path |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // write gid mappings |
| 133 | + if len(s.container.config.GIDMappings) > 0 { |
| 134 | + if s.container.config.RootlessEUID { |
| 135 | + if path, err := execabs.LookPath("newgidmap"); err == nil { |
| 136 | + gidMapPath = path |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /* Set up mappings. */ |
| 143 | + if err := system.UpdateUidmap(uidMapPath, s.cmd.Process.Pid, s.container.config.UIDMappings); err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + return system.UpdateGidmap(gidMapPath, s.cmd.Process.Pid, s.container.config.GIDMappings) |
| 147 | +} |
0 commit comments