1
1
use bitflags:: bitflags;
2
2
use 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 ,
5
6
} ;
6
7
7
8
use crate :: backend:: c:: c_int;
@@ -106,6 +107,9 @@ pub fn move_into_link_name_space(
106
107
fd : BorrowedFd < ' _ > ,
107
108
allowed_type : Option < LinkNameSpaceType > ,
108
109
) -> io:: Result < ( ) > {
110
+ if crate :: fs:: fstatfs ( fd) ?. f_type != i64:: from ( NSFS_MAGIC ) {
111
+ return Err ( io:: Errno :: BADF ) ;
112
+ }
109
113
let allowed_type = allowed_type. map_or ( 0 , |t| t as c_int ) ;
110
114
syscalls:: setns ( fd, allowed_type) . map ( |_r| ( ) )
111
115
}
@@ -124,6 +128,13 @@ pub fn move_into_thread_name_spaces(
124
128
fd : BorrowedFd < ' _ > ,
125
129
allowed_types : ThreadNameSpaceType ,
126
130
) -> 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
+ }
127
138
syscalls:: setns ( fd, allowed_types. bits ( ) as c_int ) . map ( |_r| ( ) )
128
139
}
129
140
0 commit comments