Skip to content

Commit 4593ae8

Browse files
committed
kernel: Allow to re-enable sucompat
1 parent 9bb39ff commit 4593ae8

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

kernel/sucompat.c

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ int ksu_handle_devpts(struct inode *inode)
191191

192192
#ifdef CONFIG_KPROBES
193193

194-
static int sys_faccessat_handler_pre(struct kprobe *p, struct pt_regs *regs)
194+
static int faccessat_handler_pre(struct kprobe *p, struct pt_regs *regs)
195195
{
196196
struct pt_regs *real_regs = PT_REAL_REGS(regs);
197197
int *dfd = (int *)&PT_REGS_PARM1(real_regs);
@@ -202,17 +202,18 @@ static int sys_faccessat_handler_pre(struct kprobe *p, struct pt_regs *regs)
202202
return ksu_handle_faccessat(dfd, filename_user, mode, NULL);
203203
}
204204

205-
static int sys_newfstatat_handler_pre(struct kprobe *p, struct pt_regs *regs)
205+
static int newfstatat_handler_pre(struct kprobe *p, struct pt_regs *regs)
206206
{
207207
struct pt_regs *real_regs = PT_REAL_REGS(regs);
208208
int *dfd = (int *)&PT_REGS_PARM1(real_regs);
209-
const char __user **filename_user = (const char **)&PT_REGS_PARM2(real_regs);
209+
const char __user **filename_user =
210+
(const char **)&PT_REGS_PARM2(real_regs);
210211
int *flags = (int *)&PT_REGS_SYSCALL_PARM4(real_regs);
211212

212213
return ksu_handle_stat(dfd, filename_user, flags);
213214
}
214215

215-
static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
216+
static int execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
216217
{
217218
struct pt_regs *real_regs = PT_REAL_REGS(regs);
218219
const char __user **filename_user =
@@ -222,21 +223,6 @@ static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
222223
NULL);
223224
}
224225

225-
static struct kprobe faccessat_kp = {
226-
.symbol_name = SYS_FACCESSAT_SYMBOL,
227-
.pre_handler = sys_faccessat_handler_pre,
228-
};
229-
230-
static struct kprobe newfstatat_kp = {
231-
.symbol_name = SYS_NEWFSTATAT_SYMBOL,
232-
.pre_handler = sys_newfstatat_handler_pre,
233-
};
234-
235-
static struct kprobe execve_kp = {
236-
.symbol_name = SYS_EXECVE_SYMBOL,
237-
.pre_handler = sys_execve_handler_pre,
238-
};
239-
240226
static int pts_unix98_lookup_pre(struct kprobe *p, struct pt_regs *regs)
241227
{
242228
struct inode *inode;
@@ -246,35 +232,56 @@ static int pts_unix98_lookup_pre(struct kprobe *p, struct pt_regs *regs)
246232
return ksu_handle_devpts(inode);
247233
}
248234

249-
static struct kprobe pts_unix98_lookup_kp = { .symbol_name =
250-
"pts_unix98_lookup",
251-
.pre_handler =
252-
pts_unix98_lookup_pre };
253-
254235
#endif
255236

237+
static struct kprobe *init_kprobe(const char *name,
238+
kprobe_pre_handler_t handler)
239+
{
240+
struct kprobe *kp = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
241+
if (!kp)
242+
return NULL;
243+
kp->symbol_name = name;
244+
kp->pre_handler = handler;
245+
246+
int ret = register_kprobe(kp);
247+
pr_info("sucompat: register_%s kprobe: %d\n", name, ret);
248+
if (ret) {
249+
kfree(kp);
250+
return NULL;
251+
}
252+
253+
return kp;
254+
}
255+
256+
static void destroy_kprobe(struct kprobe **kp_ptr)
257+
{
258+
struct kprobe *kp = *kp_ptr;
259+
if (!kp)
260+
return;
261+
unregister_kprobe(kp);
262+
synchronize_rcu();
263+
kfree(kp);
264+
*kp_ptr = NULL;
265+
}
266+
267+
static struct kprobe *su_kps[4];
268+
256269
// sucompat: permited process can execute 'su' to gain root access.
257270
void ksu_sucompat_init()
258271
{
259272
#ifdef CONFIG_KPROBES
260-
int ret;
261-
ret = register_kprobe(&execve_kp);
262-
pr_info("sucompat: execve_kp: %d\n", ret);
263-
ret = register_kprobe(&newfstatat_kp);
264-
pr_info("sucompat: newfstatat_kp: %d\n", ret);
265-
ret = register_kprobe(&faccessat_kp);
266-
pr_info("sucompat: faccessat_kp: %d\n", ret);
267-
ret = register_kprobe(&pts_unix98_lookup_kp);
268-
pr_info("sucompat: devpts_kp: %d\n", ret);
273+
su_kps[0] = init_kprobe(SYS_EXECVE_SYMBOL, execve_handler_pre);
274+
su_kps[1] = init_kprobe(SYS_FACCESSAT_SYMBOL, faccessat_handler_pre);
275+
su_kps[2] = init_kprobe(SYS_NEWFSTATAT_SYMBOL, newfstatat_handler_pre);
276+
su_kps[3] = init_kprobe("pts_unix98_lookup", pts_unix98_lookup_pre);
269277
#endif
270278
}
271279

272280
void ksu_sucompat_exit()
273281
{
274282
#ifdef CONFIG_KPROBES
275-
unregister_kprobe(&execve_kp);
276-
unregister_kprobe(&newfstatat_kp);
277-
unregister_kprobe(&faccessat_kp);
278-
unregister_kprobe(&pts_unix98_lookup_kp);
283+
for (int i = 0; i < ARRAY_SIZE(su_kps); i++) {
284+
destroy_kprobe(&su_kps[i]);
285+
}
279286
#endif
280287
}

manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ fun SettingScreen(navigator: DestinationsNavigator) {
181181
title = stringResource(id = R.string.settings_disable_su),
182182
summary = stringResource(id = R.string.settings_disable_su_summary),
183183
checked = isSuDisabled,
184-
enabled = !isSuDisabled // we can't re-enable su if it's disabled.
185184
) { checked ->
186185
val shouldEnable = !checked
187186
if (Natives.setSuEnabled(shouldEnable)) {
@@ -502,4 +501,4 @@ private fun TopBar(
502501
@Composable
503502
private fun SettingsPreview() {
504503
SettingScreen(EmptyDestinationsNavigator)
505-
}
504+
}

0 commit comments

Comments
 (0)