|
| 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 |
0 commit comments