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