@@ -703,6 +703,106 @@ def test_mount_help():
703703
704704 return 0
705705
706+ def test_bind_mount_symlink_nofollow ():
707+ root = get_tests_root ()
708+ file_target = os .path .join (root , "a-file" )
709+ symlink = os .path .join (root , "a-symlink" )
710+ target_content = file_target
711+ file_target_content = "inside-the-file"
712+
713+ with open (file_target , "w+" ) as f :
714+ f .write (file_target_content )
715+
716+ os .symlink (target_content , symlink )
717+
718+ def prepare_rootfs (rootfs ):
719+ path = os .path .join (rootfs , "target" )
720+ os .symlink ("point-to-nowhere" , path )
721+
722+ for userns in [True , False ]:
723+ for src_nofollow in [True , False ]:
724+ conf = base_config ()
725+ add_all_namespaces (conf , userns = userns )
726+
727+ if userns :
728+ getMapping = lambda x : [
729+ {
730+ "containerID" : 0 ,
731+ "hostID" : x ,
732+ "size" : 1
733+ }
734+ ]
735+ conf ['linux' ]['uidMappings' ] = getMapping (os .geteuid ())
736+ conf ['linux' ]['gidMappings' ] = getMapping (os .getegid ())
737+
738+ if src_nofollow :
739+ options = ["bind" , "dest-nofollow" , "src-nofollow" ]
740+ conf ['process' ]['args' ] = ['/init' , 'readlink' , '/target' ]
741+ expected = target_content
742+ else :
743+ options = ["bind" , "dest-nofollow" ]
744+ conf ['process' ]['args' ] = ['/init' , 'cat' , '/target' ]
745+ expected = file_target_content
746+
747+ mount_opt = {"destination" : "/target" , "type" : "bind" , "source" : symlink , "options" : options }
748+ conf ['mounts' ].append (mount_opt )
749+
750+ try :
751+ out , _ = run_and_get_output (conf , hide_stderr = True ,callback_prepare_rootfs = prepare_rootfs )
752+ sys .stderr .write ("got output %s with configuration userns=%s, src-nofollow=%s\n " % (out , userns , src_nofollow ))
753+ if expected not in out :
754+ return - 1
755+ except Exception as e :
756+ sys .stderr .write ("error %s\n " % e )
757+ return - 1
758+
759+ return 0
760+
761+ def test_bind_mount_file_nofollow ():
762+ root = get_tests_root ()
763+ target = os .path .join (root , "a-file" )
764+ target_content = "content-of-file"
765+
766+ with open (target , "w+" ) as f :
767+ f .write (target_content )
768+
769+ def prepare_rootfs (rootfs ):
770+ path = os .path .join (rootfs , "symlink" )
771+ os .symlink ("point-to-nowhere" , path )
772+
773+ for userns in [True , False ]:
774+ for src_nofollow in [True , False ]:
775+ conf = base_config ()
776+ conf ['process' ]['args' ] = ['/init' , 'cat' , '/symlink' ]
777+ add_all_namespaces (conf , userns = userns )
778+
779+ if userns :
780+ getMapping = lambda x : [
781+ {
782+ "containerID" : 0 ,
783+ "hostID" : x ,
784+ "size" : 1
785+ }
786+ ]
787+ conf ['linux' ]['uidMappings' ] = getMapping (os .geteuid ())
788+ conf ['linux' ]['gidMappings' ] = getMapping (os .getegid ())
789+
790+ if src_nofollow :
791+ options = ["bind" , "dest-nofollow" , "src-nofollow" ]
792+ else :
793+ options = ["bind" , "dest-nofollow" ]
794+ mount_opt = {"destination" : "/symlink" , "type" : "bind" , "source" : target , "options" : options }
795+ conf ['mounts' ].append (mount_opt )
796+
797+ try :
798+ out , _ = run_and_get_output (conf , hide_stderr = True ,callback_prepare_rootfs = prepare_rootfs )
799+ sys .stderr .write ("got output %s with configuration userns=%s, src-nofollow=%s\n " % (out , userns , src_nofollow ))
800+ if target_content not in out :
801+ return 1
802+ except Exception as e :
803+ sys .stderr .write ("error %s\n " % e )
804+ return 0
805+
706806all_tests = {
707807 "mount-ro" : test_mount_ro ,
708808 "mount-rro" : test_mount_rro ,
@@ -732,6 +832,8 @@ def test_mount_help():
732832 "mount-ro-cgroup" : test_ro_cgroup ,
733833 "mount-cgroup-without-netns" : test_cgroup_mount_without_netns ,
734834 "mount-copy-symlink" : test_copy_symlink ,
835+ "mount-bind-mount-symlink-nofollow" : test_bind_mount_symlink_nofollow ,
836+ "mount-bind-mount-file-nofollow" : test_bind_mount_file_nofollow ,
735837 "mount-tmpfs-permissions" : test_mount_tmpfs_permissions ,
736838 "mount-add-remove-mounts" : test_add_remove_mounts ,
737839 "mount-help" : test_mount_help ,
0 commit comments