1- use anyhow:: { Context , Result , ensure } ;
1+ use anyhow:: { Context , Result } ;
22use aya:: maps:: { HashMap , MapData } ;
33use aya:: programs:: { CgroupAttachMode , CgroupDevice , Link } ;
44use std:: ffi:: OsStr ;
@@ -23,87 +23,6 @@ bitflags::bitflags! {
2323 }
2424}
2525
26- pub trait DeviceAccessController {
27- /// Set the permission for a specific device.
28- fn set_permission (
29- & mut self ,
30- ty : DeviceType ,
31- major : u32 ,
32- minor : u32 ,
33- access : Access ,
34- ) -> Result < ( ) > ;
35- }
36-
37- pub struct DeviceAccessControllerV1 {
38- cgroup : PathBuf ,
39- }
40-
41- impl DeviceAccessControllerV1 {
42- pub fn new ( cgroup : & Path ) -> Result < Self > {
43- ensure ! (
44- cgroup. is_dir( ) ,
45- "cgroup {} does not exist" ,
46- cgroup. display( )
47- ) ;
48-
49- Ok ( Self {
50- cgroup : cgroup. to_owned ( ) ,
51- } )
52- }
53- }
54-
55- impl DeviceAccessController for DeviceAccessControllerV1 {
56- fn set_permission (
57- & mut self ,
58- ty : DeviceType ,
59- major : u32 ,
60- minor : u32 ,
61- access : Access ,
62- ) -> Result < ( ) > {
63- let mut denied = String :: with_capacity ( 3 ) ;
64- let mut allowed = String :: with_capacity ( 3 ) ;
65-
66- let ty = match ty {
67- DeviceType :: Character => 'c' ,
68- DeviceType :: Block => 'b' ,
69- } ;
70-
71- if access. contains ( Access :: READ ) {
72- allowed. push ( 'r' ) ;
73- } else {
74- denied. push ( 'r' ) ;
75- }
76-
77- if access. contains ( Access :: WRITE ) {
78- allowed. push ( 'w' ) ;
79- } else {
80- denied. push ( 'w' ) ;
81- }
82-
83- if access. contains ( Access :: MKNOD ) {
84- allowed. push ( 'm' ) ;
85- } else {
86- denied. push ( 'm' ) ;
87- }
88-
89- if !denied. is_empty ( ) {
90- std:: fs:: write (
91- self . cgroup . join ( "devices.deny" ) ,
92- format ! ( "{ty} {major}:{minor} {denied}" ) ,
93- ) ?;
94- }
95-
96- if !allowed. is_empty ( ) {
97- std:: fs:: write (
98- self . cgroup . join ( "devices.allow" ) ,
99- format ! ( "{ty} {major}:{minor} {allowed}" ) ,
100- ) ?;
101- }
102-
103- Ok ( ( ) )
104- }
105- }
106-
10726#[ repr( C ) ] // This is read as POD by the BPF program.
10827#[ derive( Clone , Copy ) ]
10928struct Device {
@@ -115,12 +34,18 @@ struct Device {
11534// SAFETY: Device is `repr(C)`` and has no padding.
11635unsafe impl aya:: Pod for Device { }
11736
118- pub struct DeviceAccessControllerV2 {
37+ pub struct DeviceAccessController {
11938 map : HashMap < MapData , Device , u32 > ,
12039 pin : PathBuf ,
12140}
12241
123- impl DeviceAccessControllerV2 {
42+ impl Drop for DeviceAccessController {
43+ fn drop ( & mut self ) {
44+ let _ = std:: fs:: remove_file ( & self . pin ) ;
45+ }
46+ }
47+
48+ impl DeviceAccessController {
12449 pub fn new ( cgroup : & Path ) -> Result < Self > {
12550 // cgroup is of form "/sys/fs/cgroup/system.slice/xxx-yyy.scope", and we can use
12651 // the last part as unique identifier.
@@ -174,16 +99,9 @@ impl DeviceAccessControllerV2 {
17499
175100 Ok ( Self { map, pin } )
176101 }
177- }
178102
179- impl Drop for DeviceAccessControllerV2 {
180- fn drop ( & mut self ) {
181- let _ = std:: fs:: remove_file ( & self . pin ) ;
182- }
183- }
184-
185- impl DeviceAccessController for DeviceAccessControllerV2 {
186- fn set_permission (
103+ /// Set the permission for a specific device.
104+ pub fn set_permission (
187105 & mut self ,
188106 ty : DeviceType ,
189107 major : u32 ,
0 commit comments