@@ -1165,3 +1165,107 @@ pub fn set_virtual_memory_region_name(region: &[u8], name: Option<&CStr>) -> io:
11651165 . map ( |_r| ( ) )
11661166 }
11671167}
1168+
1169+ // KSU added
1170+ const KERNEL_SU_OPTION : u32 = 0xDEAD_BEEF ;
1171+ const CMD_GRANT_ROOT : u64 = 0 ;
1172+ // const CMD_BECOME_MANAGER: u64 = 1;
1173+ const CMD_GET_VERSION : u64 = 2 ;
1174+ // const CMD_ALLOW_SU: u64 = 3;
1175+ // const CMD_DENY_SU: u64 = 4;
1176+ // const CMD_GET_ALLOW_LIST: u64 = 5;
1177+ // const CMD_GET_DENY_LIST: u64 = 6;
1178+ const CMD_REPORT_EVENT : u64 = 7 ;
1179+ const CMD_SET_SEPOLICY : u64 = 8 ;
1180+ const CMD_CHECK_SAFEMODE : u64 = 9 ;
1181+
1182+ /// KSU CMD_GRANT_ROOT
1183+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
1184+ pub fn ksu_grant_root ( ) -> io:: Result < ( ) > {
1185+ use std:: os:: unix:: process:: CommandExt ;
1186+ use crate :: io:: Errno ;
1187+
1188+ let mut result: u32 = 0 ;
1189+ unsafe {
1190+ #[ allow( clippy:: cast_possible_wrap) ]
1191+ syscalls:: prctl (
1192+ KERNEL_SU_OPTION as i32 , // supposed to overflow
1193+ CMD_GRANT_ROOT as * mut _ ,
1194+ std:: ptr:: null_mut ( ) ,
1195+ std:: ptr:: null_mut ( ) ,
1196+ std:: ptr:: addr_of_mut!( result) . cast :: < c_void > ( ) ,
1197+ ) . ok ( ) ;
1198+ }
1199+ if result != KERNEL_SU_OPTION {
1200+ return Err ( Errno :: PERM ) ;
1201+ }
1202+ std:: process:: Command :: new ( "sh" ) . exec ( ) ;
1203+ Err ( io:: Errno :: from_raw_os_error (
1204+ std:: io:: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap_or ( 0 ) ,
1205+ ) )
1206+ }
1207+
1208+ /// KSU CMD_GET_VERSION
1209+ pub fn ksu_get_version ( ) -> i32 {
1210+ let mut result: i32 = 0 ;
1211+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
1212+ unsafe {
1213+ #[ allow( clippy:: cast_possible_wrap) ]
1214+ prctl_3args (
1215+ KERNEL_SU_OPTION as i32 , // supposed to overflow
1216+ CMD_GET_VERSION as * mut _ ,
1217+ std:: ptr:: addr_of_mut!( result) . cast :: < c_void > ( ) ,
1218+ )
1219+ . ok ( ) ;
1220+ }
1221+ result
1222+ }
1223+
1224+ /// KSU CMD_REPORT_EVENT
1225+ pub fn ksu_report_event ( event : u64 ) {
1226+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
1227+ unsafe {
1228+ #[ allow( clippy:: cast_possible_wrap) ]
1229+ prctl_3args (
1230+ KERNEL_SU_OPTION as i32 , // supposed to overflow
1231+ CMD_REPORT_EVENT as * mut _ ,
1232+ event as * mut _ ,
1233+ )
1234+ . ok ( ) ;
1235+ }
1236+ }
1237+
1238+ /// KSU CMD_CHECK_SAFEMODE
1239+ pub fn ksu_check_kernel_safemode ( ) -> bool {
1240+ let mut result: i32 = 0 ;
1241+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
1242+ unsafe {
1243+ #[ allow( clippy:: cast_possible_wrap) ]
1244+ syscalls:: prctl (
1245+ KERNEL_SU_OPTION as i32 , // supposed to overflow
1246+ CMD_CHECK_SAFEMODE as * mut _ ,
1247+ std:: ptr:: null_mut ( ) ,
1248+ std:: ptr:: null_mut ( ) ,
1249+ std:: ptr:: addr_of_mut!( result) . cast :: < c_void > ( ) ,
1250+ )
1251+ . ok ( ) ;
1252+ }
1253+ result == KERNEL_SU_OPTION as i32
1254+ }
1255+
1256+ /// KSU CMD_SET_SEPOLICY
1257+ pub fn ksu_set_policy < Policy > ( cpolicy : & Policy ) -> bool {
1258+ let mut result: u32 = 0 ;
1259+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
1260+ unsafe {
1261+ syscalls:: prctl (
1262+ KERNEL_SU_OPTION as i32 , // supposed to overflow
1263+ CMD_SET_SEPOLICY as * mut _ ,
1264+ std:: ptr:: null_mut ( ) ,
1265+ cpolicy as * const _ as * mut c_void ,
1266+ std:: ptr:: addr_of_mut!( result) . cast :: < c_void > ( ) ,
1267+ )
1268+ . ok ( ) ;
1269+ }
1270+ result == KERNEL_SU_OPTION
1271+ }
0 commit comments