Skip to content

Commit 5084a80

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 dbcdbe8 commit 5084a80

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

kernel/core_hook.c

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ static void nuke_ext4_sysfs() {
250250
static void nuke_ext4_sysfs() { }
251251
#endif
252252

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

275-
if (!from_root && !from_manager) {
285+
if (!from_root && !from_manager
286+
&& !(is_allow_su() && is_system_bin_su())) {
276287
// only root or manager can access this interface
277288
return 0;
278289
}
@@ -415,6 +426,30 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
415426
return 0;
416427
}
417428

429+
if (arg2 == CMD_ENABLE_SU) {
430+
bool enabled = (arg3 != 0);
431+
if (enabled == ksu_su_compat_enabled) {
432+
pr_info("cmd enable su but no need to change.\n");
433+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {// return the reply_ok directly
434+
pr_err("prctl reply error, cmd: %lu\n", arg2);
435+
}
436+
return 0;
437+
}
438+
439+
if (enabled) {
440+
ksu_sucompat_init();
441+
} else {
442+
ksu_sucompat_exit();
443+
}
444+
ksu_su_compat_enabled = enabled;
445+
446+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
447+
pr_err("prctl reply error, cmd: %lu\n", arg2);
448+
}
449+
450+
return 0;
451+
}
452+
418453
// all other cmds are for 'root manager'
419454
if (!from_manager) {
420455
return 0;
@@ -469,30 +504,6 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
469504
return 0;
470505
}
471506

472-
if (arg2 == CMD_ENABLE_SU) {
473-
bool enabled = (arg3 != 0);
474-
if (enabled == ksu_su_compat_enabled) {
475-
pr_info("cmd enable su but no need to change.\n");
476-
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {// return the reply_ok directly
477-
pr_err("prctl reply error, cmd: %lu\n", arg2);
478-
}
479-
return 0;
480-
}
481-
482-
if (enabled) {
483-
ksu_sucompat_init();
484-
} else {
485-
ksu_sucompat_exit();
486-
}
487-
ksu_su_compat_enabled = enabled;
488-
489-
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
490-
pr_err("prctl reply error, cmd: %lu\n", arg2);
491-
}
492-
493-
return 0;
494-
}
495-
496507
return 0;
497508
}
498509

0 commit comments

Comments
 (0)