Buffer overflow protection#GCC Stack-Smashing Protector (ProPolice)

{{Short description|Software security techniques}}

Buffer overflow protection is any of various techniques used during software development to enhance the security of executable programs by detecting buffer overflows on stack-allocated variables, and preventing them from causing program misbehavior or from becoming serious security vulnerabilities. A stack buffer overflow occurs when a program writes to a memory address on the program's call stack outside of the intended data structure, which is usually a fixed-length buffer. Stack buffer overflow bugs are caused when a program writes more data to a buffer located on the stack than what is actually allocated for that buffer. This almost always results in corruption of adjacent data on the stack, which could lead to program crashes, incorrect operation, or security issues.

Typically, buffer overflow protection modifies the organization of stack-allocated data so it includes a canary value that, when destroyed by a stack buffer overflow, shows that a buffer preceding it in memory has been overflowed. By verifying the canary value, execution of the affected program can be terminated, preventing it from misbehaving or from allowing an attacker to take control over it. Other buffer overflow protection techniques include bounds checking, which checks accesses to each allocated block of memory so they cannot go beyond the actually allocated space, and tagging, which ensures that memory allocated for storing data cannot contain executable code.

Overfilling a buffer allocated on the stack is more likely to influence program execution than overfilling a buffer on the heap because the stack contains the return addresses for all active function calls. However, similar implementation-specific protections also exist against heap-based overflows.

There are several implementations of buffer overflow protection, including those for the GNU Compiler Collection, LLVM, Microsoft Visual Studio, and other compilers.

Overview

{{Main|Stack buffer overflow}}

A stack buffer overflow occurs when a program writes to a memory address on the program's call stack outside of the intended data structure, which is usually a fixed-length buffer. Stack buffer overflow bugs are caused when a program writes more data to a buffer located on the stack than what is actually allocated for that buffer. This almost always results in corruption of adjacent data on the stack, and in cases where the overflow was triggered by mistake, will often cause the program to crash or operate incorrectly. Stack buffer overflow is a type of the more general programming malfunction known as buffer overflow (or buffer overrun). Overfilling a buffer on the stack is more likely to derail program execution than overfilling a buffer on the heap because the stack contains the return addresses for all active function calls.{{cite web |last1=Fithen |first1=William L. |last2=Seacord |first2=Robert |publisher=US CERT |title=VT-MB. Violation of Memory Bounds |url=https://www.securecoding.cert.org/confluence/display/sci/VT-MB.+Violation+of+Memory+Bounds |date=2007-03-27 }}

Stack buffer overflow can be caused deliberately as part of an attack known as stack smashing. If the affected program is running with special privileges, or if it accepts data from untrusted network hosts (for example, a public webserver), then the bug is a potential security vulnerability that allows an attacker to inject executable code into the running program and take control of the process. This is one of the oldest and more reliable methods for attackers to gain unauthorized access to a computer.{{cite journal |last=Levy |first=Elias |author-link=Elias Levy |title=Smashing The Stack for Fun and Profit |journal=Phrack |volume=7 |issue=49 |page=14 |date=1996-11-08 |url=http://www.phrack.org/issues/49/14.html#article }}

Typically, buffer overflow protection modifies the organization of data in the stack frame of a function call to include a "canary" value that, when destroyed, shows that a buffer preceding it in memory has been overflowed. This provides the benefit of preventing an entire class of attacks. According to some researchers,{{cite web|url=https://www.cpe.ku.ac.th/~mcs/courses/2005_02/214573/papers/buffer_overflows.pdf |title=Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade* |url-status=dead |archive-url=https://web.archive.org/web/20130309083252/http://tmp-www.cpe.ku.ac.th/~mcs/courses/2005_02/214573/papers/buffer_overflows.pdf |archive-date=2013-03-09 }} the performance impact of these techniques is negligible.

Stack-smashing protection is unable to protect against certain forms of attack. For example, it cannot protect against buffer overflows in the heap. There is no sane way to alter the layout of data within a structure; structures are expected to be the same between modules, especially with shared libraries. Any data in a structure after a buffer is impossible to protect with canaries; thus, programmers must be very careful about how they organize their variables and use their structures.

Canaries

{{Unreferenced section|date=November 2023}}

Canaries or canary words or stack cookies are known values that are placed between a buffer and control data on the stack to monitor buffer overflows. When the buffer overflows, the first data to be corrupted will usually be the canary, and a failed verification of the canary data will therefore alert of an overflow, which can then be handled, for example, by invalidating the corrupted data. A canary value should not be confused with a sentinel value.

The terminology is a reference to the historic practice of using canaries in coal mines, since they would be affected by toxic gases earlier than the miners, thus providing a biological warning system. Canaries are alternately known as stack cookies, which is meant to evoke the image of a "broken cookie" when the value is corrupted.

There are three types of canaries in use: terminator, random, and random XOR. Current versions of StackGuard support all three, while ProPolice supports terminator and random canaries.

=Terminator canaries=

Terminator canaries use the observation that most buffer overflow attacks are based on certain string operations which end at string terminators. The reaction to this observation is that the canaries are built of null terminators, CR, LF, and FF. As a result, the attacker must write a null character before writing the return address to avoid altering the canary. This prevents attacks using strcpy() and other methods that return upon copying a null character, while the undesirable result is that the canary is known. Even with the protection, an attacker could potentially overwrite the canary with its known value and control information with mismatched values, thus passing the canary check code, which is executed soon before the specific processor's return-from-call instruction.

=Random canaries=

Random canaries are randomly generated, usually from an entropy-gathering daemon, in order to prevent an attacker from knowing their value. Usually, it is not logically possible or plausible to read the canary for exploiting; the canary is a secure value known only by those who need to know it—the buffer overflow protection code in this case.

Normally, a random canary is generated at program initialization, and stored in a global variable. This variable is usually padded by unmapped pages so that attempting to read it using any kinds of tricks that exploit bugs to read off RAM cause a segmentation fault, terminating the program. It may still be possible to read the canary if the attacker knows where it is or can get the program to read from the stack.

=Random XOR canaries=

Random XOR canaries are random canaries that are XOR-scrambled using all or part of the control data. In this way, once the canary or the control data is clobbered, the canary value is wrong.

Random XOR canaries have the same vulnerabilities as random canaries, except that the "read from stack" method of getting the canary is a bit more complicated. The attacker must get the canary, the algorithm, and the control data in order to re-generate the original canary needed to spoof the protection.

In addition, random XOR canaries can protect against a certain type of attack involving overflowing a buffer in a structure into a pointer to change the pointer to point at a piece of control data. Because of the XOR encoding, the canary will be wrong if the control data or return value is changed. Because of the pointer, the control data or return value can be changed without overflowing over the canary.

Although these canaries protect the control data from being altered by clobbered pointers, they do not protect any other data or the pointers themselves. Function pointers especially are a problem here, as they can be overflowed into and can execute shellcode when called.

Bounds checking

{{Main|Bounds checking}}

Bounds checking is a compiler-based technique that adds run-time bounds information for each allocated block of memory, and checks all pointers against those at run-time. For C and C++, bounds checking can be performed at pointer calculation time{{cite web |url=http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html |title=Bounds Checking for C |publisher=Doc.ic.ac.uk |access-date=2014-04-27 |archive-url=https://web.archive.org/web/20160326081542/https://www.doc.ic.ac.uk/~phjk/BoundsChecking.html |archive-date=2016-03-26 |url-status=dead }} or at dereference time.{{cite web|url=http://sva.cs.illinois.edu/sva.html |title=SAFECode: Secure Virtual Architecture |publisher=Sva.cs.illinois.edu |date=2009-08-12 |access-date=2014-04-27}}{{cite web|url=https://code.google.com/p/address-sanitizer/|title=google/sanitizers|date=19 June 2021}}{{cite web |url=http://staff.aist.go.jp/y.oiwa/FailSafeC/index-en.html |title=Fail-Safe C: Top Page |publisher=Staff.aist.go.jp |date=2013-05-07 |access-date=2014-04-27 |archive-url=https://web.archive.org/web/20160707163127/https://staff.aist.go.jp/y.oiwa/FailSafeC/index-en.html |archive-date=2016-07-07 |url-status=dead }}

Implementations of this approach use either a central repository, which describes each allocated block of memory, or fat pointers, which contain both the pointer and additional data, describing the region that they point to.

Tagging

Tagging{{cite web |url=http://www.feustel.us/Feustel%20&%20Associates/Advantages.pdf |title=Tuesday, April 05, 2005 |website=Feustel.us |access-date=2016-09-17 |archive-url=https://web.archive.org/web/20160623195112/http://www.feustel.us/Feustel%20%26%20Associates/Advantages.pdf |archive-date=June 23, 2016 |url-status=dead }} is a compiler-based or hardware-based (requiring a tagged architecture) technique for tagging the type of a piece of data in memory, used mainly for type checking. By marking certain areas of memory as non-executable, it effectively prevents memory allocated to store data from containing executable code. Also, certain areas of memory can be marked as non-allocated, preventing buffer overflows.

Historically, tagging has been used for implementing high-level programming languages;{{cite journal |title=Tags and type checking in LISP: hardware and software approaches |year=1987 |publisher=ACM|doi=10.1145/36204.36183 |last1=Steenkiste |first1=Peter |last2=Hennessy |first2=John |journal=ACM SIGOPS Operating Systems Review |volume=21 |issue=4 |pages=50–59 |doi-access=free }} with appropriate support from the operating system, tagging can also be used to detect buffer overflows.{{cite web |url=http://public.support.unisys.com/aseries/docs/clearpath-mcp-11.0/pdf/38347639-000.pdf |title=ClearPath Enterprise Servers MCP Security Overview |publisher=Public.support.unisys.com |access-date=2014-04-27 |archive-url=https://web.archive.org/web/20130124070111/http://public.support.unisys.com/aseries/docs/clearpath-mcp-11.0/pdf/38347639-000.pdf |archive-date=2013-01-24 |url-status=dead }} An example is the NX bit hardware feature, supported by Intel, AMD and ARM processors.

Implementations

={{Anchor|STACKGUARD|PROPOLICE}}GNU Compiler Collection (GCC)=

Stack-smashing protection was first implemented by StackGuard in 1997, and published at the 1998 USENIX Security Symposium.{{cite web|url=http://www.usenix.org/publications/library/proceedings/sec98/full_papers/cowan/cowan_html/cowan.html |title=Papers - 7th USENIX Security Symposium, 1998 |publisher=Usenix.org |date=2002-04-12 |access-date=2014-04-27}} StackGuard was introduced as a set of patches to the Intel x86 backend of GCC 2.7. StackGuard was maintained for the Immunix Linux distribution from 1998 to 2003, and was extended with implementations for terminator, random and random XOR canaries. StackGuard was suggested for inclusion in GCC 3.x at the GCC 2003 Summit Proceedings,{{cite web|url=http://www.linux.org.uk/~ajh/gcc/gccsummit-2003-proceedings.pdf |title=Proceedings of the GCC Developers Summit |date=May 2003 |access-date=2016-09-17 |url-status=bot: unknown |archive-url=https://web.archive.org/web/20040715225038/http://www.linux.org.uk/~ajh/gcc/gccsummit-2003-proceedings.pdf |archive-date=2004-07-15 }} but this was never achieved.

From 2001 to 2005, IBM developed GCC patches for stack-smashing protection, known as ProPolice.{{cite web|url=http://www.research.ibm.com/trl/projects/security/ssp/ |title=GCC extension for protecting applications from stack-smashing attacks |publisher=Research.ibm.com |access-date=2014-04-27}} It improved on the idea of StackGuard by placing buffers after local pointers and function arguments in the stack frame. This helped avoid the corruption of pointers, preventing access to arbitrary memory locations.

Red Hat engineers identified problems with ProPolice though, and in 2005 re-implemented stack-smashing protection for inclusion in GCC 4.1.{{cite web|url=https://gcc.gnu.org/gcc-4.1/changes.html |title=GCC 4.1 Release Series — Changes, New Features, and Fixes - GNU Project - Free Software Foundation (FSF) |publisher=Gcc.gnu.org |access-date=2014-04-27}}{{cite web|url=https://gcc.gnu.org/ml/gcc-patches/2005-05/msg01193.html |title=Richard Henderson - [rfc] reimplementation of ibm stack-smashing protector |publisher=Gcc.gnu.org |access-date=2014-04-27}} This work introduced the -fstack-protector flag, which protects only some vulnerable functions, and the -fstack-protector-all flag, which protects all functions whether they need it or not.{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Optimize-Options.html#Optimize-Options |title=Optimize Options - Using the GNU Compiler Collection (GCC) |publisher=Gcc.gnu.org |access-date=2014-04-27}}

In 2012, Google engineers implemented the -fstack-protector-strong flag to strike a better balance between security and performance.{{cite web|url=https://gcc.gnu.org/ml/gcc-patches/2012-06/msg00974.html |title=Han Shen(ææ) - [PATCH] Add a new option "-fstack-protector-strong" (patch / doc inside) |publisher=Gcc.gnu.org |date=2012-06-14 |access-date=2014-04-27}} This flag protects more kinds of vulnerable functions than -fstack-protector does, but not every function, providing better performance than -fstack-protector-all. It is available in GCC since its version 4.9.{{cite web|last1=Edge|first1=Jake|title="Strong" stack protection for GCC|url=https://lwn.net/Articles/584225/|website=Linux Weekly News|access-date=28 November 2014|date=February 5, 2014|quote=It has made its way into GCC 4.9}}

All Fedora packages are compiled with -fstack-protector since Fedora Core 5, and -fstack-protector-strong since Fedora 20.{{cite web|url=https://fedoraproject.org/wiki/Security_Features#Stack_Smash_Protection.2C_Buffer_Overflow_Detection.2C_and_Variable_Reordering |title=Security Features |publisher=FedoraProject |date=2013-12-11 |access-date=2014-04-27}}{{cite web|url=https://fedorahosted.org/fesco/ticket/1128 |title=#1128 (switching from "-fstack-protector" to "-fstack-protector-strong" in Fedora 20) – FESCo |publisher=Fedorahosted.org |access-date=2014-04-27}} Most packages in Ubuntu are compiled with -fstack-protector since 6.10.{{cite web|url=https://wiki.ubuntu.com/Security/Features#stack-protector |title=Security/Features - Ubuntu Wiki |publisher=Wiki.ubuntu.com |access-date=2014-04-27}} Every Arch Linux package is compiled with -fstack-protector since 2011.{{cite web|url=https://bugs.archlinux.org/task/18864 |title=FS#18864 : Consider enabling GCC's stack-smashing protection (ProPolice, SSP) for all packages |publisher=Bugs.archlinux.org |access-date=2014-04-27}} All Arch Linux packages built since 4 May 2014 use -fstack-protector-strong.{{cite web |url=https://projects.archlinux.org/svntogit/packages.git/commit/trunk?h=packages/pacman&id=695ca25d4c24f3bd3b8c350d64f2697c733d5169 |archive-url=https://archive.today/20140718035407/https://projects.archlinux.org/svntogit/packages.git/commit/trunk?h=packages/pacman&id=695ca25d4c24f3bd3b8c350d64f2697c733d5169 |url-status=dead |archive-date=July 18, 2014 |title=svntogit/packages.git - Git clone of the 'packages' repository }} Stack protection is only used for some packages in Debian,{{cite web |url=http://outflux.net/debian/hardening/ |title=Debian Security Hardening Statistics |publisher=Outflux.net |access-date=2014-04-27 |archive-date=2014-04-28 |archive-url=https://web.archive.org/web/20140428012424/http://outflux.net/debian/hardening/ |url-status=dead }} and only for the FreeBSD base system since 8.0.{{cite web|url=http://www.freebsd.org/releases/8.0R/relnotes.html |title=FreeBSD 8.0-RELEASE Release Notes |publisher=Freebsd.org |date=2013-11-13 |access-date=2014-04-27}} Stack protection is standard in certain operating systems, including OpenBSD,{{cite web| url = https://man.openbsd.org/gcc-local.1| title = OpenBSD's gcc-local(1) manual page| quote = gcc comes with the ProPolice stack protection extension, which is enabled by default.}} Hardened Gentoo{{cite web|url=https://wiki.gentoo.org/wiki/Hardened/Toolchain#Default_addition_of_the_Stack_Smashing_Protector_.28SSP.29|title=Hardened/Toolchain - Gentoo Wiki|quote=The Gentoo hardened GCC switches on the stack protector by default unless explicitly requested not to.|date=2016-07-31}} and DragonFly BSD.{{Citation needed|date=September 2013}}

StackGuard and ProPolice cannot protect against overflows in automatically allocated structures that overflow into function pointers. ProPolice at least will rearrange the allocation order to get such structures allocated before function pointers. A separate mechanism for pointer protection was proposed in PointGuard{{cite web|url=http://www.usenix.org/events/sec03/tech/full_papers/cowan/cowan_html/index.html|title=12th USENIX Security Symposium — Technical Paper}} and is available on Microsoft Windows.{{cite web|url=http://blogs.msdn.com/michael_howard/archive/2006/08/16/702707.aspx|title=MSDN Blogs – Get the latest information, insights, announcements, and news from Microsoft experts and developers in the MSDN blogs.|date=6 August 2021 }}

=Microsoft Visual Studio=

The compiler suite from Microsoft implements buffer overflow protection since version 2003 through the {{Mono|/GS}} command-line switch, which is enabled by default since version 2005.{{cite web|url=http://msdn.microsoft.com/en-us/library/8dbf701c(VS.80).aspx |title=/GS (Buffer Security Check) (C++) |website=msdn.microsoft.com |access-date=2014-04-27}} Using {{Mono|/GS-}} disables the protection.

=IBM Compiler=

Stack-smashing protection can be turned on by the compiler flag -qstackprotect.{{cite web|url=http://publib.boulder.ibm.com/infocenter/comphelp/v111v131/topic/com.ibm.xlc111.aix.doc/compiler_ref/opt_stackprotect.html |title=qstackprotect |publisher=Publib.boulder.ibm.com |access-date=2014-04-27}}

=Clang/[[LLVM]]=

Clang supports the same -fstack-protector options as GCC{{cite web|url=https://lists.llvm.org/pipermail/cfe-dev/2017-April/053662.html |publisher=Clang.llvm.org |title=Clang mailing list |date=28 April 2017 |access-date=2022-11-16}} and a stronger "safe stack" ({{tt|1=-fsanitize=safe-stack}}) system with similarly low performance impact.{{cite web |title=SafeStack — Clang 17.0.0git documentation |url=https://releases.llvm.org/15.0.0/tools/clang/docs/SafeStack.html |website=clang.llvm.org}} Clang also has three buffer overflow detectors, namely AddressSanitizer (-fsanitize=address), UBSan (-fsanitize=bounds),{{cite web|url=http://clang.llvm.org/docs/UsersManual.html |title=Clang Compiler User's Manual — Clang 3.5 documentation |publisher=Clang.llvm.org |access-date=2014-04-27}}

and the unofficial SafeCode (last updated for LLVM 3.0).{{cite web|url=http://safecode.cs.illinois.edu/ |title=SAFECode |publisher=Safecode.cs.illinois.edu |access-date=2014-04-27}}

These systems have different tradeoffs in terms of performance penalty, memory overhead, and classes of detected bugs. Stack protection is standard in certain operating systems, including OpenBSD.{{cite web| url = https://man.openbsd.org/clang-local.1| title = OpenBSD's clang-local(1) manual page| quote = clang comes with stack protection enabled by default, equivalent to the -fstack-protector-strong option on other systems.}}

=Intel Compiler=

Intel's C and C++ compiler supports stack-smashing protection with options similar to those provided by GCC and Microsoft Visual Studio.{{cite web|url=https://software.intel.com/en-us/node/523162 |title=User and Reference Guide for the Intel C++ Compiler 15.0: fstack-security-check, GS |access-date=2015-02-13 |website=software.intel.com}}

= {{Anchor|FSC}}Fail-Safe C =

Fail-Safe C is an open-source memory-safe ANSI C compiler that performs bounds checking based on fat pointers and object-oriented memory access.{{cite web|url=http://staff.aist.go.jp/y.oiwa/publications/2005-PhDthesis.pdf |title=thesis.dvi |website=Staff.aist.go.jp |access-date=2016-09-17}}

=StackGhost (hardware-based)=

Invented by Mike Frantzen, StackGhost is a simple tweak to the register window spill/fill routines which makes buffer overflows much more difficult to exploit. It uses a unique hardware feature of the Sun Microsystems SPARC architecture (that being: deferred on-stack in-frame register window spill/fill) to detect modifications of return pointers (a common way for an exploit to hijack execution paths) transparently, automatically protecting all applications without requiring binary or source modifications. The performance impact is negligible, less than one percent. The resulting gdb issues were resolved by Mark Kettenis two years later, allowing enabling of the feature. Following this event, the StackGhost code was integrated (and optimized) into OpenBSD/SPARC.

See also

References

{{Reflist}}