|
| 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 | +type NsExecSetup struct { |
| 31 | + process *containerProcess |
| 32 | +} |
| 33 | + |
| 34 | +const bufSize int = 4 |
| 35 | + |
| 36 | +// parseNsExecSync runs the given callback function on each message received |
| 37 | +// from the child. It will return once the child sends SYNC_RECVPID_PLS. |
| 38 | +func parseNsExecSync(r io.Reader, fn func(NsExecSyncMsg) error) error { |
| 39 | + logrus.Debugf("start to communicate with the nsexec\n") |
| 40 | + var msg NsExecSyncMsg |
| 41 | + var buf [bufSize]byte |
| 42 | + native := nl.NativeEndian() |
| 43 | + |
| 44 | + for { |
| 45 | + if _, err := io.ReadAtLeast(r, buf[:], bufSize); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + msg = NsExecSyncMsg(native.Uint32(buf[:])) |
| 49 | + if err := fn(msg); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + if msg == syncRecvPidPls { |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + logrus.Debugf("finished communicating with the nsexec\n") |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// ackSyncMsg is used to send a message to the child. |
| 61 | +func ackSyncMsg(f *os.File, msg NsExecSyncMsg) error { |
| 62 | + var buf [bufSize]byte |
| 63 | + native := nl.NativeEndian() |
| 64 | + native.PutUint32(buf[:], uint32(msg)) |
| 65 | + if _, err := unix.Write(int(f.Fd()), buf[:]); err != nil { |
| 66 | + logrus.Debugf("failed to write message to nsexec: %v", err) |
| 67 | + return err |
| 68 | + } |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// helpDoingNsExec is used to help the process to communicate with the nsexec. |
| 73 | +func (s *NsExecSetup) helpDoingNsExec() error { |
| 74 | + syncSock := s.process.comm.stage1SockParent |
| 75 | + err := parseNsExecSync(syncSock, func(msg NsExecSyncMsg) error { |
| 76 | + switch msg { |
| 77 | + case syncUsermapPls: |
| 78 | + logrus.Debugf("stage-1 requested userns mappings") |
| 79 | + if err := s.setupUsermap(); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + return ackSyncMsg(syncSock, syncUsermapAck) |
| 83 | + case syncRecvPidPls: |
| 84 | + logrus.Debugf("stage-1 reports pid") |
| 85 | + var pid uint32 |
| 86 | + if err := binary.Read(syncSock, nl.NativeEndian(), &pid); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + s.process.childPid = int(pid) |
| 90 | + return ackSyncMsg(syncSock, syncRecvPidAck) |
| 91 | + case syncTimeOffsetsPls: |
| 92 | + logrus.Debugf("stage-1 request to configure timens offsets") |
| 93 | + if err := system.UpdateTimeNsOffsets(s.process.cmd.Process.Pid, s.process.container.config.TimeOffsets); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + return ackSyncMsg(syncSock, syncTimeOffsetsAck) |
| 97 | + default: |
| 98 | + } |
| 99 | + return fmt.Errorf("unexpected message %d", msg) |
| 100 | + }) |
| 101 | + _ = syncSock.Close() |
| 102 | + return err |
| 103 | +} |
| 104 | + |
| 105 | +// setupUsermap is used to set up the user mappings. |
| 106 | +func (s *NsExecSetup) setupUsermap() error { |
| 107 | + var uidMapPath, gidMapPath string |
| 108 | + |
| 109 | + // Enable setgroups(2) if we've been asked to. But we also have to explicitly |
| 110 | + // disable setgroups(2) if we're creating a rootless container for single-entry |
| 111 | + // mapping. (this is required since Linux 3.19). |
| 112 | + // For rootless multi-entry mapping, we should use newuidmap/newgidmap |
| 113 | + // to do mapping user namespace. |
| 114 | + if s.process.config.RootlessEUID && !requiresRootOrMappingTool(s.process.config.Config.GIDMappings) { |
| 115 | + _ = system.UpdateSetgroups(s.process.cmd.Process.Pid, system.SetgroupsDeny) |
| 116 | + } |
| 117 | + |
| 118 | + nsMaps := make(map[configs.NamespaceType]string) |
| 119 | + for _, ns := range s.process.container.config.Namespaces { |
| 120 | + if ns.Path != "" { |
| 121 | + nsMaps[ns.Type] = ns.Path |
| 122 | + } |
| 123 | + } |
| 124 | + _, joinExistingUser := nsMaps[configs.NEWUSER] |
| 125 | + if !joinExistingUser { |
| 126 | + // write uid mappings |
| 127 | + if len(s.process.container.config.UIDMappings) > 0 { |
| 128 | + if s.process.container.config.RootlessEUID { |
| 129 | + if path, err := execabs.LookPath("newuidmap"); err == nil { |
| 130 | + uidMapPath = path |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // write gid mappings |
| 136 | + if len(s.process.container.config.GIDMappings) > 0 { |
| 137 | + if s.process.container.config.RootlessEUID { |
| 138 | + if path, err := execabs.LookPath("newgidmap"); err == nil { |
| 139 | + gidMapPath = path |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /* Set up mappings. */ |
| 146 | + if err := system.UpdateUidmap(uidMapPath, s.process.cmd.Process.Pid, s.process.container.config.UIDMappings); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + return system.UpdateGidmap(gidMapPath, s.process.cmd.Process.Pid, s.process.container.config.GIDMappings) |
| 150 | +} |
0 commit comments