Skip to content

Commit f474c61

Browse files
committed
Use an *int for srcFD.
Previously to this commit, we used a string for srcFD as /proc/self/fd/NN. This commit modified to this behavior, so srcFD is only an *int and the full path is constructed in mountViaFDs() if srcFD is different than nil. Signed-off-by: Francis Laniel <[email protected]>
1 parent 83a0695 commit f474c61

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

libcontainer/container_linux.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,10 +1419,7 @@ func (c *Container) prepareCriuRestoreMounts(mounts []*configs.Mount) error {
14191419
// set up in the order they are configured.
14201420
if m.Device == "bind" {
14211421
if err := utils.WithProcfd(c.config.Rootfs, m.Destination, func(dstFD string) error {
1422-
if err := mountViaFDs(m.Source, "", m.Destination, dstFD, "", unix.MS_BIND|unix.MS_REC, ""); err != nil {
1423-
return err
1424-
}
1425-
return nil
1422+
return mountViaFDs(m.Source, nil, m.Destination, dstFD, "", unix.MS_BIND|unix.MS_REC, "")
14261423
}); err != nil {
14271424
return err
14281425
}

libcontainer/mount_linux.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type mountError struct {
1111
op string
1212
source string
13-
srcFD string
13+
srcFD *int
1414
target string
1515
dstFD string
1616
flags uintptr
@@ -24,8 +24,8 @@ func (e *mountError) Error() string {
2424

2525
if e.source != "" {
2626
out += "src=" + e.source + ", "
27-
if e.srcFD != "" {
28-
out += "srcFD=" + e.srcFD + ", "
27+
if e.srcFD != nil {
28+
out += "srcFD=" + strconv.Itoa(*e.srcFD) + ", "
2929
}
3030
}
3131
out += "dst=" + e.target
@@ -53,21 +53,23 @@ func (e *mountError) Unwrap() error {
5353
// mount is a simple unix.Mount wrapper, returning an error with more context
5454
// in case it failed.
5555
func mount(source, target, fstype string, flags uintptr, data string) error {
56-
return mountViaFDs(source, "", target, "", fstype, flags, data)
56+
return mountViaFDs(source, nil, target, "", fstype, flags, data)
5757
}
5858

5959
// mountViaFDs is a unix.Mount wrapper which uses srcFD instead of source,
60-
// and dstFD instead of target, unless those are empty. The *FD arguments,
61-
// if non-empty, are expected to be in the form of a path to an opened file
62-
// descriptor on procfs (i.e. "/proc/self/fd/NN").
60+
// and dstFD instead of target, unless those are empty.
61+
// If srcFD is different than nil, its path (i.e. "/proc/self/fd/NN") will be
62+
// constructed by this function.
63+
// dstFD argument, if non-empty, is expected to be in the form of a path to an
64+
// opened file descriptor on procfs (i.e. "/proc/self/fd/NN").
6365
//
6466
// If case an FD is used instead of a source or a target path, the
6567
// corresponding path is only used to add context to an error in case
6668
// the mount operation has failed.
67-
func mountViaFDs(source, srcFD, target, dstFD, fstype string, flags uintptr, data string) error {
69+
func mountViaFDs(source string, srcFD *int, target, dstFD, fstype string, flags uintptr, data string) error {
6870
src := source
69-
if srcFD != "" {
70-
src = srcFD
71+
if srcFD != nil {
72+
src = "/proc/self/fd/" + strconv.Itoa(*srcFD)
7173
}
7274
dst := target
7375
if dstFD != "" {

libcontainer/rootfs_linux.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ type mountConfig struct {
4040
// mountEntry contains mount data specific to a mount point.
4141
type mountEntry struct {
4242
*configs.Mount
43-
srcFD string
43+
srcFD *int
4444
idmapFD int
4545
}
4646

4747
func (m *mountEntry) src() string {
48-
if m.srcFD != "" {
49-
return m.srcFD
48+
if m.srcFD != nil {
49+
return "/proc/self/fd/" + strconv.Itoa(*m.srcFD)
5050
}
5151
return m.Source
5252
}
@@ -90,15 +90,15 @@ func prepareRootfs(pipe io.ReadWriter, iConfig *initConfig, mountFds mountFds) (
9090
// Just before the loop we checked that if not empty, len(mountFds) == len(config.Mounts).
9191
// Therefore, we can access mountFds[i] without any concerns.
9292
if mountFds.sourceFds != nil && mountFds.sourceFds[i] != -1 {
93-
entry.srcFD = "/proc/self/fd/" + strconv.Itoa(mountFds.sourceFds[i])
93+
entry.srcFD = &mountFds.sourceFds[i]
9494
}
9595

9696
// We validated before we can access idmapFds[i].
9797
if mountFds.idmapFds != nil && mountFds.idmapFds[i] != -1 {
9898
entry.idmapFD = mountFds.idmapFds[i]
9999
}
100100

101-
if entry.idmapFD != -1 && entry.srcFD != "" {
101+
if entry.idmapFD != -1 && entry.srcFD != nil {
102102
return fmt.Errorf("malformed mountFds and idmapFds slice, entry: %v has fds in both slices", i)
103103
}
104104

@@ -297,7 +297,7 @@ func mountCgroupV1(m *configs.Mount, c *mountConfig) error {
297297
data = cgroups.CgroupNamePrefix + data
298298
source = "systemd"
299299
}
300-
return mountViaFDs(source, "", b.Destination, dstFD, "cgroup", uintptr(flags), data)
300+
return mountViaFDs(source, nil, b.Destination, dstFD, "cgroup", uintptr(flags), data)
301301
}); err != nil {
302302
return err
303303
}
@@ -329,7 +329,7 @@ func mountCgroupV2(m *configs.Mount, c *mountConfig) error {
329329
return err
330330
}
331331
err = utils.WithProcfd(c.root, m.Destination, func(dstFD string) error {
332-
return mountViaFDs(m.Source, "", m.Destination, dstFD, "cgroup2", uintptr(m.Flags), m.Data)
332+
return mountViaFDs(m.Source, nil, m.Destination, dstFD, "cgroup2", uintptr(m.Flags), m.Data)
333333
})
334334
if err == nil || !(errors.Is(err, unix.EPERM) || errors.Is(err, unix.EBUSY)) {
335335
return err
@@ -403,7 +403,7 @@ func doTmpfsCopyUp(m mountEntry, rootfs, mountLabel string) (Err error) {
403403
return fmt.Errorf("tmpcopyup: failed to copy %s to %s (%s): %w", m.Destination, dstFD, tmpDir, err)
404404
}
405405
// Now move the mount into the container.
406-
if err := mountViaFDs(tmpDir, "", m.Destination, dstFD, "", unix.MS_MOVE, ""); err != nil {
406+
if err := mountViaFDs(tmpDir, nil, m.Destination, dstFD, "", unix.MS_MOVE, ""); err != nil {
407407
return fmt.Errorf("tmpcopyup: failed to move mount: %w", err)
408408
}
409409
return nil
@@ -497,7 +497,7 @@ func mountToRootfs(c *mountConfig, m mountEntry) error {
497497
// system type and data arguments are ignored:
498498
// https://man7.org/linux/man-pages/man2/mount.2.html
499499
// We also ignore procfd because we want to act on dest.
500-
if err := mountViaFDs("", "", dest, dstFD, "", uintptr(pflag), ""); err != nil {
500+
if err := mountViaFDs("", nil, dest, dstFD, "", uintptr(pflag), ""); err != nil {
501501
return err
502502
}
503503
}
@@ -733,7 +733,7 @@ func bindMountDeviceNode(rootfs, dest string, node *devices.Device) error {
733733
_ = f.Close()
734734
}
735735
return utils.WithProcfd(rootfs, dest, func(dstFD string) error {
736-
return mountViaFDs(node.Path, "", dest, dstFD, "bind", unix.MS_BIND, "")
736+
return mountViaFDs(node.Path, nil, dest, dstFD, "bind", unix.MS_BIND, "")
737737
})
738738
}
739739

@@ -1154,7 +1154,7 @@ func mountPropagate(m mountEntry, rootfs string, mountLabel string) error {
11541154
// target needs to be re-opened.
11551155
if err := utils.WithProcfd(rootfs, m.Destination, func(dstFD string) error {
11561156
for _, pflag := range m.PropagationFlags {
1157-
if err := mountViaFDs("", "", m.Destination, dstFD, "", uintptr(pflag), ""); err != nil {
1157+
if err := mountViaFDs("", nil, m.Destination, dstFD, "", uintptr(pflag), ""); err != nil {
11581158
return err
11591159
}
11601160
}

0 commit comments

Comments
 (0)