Skip to content

Commit 0de076f

Browse files
committed
kernel: add check flag on escape_to_root
this just adds a flag that if when true, we check if its already root and return. kthread will need this check skipped, so add a way for it to happen. Signed-off-by: backslashxx <[email protected]>
1 parent dadbaa5 commit 0de076f

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

kernel/core_hook.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,18 @@ static void disable_seccomp()
123123
#endif
124124
}
125125

126-
void escape_to_root(void)
126+
void escape_to_root(bool do_check_first)
127127
{
128128
struct cred *cred;
129129

130-
cred = prepare_creds();
131-
if (!cred) {
132-
pr_warn("prepare_creds failed!\n");
130+
if (do_check_first && current_euid().val == 0) {
131+
pr_warn("Already root, don't escape!\n");
133132
return;
134133
}
135134

136-
if (cred->euid.val == 0) {
137-
pr_warn("Already root, don't escape!\n");
138-
abort_creds(cred);
135+
cred = prepare_creds();
136+
if (!cred) {
137+
pr_warn("prepare_creds failed!\n");
139138
return;
140139
}
141140

@@ -331,7 +330,7 @@ int ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3,
331330
if (arg2 == CMD_GRANT_ROOT) {
332331
if (is_allow_su()) {
333332
pr_info("allow root for: %d\n", current_uid().val);
334-
escape_to_root();
333+
escape_to_root(true);
335334
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
336335
pr_err("grant_root: prctl reply error\n");
337336
}

kernel/ksud.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ extern u32 ksu_devpts_sid;
1414
extern bool ksu_execveat_hook __read_mostly;
1515
extern int ksu_handle_pre_ksud(const char *filename);
1616

17+
extern void escape_to_root(bool do_check_first);
18+
1719
#endif

kernel/sucompat.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#define SU_PATH "/system/bin/su"
2424
#define SH_PATH "/system/bin/sh"
2525

26-
extern void escape_to_root();
27-
2826
static bool ksu_sucompat_non_kp __read_mostly = true;
2927

3028
static void __user *userspace_stack_buffer(const void *d, size_t len)
@@ -85,7 +83,7 @@ static int ksu_sucompat_user_common(const char __user **filename_user,
8583
if (escalate) {
8684
pr_info("%s su found\n", syscall_name);
8785
*filename_user = ksud_user_path();
88-
escape_to_root(); // escalate !!
86+
escape_to_root(true); // escalate !!
8987
} else {
9088
pr_info("%s su->sh!\n", syscall_name);
9189
*filename_user = sh_user_path();
@@ -147,7 +145,7 @@ int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
147145
pr_info("do_execveat_common su found\n");
148146
memcpy((void *)filename->name, sh, sizeof(sh));
149147

150-
escape_to_root();
148+
escape_to_root(true);
151149

152150
return 0;
153151
}

0 commit comments

Comments
 (0)