linux_hardening_guide:sysctl
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux_hardening_guide:sysctl [2025/05/31 14:40] – created peter | linux_hardening_guide:sysctl [2025/05/31 15:19] (current) – peter | ||
---|---|---|---|
Line 33: | Line 33: | ||
---- | ---- | ||
- | ====== Recommended sysctl settings to change ====== | + | ====== Recommended sysctl settings to change |
- | ===== kernel.kptr_restrict=2 | + | <code bash> |
+ | kernel.kptr_restrict=2 | ||
+ | </ | ||
A kernel pointer points to a specific location in kernel memory. | A kernel pointer points to a specific location in kernel memory. | ||
+ | |||
* These can be very useful in exploiting the kernel, but kernel pointers are not hidden by default — it is easy to uncover them by, for example, reading the contents of / | * These can be very useful in exploiting the kernel, but kernel pointers are not hidden by default — it is easy to uncover them by, for example, reading the contents of / | ||
* This setting aims to mitigate kernel pointer leaks. | * This setting aims to mitigate kernel pointer leaks. | ||
Line 44: | Line 47: | ||
---- | ---- | ||
- | ===== kernel.dmesg_restrict=1 | + | <code bash> |
+ | kernel.dmesg_restrict=1 | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
- | dmesg is the kernel log. | ||
* It exposes a large amount of useful kernel debugging information, | * It exposes a large amount of useful kernel debugging information, | ||
* Changing the above sysctl restricts the kernel log to the CAP_SYSLOG capability. | * Changing the above sysctl restricts the kernel log to the CAP_SYSLOG capability. | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * In the Linux kernel, "root privileges" | ||
+ | * This is helpful in applying the principle of least privilege — instead of giving a process total root privileges, you can grant them only a specific subset instead. | ||
+ | * For example, if a program simply needs to set your system time, then it only needs CAP_SYS_TIME rather than total root. | ||
+ | * This could limit the potential damage that can be done; however, you must still be cautious with granting capabilities, | ||
+ | |||
+ | </ | ||
---- | ---- | ||
- | ===== kernel.printk=3 3 3 3 ===== | + | <code bash> |
+ | kernel.printk=3 3 3 3 | ||
+ | </ | ||
Despite the value of dmesg_restrict, | Despite the value of dmesg_restrict, | ||
+ | |||
* Malware that is able to record the screen during boot may be able to abuse this to gain higher privileges. | * Malware that is able to record the screen during boot may be able to abuse this to gain higher privileges. | ||
* This option prevents those information leaks. | * This option prevents those information leaks. | ||
Line 61: | Line 80: | ||
---- | ---- | ||
- | ===== kernel.unprivileged_bpf_disabled=1 ===== | + | <code bash> |
+ | kernel.unprivileged_bpf_disabled=1 | ||
+ | net.core.bpf_jit_harden=2 | ||
+ | </ | ||
eBPF exposes quite large attack surface | eBPF exposes quite large attack surface | ||
* As such, it must be restricted. | * As such, it must be restricted. | ||
- | * These sysctls restrict eBPF to the CAP_BPF capability (CAP_SYS_ADMIN on kernel versions prior to 5.8) and enable JIT hardening techniques, such as constant blinding. | + | * These sysctls restrict eBPF to the CAP_BPF capability (CAP_SYS_ADMIN on kernel versions prior to 5.8) and enable JIT hardening techniques, such as [[https:// |
---- | ---- | ||
- | ===== net.core.bpf_jit_harden=2 ===== | + | <code bash> |
+ | dev.tty.ldisc_autoload=0 | ||
+ | </ | ||
- | Same reason as for **kernel.unprivileged_bpf_disabled=1**. | + | This [[https:// |
- | eBPF exposes quite large attack surface | + | ---- |
- | * As such, it must be restricted. | + | |
- | * These sysctls | + | <code bash> |
+ | vm.unprivileged_userfaultfd=0 | ||
+ | </ | ||
+ | |||
+ | The **[[https:// | ||
+ | |||
+ | * Due to this, this sysctl is used to restrict | ||
---- | ---- | ||
- | ===== dev.tty.ldisc_autoload=0 ===== | + | <code bash> |
+ | kernel.kexec_load_disabled=1 | ||
+ | </ | ||
- | This restricts loading TTY line disciplines | + | [[https:// |
+ | |||
+ | * This functionality can be abused | ||
---- | ---- | ||
- | ===== vm.unprivileged_userfaultfd=0 ===== | + | <code bash> |
+ | kernel.sysrq=4 | ||
+ | </ | ||
- | The **userfaultfd()** syscall is often abused | + | The [[https:// |
+ | | ||
+ | | ||
+ | | ||
- | * Due to this, this sysctl is used to restrict this syscall | + | ---- |
+ | |||
+ | <code bash> | ||
+ | kernel.unprivileged_userns_clone=0 | ||
+ | </ | ||
+ | |||
+ | User namespaces are a feature in the kernel which aim to improve sandboxing and make it easily accessible for unprivileged users. | ||
+ | |||
+ | * However, | ||
+ | * For unprivileged sandboxing, it is instead recommended | ||
+ | * This topic is covered further in the sandboxing section. | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * If your kernel does not include this patch, you can alternatively disable user namespaces completely (including for root) by setting **user.max_user_namespaces=0**. | ||
+ | |||
+ | </ | ||
---- | ---- | ||
- | ===== kernel.kexec_load_disabled=1 ===== | + | <code bash> |
+ | kernel.perf_event_paranoid=3 | ||
+ | </ | ||
- | kexec is a system call that is used to boot another kernel during runtime. | + | [[https:// |
- | * This functionality can be abused | + | |
+ | This sysctl restricts all usage of performance events to the CAP_PERFMON capability (CAP_SYS_ADMIN on kernel versions prior to 5.8). | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * Otherwise, this setting is equivalent | ||
+ | |||
+ | </ | ||
---- | ---- | ||
- | ===== kernel.sysrq=4 ===== | + | ====== |
- | The SysRq key exposes a lot of potentially dangerous debugging functionality to unprivileged users. | + | <code bash> |
- | * Contrary to common assumptions, | + | net.ipv4.tcp_syncookies=1 |
- | * The value of this sysctl makes it so that a user can only use the secure attention key, which will be necessary for accessing root securely. | + | </ |
- | * Alternatively, | + | |
+ | This helps protect against [[https:// | ||
---- | ---- | ||
- | ===== kernel.unprivileged_userns_clone=0 ===== | + | <code bash> |
+ | net.ipv4.tcp_rfc1337=1 | ||
+ | </ | ||
+ | This protects against [[https:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | net.ipv4.conf.all.rp_filter=1 | ||
+ | net.ipv4.conf.default.rp_filter=1 | ||
+ | </ | ||
+ | |||
+ | These enable source validation of packets received from all interfaces of the machine. | ||
+ | |||
+ | * This protects against [[https:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | net.ipv4.conf.all.accept_redirects=0 | ||
+ | net.ipv4.conf.default.accept_redirects=0 | ||
+ | net.ipv4.conf.all.secure_redirects=0 | ||
+ | net.ipv4.conf.default.secure_redirects=0 | ||
+ | net.ipv6.conf.all.accept_redirects=0 | ||
+ | net.ipv6.conf.default.accept_redirects=0 | ||
+ | net.ipv4.conf.all.send_redirects=0 | ||
+ | net.ipv4.conf.default.send_redirects=0 | ||
+ | </ | ||
+ | |||
+ | These disable ICMP redirect acceptance and sending to prevent [[https:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | net.ipv4.icmp_echo_ignore_all=1 | ||
+ | </ | ||
+ | |||
+ | This setting makes your system ignore all ICMP requests to avoid Smurf attacks, make the device more difficult to enumerate on the network and prevent clock fingerprinting through ICMP timestamps. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | net.ipv4.conf.all.accept_source_route=0 | ||
+ | net.ipv4.conf.default.accept_source_route=0 | ||
+ | net.ipv6.conf.all.accept_source_route=0 | ||
+ | net.ipv6.conf.default.accept_source_route=0 | ||
+ | </ | ||
+ | |||
+ | Source routing is a mechanism that allows users to redirect network traffic. | ||
+ | |||
+ | * As this can be used to perform man-in-the-middle attacks in which the traffic is redirected for nefarious purposes, the above settings disable this functionality. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | net.ipv6.conf.all.accept_ra=0 | ||
+ | net.ipv6.conf.default.accept_ra=0 | ||
+ | </ | ||
+ | |||
+ | Malicious IPv6 router advertisements can result in a man-in-the-middle attack, so they should be disabled. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | net.ipv4.tcp_sack=0 | ||
+ | net.ipv4.tcp_dsack=0 | ||
+ | net.ipv4.tcp_fack=0 | ||
+ | </ | ||
+ | |||
+ | This disables TCP SACK. | ||
+ | |||
+ | * SACK is commonly exploited and unnecessary in many circumstances, | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== Recommended sysctl settings to change for User Space ====== | ||
+ | |||
+ | <code bash> | ||
+ | kernel.yama.ptrace_scope=2 | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | * This restricts usage of ptrace to only processes with the CAP_SYS_PTRACE capability. | ||
+ | * Alternatively, | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | vm.mmap_rnd_bits=32 | ||
+ | vm.mmap_rnd_compat_bits=16 | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | The values of these sysctls must be set in relation to the CPU architecture. The above values are compatible with x86, but other architectures may differ. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | fs.protected_symlinks=1 | ||
+ | fs.protected_hardlinks=1 | ||
+ | </ | ||
+ | |||
+ | This only permits symlinks to be followed when outside of a world-writable sticky directory, when the owner of the symlink and follower match or when the directory owner matches the symlink' | ||
+ | |||
+ | * This also prevents hardlinks from being created by users that do not have read/write access to the source file. | ||
+ | * Both of these prevent many common [[https:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code bash> | ||
+ | fs.protected_fifos=2 | ||
+ | fs.protected_regular=2 | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ---- | ||
Line 124: | Line 309: | ||
... | ... | ||
... | ... | ||
- | # Network Security | + | # Network Security. |
net.ipv4.conf.all.send_redirects = 0 | net.ipv4.conf.all.send_redirects = 0 | ||
net.ipv4.conf.default.send_redirects = 0 | net.ipv4.conf.default.send_redirects = 0 | ||
Line 144: | Line 329: | ||
net.ipv4.tcp_syn_retries = 5 | net.ipv4.tcp_syn_retries = 5 | ||
- | # IPv6 Security (if enabled) | + | # IPv6 Security (if enabled). |
net.ipv6.conf.all.accept_redirects = 0 | net.ipv6.conf.all.accept_redirects = 0 | ||
net.ipv6.conf.default.accept_redirects = 0 | net.ipv6.conf.default.accept_redirects = 0 | ||
Line 150: | Line 335: | ||
net.ipv6.conf.default.accept_source_route = 0 | net.ipv6.conf.default.accept_source_route = 0 | ||
- | # Process Security | + | # Process Security. |
kernel.randomize_va_space = 2 | kernel.randomize_va_space = 2 | ||
kernel.kptr_restrict = 2 | kernel.kptr_restrict = 2 | ||
Line 161: | Line 346: | ||
- | # File System Security | + | # File System Security. |
fs.protected_hardlinks = 1 | fs.protected_hardlinks = 1 | ||
fs.protected_symlinks = 1 | fs.protected_symlinks = 1 | ||
Line 168: | Line 353: | ||
fs.protected_regular = 2 | fs.protected_regular = 2 | ||
- | # Additional Security Measures | + | # Additional Security Measures. |
- | dev.tty.ldisc_autoload = 0 | + | #dev.tty.ldisc_autoload = 0 |
# | # | ||
kernel.core_uses_pid = 1 | kernel.core_uses_pid = 1 |
linux_hardening_guide/sysctl.1748702438.txt.gz · Last modified: 2025/05/31 14:40 by peter