@@ -187,10 +187,10 @@ func prepareTmp(topTmpDir string) (string, error) {
187187 if err != nil {
188188 return "" , err
189189 }
190- if err := mount (tmpdir , tmpdir , "" , " bind" , unix .MS_BIND , "" ); err != nil {
190+ if err := mount (tmpdir , tmpdir , "bind" , unix .MS_BIND , "" ); err != nil {
191191 return "" , err
192192 }
193- if err := mount ("" , tmpdir , "" , "" , uintptr (unix .MS_PRIVATE ), "" ); err != nil {
193+ if err := mount ("" , tmpdir , "" , uintptr (unix .MS_PRIVATE ), "" ); err != nil {
194194 return "" , err
195195 }
196196 return tmpdir , nil
@@ -262,7 +262,7 @@ func mountCgroupV1(m *configs.Mount, c *mountConfig) error {
262262 if err := os .MkdirAll (subsystemPath , 0o755 ); err != nil {
263263 return err
264264 }
265- if err := utils .WithProcfd (c .root , b .Destination , func (procfd string ) error {
265+ if err := utils .WithProcfd (c .root , b .Destination , func (dstFD string ) error {
266266 flags := defaultMountFlags
267267 if m .Flags & unix .MS_RDONLY != 0 {
268268 flags = flags | unix .MS_RDONLY
@@ -275,7 +275,7 @@ func mountCgroupV1(m *configs.Mount, c *mountConfig) error {
275275 data = cgroups .CgroupNamePrefix + data
276276 source = "systemd"
277277 }
278- return mount (source , b .Destination , procfd , "cgroup" , uintptr (flags ), data )
278+ return mountViaFDs (source , "" , b .Destination , dstFD , "cgroup" , uintptr (flags ), data )
279279 }); err != nil {
280280 return err
281281 }
@@ -306,8 +306,8 @@ func mountCgroupV2(m *configs.Mount, c *mountConfig) error {
306306 if err := os .MkdirAll (dest , 0o755 ); err != nil {
307307 return err
308308 }
309- err = utils .WithProcfd (c .root , m .Destination , func (procfd string ) error {
310- return mount (m .Source , m .Destination , procfd , "cgroup2" , uintptr (m .Flags ), m .Data )
309+ err = utils .WithProcfd (c .root , m .Destination , func (dstFD string ) error {
310+ return mountViaFDs (m .Source , "" , m .Destination , dstFD , "cgroup2" , uintptr (m .Flags ), m .Data )
311311 })
312312 if err == nil || ! (errors .Is (err , unix .EPERM ) || errors .Is (err , unix .EBUSY )) {
313313 return err
@@ -373,15 +373,15 @@ func doTmpfsCopyUp(m *configs.Mount, rootfs, mountLabel string) (Err error) {
373373 }
374374 }()
375375
376- return utils .WithProcfd (rootfs , m .Destination , func (procfd string ) (Err error ) {
376+ return utils .WithProcfd (rootfs , m .Destination , func (dstFD string ) (Err error ) {
377377 // Copy the container data to the host tmpdir. We append "/" to force
378378 // CopyDirectory to resolve the symlink rather than trying to copy the
379379 // symlink itself.
380- if err := fileutils .CopyDirectory (procfd + "/" , tmpDir ); err != nil {
381- return fmt .Errorf ("tmpcopyup: failed to copy %s to %s (%s): %w" , m .Destination , procfd , tmpDir , err )
380+ if err := fileutils .CopyDirectory (dstFD + "/" , tmpDir ); err != nil {
381+ return fmt .Errorf ("tmpcopyup: failed to copy %s to %s (%s): %w" , m .Destination , dstFD , tmpDir , err )
382382 }
383383 // Now move the mount into the container.
384- if err := mount (tmpDir , m .Destination , procfd , "" , unix .MS_MOVE , "" ); err != nil {
384+ if err := mountViaFDs (tmpDir , "" , m .Destination , dstFD , "" , unix .MS_MOVE , "" ); err != nil {
385385 return fmt .Errorf ("tmpcopyup: failed to move mount: %w" , err )
386386 }
387387 return nil
@@ -688,8 +688,8 @@ func bindMountDeviceNode(rootfs, dest string, node *devices.Device) error {
688688 if f != nil {
689689 _ = f .Close ()
690690 }
691- return utils .WithProcfd (rootfs , dest , func (procfd string ) error {
692- return mount (node .Path , dest , procfd , "bind" , unix .MS_BIND , "" )
691+ return utils .WithProcfd (rootfs , dest , func (dstFD string ) error {
692+ return mountViaFDs (node .Path , "" , dest , dstFD , "bind" , unix .MS_BIND , "" )
693693 })
694694}
695695
@@ -786,7 +786,7 @@ func rootfsParentMountPrivate(rootfs string) error {
786786 // shared. Secondly when we bind mount rootfs it will propagate to
787787 // parent namespace and we don't want that to happen.
788788 if sharedMount {
789- return mount ("" , parentMount , "" , "" , unix .MS_PRIVATE , "" )
789+ return mount ("" , parentMount , "" , unix .MS_PRIVATE , "" )
790790 }
791791
792792 return nil
@@ -797,7 +797,7 @@ func prepareRoot(config *configs.Config) error {
797797 if config .RootPropagation != 0 {
798798 flag = config .RootPropagation
799799 }
800- if err := mount ("" , "/" , "" , "" , uintptr (flag ), "" ); err != nil {
800+ if err := mount ("" , "/" , "" , uintptr (flag ), "" ); err != nil {
801801 return err
802802 }
803803
@@ -808,13 +808,13 @@ func prepareRoot(config *configs.Config) error {
808808 return err
809809 }
810810
811- return mount (config .Rootfs , config .Rootfs , "" , " bind" , unix .MS_BIND | unix .MS_REC , "" )
811+ return mount (config .Rootfs , config .Rootfs , "bind" , unix .MS_BIND | unix .MS_REC , "" )
812812}
813813
814814func setReadonly () error {
815815 flags := uintptr (unix .MS_BIND | unix .MS_REMOUNT | unix .MS_RDONLY )
816816
817- err := mount ("" , "/" , "" , "" , flags , "" )
817+ err := mount ("" , "/" , "" , flags , "" )
818818 if err == nil {
819819 return nil
820820 }
@@ -823,7 +823,7 @@ func setReadonly() error {
823823 return & os.PathError {Op : "statfs" , Path : "/" , Err : err }
824824 }
825825 flags |= uintptr (s .Flags )
826- return mount ("" , "/" , "" , "" , flags , "" )
826+ return mount ("" , "/" , "" , flags , "" )
827827}
828828
829829func setupPtmx (config * configs.Config ) error {
@@ -881,7 +881,7 @@ func pivotRoot(rootfs string) error {
881881 // known to cause issues due to races where we still have a reference to a
882882 // mount while a process in the host namespace are trying to operate on
883883 // something they think has no mounts (devicemapper in particular).
884- if err := mount ("" , "." , "" , "" , unix .MS_SLAVE | unix .MS_REC , "" ); err != nil {
884+ if err := mount ("" , "." , "" , unix .MS_SLAVE | unix .MS_REC , "" ); err != nil {
885885 return err
886886 }
887887 // Perform the unmount. MNT_DETACH allows us to unmount /proc/self/cwd.
@@ -930,7 +930,7 @@ func msMoveRoot(rootfs string) error {
930930 for _ , info := range mountinfos {
931931 p := info .Mountpoint
932932 // Be sure umount events are not propagated to the host.
933- if err := mount ("" , p , "" , "" , unix .MS_SLAVE | unix .MS_REC , "" ); err != nil {
933+ if err := mount ("" , p , "" , unix .MS_SLAVE | unix .MS_REC , "" ); err != nil {
934934 if errors .Is (err , unix .ENOENT ) {
935935 // If the mountpoint doesn't exist that means that we've
936936 // already blasted away some parent directory of the mountpoint
@@ -945,15 +945,15 @@ func msMoveRoot(rootfs string) error {
945945 } else {
946946 // If we have not privileges for umounting (e.g. rootless), then
947947 // cover the path.
948- if err := mount ("tmpfs" , p , "" , " tmpfs" , 0 , "" ); err != nil {
948+ if err := mount ("tmpfs" , p , "tmpfs" , 0 , "" ); err != nil {
949949 return err
950950 }
951951 }
952952 }
953953 }
954954
955955 // Move the rootfs on top of "/" in our mount namespace.
956- if err := mount (rootfs , "/" , "" , "" , unix .MS_MOVE , "" ); err != nil {
956+ if err := mount (rootfs , "/" , "" , unix .MS_MOVE , "" ); err != nil {
957957 return err
958958 }
959959 return chroot ()
@@ -991,7 +991,7 @@ func createIfNotExists(path string, isDir bool) error {
991991
992992// readonlyPath will make a path read only.
993993func readonlyPath (path string ) error {
994- if err := mount (path , path , "" , "" , unix .MS_BIND | unix .MS_REC , "" ); err != nil {
994+ if err := mount (path , path , "" , unix .MS_BIND | unix .MS_REC , "" ); err != nil {
995995 if errors .Is (err , os .ErrNotExist ) {
996996 return nil
997997 }
@@ -1004,7 +1004,7 @@ func readonlyPath(path string) error {
10041004 }
10051005 flags := uintptr (s .Flags ) & (unix .MS_NOSUID | unix .MS_NODEV | unix .MS_NOEXEC )
10061006
1007- if err := mount (path , path , "" , "" , flags | unix .MS_BIND | unix .MS_REMOUNT | unix .MS_RDONLY , "" ); err != nil {
1007+ if err := mount (path , path , "" , flags | unix .MS_BIND | unix .MS_REMOUNT | unix .MS_RDONLY , "" ); err != nil {
10081008 return err
10091009 }
10101010
@@ -1025,7 +1025,7 @@ func remountReadonly(m *configs.Mount) error {
10251025 // nosuid, etc.). So, let's use that case so that we can do
10261026 // this re-mount without failing in a userns.
10271027 flags |= unix .MS_REMOUNT | unix .MS_BIND | unix .MS_RDONLY
1028- if err := mount ("" , dest , "" , "" , uintptr (flags ), "" ); err != nil {
1028+ if err := mount ("" , dest , "" , uintptr (flags ), "" ); err != nil {
10291029 if errors .Is (err , unix .EBUSY ) {
10301030 time .Sleep (100 * time .Millisecond )
10311031 continue
@@ -1043,9 +1043,9 @@ func remountReadonly(m *configs.Mount) error {
10431043// For files, maskPath bind mounts /dev/null over the top of the specified path.
10441044// For directories, maskPath mounts read-only tmpfs over the top of the specified path.
10451045func maskPath (path string , mountLabel string ) error {
1046- if err := mount ("/dev/null" , path , "" , "" , unix .MS_BIND , "" ); err != nil && ! errors .Is (err , os .ErrNotExist ) {
1046+ if err := mount ("/dev/null" , path , "" , unix .MS_BIND , "" ); err != nil && ! errors .Is (err , os .ErrNotExist ) {
10471047 if errors .Is (err , unix .ENOTDIR ) {
1048- return mount ("tmpfs" , path , "" , " tmpfs" , unix .MS_RDONLY , label .FormatMountLabel ("" , mountLabel ))
1048+ return mount ("tmpfs" , path , "tmpfs" , unix .MS_RDONLY , label .FormatMountLabel ("" , mountLabel ))
10491049 }
10501050 return err
10511051 }
@@ -1060,28 +1060,28 @@ func writeSystemProperty(key, value string) error {
10601060}
10611061
10621062func remount (m * configs.Mount , rootfs string , mountFd * int ) error {
1063- source := m . Source
1063+ srcFD := ""
10641064 if mountFd != nil {
1065- source = "/proc/self/fd/" + strconv .Itoa (* mountFd )
1065+ srcFD = "/proc/self/fd/" + strconv .Itoa (* mountFd )
10661066 }
10671067
1068- return utils .WithProcfd (rootfs , m .Destination , func (procfd string ) error {
1068+ return utils .WithProcfd (rootfs , m .Destination , func (dstFD string ) error {
10691069 flags := uintptr (m .Flags | unix .MS_REMOUNT )
1070- err := mount ( source , m .Destination , procfd , m .Device , flags , "" )
1070+ err := mountViaFDs ( m . Source , srcFD , m .Destination , dstFD , m .Device , flags , "" )
10711071 if err == nil {
10721072 return nil
10731073 }
10741074 // Check if the source has ro flag...
10751075 var s unix.Statfs_t
1076- if err := unix .Statfs (source , & s ); err != nil {
1077- return & os.PathError {Op : "statfs" , Path : source , Err : err }
1076+ if err := unix .Statfs (m . Source , & s ); err != nil {
1077+ return & os.PathError {Op : "statfs" , Path : m . Source , Err : err }
10781078 }
10791079 if s .Flags & unix .MS_RDONLY != unix .MS_RDONLY {
10801080 return err
10811081 }
10821082 // ... and retry the mount with ro flag set.
10831083 flags |= unix .MS_RDONLY
1084- return mount ( source , m .Destination , procfd , m .Device , flags , "" )
1084+ return mountViaFDs ( m . Source , srcFD , m .Destination , dstFD , m .Device , flags , "" )
10851085 })
10861086}
10871087
@@ -1100,26 +1100,26 @@ func mountPropagate(m *configs.Mount, rootfs string, mountLabel string, mountFd
11001100 flags &= ^ unix .MS_RDONLY
11011101 }
11021102
1103+ srcFD := ""
1104+ if mountFd != nil {
1105+ srcFD = "/proc/self/fd/" + strconv .Itoa (* mountFd )
1106+ }
1107+
11031108 // Because the destination is inside a container path which might be
11041109 // mutating underneath us, we verify that we are actually going to mount
11051110 // inside the container with WithProcfd() -- mounting through a procfd
11061111 // mounts on the target.
1107- source := m .Source
1108- if mountFd != nil {
1109- source = "/proc/self/fd/" + strconv .Itoa (* mountFd )
1110- }
1111-
1112- if err := utils .WithProcfd (rootfs , m .Destination , func (procfd string ) error {
1113- return mount (source , m .Destination , procfd , m .Device , uintptr (flags ), data )
1112+ if err := utils .WithProcfd (rootfs , m .Destination , func (dstFD string ) error {
1113+ return mountViaFDs (m .Source , srcFD , m .Destination , dstFD , m .Device , uintptr (flags ), data )
11141114 }); err != nil {
11151115 return err
11161116 }
11171117 // We have to apply mount propagation flags in a separate WithProcfd() call
11181118 // because the previous call invalidates the passed procfd -- the mount
11191119 // target needs to be re-opened.
1120- if err := utils .WithProcfd (rootfs , m .Destination , func (procfd string ) error {
1120+ if err := utils .WithProcfd (rootfs , m .Destination , func (dstFD string ) error {
11211121 for _ , pflag := range m .PropagationFlags {
1122- if err := mount ("" , m .Destination , procfd , "" , uintptr (pflag ), "" ); err != nil {
1122+ if err := mountViaFDs ("" , "" , m .Destination , dstFD , "" , uintptr (pflag ), "" ); err != nil {
11231123 return err
11241124 }
11251125 }
0 commit comments