11use bitflags:: bitflags;
22use linux_raw_sys:: general:: {
3- CLONE_FILES , CLONE_FS , CLONE_NEWCGROUP , CLONE_NEWIPC , CLONE_NEWNET , CLONE_NEWNS , CLONE_NEWPID ,
4- CLONE_NEWTIME , CLONE_NEWUSER , CLONE_NEWUTS , CLONE_SYSVSEM ,
3+ ANON_INODE_FS_MAGIC , CLONE_FILES , CLONE_FS , CLONE_NEWCGROUP , CLONE_NEWIPC , CLONE_NEWNET ,
4+ CLONE_NEWNS , CLONE_NEWPID , CLONE_NEWTIME , CLONE_NEWUSER , CLONE_NEWUTS , CLONE_SYSVSEM ,
5+ NSFS_MAGIC , PID_FS_MAGIC ,
56} ;
67
78use crate :: backend:: c:: c_int;
@@ -106,6 +107,9 @@ pub fn move_into_link_name_space(
106107 fd : BorrowedFd < ' _ > ,
107108 allowed_type : Option < LinkNameSpaceType > ,
108109) -> io:: Result < ( ) > {
110+ if crate :: fs:: fstatfs ( fd) ?. f_type != i64:: from ( NSFS_MAGIC ) {
111+ return Err ( io:: Errno :: BADF ) ;
112+ }
109113 let allowed_type = allowed_type. map_or ( 0 , |t| t as c_int ) ;
110114 syscalls:: setns ( fd, allowed_type) . map ( |_r| ( ) )
111115}
@@ -124,6 +128,13 @@ pub fn move_into_thread_name_spaces(
124128 fd : BorrowedFd < ' _ > ,
125129 allowed_types : ThreadNameSpaceType ,
126130) -> io:: Result < ( ) > {
131+ // When PIDFDs were added to the Linux kernel in version 5.1 they were implemented
132+ // with anonymous inodes. Later in Linux 6.9 the implementation was upgraded to use
133+ // a new "PID FS".
134+ let f_type = crate :: fs:: fstatfs ( fd) ?. f_type ;
135+ if f_type != i64:: from ( PID_FS_MAGIC ) && f_type != i64:: from ( ANON_INODE_FS_MAGIC ) {
136+ return Err ( io:: Errno :: BADF ) ;
137+ }
127138 syscalls:: setns ( fd, allowed_types. bits ( ) as c_int ) . map ( |_r| ( ) )
128139}
129140
0 commit comments