forked from deepfence/ebpfguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable-bpf-lsm.py
executable file
·65 lines (51 loc) · 1.87 KB
/
enable-bpf-lsm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import sys
import logging
logging.basicConfig()
log = logging.getLogger(None)
log.setLevel(logging.INFO)
def main():
try:
with open("/sys/kernel/security/lsm", "r") as f:
lsms = f.read().strip().split(",")
except Exception as e:
log.error(
"Couldn't open lsm capabilities pseudo file. Check if your kernel supports lsm."
)
sys.exit(-1)
if "bpf" in lsms:
log.info("BPF LSM already enabled")
return
lsms.append("bpf")
content = []
bpf_line = None
with open("/etc/default/grub") as fd:
for i, l in enumerate(fd):
if l.startswith("GRUB_CMDLINE_LINUX_DEFAULT="):
if bpf_line:
log.warning(
"Multiple GRUB_CMDLINE_LINUX_DEFAULT. Only last one takes effect. Check your configuration. This script will modify last occurrence only."
)
bpf_line = (i, l)
else:
content.append(l)
idx, effective_grub_cmdline = bpf_line
if not effective_grub_cmdline:
log.error("""No line starting with "GRUB_CMDLINE_LINUX_DEFAULT=".""")
sys.exit(-2)
if "lsm" in effective_grub_cmdline:
log.warning(
f"""LSMs explicitly declared in /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT. Edit manually and append bpf value."""
)
sys.exit(-3)
modified_cmdline = effective_grub_cmdline.lstrip('GRUB_CMDLINE_LINUX_DEFAULT="').rstrip('"\n')
cmdline_lsm = "lsm={}".format(",".join(lsms))
if modified_cmdline == "":
modified_cmdline = cmdline_lsm
else:
modified_cmdline += " " + cmdline_lsm
modified_cmdline = 'GRUB_CMDLINE_LINUX_DEFAULT="{}"\n'.format(modified_cmdline)
content.insert(idx, modified_cmdline)
print("".join(content))
if __name__ == "__main__":
main()