@@ -51,6 +51,19 @@ struct Config {
5151}
5252
5353fn mknod ( file_name : & OsStr , config : Config ) -> i32 {
54+ if !matches ! (
55+ config. file_type,
56+ FileType :: BlockDevice | FileType :: CharacterDevice | FileType :: Fifo
57+ ) {
58+ let _ = writeln ! (
59+ io:: stderr( ) ,
60+ "{}: {}" ,
61+ uucore:: execution_phrase( ) ,
62+ io:: Error :: from( rustix:: io:: Errno :: INVAL )
63+ ) ;
64+ return 1 ;
65+ }
66+
5467 #[ cfg( all( feature = "selinux" , any( target_os = "android" , target_os = "linux" ) ) ) ]
5568 let selinux_enabled = uucore:: selinux:: is_selinux_enabled ( ) ;
5669 #[ cfg( not( all( feature = "selinux" , any( target_os = "android" , target_os = "linux" ) ) ) ) ]
@@ -278,3 +291,36 @@ fn parse_type(tpe: &str) -> Result<FileType, String> {
278291 _ => Err ( translate ! ( "mknod-error-invalid-device-type" , "type" => tpe. quote( ) ) ) ,
279292 } )
280293}
294+
295+ #[ cfg( test) ]
296+ mod tests {
297+ use super :: { Config , FileType , Mode , mknod} ;
298+ use rstest:: rstest;
299+ use tempfile:: tempdir;
300+
301+ #[ rstest]
302+ #[ case:: regular_file( FileType :: RegularFile ) ]
303+ #[ case:: directory( FileType :: Directory ) ]
304+ #[ case:: symlink( FileType :: Symlink ) ]
305+ #[ case:: socket( FileType :: Socket ) ]
306+ #[ case:: unknown( FileType :: Unknown ) ]
307+ fn test_mknod_rejects_unsupported_file_type ( #[ case] file_type : FileType ) {
308+ let dir = tempdir ( ) . unwrap ( ) ;
309+ let path = dir. path ( ) . join ( "node" ) ;
310+ let config = Config {
311+ mode : Mode :: RWXU ,
312+ file_type,
313+ use_umask : true ,
314+ dev : 0 ,
315+ #[ cfg( any(
316+ all( feature = "selinux" , any( target_os = "android" , target_os = "linux" ) ) ,
317+ all( feature = "smack" , target_os = "linux" ) ,
318+ ) ) ]
319+ set_security_context : false ,
320+ context : None ,
321+ } ;
322+
323+ assert_eq ! ( mknod( path. as_os_str( ) , config) , 1 ) ;
324+ assert ! ( path. symlink_metadata( ) . is_err( ) ) ;
325+ }
326+ }
0 commit comments