Some Linux security improvements
The security of a Linux system can be further improved as will be outlined in the chapters of this blog entry. These chapters will discuss how to harden the different layers of the operating system and are based on the Madaidans-insecurities page, various Linux man pages and the security considerations of PlagueOS and secureblue. Hardening the system is done to prevent as many exploits as possible. Such that in the end, you, and only you are in control of your system.
Linux Security modules
Linux Security Modules (LSM) is a framework that allows the implementation of various security models in the Linux kernel.
These security modules may be enabled by adding them to the kernel cmdline:
Landlock
Landlock (landlock) is an access-control system that enables any processes to securely restrict themselves and their future children, i.e. sandboxing.
Lockdown
Lockdown (lockdown) prevents both direct and indirect access to a running kernel image, attempting to protect against unauthorized modification of the kernel image and to prevent access to security and cryptographic data located in kernel memory, whilst still permitting driver modules to be loaded.
Yama
Yama (yama) restricts the usage of ptrace (process-trace). Where ptrace is a system call that enables the tracing of a process or signalling to a process from within another process. Although by default (without yama) only limited communication is possible due to the small fixed-size block of memory that can be passed between the two processes. Yama attaches a ptrace permission level (0-3) to each process, with these levels defined as
| Level | Restriction |
|---|---|
| 0 | No |
| 1 | Descendants-only attach |
| 2 | Admin-only attach |
| 3 | No attach |
and therefore restricts which processes can trace or signal other processes, helping to mitigate certain types of attacks, such as privilege escalation.
Integrity Policy Enforcement
Integrity Policy Enforcement (IPE) (integrity) takes a complementary approach to access control. Focusing on the immutable security properties inherent to system components. These properties are fundamental attributes or features of a system component that cannot be altered, ensuring a consistent and reliable basis for security decisions.
SELinux
Security-Enhanced Linux implements mandatory access control (MAC) policies that restrict how processes interact with each other and with files.
AppArmor
AppArmor is a security module that provides a simpler alternative to SELinux. It can dissallow access to files which the process would not require, as defined by the apparmor profile. Install the necessary packages: (1)
- For Gentoo Linux make sure to set the
apparmorUSE flag.
and add it to the boot runlevel:
and add it to the boot runlevel:
Add apparmor to the kernel cmdline to make it operational:
Then reconfigure the kernel:
You can check the status of apparmor with apparmor-utils:
Kernel boot parameters
Boot parameters configure the bootloader to parse the relevant settings to the kernel at boot. Hardening the boot process will improve the overall security of the system. The listed boot parameters in this chapter can be parsed into the kernel cmdline:
Mitigations of system vulnerabilities
-
The setting
slab_nomergedisables slab merging which helps to protect against heap exploitation. -
The settings
init_on_alloc=1 init_on_free=1enable zeroing of memory during allocation and free time, which helps to mitigate use-after-free vulnerabilities. -
The setting
page_alloc.shuffle=1randomises page allocator freelists, making page allocations less predictable. (1)- Setting this parameter actually improves performance.
-
The setting
pti=onenables kernel page-table isolation that mitigates the meltdown vulnerability and helps to protect against attempts to bypass kernel address space layout randomisation. -
The setting
randomize_kstack_offset=onrandomises the kernel stack offset on each syscall, which helps to protect against attacks that rely on deterministic kernel stack layouts. -
The setting
vsyscall=nonedisables the deprecatedvsyscalls. -
The setting
debugfs=offdisables the debugfs, removing a source of sensitive information about the kernel. -
The setting
module.sig_enforce=1enforces that only signed kernel modules can be loaded. -
The setting
lockdown=confidentialitysets the strictest option of thelockdownsecurity module. -
The setting
mce=0causes the kernel to panic on uncorrectable errors in ECC memory. This setting is unnecessary for non-ECC memory. (1)- ECC memory from a security and a redundancy perspective is always recommended. The ZFS filesystem also functions better with ECC memory.
Hardware specific mitigations of vulnerabilities
-
The setting
spectre_v2=onenables the mitigation of spectre, a speculative execution CPU vulnerability that is present in all pre-2019 CPUs. -
The setting
spec_store_bypass_disable=ondisables Speculative Store Bypass (SSB), in all pre-2019 CPUs there is a vulnerability in the SSB. -
The setting
tsx=offdisables Transactional Synchronisation Extensions (TSX), which is a feature of pre-2019 Intel CPUs. TSXs are vulnerable to cache side-channel attacks. -
The setting
tsx_async_abort=fullmitigates the TSX vulnerability if you are stupid enough to keep TSX enabled. -
The setting
mds=fullenables the mitigation of the Micro-architectural Data Sampling (MDS), a set of weaknesses in pre-2020 Intel x86_64 CPUs. -
The setting
l1tf=flushmitigates the L1 Terminal Fault vulnerability present in pre-2019, by conditional flushing of the Level 1 Data Cache. -
The setting
kvm.nx_huge_pages=forcemitigates the iTLB multihit vulnerability present in pre-2019 Intel CPUs.
So if you have a pre-2019 Intel CPU, throw it out of the window right now! The performance hit is quite significant with all these mitigations.
Hardening the boot process
-
The settings
quiet loglevel=0prevent information leaks during boot and must be used in combination with thekernel.printksysctl setting. -
The settings
rd.shell=0 rd.emergency.reboot=rebootimpose that at critical failure in the boot process the system should be rebooted and that the shell cannot be accessed at all times during this process. Hardening the boot process.
Kernel sysctl settings
Kernel self-protection can be configured by creating:
| /etc/sysctl.d/kernel.conf | |
|---|---|
- Mitigate kernel pointer leaks.
- Restrict kernel log to
CAP_SYSLOGcapability. - Restrict kernel log in console during boot.
- Restrict eBPF to
CAP_SYSLOGcapability. - Restrict eBPF to
CAP_SYSLOGcapability. - Restrict TTY line disciplines to
CAP_SYS_MODULEcapability. - Disable kexec (system call to boot another kernel during runtime).
- Disable SysRq key (debugging functionality).
- Restrict performance events to
CAP_PERFORMcapability.
Network protection can be configured by creating:
- Ignore all ICMP requests, to avoid Smurf attacks.
- Restricts resources handling SYN requests, helps protect against SYN flood attacks.
- Drops RST packets for sockets in the time-wait state, to avoid time-wait assassination attacks.
- Disables TCP SACK, for servers it could be relevant to keep this enabled.
- Disables TCP SACK, for servers it could be relevant to keep this enabled.
- Disables TCP SACK, for servers it could be relevant to keep this enabled.
- Enables source validation of packets received from all interfaces, to avoid IP spoofing.
- Enables source validation of packets received from all interfaces, to avoid IP spoofing.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables ICMP redirect acceptance and sending to prevent man-in-the-middle attacks.
- Disables source routing to prevent man-in-the-middle attacks.
- Disables source routing to prevent man-in-the-middle attacks.
- Disables source routing to prevent man-in-the-middle attacks.
- Disables source routing to prevent man-in-the-middle attacks.
Protection of the user space can be configured by creating:
| /etc/sysctl.d/user.conf | |
|---|---|
- Restrict
ptraceusage to level 2 (only processes withCAP_SYS_PTRACEcapability). - Increase the entropy for mmap ASLR, compatible with
x86_64. - Increase the entropy for mmap ASLR, compatible with
x86_64. - Restricts symlink following to only well-defined owner paths, preventing TOC/TOU races.
- Restricts symlink following to only well-defined owner paths, preventing TOC/TOU races.
- Prevent file creation in high-risk environments, helps to protect against spoofing attakcs.
- Prevent file creation in high-risk environments, helps to protect against spoofing attakcs.
Hardened memory allocator
The default memory allocator of musl is already reasonably secure but not as secure as hardened-malloc, install it with:
and set it to system-wide edit:
- If problems with graphical applications occur the light variant of hardened-malloc
/usr/lib/libhardened_malloc-light.somay also be used instead of/usr/lib/libhardened_malloc.so.
To accomodate the large number of guard pages created by hardened-malloc impose that we should set the following:
Entropy
Improve the security of the system by increasing the entropy with the jitterentropy kernel module, install it with:
and make sure that the module gets loaded: