@@ -398,32 +398,43 @@ func doTmpfsCopyUp(m *configs.Mount, rootfs, mountLabel string) (Err error) {
398398
399399func mountToRootfs (m * configs.Mount , c * mountConfig ) error {
400400 rootfs := c .root
401- mountLabel := c .label
402- mountFd := c .fd
403- dest , err := securejoin .SecureJoin (rootfs , m .Destination )
404- if err != nil {
405- return err
406- }
407401
402+ // procfs and sysfs are special because we need to ensure they are actually
403+ // mounted on a specific path in a container without any funny business.
408404 switch m .Device {
409405 case "proc" , "sysfs" :
410406 // If the destination already exists and is not a directory, we bail
411- // out This is to avoid mounting through a symlink or similar -- which
407+ // out. This is to avoid mounting through a symlink or similar -- which
412408 // has been a "fun" attack scenario in the past.
413409 // TODO: This won't be necessary once we switch to libpathrs and we can
414410 // stop all of these symlink-exchange attacks.
411+ dest := filepath .Clean (m .Destination )
412+ if ! strings .HasPrefix (dest , rootfs ) {
413+ // Do not use securejoin as it resolves symlinks.
414+ dest = filepath .Join (rootfs , dest )
415+ }
415416 if fi , err := os .Lstat (dest ); err != nil {
416417 if ! os .IsNotExist (err ) {
417418 return err
418419 }
419- } else if fi .Mode () & os . ModeDir == 0 {
420+ } else if ! fi .IsDir () {
420421 return fmt .Errorf ("filesystem %q must be mounted on ordinary directory" , m .Device )
421422 }
422423 if err := os .MkdirAll (dest , 0o755 ); err != nil {
423424 return err
424425 }
425- // Selinux kernels do not support labeling of /proc or /sys
426+ // Selinux kernels do not support labeling of /proc or /sys.
426427 return mountPropagate (m , rootfs , "" , nil )
428+ }
429+
430+ mountLabel := c .label
431+ mountFd := c .fd
432+ dest , err := securejoin .SecureJoin (rootfs , m .Destination )
433+ if err != nil {
434+ return err
435+ }
436+
437+ switch m .Device {
427438 case "mqueue" :
428439 if err := os .MkdirAll (dest , 0o755 ); err != nil {
429440 return err
0 commit comments