@@ -288,7 +288,98 @@ def test_net_devices():
288288
289289 return 0
290290
291+ def test_mknod_fifo_device ():
292+ if is_rootless ():
293+ return 77
294+
295+ conf = base_config ()
296+ add_all_namespaces (conf )
297+ conf ['process' ]['args' ] = ['/init' , 'isfifo' , '/dev/testfifo' ]
298+ conf ['linux' ]['devices' ] = [
299+ {"path" : "/dev/testfifo" , "type" : "p" , "fileMode" : 0o0660 , "uid" : 1 , "gid" : 2 }
300+ ]
301+ try :
302+ run_and_get_output (conf )
303+ except Exception as e :
304+ sys .stderr .write (f"test_mknod_fifo_device failed: %s\n " % e )
305+ return - 1
306+ return 0
307+
308+ def test_mknod_char_device ():
309+ if is_rootless ():
310+ return 77
311+
312+ conf = base_config ()
313+ add_all_namespaces (conf )
314+ conf ['process' ]['args' ] = ['/init' , 'ischar' , '/dev/testchar' ]
315+ conf ['linux' ]['devices' ] = [
316+ {"path" : "/dev/testchar" , "type" : "c" , "major" : 251 , "minor" : 1 , "fileMode" : 0o0640 , "uid" : 3 , "gid" : 4 }
317+ ]
318+ try :
319+ run_and_get_output (conf )
320+ except Exception as e :
321+ sys .stderr .write (f"test_mknod_char_device failed: { e } \n " )
322+ return - 1
323+ return 0
324+
325+ def test_allow_device_read_only ():
326+ if is_rootless ():
327+ return 77
328+
329+ try :
330+ # Best effort load
331+ subprocess .run (["modprobe" , "null_blk" , "nr_devices=1" ])
332+ except :
333+ pass
334+ try :
335+ st = os .stat ("/dev/nullb0" )
336+ major , minor = os .major (st .st_rdev ), os .minor (st .st_rdev )
337+ except :
338+ return 77
339+
340+ conf = base_config ()
341+ add_all_namespaces (conf )
342+
343+ conf ['linux' ]['devices' ] = [{
344+ "path" : "/dev/controlledchar" ,
345+ "type" : "b" ,
346+ "major" : major ,
347+ "minor" : minor ,
348+ "fileMode" : 0o0666
349+ }]
350+ conf ['linux' ]['resources' ] = {
351+ "devices" : [
352+ {"allow" : False , "access" : "rwm" },
353+ {"allow" : True , "type" : "b" , "major" : major , "minor" : minor , "access" : "r" },
354+ ]
355+ }
356+
357+ conf ['process' ]['args' ] = ['/init' , 'open' , '/dev/controlledchar' ]
358+ try :
359+ run_and_get_output (conf )
360+ except Exception as e :
361+ sys .stderr .write (f"test_allow_device_read_only failed: %s\n " % e )
362+ return - 1
363+
364+ conf ['process' ]['args' ] = ['/init' , 'openwronly' , '/dev/controlledchar' ]
365+ try :
366+ run_and_get_output (conf )
367+ sys .stderr .write ("test_allow_device_read_only: write access was unexpectedly allowed.\n " )
368+ return 1
369+ except Exception as e :
370+ output_str = getattr (e , 'output' , b'' ).decode (errors = 'ignore' )
371+ if "Operation not permitted" in output_str or "Permission denied" in output_str :
372+ return 0
373+ else :
374+ sys .stderr .write (f"test_allow_device_read_only (write attempt) failed with: %s, output: %s\n " % (e , output_str ))
375+ return 1
376+
377+ return 1
378+
291379all_tests = {
380+ "mknod-fifo-device" : test_mknod_fifo_device ,
381+ "mknod-char-device" : test_mknod_char_device ,
382+ "allow-device-read-only" : test_allow_device_read_only ,
292383 "owner-device" : test_owner_device ,
293384 "deny-devices" : test_deny_devices ,
294385 "allow-device" : test_allow_device ,
0 commit comments