June 24th, 2024

GCC's new fortification level: The gains and costs

GCC introduces _FORTIFY_SOURCE=3 for enhanced security by detecting buffer overflows in C programs at runtime. This level offers precise object size estimates, improving fortification coverage and revealing more issues in glibc. Despite potential impacts, the security benefits outweigh costs, emphasizing the importance of fortification for application security.

Read original articleLink Icon
GCC's new fortification level: The gains and costs

This article discusses the introduction of a new fortification level, _FORTIFY_SOURCE=3, in GCC to enhance security by detecting buffer overflows and bugs in C programs at runtime. This new level offers improved buffer size detection through the __builtin_dynamic_object_size builtin, which provides precise object size estimates at execution time, leading to better fortification coverage compared to _FORTIFY_SOURCE=2. The implementation of _FORTIFY_SOURCE=3 in GCC has revealed more buffer overflow issues, including those in the GNU C library (glibc), prompting fixes and improvements in programming patterns. The increased fortification coverage has been demonstrated to significantly enhance security, with examples showing substantial improvements in fortification metrics for various applications. Despite potential impacts on code size and performance, the benefits of improved security coverage outweigh the costs associated with implementing _FORTIFY_SOURCE=3. This advancement underscores the ongoing importance of object size determination and fortification in enhancing application security and mitigating vulnerabilities.

Related

As you learn Forth, it learns from you (1981)

As you learn Forth, it learns from you (1981)

The Forth programming language is highlighted for its unique features like extensibility, speed, and efficiency. Contrasted with Basic, Forth's threaded code system and data handling methods make it versatile.

A buffer overflow in the XNU kernel

A buffer overflow in the XNU kernel

CVE-2024-27815 is a buffer overflow bug in XNU kernel affecting macOS, iOS, and visionOS. Apple swiftly released xnu-10063.121.3 to fix the issue, impacting kernels with CONFIG_MBUF_MCACHE. The bug allows attackers to trigger a crash by copying data beyond allocated space.

Memory sealing for the GNU C Library

Memory sealing for the GNU C Library

The GNU C Library introduces mseal() system call for enhanced security by preventing address space changes. Adhemerval Zanella's patch series adds support, improving memory manipulation protection in upcoming releases.

Own Constant Folder in C/C++

Own Constant Folder in C/C++

Neil Henning discusses precision issues in clang when using the sqrtps intrinsic with -ffast-math, suggesting inline assembly for instruction selection. He introduces a workaround using __builtin_constant_p for constant folding optimization, enhancing code efficiency.

How GCC and Clang handle statically known undefined behaviour

How GCC and Clang handle statically known undefined behaviour

Discussion on compilers handling statically known undefined behavior (UB) in C code reveals insights into optimizations. Compilers like gcc and clang optimize based on undefined language semantics, potentially crashing programs or ignoring problematic code. UB avoidance is crucial for program predictability and security. Compilers differ in handling UB, with gcc and clang showing variations in crash behavior and warnings. LLVM's 'poison' values allow optimizations despite UB, reflecting diverse compiler approaches. Compiler responses to UB are subjective, influenced by developers and user requirements.

Link Icon 13 comments
By @dwattttt - 5 months
> the program continued using the same pointer, not the realloc call result, since the old pointer did not change

> In this context, it is a bug in the application

This is a non-intuitive result by the normal lens C pointers are viewed by; these two pointers compared equal, how could using one differ from using the other? Pointer provenance rears its ugly head here though; one of those "pointers that have the same value"'s provenance is different to the other. By the standard, they _are_ allowed to be treated differently, and honestly the standard requires it.

https://www.ralfj.de/blog/2020/12/14/provenance.html is pretty accessible, and shockingly on point: "just because two pointers point to the same address, does not mean they are equal in the sense that they can be used interchangeably".

EDIT: spelling

By @o11c - 5 months
The `realloc` problem isn't the only one - it also breaks many formerly-well-defined programs that use `malloc_usable_size`.

Instead of fixing this, the developers behind the "dynamic object size" push have been changing the documentation to declare any use of `malloc_usable_size` buggy even though it used to be explicitly documented as safe.

I suspect that GCC's optimization passes will break even C-standard-compliant use of `realloc`, similar to how ASAN can break due to dynamic equality checks between pointers of different provenance.

Life would be much simpler for many of us if the standards committee bothered to standardize a function that says "give me a buffer of semi-arbitrary size and tell me how big it is; I promise to resize it later", which is very widely intended. An explicit "realloc in place only, else fail" would also make many more useful programs feasible to write.

By @dwheeler - 5 months
For a comprehensive set of recommended compiler options for hardening C and C++ programs, see this OpenSSF guide: https://best.openssf.org/Compiler-Hardening-Guides/Compiler-...
By @matheusmoreira - 5 months
I wish these compiler protocols and interfaces were better documented. They just assume you're using glibc for support. I want to fully integrate them with my freestanding nolibc C projects but I can't figure out how to do it.

The compiler's stack canaries were simple enough. The only issue was the ugly symbols. I requested that a feature be added to override the symbol generated by the compiler so I could use good names instead.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113694

Things like instrumentation and sanitizers though? No idea. Even asked about it on Stack Overflow and got no answers to this day.

https://stackoverflow.com/q/77688456

I assume these object sizing builtins make use of function attributes such as malloc, alloc_align and alloc_size. I've added all of those attributes to my memory allocator but I'm not quite sure if they're doing anything useful.

By @jupp0r - 5 months
I would imagine false positives could be a huge problem. The behavior if a violation is detected is to gracefully terminate the program, so you could end up with more (but less exploitable) crashes than without FORTIFY_SOURCE.
By @temac - 5 months
Unfortunately _FORTIFY_SOURCE=2 is documented as making some conforming program fail, and it seems hard to find precise informations on this subject, so i'd be hesitant to use it, as well as _FORTIFY_SOURCE=3
By @throwaway81523 - 5 months
Title should be: GCC's new (2022) fortification level... ;)
By @loeg - 5 months
(2022)
By @rdtsc - 5 months
Just curious, does anyone run with _FORTIFY_SOURCE=3 in production? Did you catch any overflows because of it, and most importantly, is there a noticeable performance degradation?
By @ngneer - 5 months
I would encourage the community to find a unit of measurement for security. Every defense can be breached, so every defense has an active region, much like transistors. Question is how to quantify.
By @notorandit - 5 months
> C programs routinely suffer from memory management problems.

Again? There is a typo there. - programs + programmers

By @clarionbell - 5 months
Oddly the example doesn't work on my GCC 13.3.1, regression or just bad copy paste?
By @jiveturkey - 5 months
article date 2022

the new feature appeared in 2021