Skip to content

Commit 6b5fedf

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 c4b67ea commit 6b5fedf

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
}
@@ -426,6 +437,30 @@ LSM_HANDLER_TYPE ksu_handle_prctl(int option, unsigned long arg2, unsigned long
426437
return 0;
427438
}
428439

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

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

0 commit comments

Comments
 (0)