Skip to content

Commit 2393b1c

Browse files
committed
Use simple fd field for mountEntry.
We cannot have both srcFD and idMapFD set at the same time. So, we can simplify this struct to only have one field which is used a srcFD most of the time and as idMapFD when we do an id map mount. Signed-off-by: Francis Laniel <[email protected]>
1 parent f474c61 commit 2393b1c

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

libcontainer/rootfs_linux.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ type mountConfig struct {
4040
// mountEntry contains mount data specific to a mount point.
4141
type mountEntry struct {
4242
*configs.Mount
43-
srcFD *int
44-
idmapFD int
43+
fd *int
4544
}
4645

4746
func (m *mountEntry) src() string {
48-
if m.srcFD != nil {
49-
return "/proc/self/fd/" + strconv.Itoa(*m.srcFD)
47+
if m.fd != nil {
48+
return "/proc/self/fd/" + strconv.Itoa(*m.fd)
5049
}
5150
return m.Source
5251
}
@@ -86,20 +85,19 @@ func prepareRootfs(pipe io.ReadWriter, iConfig *initConfig, mountFds mountFds) (
8685
cgroupns: config.Namespaces.Contains(configs.NEWCGROUP),
8786
}
8887
for i, m := range config.Mounts {
89-
entry := mountEntry{Mount: m, idmapFD: -1}
88+
entry := mountEntry{Mount: m}
9089
// Just before the loop we checked that if not empty, len(mountFds) == len(config.Mounts).
9190
// Therefore, we can access mountFds[i] without any concerns.
9291
if mountFds.sourceFds != nil && mountFds.sourceFds[i] != -1 {
93-
entry.srcFD = &mountFds.sourceFds[i]
92+
entry.fd = &mountFds.sourceFds[i]
9493
}
9594

9695
// We validated before we can access idmapFds[i].
9796
if mountFds.idmapFds != nil && mountFds.idmapFds[i] != -1 {
98-
entry.idmapFD = mountFds.idmapFds[i]
99-
}
100-
101-
if entry.idmapFD != -1 && entry.srcFD != nil {
102-
return fmt.Errorf("malformed mountFds and idmapFds slice, entry: %v has fds in both slices", i)
97+
if entry.fd != nil {
98+
return fmt.Errorf("malformed mountFds and idmapFds slice, entry: %v has fds in both slices", i)
99+
}
100+
entry.fd = &mountFds.idmapFds[i]
103101
}
104102

105103
if err := mountToRootfs(mountConfig, entry); err != nil {
@@ -482,10 +480,10 @@ func mountToRootfs(c *mountConfig, m mountEntry) error {
482480
}
483481

484482
if m.IsBind() && m.IsIDMapped() {
485-
if m.idmapFD == -1 {
483+
if m.fd == nil {
486484
return fmt.Errorf("error creating mount %+v: idmapFD is invalid, should point to a valid fd", m)
487485
}
488-
if err := unix.MoveMount(m.idmapFD, "", -1, dest, unix.MOVE_MOUNT_F_EMPTY_PATH); err != nil {
486+
if err := unix.MoveMount(*m.fd, "", -1, dest, unix.MOVE_MOUNT_F_EMPTY_PATH); err != nil {
489487
return fmt.Errorf("error on unix.MoveMount %+v: %w", m, err)
490488
}
491489

@@ -1106,7 +1104,7 @@ func writeSystemProperty(key, value string) error {
11061104
func remount(m mountEntry, rootfs string) error {
11071105
return utils.WithProcfd(rootfs, m.Destination, func(dstFD string) error {
11081106
flags := uintptr(m.Flags | unix.MS_REMOUNT)
1109-
err := mountViaFDs(m.Source, m.srcFD, m.Destination, dstFD, m.Device, flags, "")
1107+
err := mountViaFDs(m.Source, m.fd, m.Destination, dstFD, m.Device, flags, "")
11101108
if err == nil {
11111109
return nil
11121110
}
@@ -1121,7 +1119,7 @@ func remount(m mountEntry, rootfs string) error {
11211119
}
11221120
// ... and retry the mount with ro flag set.
11231121
flags |= unix.MS_RDONLY
1124-
return mountViaFDs(m.Source, m.srcFD, m.Destination, dstFD, m.Device, flags, "")
1122+
return mountViaFDs(m.Source, m.fd, m.Destination, dstFD, m.Device, flags, "")
11251123
})
11261124
}
11271125

@@ -1145,7 +1143,7 @@ func mountPropagate(m mountEntry, rootfs string, mountLabel string) error {
11451143
// inside the container with WithProcfd() -- mounting through a procfd
11461144
// mounts on the target.
11471145
if err := utils.WithProcfd(rootfs, m.Destination, func(dstFD string) error {
1148-
return mountViaFDs(m.Source, m.srcFD, m.Destination, dstFD, m.Device, uintptr(flags), data)
1146+
return mountViaFDs(m.Source, m.fd, m.Destination, dstFD, m.Device, uintptr(flags), data)
11491147
}); err != nil {
11501148
return err
11511149
}

0 commit comments

Comments
 (0)