Skip to content

Commit 6625405

Browse files
committed
kernel: extras/avc_spoof: add kprobe support
I'll just paste code comments. I've already done this standalone on https://github.com/backslashxx/selinux_avc_spoof_lkm -- just pass both arg2 and arg3 to original handler this removes all the headache. for < 4.17 int slow_avc_audit(u32 ssid, u32 tsid for >= 4.17 int slow_avc_audit(struct selinux_state *state, u32 ssid, u32 tsid for >= 6.4 int slow_avc_audit(u32 ssid, u32 tsid not to mention theres also DKSU_HAS_SELINUX_STATE since its hard to make sure this selinux state thing cross crossing with 4.17 ~ 6.4's where slow_avc_audit changes abi (tsid in arg2 vs arg3) lets just pass both to the handler Signed-off-by: backslashxx <[email protected]>
1 parent f5dd642 commit 6625405

File tree

1 file changed

+95
-17
lines changed

1 file changed

+95
-17
lines changed

kernel/extras.c

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,17 @@
55
#include "ksud.h"
66
#include "kernel_compat.h"
77

8+
// sorry for the ifdef hell
9+
// but im too lazy to fragment this out.
10+
// theres only one feature so far anyway
11+
// - xx, 20251019
12+
813
static u32 su_sid = 0;
914
static u32 kernel_sid = 0;
1015

1116
// init as disabled by default
1217
static atomic_t disable_spoof = ATOMIC_INIT(1);
1318

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-
2919
static int get_sid()
3020
{
3121
// dont load at all if we cant get sids
@@ -45,10 +35,94 @@ static int get_sid()
4535
return 0;
4636
}
4737

38+
int ksu_handle_slow_avc_audit(u32 *tsid)
39+
{
40+
if (atomic_read(&disable_spoof))
41+
return 0;
42+
43+
// if tsid is su, we just replace it
44+
// unsure if its enough, but this is how it is aye?
45+
if (*tsid == su_sid) {
46+
pr_info("avc_spoof/slow_avc_audit: replacing su_sid: %u with kernel_sid: %u\n", su_sid, kernel_sid);
47+
*tsid = kernel_sid;
48+
}
49+
50+
return 0;
51+
}
52+
53+
#ifdef CONFIG_KPROBES
54+
#include <linux/kprobes.h>
55+
#include <linux/slab.h>
56+
#include "arch.h"
57+
static struct kprobe *slow_avc_audit_kp;
58+
// .symbol_name = "slow_avc_audit",
59+
// .pre_handler = slow_avc_audit_pre_handler,
60+
static int slow_avc_audit_pre_handler(struct kprobe *p, struct pt_regs *regs)
61+
{
62+
if (atomic_read(&disable_spoof))
63+
return 0;
64+
65+
/*
66+
* just pass both arg2 and arg3 to original handler
67+
* this removes all the headache.
68+
* for < 4.17 int slow_avc_audit(u32 ssid, u32 tsid
69+
* for >= 4.17 int slow_avc_audit(struct selinux_state *state, u32 ssid, u32 tsid
70+
* for >= 6.4 int slow_avc_audit(u32 ssid, u32 tsid
71+
* not to mention theres also DKSU_HAS_SELINUX_STATE
72+
* since its hard to make sure this selinux state thing
73+
* cross crossing with 4.17 ~ 6.4's where slow_avc_audit
74+
* changes abi (tsid in arg2 vs arg3)
75+
* lets just pass both to the handler
76+
*/
77+
78+
u32 *tsid = (u32 *)&PT_REGS_PARM2(regs);
79+
ksu_handle_slow_avc_audit(tsid);
80+
81+
*tsid = (u32 *)&PT_REGS_PARM3(regs);
82+
ksu_handle_slow_avc_audit(tsid);
83+
84+
return 0;
85+
}
86+
87+
// copied from upstream
88+
static struct kprobe *init_kprobe(const char *name,
89+
kprobe_pre_handler_t handler)
90+
{
91+
struct kprobe *kp = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
92+
if (!kp)
93+
return NULL;
94+
kp->symbol_name = name;
95+
kp->pre_handler = handler;
96+
97+
int ret = register_kprobe(kp);
98+
pr_info("sucompat: register_%s kprobe: %d\n", name, ret);
99+
if (ret) {
100+
kfree(kp);
101+
return NULL;
102+
}
103+
104+
return kp;
105+
}
106+
static void destroy_kprobe(struct kprobe **kp_ptr)
107+
{
108+
struct kprobe *kp = *kp_ptr;
109+
if (!kp)
110+
return;
111+
unregister_kprobe(kp);
112+
synchronize_rcu();
113+
kfree(kp);
114+
*kp_ptr = NULL;
115+
}
116+
#endif // CONFIG_KPROBES
117+
48118
void avc_spoof_exit(void)
49119
{
120+
#ifdef CONFIG_KPROBES
121+
pr_info("avc_spoof/exit: unregister slow_avc_audit kprobe!\n");
122+
destroy_kprobe(&slow_avc_audit_kp);
123+
#endif
50124
atomic_set(&disable_spoof, 1);
51-
pr_info("avc_spoof/init: slow_avc_audit spoofing disabled!\n");
125+
pr_info("avc_spoof/exit: slow_avc_audit spoofing disabled!\n");
52126
}
53127

54128
void avc_spoof_init(void)
@@ -58,7 +132,11 @@ void avc_spoof_init(void)
58132
pr_info("avc_spoof/init: sid grab fail!\n");
59133
return;
60134
}
61-
135+
136+
#ifdef CONFIG_KPROBES
137+
pr_info("avc_spoof/init: register slow_avc_audit kprobe!\n");
138+
slow_avc_audit_kp = init_kprobe("slow_avc_audit", slow_avc_audit_pre_handler);
139+
#endif
62140
// once we get the sids, we can now enable the hook handler
63141
atomic_set(&disable_spoof, 0);
64142

0 commit comments

Comments
 (0)