Skip to content

Commit d96696a

Browse files
nampudbackslashxx
authored andcommitted
kernel: core_hook: add support for KernelNoSU
reorder ksu_handle_prctl checks a bit to allow non-manager to use CMD 15 this allows us to piggyback a small su to KernelSU's permission system after disabling kernel sucompat from: Relax prctl perm check - nampud@95125c3 Allow prctl only for root or manager or su binary - nampud@fa7af67 Refine prctl access check, allow /product/bin/su - nampud@dd466dc Refine prctl check a little bit more - nampud@e7c5b24 Signed-off-by: backslashxx <[email protected]>
1 parent 7c6aa1d commit d96696a

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

kernel/core_hook.c

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ static void nuke_ext4_sysfs() {
252252
static void nuke_ext4_sysfs() { }
253253
#endif
254254

255+
static bool is_system_bin_su()
256+
{
257+
// YES in_execve becomes 0 when it succeeds.
258+
if (!current->mm || current->in_execve)
259+
return false;
260+
261+
// quick af check
262+
return (current->mm->exe_file && !strcmp(current->mm->exe_file->f_path.dentry->d_name.name, "su"));
263+
}
264+
255265
LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3,
256266
unsigned long arg4, unsigned long arg5)
257267
{
@@ -274,7 +284,8 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
274284
bool from_root = 0 == current_uid().val;
275285
bool from_manager = is_manager();
276286

277-
if (!from_root && !from_manager) {
287+
if (!from_root && !from_manager
288+
&& !(is_allow_su() && is_system_bin_su())) {
278289
// only root or manager can access this interface
279290
return 0;
280291
}
@@ -428,6 +439,29 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
428439
return 0;
429440
}
430441

442+
if (arg2 == CMD_ENABLE_SU) {
443+
bool enabled = (arg3 != 0);
444+
if (enabled == ksu_su_compat_enabled) {
445+
pr_info("cmd enable su but no need to change.\n");
446+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {// return the reply_ok directly
447+
pr_err("prctl reply error, cmd: %lu\n", arg2);
448+
}
449+
return 0;
450+
}
451+
452+
if (enabled) {
453+
ksu_sucompat_init();
454+
} else {
455+
ksu_sucompat_exit();
456+
}
457+
ksu_su_compat_enabled = enabled;
458+
459+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
460+
pr_err("prctl reply error, cmd: %lu\n", arg2);
461+
}
462+
return 0;
463+
}
464+
431465
// all other cmds are for 'root manager'
432466
if (!from_manager) {
433467
return 0;
@@ -482,30 +516,6 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
482516
return 0;
483517
}
484518

485-
if (arg2 == CMD_ENABLE_SU) {
486-
bool enabled = (arg3 != 0);
487-
if (enabled == ksu_su_compat_enabled) {
488-
pr_info("cmd enable su but no need to change.\n");
489-
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {// return the reply_ok directly
490-
pr_err("prctl reply error, cmd: %lu\n", arg2);
491-
}
492-
return 0;
493-
}
494-
495-
if (enabled) {
496-
ksu_sucompat_init();
497-
} else {
498-
ksu_sucompat_exit();
499-
}
500-
ksu_su_compat_enabled = enabled;
501-
502-
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
503-
pr_err("prctl reply error, cmd: %lu\n", arg2);
504-
}
505-
506-
return 0;
507-
}
508-
509519
return 0;
510520
}
511521

0 commit comments

Comments
 (0)