Skip to content

Commit 1762988

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 59b3c56 commit 1762988

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
struct mount_entry {
256266
char *umountable;
257267
struct list_head list;
@@ -280,7 +290,8 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
280290
bool from_root = 0 == current_uid().val;
281291
bool from_manager = is_manager();
282292

283-
if (!from_root && !from_manager) {
293+
if (!from_root && !from_manager
294+
&& !(is_allow_su() && is_system_bin_su())) {
284295
// only root or manager can access this interface
285296
return 0;
286297
}
@@ -476,6 +487,29 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
476487
return 0;
477488
}
478489

490+
if (arg2 == CMD_ENABLE_SU) {
491+
bool enabled = (arg3 != 0);
492+
if (enabled == ksu_su_compat_enabled) {
493+
pr_info("cmd enable su but no need to change.\n");
494+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {// return the reply_ok directly
495+
pr_err("prctl reply error, cmd: %lu\n", arg2);
496+
}
497+
return 0;
498+
}
499+
500+
if (enabled) {
501+
ksu_sucompat_init();
502+
} else {
503+
ksu_sucompat_exit();
504+
}
505+
ksu_su_compat_enabled = enabled;
506+
507+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
508+
pr_err("prctl reply error, cmd: %lu\n", arg2);
509+
}
510+
return 0;
511+
}
512+
479513
// all other cmds are for 'root manager'
480514
if (!from_manager) {
481515
return 0;
@@ -530,30 +564,6 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
530564
return 0;
531565
}
532566

533-
if (arg2 == CMD_ENABLE_SU) {
534-
bool enabled = (arg3 != 0);
535-
if (enabled == ksu_su_compat_enabled) {
536-
pr_info("cmd enable su but no need to change.\n");
537-
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {// return the reply_ok directly
538-
pr_err("prctl reply error, cmd: %lu\n", arg2);
539-
}
540-
return 0;
541-
}
542-
543-
if (enabled) {
544-
ksu_sucompat_init();
545-
} else {
546-
ksu_sucompat_exit();
547-
}
548-
ksu_su_compat_enabled = enabled;
549-
550-
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
551-
pr_err("prctl reply error, cmd: %lu\n", arg2);
552-
}
553-
554-
return 0;
555-
}
556-
557567
return 0;
558568
}
559569

0 commit comments

Comments
 (0)