Hardened/Gentoo Hardened and Stack Clash

Executive summary
With Gentoo Hardened no ebuilds compiled with a hardened toolchain with version 4.8 or higher should be affected by this issue as  is enabled by default. The only known exceptions are and (on )  which disable this feature.

Introduction
The Gentoo Hardened team has been made aware of a set of vulnerabilities known as Stack Clash.

These vulnerabilities involve the process stack pointer jumping onto other memory regions allowing the attacker to either modify the contents of the stack itself or the memory region on which the stack overflows. As a result, an attacker may be able to modify the data of affected programs or their execution flow and trigger the execution of arbitrary code.

The existence of such issues has been known since at least 2005 when Gael Delalleau presented an example of such an issue affecting  in.

Requirements to perform the attack

 * The attacker needs to be able to move the stack pointer onto an adjacent memory region.
 * The attacker needs to ensure this happens without accessing the gap between the stack and the next region.
 * The attacker needs to be able overwrite the area where the stack overlaps with the other section in a meaningful way. For example, overwriting the return address of a function to take control of the execution flow.

Effect of Gentoo Hardened protections
As a way to make exploitation of such issues harder vanilla Linux kernels introduced a 4kiB gap between the stack and the other regions called guard page, on PaX based kernels like the size of this gap defaults at 64kiB and can be modified using. As shown by Qualys' advisory, such gap isn't large enough for all allocations and can be jumped over in some cases.

It should be taken into account that this measure was never meant as a final solution as this problem can only be addressed correctly during code generation. Therefore, increasing the size of this gap will not deter all exploitation attempts as large enough allocations may still be able to jump over the gap. It will though affect the processes running on the system and limit the amount of virtual memory space available to them. Although this might not be a problem on 64 bit architectures where the virtual memory space is quite large, on 32 bit architectures this may still be problematic and should be taken into account before using a large heap_stack_gap value.

The Gentoo Hardened toolchain also makes use of 's Stack Checking feature using since  4.8. This makes add stack probing code when large allocations are made ensuring that the guard page or the stack gap are accessed. This will then prevent the attack by killing the process.

Although has some known bugs these doesn't seem to affect the specific packages pointed out by Qualys although they might allow exploitability of other yet unknown bugs. Also, since may leave a small (less than 6) number of pages unchecked, use of  is strongly recommended since it has a default gap size of 16 pages in x86 an amd64 arches.

To be fully effective, requires that all the source files used by the process have been compiled making use of this feature. This is because may optimize away some of the checks that are considered redundant.

An assesment over the Gentoo Portage tree hints that only two packages disable this feature. Some ebuilds for disable the flag to address. On the other hand, some ebuilds for disables this feature on HPPA builds to address.

It is possible to check the version of the compiler used on a specific ELF binary by checking the value of the section as long as it was not stripped. For this the command can be used, for example to check  run the following command: Users of the portage feature should instead check the debug file on. For example the prior example for would be:

All versions of the toolchain make use of which, although not preventing the issue, will make it harder for an attacker to take control of the execution flow of the program as depending on how the attack is performed the attacker may need to find out the value of the canary in the stack.

In this case caution is advised because the amount of ebuilds disabling SSP is much larger and, shall the frame of any function provided by the binaries generated using these ebuilds end up on the overflowed into memory section, the attacker will be able to overwrite their return pointer.

All of the binaries in Gentoo Hardened are also compiled as PIE binaries unless ebuilds or the upstream buildsystem disables it. This allows using ASLR which both vanilla and PaX kernels provide with the second being noticeably stronger. This will make it harder for an attacker to figure out a valid address on which to return.

Finally, allows enabling Deter exploit bruteforcing. This feature limits the number of attack attempts per second that can be made against SUID binaries making exploitation noticeably harder.

On the increased entropy provided by ASLR will require a large number of attack attempts before succeeding at taking control of the program reliably. This, along with the slowdown introduced by Deter exploit bruteforcing, will make the attack require a huge amount of time to succeed. Because of this, such attacks will be unfeasible in the majority of situations.

Conclusion
With the exception of exploits able to make use of to jump over the thread gap or (on HPPA systems)  users of Gentoo Hardened who have recompiled their userspaces using  4.8 or higher are protected against stack overflow issues.

Users of prior versions of have also partial protection against some of the exploits thanks to ASLR, PIE, Deter exploit bruteforcing and even SSP although they should reconsider rebuilding their userspace with a more modern version of the hardened toolchain.

Further reading reading and sources

 * Advisory by Qualys
 * Jeff Law on -fstack-check needing full coverage to be effective
 * Grsecurity's project comments on the issue
 * GCC's Stack Checking
 * GCC's Stack Checking

Thanks
The Gentoo Hardened team would like to thank the PaX Team for its outstanding work and its support whilst writting this statement.

We'd also like to thank Zorry for his hard work introducing on the toolchain.

The latest version of this statement can be found on