sodcalculator

10 Tech How-To

Harden Linux Kernel Against Heap Corruption Exploits

Guide 10-38236 · Updated · Step by step

This guide explains how to reduce the risk of heap corruption exploits on a Linux system by adjusting kernel settings. Heap corruption is a common vulnerability class where attackers overwrite memory management metadata to gain code execution. By enabling kernel hardening features and disabling unnecessary functionality, you make exploitation significantly harder. You will need root access and a recent Linux kernel (4.x or newer recommended).

What You’ll Need

  • Root or sudo access to the system
  • Linux kernel 4.x or newer (most modern distributions qualify)
  • A text editor (e.g., nano, vim) for editing sysctl.conf or GRUB configuration
  • Ability to reboot the system (for kernel command-line changes)

Step-by-Step

  1. Check current kernel version and settings. Run uname -r to see the kernel version. Then check current sysctl values with sysctl -a | grep -E 'randomize|slab|hardened|kptr' to see what is already enabled.
  2. Enable kernel address space layout randomization (KASLR). KASLR randomizes the kernel’s memory layout, making heap addresses unpredictable. Ensure it is enabled by adding kaslr to the kernel command line in your bootloader (e.g., GRUB). Edit /etc/default/grub, find the line GRUB_CMDLINE_LINUX_DEFAULT, and add kaslr if not present. Then run update-grub (or grub2-mkconfig on RHEL-based systems) and reboot.
  3. Enable kernel heap randomization (slab freelist randomization). This randomizes the order of free objects in the slab allocator, making heap feng shui attacks harder. Set the sysctl parameter: sysctl -w kernel.slab_nomerge=1 and sysctl -w kernel.kptr_restrict=2. The first prevents merging of similarly sized caches, and the second hides kernel pointers from unprivileged users. To make these permanent, add them to /etc/sysctl.conf or a file in /etc/sysctl.d/.
  4. Enable kernel page table isolation (KPTI). KPTI separates user and kernel page tables, mitigating Meltdown and also reducing the impact of some heap attacks. It is enabled by default on most modern kernels, but verify with grep -q 'pti=on' /proc/cmdline or check dmesg | grep 'Kernel/User page tables isolation'. If not enabled, add pti=on to the kernel command line.
  5. Enable strict kernel memory permissions. Set kernel.dmesg_restrict=1 to prevent unprivileged users from reading kernel logs, which could leak heap addresses. Also set kernel.perf_event_paranoid=3 to restrict performance events that could be used to probe kernel memory.
  6. Disable legacy and unnecessary features. Disable the vsyscall emulation by adding vsyscall=none to the kernel command line. This removes a known attack surface for heap-based exploits. Also consider disabling sysrq if not needed: kernel.sysrq=0.
  7. Enable hardened memory allocator options. Some kernels support the slub_debug=FZPU option to enable sanity checks and poisoning in the SLUB allocator. Add slub_debug=FZPU to the kernel command line. This catches some heap corruption early but adds performance overhead.
  8. Apply and verify changes. After editing sysctl files, run sysctl --system to apply them immediately. For kernel command-line changes, reboot. After reboot, verify with sysctl kernel.slab_nomerge kernel.kptr_restrict kernel.dmesg_restrict and check dmesg | grep -i 'slub_debug' to confirm slub debug is active.

Common Mistakes

  • Forgetting to make sysctl changes permanent: setting with sysctl -w only lasts until reboot unless added to /etc/sysctl.conf or a drop-in file.
  • Using slub_debug on production systems without performance testing: the overhead can be significant, especially on memory-heavy workloads.
  • Setting kernel.kptr_restrict=2 breaks some monitoring tools that need kernel pointers; test after applying.
  • Not updating GRUB after editing /etc/default/grub; you must run update-grub for changes to take effect.
  • Assuming all hardening is enabled by default: many distributions have conservative defaults, so verify each setting.

Tips

  • Use a separate sysctl file like /etc/sysctl.d/99-hardening.conf to keep your changes organized and easy to revert.
  • Test the system after each change to ensure stability and performance are acceptable.
  • Combine these kernel settings with userspace hardening like compiler stack protector and ASLR for a defense-in-depth approach.
  • Keep the kernel updated to the latest stable version, as new mitigations are added regularly.
  • If you use containers, note that these settings affect the host kernel and all containers, so test with your container workloads.

Troubleshooting

  • System fails to boot after adding kernel parameters: If you cannot boot, use the recovery mode or a live CD to edit the GRUB command line temporarily. Remove the problematic parameter and boot, then fix the configuration.
  • Performance degradation: If you notice significant slowdowns, especially with slub_debug=FZPU, consider disabling it or using a less aggressive option like slub_debug=F (sanity checks only).
  • Tools break due to kptr_restrict: Some tools like perf or bpf may require kernel pointers. Set kernel.kptr_restrict=1 instead of 2 if you need restricted but not full hiding.
  • sysctl settings not applied: Ensure the file permissions are correct (readable by root) and that you ran sysctl --system. Check for typos in the parameter names.
  • Changes not persistent after reboot: Verify that the sysctl file is in the correct directory and that the parameters are not overridden by other files (e.g., /etc/sysctl.d/99-hardening.conf should have higher priority than /etc/sysctl.conf).

Frequently Asked Questions

Does this guarantee protection against heap corruption?

No, these settings make exploitation harder but do not eliminate the risk. They raise the bar for attackers, but a determined attacker with a zero-day could still bypass them.

Will these settings affect application performance?

Some settings, especially slub_debug and slab_nomerge, can add overhead. The impact varies by workload; test in your environment.

Can I apply these settings on a live system without reboot?

Most sysctl settings can be applied immediately with sysctl -w. Kernel command-line parameters require a reboot to take effect.

Are these settings compatible with cloud VMs?

Yes, but some hypervisors may not support certain features like KASLR. Check your VM’s kernel command line to see if kaslr is active.

Do I need to configure anything in the bootloader?

Yes, for kernel command-line parameters like kaslr, pti=on, vsyscall=none, and slub_debug, you must edit GRUB or your bootloader’s configuration and reboot.

What if my distribution already enables some of these?

Many modern distributions enable KASLR and KPTI by default. You can check /proc/cmdline and sysctl to see what is already active, and only add the missing ones.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →