linux_hardening_guide:sysctl
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
linux_hardening_guide:sysctl [2025/05/31 14:54] – peter | linux_hardening_guide:sysctl [2025/05/31 15:19] (current) – peter | ||
---|---|---|---|
Line 35: | Line 35: | ||
====== Recommended sysctl settings to change for Kernel self-protection ====== | ====== Recommended sysctl settings to change for Kernel self-protection ====== | ||
- | ===== 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, | ||
+ | |||
+ | </ | ||
---- | ---- | ||
Line 66: | Line 82: | ||
<code bash> | <code bash> | ||
kernel.unprivileged_bpf_disabled=1 | 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 restrict eBPF to the CAP_BPF | + | |
---- | ---- | ||
- | ===== dev.tty.ldisc_autoload=0 ===== | + | <code bash> |
- | + | vm.unprivileged_userfaultfd=0 | |
- | This restricts loading TTY line disciplines to the CAP_SYS_MODULE capability to prevent unprivileged attackers from loading vulnerable line disciplines with the TIOCSETD ioctl, which has been abused in a number of exploits before. | + | </ |
- | + | ||
- | ---- | + | |
- | + | ||
- | ===== vm.unprivileged_userfaultfd=0 | + | |
- | The **userfaultfd()** syscall is often abused to exploit use-after-free flaws. | + | The **[[https:// |
* Due to this, this sysctl is used to restrict this syscall to the CAP_SYS_PTRACE capability. | * Due to this, this sysctl is used to restrict this syscall to the CAP_SYS_PTRACE capability. | ||
Line 98: | Line 109: | ||
---- | ---- | ||
- | ===== kernel.kexec_load_disabled=1 | + | <code bash> |
+ | kernel.kexec_load_disabled=1 | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
- | kexec is a system call that is used to boot another kernel during runtime. | ||
* This functionality can be abused to load a malicious kernel and gain arbitrary code execution in kernel mode, so this sysctl disables it. | * This functionality can be abused to load a malicious kernel and gain arbitrary code execution in kernel mode, so this sysctl disables it. | ||
---- | ---- | ||
- | ===== kernel.sysrq=4 | + | <code bash> |
+ | kernel.sysrq=4 | ||
+ | </ | ||
- | The SysRq key exposes a lot of potentially dangerous debugging functionality to unprivileged users. | + | The [[https:// |
- | * Contrary to common assumptions, | + | * Contrary to common assumptions, |
- | * 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. | + | * The value of this sysctl makes it so that a user can only use the [[https:// |
* Alternatively, | * Alternatively, | ||
---- | ---- | ||
- | ===== kernel.unprivileged_userns_clone=0 | + | <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. | User namespaces are a feature in the kernel which aim to improve sandboxing and make it easily accessible for unprivileged users. | ||
Line 131: | Line 149: | ||
---- | ---- | ||
- | ===== kernel.perf_event_paranoid=3 | + | <code bash> |
+ | kernel.perf_event_paranoid=3 | ||
+ | </ | ||
- | Performance events add considerable kernel attack surface and have caused abundant vulnerabilities. | + | [[https:// |
This sysctl restricts all usage of performance events to the CAP_PERFMON capability (CAP_SYS_ADMIN on kernel versions prior to 5.8). | This sysctl restricts all usage of performance events to the CAP_PERFMON capability (CAP_SYS_ADMIN on kernel versions prior to 5.8). | ||
Line 140: | Line 160: | ||
**NOTE: | **NOTE: | ||
- | * Otherwise, this setting is equivalent to **kernel.perf_event_paranoid=2**, | + | * Otherwise, this setting is equivalent to **[[https:// |
</ | </ | ||
Line 148: | Line 168: | ||
====== Recommended sysctl settings to change for Networks ====== | ====== Recommended sysctl settings to change for Networks ====== | ||
- | ===== net.ipv4.tcp_syncookies=1 | + | <code bash> |
+ | net.ipv4.tcp_syncookies=1 | ||
+ | </ | ||
- | This helps protect against SYN flood attacks, which are a form of denial-of-service attack, in which an attacker sends a large amount of bogus SYN requests in an attempt to consume enough resources to make the system unresponsive to legitimate traffic. | + | This helps protect against |
---- | ---- | ||
- | ===== net.ipv4.tcp_rfc1337=1 | + | <code bash> |
+ | net.ipv4.tcp_rfc1337=1 | ||
+ | </ | ||
- | This protects against time-wait assassination by dropping RST packets for sockets in the time-wait state. | + | This protects against |
---- | ---- | ||
Line 165: | Line 189: | ||
</ | </ | ||
- | These enable source validation of packets received from all interfaces of the machine. This protects against IP spoofing, in which an attacker sends a packet with a fraudulent IP address. | + | These enable source validation of packets received from all interfaces of the machine. |
+ | |||
+ | * This protects against | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <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:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
---- | ---- |
linux_hardening_guide/sysctl.1748703240.txt.gz · Last modified: 2025/05/31 14:54 by peter