Skip to content

Commit c5f0231

Browse files
committed
security: add Baseband-guard LSM module
Add Baseband-guard as a lightweight Linux Security Module for Android kernel protection. This module provides kernel-level protection against unauthorized writes to critical partitions and device nodes Signed-off-by: Kaifeng Zou <[email protected]>
1 parent f023ac4 commit c5f0231

File tree

9 files changed

+103
-1
lines changed

9 files changed

+103
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,7 @@ all_kmi_symbols
175175
# KernelSU submodules
176176
/KernelSU/*
177177
!/KernelSU/.git
178+
179+
# BBG submodules
180+
/Baseband-guard/*
181+
!/Baseband-guard/.git

Baseband-guard

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 1c35b9eb8e07003e8cbcdcda7ee6a8bc4085759d

arch/arm64/configs/gki_defconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,11 @@ CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y
809809

810810
CONFIG_MQ_IOSCHED_SSG=y
811811
CONFIG_MQ_IOSCHED_SSG_CGROUP=y
812+
813+
CONFIG_LSM="landlock,lockdown,yama,loadpin,safesetid,selinux,smack,tomoyo,apparmor,bpf,baseband_guard"
814+
815+
CONFIG_BBG=y
816+
CONFIG_BBG_ALLOW_IN_RECOVERY=y
817+
CONFIG_BBG_BLOCK_BOOT=n
818+
CONFIG_BBG_DEBUG=y
819+
CONFIG_BBG_ANTI_SPOOF_DOMAIN=3

security/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,6 @@ config LSM
292292

293293
source "security/Kconfig.hardening"
294294

295+
source "security/baseband-guard/Kconfig"
295296
endmenu
296297

security/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/
2727

2828
# Object integrity file lists
2929
obj-$(CONFIG_INTEGRITY) += integrity/
30+
31+
obj-$(CONFIG_BBG) += baseband-guard/

security/baseband-guard

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Baseband-guard

security/selinux/Makefile

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
obj-$(CONFIG_SECURITY_SELINUX) := selinux.o
77

8-
selinux-y := avc.o hooks.o selinuxfs.o netlink.o nlmsgtab.o netif.o \
8+
selinux-y := avc.o hooks.o netlink.o nlmsgtab.o netif.o \
99
netnode.o netport.o status.o \
1010
ss/ebitmap.o ss/hashtab.o ss/symtab.o ss/sidtab.o ss/avtab.o \
1111
ss/policydb.o ss/services.o ss/conditional.o ss/mls.o ss/context.o
@@ -32,3 +32,52 @@ targets += flask.h av_permissions.h
3232
# $(obj)/flask.h $(obj)/av_permissions.h &: scripts/selinux/...
3333
$(obj)/flask.h: scripts/selinux/genheaders/genheaders FORCE
3434
$(call if_changed,flask)
35+
36+
ifeq ($(CONFIG_BBG),y)
37+
38+
BBG_SELINUXFS_C := $(srctree)/security/selinux/selinuxfs.c
39+
BBG_EXTERN_STRING := "bbg_process_setpermissive"
40+
BBG_HOOK_STRING := "if (!new_value && bbg_process_setpermissive())"
41+
42+
ifeq ($(shell grep -q "[[:space:]]*if (new_value != selinux_enforcing) {" $(BBG_SELINUXFS_C) && echo true), true)
43+
$(info -- BBG: Patching selinuxfs for kernel using selinux_enforcing)
44+
define BBG_HOOK_SED_CMD
45+
sed -i '/if (new_value != selinux_enforcing) {/a \
46+
if (!new_value && bbg_process_setpermissive()) { \
47+
length = -EACCES; \
48+
goto out; \
49+
}'
50+
endef
51+
else
52+
$(info -- BBG: Patching selinuxfs for kernel using old_value)
53+
define BBG_HOOK_SED_CMD
54+
sed -i '/if (new_value != old_value) {/a \
55+
if (!new_value && bbg_process_setpermissive()) { \
56+
length = -EACCES; \
57+
goto out; \
58+
}'
59+
endef
60+
endif
61+
62+
$(obj)/.bbg_patched: $(BBG_SELINUXFS_C) FORCE
63+
@echo "BBG: Checking/Patching $(BBG_SELINUXFS_C)"; \
64+
if ! grep -q $(BBG_EXTERN_STRING) $(BBG_SELINUXFS_C); then \
65+
echo "BBG: Applying extern declaration patch..."; \
66+
sed -i '/^#ifdef CONFIG_SECURITY_SELINUX_DEVELOP/a extern int bbg_process_setpermissive(void);' $(BBG_SELINUXFS_C); \
67+
fi; \
68+
if ! grep -q $(BBG_HOOK_STRING) $(BBG_SELINUXFS_C); then \
69+
echo "BBG: Applying hook for kernel $(VERSION).$(PATCHLEVEL)..."; \
70+
$(BBG_HOOK_SED_CMD) $(BBG_SELINUXFS_C); \
71+
fi; \
72+
if ! grep -q $(BBG_EXTERN_STRING) $(BBG_SELINUXFS_C); then \
73+
echo "ERROR: BBG Auto Hook failed! Final check failed." >&2; \
74+
exit 1; \
75+
fi; \
76+
touch $@
77+
78+
$(obj)/selinuxfs.o: $(obj)/.bbg_patched
79+
selinux-y += selinuxfs.o
80+
81+
else
82+
selinux-y += selinuxfs.o
83+
endif

security/selinux/Makefile.bak

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
#
3+
# Makefile for building the SELinux module as part of the kernel tree.
4+
#
5+
6+
obj-$(CONFIG_SECURITY_SELINUX) := selinux.o
7+
8+
selinux-y := avc.o hooks.o selinuxfs.o netlink.o nlmsgtab.o netif.o \
9+
netnode.o netport.o status.o \
10+
ss/ebitmap.o ss/hashtab.o ss/symtab.o ss/sidtab.o ss/avtab.o \
11+
ss/policydb.o ss/services.o ss/conditional.o ss/mls.o ss/context.o
12+
13+
selinux-$(CONFIG_SECURITY_NETWORK_XFRM) += xfrm.o
14+
15+
selinux-$(CONFIG_NETLABEL) += netlabel.o
16+
17+
selinux-$(CONFIG_SECURITY_INFINIBAND) += ibpkey.o
18+
19+
selinux-$(CONFIG_IMA) += ima.o
20+
21+
ccflags-y := -I$(srctree)/security/selinux -I$(srctree)/security/selinux/include
22+
23+
$(addprefix $(obj)/,$(selinux-y)): $(obj)/flask.h
24+
25+
quiet_cmd_flask = GEN $(obj)/flask.h $(obj)/av_permissions.h
26+
cmd_flask = $< $(obj)/flask.h $(obj)/av_permissions.h
27+
28+
targets += flask.h av_permissions.h
29+
# once make >= 4.3 is required, we can use grouped targets in the rule below,
30+
# which basically involves adding both headers and a '&' before the colon, see
31+
# the example below:
32+
# $(obj)/flask.h $(obj)/av_permissions.h &: scripts/selinux/...
33+
$(obj)/flask.h: scripts/selinux/genheaders/genheaders FORCE
34+
$(call if_changed,flask)

security/selinux/selinuxfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
135135
}
136136

137137
#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
138+
extern int bbg_process_setpermissive(void);
138139
static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
139140
size_t count, loff_t *ppos)
140141

@@ -164,6 +165,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
164165

165166
old_value = enforcing_enabled(state);
166167
if (new_value != old_value) {
168+
if (!new_value && bbg_process_setpermissive()) { length = -EACCES; goto out; }
167169
length = avc_has_perm(&selinux_state,
168170
current_sid(), SECINITSID_SECURITY,
169171
SECCLASS_SECURITY, SECURITY__SETENFORCE,

0 commit comments

Comments
 (0)