Skip to content

Commit ddf07df

Browse files
committed
kernel: synchronize with the latest 'scope-minimized manual hooks'
-v1.5 __backslashxx/KernelSU#5 ____sucompat: add is_su_allowed ____sucompat: ksu_sucompat_common -> ksu_sucompat_user_common ____sucompat: amend logic within ksu_handle_execveat_sucompat ____sucompat: move ksu_handle_execve_sucompat before ksu_handle_execveat_sucompat ____sucompat: retain some minute logic / style (ksu_sucompat_non_kp) ____sucompat: identify 'sh' (ksu_handle_execveat_sucompat) ____ksud: update comment <><><> Description addendums: kernel: sucompat: increase reliability, commonize and micro-optimize (tiann/KernelSU#2656) backslashxx/KernelSU@c4530ac ... Stale: tiann/KernelSU#2656 <><><> kernel: ksud: migrate ksud execution to security_bprm_check (tiann/KernelSU#2653) backslashxx/KernelSU@cd07ae2 ... as for envp, we pass the pointer then hunt for it when needed My reasoning on adding a fallback on usercopy is that on some devices a fault happens, and it copies garbled data. On my creation of this, I actually had to lock that _nofault copy on a spinlock as a way to mimic preempt_disable/enable without actually doing it. As per user reports, no failed _nofault copies anyway but we have-to-have a fallback for resilience. ... With that, It also provides an inlined copy_from_user_nofault for < 5.8. ... <><><> -https://gitlab.com/pershoot/susfs4ksu/-/tree/gki-android14-6.1-dev
1 parent 494cfd7 commit ddf07df

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

kernel/ksud.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ int ksu_handle_pre_ksud(const char *filename)
190190
if (likely(!ksu_execveat_hook))
191191
return 0;
192192

193-
// not /system/bin/init, not /init, not /system/bin/app_process
193+
// not /system/bin/init, not /init, not /system/bin/app_process (64/32 thingy)
194194
// return 0;
195195
if (likely(strcmp(filename, "/system/bin/init") && strcmp(filename, "/init")
196196
&& !strstarts(filename, "/system/bin/app_process") ))

kernel/sucompat.c

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,31 @@ static char __user *ksud_user_path(void)
6060
return userspace_stack_buffer(ksud_path, sizeof(ksud_path));
6161
}
6262

63-
static int ksu_sucompat_common(const char __user **filename_user, const char *syscall_name,
64-
const bool escalate)
63+
// every little bit helps here
64+
__attribute__((hot, no_stack_protector))
65+
static __always_inline bool is_su_allowed(const void *ptr_to_check)
6566
{
66-
if (unlikely(!filename_user))
67-
return 0;
67+
if (likely(!ksu_is_allow_uid(current_uid().val)))
68+
return false;
69+
70+
if (unlikely(!ptr_to_check))
71+
return false;
6872

6973
#ifndef CONFIG_KSU_KPROBES_HOOK
70-
if (!ksu_sucompat_non_kp) {
71-
return 0;
74+
if (!ksu_sucompat_non_kp) {
75+
return false;
7276
}
7377
#endif
7478

75-
if (!ksu_is_allow_uid(current_uid().val))
76-
return 0;
79+
return true;
80+
}
7781

82+
static int ksu_sucompat_user_common(const char __user **filename_user,
83+
const char *syscall_name,
84+
const bool escalate)
85+
{
7886
char path[sizeof(su) + 1];
79-
if (ksu_copy_from_user_retry(path, *filename_user, sizeof(path)))
87+
if (ksu_copy_from_user_retry(path, *filename_user, sizeof(path)))
8088
return 0;
8189

8290
path[sizeof(path) - 1] = '\0';
@@ -96,10 +104,14 @@ static int ksu_sucompat_common(const char __user **filename_user, const char *sy
96104
return 0;
97105
}
98106

107+
// sys_faccessat
99108
int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
100109
int *__unused_flags)
101110
{
102-
return ksu_sucompat_common(filename_user, "faccessat", false);
111+
if (!is_su_allowed((const void *)filename_user))
112+
return 0;
113+
114+
return ksu_sucompat_user_common(filename_user, "faccessat", false);
103115
}
104116

105117
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) && defined(CONFIG_KSU_SUSFS_SUS_SU)
@@ -125,9 +137,24 @@ struct filename* susfs_ksu_handle_stat(int *dfd, const char __user **filename_us
125137
}
126138
#endif
127139

140+
// sys_newfstatat, sys_fstat64
128141
int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags)
129142
{
130-
return ksu_sucompat_common(filename_user, "newfstatat", false);
143+
if (!is_su_allowed((const void *)filename_user))
144+
return 0;
145+
146+
return ksu_sucompat_user_common(filename_user, "newfstatat", false);
147+
}
148+
149+
// sys_execve, compat_sys_execve
150+
int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
151+
void *__never_use_argv, void *__never_use_envp,
152+
int *__never_use_flags)
153+
{
154+
if (!is_su_allowed((const void *)filename_user))
155+
return 0;
156+
157+
return ksu_sucompat_user_common(filename_user, "sys_execve", true);
131158
}
132159

133160
// the call from execve_handler_pre won't provided correct value for __never_use_argument, use them after fix execve_handler_pre, keeping them for consistence for manually patched code
@@ -136,17 +163,9 @@ int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
136163
int *__never_use_flags)
137164
{
138165
struct filename *filename;
166+
const char sh[] = KSUD_PATH;
139167

140-
if (unlikely(!filename_ptr))
141-
return 0;
142-
143-
#ifndef CONFIG_KSU_KPROBES_HOOK
144-
if (!ksu_sucompat_non_kp) {
145-
return 0;
146-
}
147-
#endif
148-
149-
if (!ksu_is_allow_uid(current_uid().val))
168+
if (!is_su_allowed((const void *)filename_ptr))
150169
return 0;
151170

152171
filename = *filename_ptr;
@@ -158,20 +177,13 @@ int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
158177
return 0;
159178

160179
pr_info("do_execveat_common su found\n");
161-
memcpy((void *)filename->name, ksud_path, sizeof(ksud_path));
180+
memcpy((void *)filename->name, sh, sizeof(sh));
162181

163182
ksu_escape_to_root();
164183

165184
return 0;
166185
}
167186

168-
int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
169-
void *__never_use_argv, void *__never_use_envp,
170-
int *__never_use_flags)
171-
{
172-
return ksu_sucompat_common(filename_user, "sys_execve", true);
173-
}
174-
175187
int ksu_handle_devpts(struct inode *inode)
176188
{
177189
#ifndef CONFIG_KSU_KPROBES_HOOK

0 commit comments

Comments
 (0)