Skip to content

Commit a5f85db

Browse files
committed
kernel: extras: base implementation of avc log spoofing
this exposes a new handler int ksu_handle_slow_avc_audit(u32 *tsid) which will check if su_sid is going to be printed on the audit log. Usage: ksu_handle_slow_avc_audit(&tsid); on slow_avc_audit() on security/selinux/avc.c This way, we replace sid right before that struct is created. This can also be implemented in kprobes which will be on enxt commit. Signed-off-by: backslashxx <[email protected]>
1 parent 022a287 commit a5f85db

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

kernel/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ config KSU
77
help
88
Enable kernel-level root privileges on Android System.
99

10+
config KSU_EXTRAS
11+
bool "Enable custom stuff"
12+
depends on KSU
13+
default n
14+
help
15+
Custom extensions. Experimental.
16+
Currently, only avc log spoofing is implemented.
17+
1018
config KSU_KPROBES_KSUD
1119
bool "Enable dynamic kprobes for early boot hooks"
1220
depends on KPROBES

kernel/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ kernelsu-objs += embed_ksud.o
1212
kernelsu-objs += kernel_compat.o
1313
kernelsu-objs += file_wrapper.o
1414

15+
ifeq ($(CONFIG_KSU_EXTRAS),y)
16+
kernelsu-objs += extras.o
17+
endif
18+
1519
ifeq ($(CONFIG_KSU_KPROBES_KSUD),y)
1620
kernelsu-objs += kp_ksud.o
1721
endif

kernel/extras.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <linux/security.h>
2+
#include <linux/atomic.h>
3+
4+
#include "klog.h"
5+
#include "ksud.h"
6+
#include "kernel_compat.h"
7+
8+
static u32 su_sid = 0;
9+
static u32 kernel_sid = 0;
10+
11+
// init as disabled by default
12+
static atomic_t disable_spoof = ATOMIC_INIT(1);
13+
14+
int ksu_handle_slow_avc_audit(u32 *tsid)
15+
{
16+
if (atomic_read(&disable_spoof))
17+
return 0;
18+
19+
// if tsid is su, we just replace it
20+
// unsure if its enough, but this is how it is aye?
21+
if (*tsid == su_sid) {
22+
pr_info("slow_avc_audit: replacing su_sid: %u with kernel_sid: %u\n", su_sid, kernel_sid);
23+
*tsid = kernel_sid;
24+
}
25+
26+
return 0;
27+
}
28+
29+
static int get_sid()
30+
{
31+
// dont load at all if we cant get sids
32+
int err = security_secctx_to_secid("u:r:su:s0", strlen("u:r:su:s0"), &su_sid);
33+
if (err) {
34+
pr_info("avc_spoof/get_sid: su_sid not found!\n");
35+
return -1;
36+
}
37+
pr_info("avc_spoof/get_sid: su_sid: %u\n", su_sid);
38+
39+
err = security_secctx_to_secid("u:r:kernel:s0", strlen("u:r:kernel:s0"), &kernel_sid);
40+
if (err) {
41+
pr_info("avc_spoof/get_sid: kernel_sid not found!\n");
42+
return -1;
43+
}
44+
pr_info("avc_spoof/get_sid: kernel_sid: %u\n", kernel_sid);
45+
return 0;
46+
}
47+
48+
void avc_spoof_exit(void)
49+
{
50+
atomic_set(&disable_spoof, 1);
51+
pr_info("avc_spoof/init: slow_avc_audit spoofing disabled!\n");
52+
}
53+
54+
void avc_spoof_init(void)
55+
{
56+
int ret = get_sid();
57+
if (ret) {
58+
pr_info("avc_spoof/init: sid grab fail!\n");
59+
return;
60+
}
61+
62+
// once we get the sids, we can now enable the hook handler
63+
atomic_set(&disable_spoof, 0);
64+
65+
pr_info("avc_spoof/init: slow_avc_audit spoofing enabled!\n");
66+
}

kernel/ksud.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
bool ksu_module_mounted __read_mostly = false;
3535
bool ksu_boot_completed __read_mostly = false;
3636

37+
#ifdef CONFIG_KSU_EXTRAS
38+
extern void avc_spoof_init();
39+
#else
40+
void avc_spoof_init() {}
41+
#endif
42+
3743
#ifdef CONFIG_KSU_KPROBES_KSUD
3844
extern void unregister_kprobe_thread();
3945
#endif
@@ -119,6 +125,7 @@ void on_module_mounted(void){
119125
void on_boot_completed(void){
120126
ksu_boot_completed = true;
121127
pr_info("on_boot_completed!\n");
128+
avc_spoof_init();
122129
}
123130

124131
#if defined(CONFIG_KRETPROBES) && defined(CONFIG_KSU_KPROBES_KSUD) && \

0 commit comments

Comments
 (0)