TL;DR
By default, some Linux distributions don’t randomise library base addresses independently of each other. This weakens Address Space Layout Randomisation (ASLR), making exploitation easier. We’ll show you how to enable separate library randomisation for better cyber security.
Improving ASLR Library Randomisation
- Check Current ASLR Status: First, see what’s currently enabled.
- Run the following command:
cat /proc/sys/kernel/randomize_va_space - Values mean:
- 0: No ASLR.
- 1: Randomise stack, mmap(), brk().
- 2: Also randomise heap.
- Run the following command:
- Check Library Randomisation: Determine if libraries are randomised independently.
- Use the following command to check the entropy of library base addresses:
cat /proc/self/maps | awk '{print $6}' | sort -u | wc -lThis counts unique library load addresses. A low number suggests poor randomisation.
- Use the following command to check the entropy of library base addresses:
- Enable Separate Library Randomisation: This is the core step.
- Edit
/etc/sysctl.confas root (using your favourite editor, e.g.,sudo nano /etc/sysctl.conf). - Add or modify these lines:
kernel.randomize_va_space = 2kernel.kstack_pages_min = 4096kernel.sysvipc_shmall = 4096
- Edit
- Apply the Changes: Reload the sysctl configuration.
- Run:
sudo sysctl -p - Verify the change:
cat /proc/sys/kernel/randomize_va_spaceIt should now show 2.
- Run:
- Reboot (Recommended): A full reboot ensures all processes benefit from the new ASLR settings. While not always *strictly* necessary, it’s best practice.
- Run:
sudo reboot
- Run:
- Verify After Reboot: Confirm that library randomisation is improved after the reboot.
- Repeat step 2 (using
cat /proc/self/maps | awk '{print $6}' | sort -u | wc -l) to check the number of unique library addresses. You should see a significantly higher count than before, indicating better randomisation.
- Repeat step 2 (using
Important Considerations
- Compatibility: While generally safe, very old applications might have issues with stronger ASLR. Test thoroughly after making changes.
- Performance: Stronger ASLR can introduce a small performance overhead, but it’s usually negligible on modern hardware.