Skip to content

Commit 0950fbb

Browse files
tiannOokiineko
authored andcommitted
kernel: selinux: add security_bounded_transition hook for kernel < 4.14
- torvalds/linux@af63f41 - SELinux domain transitions under NNP/nosuid environment was introduced in 4.14 by the above commit, for older kernels, we need to make sure our domain transitions are allowed when calling ksud at boot from the init Signed-off-by: Ookiineko <[email protected]>
1 parent d77ab8d commit 0950fbb

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

kernel/ksu.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "linux/fs.h"
22
#include "linux/module.h"
33
#include "linux/workqueue.h"
4+
#include "linux/version.h"
45

56
#include "allowlist.h"
67
#include "arch.h"
@@ -32,6 +33,9 @@ int ksu_handle_execveat(int *fd, struct filename **filename_ptr, void *argv,
3233

3334
extern void ksu_enable_sucompat();
3435
extern void ksu_enable_ksud();
36+
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
37+
extern void ksu_enable_selinux_compat();
38+
#endif
3539

3640
int __init kernelsu_init(void)
3741
{
@@ -56,6 +60,9 @@ int __init kernelsu_init(void)
5660
#ifdef CONFIG_KPROBES
5761
ksu_enable_sucompat();
5862
ksu_enable_ksud();
63+
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
64+
ksu_enable_selinux_compat();
65+
#endif
5966
#else
6067
#warning("KPROBES is disabled, KernelSU may not work, please check https://kernelsu.org/guide/how-to-integrate-for-non-gki.html")
6168
#endif

kernel/selinux/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
obj-y += selinux.o
22
obj-y += sepolicy.o
33
obj-y += rules.o
4+
obj-y += kernel_compat.o
45

56

67
ccflags-y += -Wno-implicit-function-declaration -Wno-strict-prototypes -Wno-int-conversion
78
ccflags-y += -Wno-macro-redefined -Wno-declaration-after-statement -Wno-unused-function
89
ccflags-y += -I$(srctree)/security/selinux -I$(srctree)/security/selinux/include
9-
ccflags-y += -I$(objtree)/security/selinux
10+
ccflags-y += -I$(objtree)/security/selinux

kernel/selinux/kernel_compat.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "linux/version.h"
2+
3+
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
4+
#include "linux/types.h"
5+
#ifdef CONFIG_KPROBES
6+
#include "linux/kprobes.h"
7+
#endif
8+
#include "avc_ss.h"
9+
10+
#include "selinux.h"
11+
#include "../klog.h" // IWYU pragma: keep
12+
#include "../arch.h"
13+
int ksu_handle_security_bounded_transition(u32 *old_sid, u32 *new_sid) {
14+
u32 init_sid, su_sid;
15+
int error;
16+
17+
if (!ss_initialized)
18+
return 0;
19+
20+
/* domain unchanged */
21+
if (*old_sid == *new_sid)
22+
return 0;
23+
24+
const char *init_domain = INIT_DOMAIN;
25+
const char *su_domain = KERNEL_SU_DOMAIN;
26+
27+
error = security_secctx_to_secid(init_domain, strlen(init_domain), &init_sid);
28+
if (error) {
29+
pr_warn("cannot get sid of init context, err %d\n", error);
30+
return 0;
31+
}
32+
33+
error = security_secctx_to_secid(su_domain, strlen(su_domain), &su_sid);
34+
if (error) {
35+
pr_warn("cannot get sid of su context, err %d\n", error);
36+
return 0;
37+
}
38+
39+
if (*old_sid == init_sid && *new_sid == su_sid) {
40+
pr_info("init to su transition found\n");
41+
*old_sid = *new_sid; // make the original func return 0
42+
}
43+
44+
return 0;
45+
}
46+
47+
#ifdef CONFIG_KPROBES
48+
static int handler_pre(struct kprobe *p, struct pt_regs *regs) {
49+
u32 *old_sid = (u32 *)&PT_REGS_PARM1(regs);
50+
u32 *new_sid = (u32 *)&PT_REGS_PARM2(regs);
51+
52+
return ksu_handle_security_bounded_transition(old_sid, new_sid);
53+
}
54+
55+
static struct kprobe kp = {
56+
.symbol_name = "security_bounded_transition",
57+
.pre_handler = handler_pre,
58+
};
59+
60+
// selinux_compat: make ksud init trigger work for kernel < 4.14
61+
void ksu_enable_selinux_compat() {
62+
int ret;
63+
64+
ret = register_kprobe(&kp);
65+
pr_info("selinux_compat: kp: %d\n", ret);
66+
}
67+
#endif
68+
#endif

kernel/selinux/selinux.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "linux/version.h"
44
#include "../klog.h" // IWYU pragma: keep
55

6-
#define KERNEL_SU_DOMAIN "u:r:su:s0"
7-
86
static u32 ksu_sid;
97

108
static int transive_to_domain(const char *domain)

kernel/selinux/selinux.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include "linux/types.h"
55

6+
#define KERNEL_SU_DOMAIN "u:r:su:s0"
7+
#define INIT_DOMAIN "u:r:init:s0"
8+
69
void setup_selinux();
710

811
void setenforce(bool);
@@ -13,4 +16,4 @@ bool is_ksu_domain();
1316

1417
void apply_kernelsu_rules();
1518

16-
#endif
19+
#endif

0 commit comments

Comments
 (0)