|
|
Line 2: |
Line 2: |
| <div style="width:100%;height:160px;border:0,margin:0;overflow: hidden;">[[File:Cheatsheets-header.jpg|link=]]</div> | | <div style="width:100%;height:160px;border:0,margin:0;overflow: hidden;">[[File:Cheatsheets-header.jpg|link=]]</div> |
| | | |
− | {| style="padding: 0;margin:0;margin-top:10px;text-align:left;" |-
| + | The Cheat Sheet Series project has been moved to [https://github.com/OWASP/CheatSheetSeries GitHub]! |
− | | valign="top" style="border-right: 1px dotted gray;padding-right:25px;" |
| |
− | Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''
| |
− | = Introduction =
| |
− | __TOC__{{TOC hidden}}
| |
− | [[C-Based Toolchain Hardening Cheat Sheet]] is a brief treatment of project settings that will help you deliver reliable and secure code when using C, C++ and Objective C languages in a number of development environments. A more in-depth treatment of this topic can be found [https://www.owasp.org/index.php/C-Based_Toolchain_Hardening here]. This cheatsheet will guide you through the steps you should take to create executables with firmer defensive postures and increased integration with the available platform security. Effectively configuring the toolchain also means your project will enjoy a number of benefits during development, including enhanced warnings and static analysis, and self-debugging code.
| |
| | | |
− | There are four areas to be examined when hardening the toolchain: configuration, integration, static analysis, and platform security. Nearly all areas are overlooked or neglected when setting up a project. The neglect appears to be pandemic, and it applies to nearly all projects including Auto-configured projects, Makefile-based, Eclipse-based, and Xcode-based. It's important to address the gaps at configuration and build time because it's difficult to impossible to [http://sourceware.org/ml/binutils/2012-03/msg00309.html add hardening on a distributed executable after the fact] on some platforms.
| + | Please visit [https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/C-Based_Toolchain_Hardening_Cheat_Sheet.md C-Based Toolchain Hardening Cheat Sheet] to see the latest version of the cheat sheet. |
− | | |
− | For those who would like a deeper treatment of the subject matter, please visit [[C-Based_Toolchain_Hardening|C-Based Toolchain Hardening]].
| |
− | | |
− | == Actionable Items ==
| |
− | | |
− | The [[C-Based Toolchain Hardening Cheat Sheet]] calls for the following actionable items:
| |
− | | |
− | * Provide debug, release, and test configurations
| |
− | * Provide an assert with useful behavior
| |
− | * Configure code to take advantage of configurations
| |
− | * Properly integrate third party libraries
| |
− | * Use the compiler's built-in static analysis capabilities
| |
− | * Integrate with platform security measures
| |
− | | |
− | The remainder of this cheat sheet briefly explains the bulleted, actionable items. For a thorough treatment, please visit the [[C-Based_Toolchain_Hardening|full article]].
| |
− | | |
− | == Build Configurations ==
| |
− | | |
− | You should support three build configurations. First is ''Debug'', second is ''Release'', and third is ''Test''. One size does '''not''' fit all, and each speaks to a different facet of the engineering process. You will use a debug build while developing, your continuous integration or build server will use test configurations, and you will ship release builds.
| |
− | | |
− | 1970's K&R code and one size fits all flags are from a bygone era. Processes have evolved and matured to meet the challenges of a modern landscape, including threats. Because tools like Autconfig and Automake [https://lists.gnu.org/archive/html/automake/2012-12/msg00019.html do not support the notion of build configurations], you should prefer to work in an Integrated Develop Environments (IDE) or write your makefiles so the desired targets are supported. In addition, Autconfig and Automake often ignore user supplied flags (it depends on the folks writing the various scripts and templates), so you might find it easier to again write a makefile from scratch rather than retrofitting existing auto tool files.
| |
− | | |
− | === Debug Builds ===
| |
− | | |
− | Debug is used during development, and the build assists you in finding problems in the code. During this phase, you develop your program and test integration with third party libraries you program depends upon. To help with debugging and diagnostics, you should define <tt>DEBUG</tt> and <tt>_DEBUG</tt> (if on a Windows platform) preprocessor macros and supply other 'debugging and diagnostic' oriented flags to the compiler and linker. Additional preprocessor macros for selected libraries are offered in the [[C-Based_Toolchain_Hardening|full article]].
| |
− | | |
− | You should use the following for GCC when building for debug: <tt>-O0</tt> (or <tt>-O1</tt>) and <tt>-g3</tt> <tt>-ggdb</tt>. No optimizations improve debuggability because optimizations often rearrange statements to improve instruction scheduling and remove unneeded code. You may need <tt>-O1</tt> to ensure some analysis is performed. <tt>-g3</tt> ensures maximum debug information is available, including symbolic constants and <tt>#defines</tt>.
| |
− | | |
− | Asserts will help you write self debugging programs. The program will alert you to the point of first failure quickly and easily. Because asserts are so powerful, the code should be completely and full instrumented with asserts that: (1) validates and asserts all program state relevant to a function or a method; (2) validates and asserts all function parameters; and (3) validates and asserts all return values for functions or methods which return a value. Because of item (3), you should be very suspicious of void functions that cannot convey failures.
| |
− | | |
− | Anywhere you have an <tt>if</tt> statement for validation, you should have an assert. Anywhere you have an assert, you should have an <tt>if</tt> statement. They go hand-in-hand. Posix states if <tt>NDEBUG</tt> is '''not''' defined, then <tt>assert</tt> [http://pubs.opengroup.org/onlinepubs/009604499/functions/assert.html "shall write information about the particular call that failed on stderr and shall call abort"]. Calling abort during development is useless behavior, so you must supply your own assert that <tt>SIGTRAP</tt>s. A Unix and Linux example of a <tt>SIGTRAP</tt> based assert is provided in the [[C-Based_Toolchain_Hardening|full article]].
| |
− | | |
− | Unlike other debugging and diagnostic methods - such as breakpoints and <tt>printf</tt> - asserts stay in forever and become silent guardians. If you accidentally nudge something in an apparently unrelated code path, the assert will snap the debugger for you. The enduring coverage means debug code - with its additional diagnostics and instrumentation - is more highly valued than unadorned release code. If code is checked in that does not have the additional debugging and diagnostics, including full assertions, you should reject the check-in.
| |
− | | |
− | === Release Builds ===
| |
− | | |
− | Release builds are diametrically opposed to debug configurations. In a release configuration, the program will be built for use in production. Your program is expected to operate correctly, securely and efficiently. The time for debugging and diagnostics is over, and your program will define <tt>NDEBUG</tt> to remove the supplemental information and behavior.
| |
− | | |
− | A release configuration should also use <tt>-O2</tt>/<tt>-O3</tt>/<tt>-Os</tt> and <tt>-g1</tt>/<tt>-g2</tt>. The optimizations will make it somewhat more difficult to make sense of a stack trace, but they should be few and far between. The <tt>-g''N''</tt> flag ensures debugging information is available for post mortem analysis. While you generate debugging information for release builds, you should strip the information before shipping and check the symbols into you version control system along with the tagged build.
| |
− | | |
− | Release builds should also consider the configuration pair of <tt>-mfunction-return=thunk</tt> and <tt>-mindirect-branch=thunk</tt>. These are the "Reptoline" fix which is an indirect branch used to thwart speculative execution CPU vulnerabilities such as Spectre and Meltdown. The CPU cannot tell what code to [speculatively] execute because it is an indirect (as opposed to a direct) branch. This is an extra layer of indirection, like calling a pointer through a pointer.
| |
− | | |
− | <tt>NDEBUG</tt> will also remove asserts from your program by defining them to <tt>void</tt> since its not acceptable to crash via <tt>abort</tt> in production. You should not depend upon assert for crash report generation because those reports could contain sensitive information and may end up on foreign systems, including for example, [http://msdn.microsoft.com/en-us/library/windows/hardware/gg487440.aspx Windows Error Reporting]. If you want a crash dump, you should generate it yourself in a controlled manner while ensuring no sensitive information is written or leaked.
| |
− | | |
− | Release builds should also curtail logging. If you followed earlier guidance, you have properly instrumented code and can determine the point of first failure quickly and easily. Simply log the failure and and relevant parameters. Remove all <tt>NSLog</tt> and similar calls because sensitive information might be logged to a system logger. Worse, the data in the logs might be egressed by backup or sync. If your default configuration includes a logging level of ten or ''maximum verbosity'', you probably lack stability and are trying to track problems in the field. That usually means your program or library is not ready for production.
| |
− | | |
− | === Test Builds ===
| |
− | | |
− | A Test build is closely related to a release build. In this build configuration, you want to be as close to production as possible, so you should be using <tt>-O2</tt>/<tt>-O3</tt>/<tt>-Os</tt> <tt>-g1</tt>/<tt>-g2</tt> and "Reptoline" configuration options. You will run your suite of ''positive'' and ''negative'' tests against the test build.
| |
− | | |
− | You will also want to exercise all functions or methods provided by the program and not just the public interfaces, so everything should be made public. For example, all member functions public (C++ classes), all selectors (Objective C), all methods (Java), and all interfaces (library or shared object) should be made available for testing. As such, you should:
| |
− | | |
− | * Add <tt>-Dprotected=public -Dprivate=public</tt> to <tt>CFLAGS</tt> and <tt>CXXFLAGS</tt>
| |
− | * Change <tt>__attribute__ ((visibility ("hidden")))</tt> to <tt>__attribute__ ((visibility ("default")))</tt>
| |
− | | |
− | Many Object Oriented purist oppose testing private interfaces, but this is not about object oriented-ness. This (''q.v.'') is about building reliable and secure software.
| |
− | | |
− | You should also concentrate on negative tests. Positive self tests are relatively useless except for functional and regression tests. Since this is your line of business or area of expertise, you should have the business logic correct when operating in a benign environment. A hostile or toxic environment is much more interesting, and that's where you want to know how your library or program will fail in the field when under attack.
| |
− | | |
− | == Library Integration ==
| |
− | | |
− | You must properly integrate and utilize libraries in your program. Proper integration includes acceptance testing, configuring for your build system, identifying libraries you ''should'' be using, and correctly using the libraries. A well integrated library can compliment your code, and a poorlly written library can detract from your program. Because a stable library with required functionality can be elusive and its tricky to integrate libraries, you should try to minimize dependencies and avoid thrid party libraries whenever possible.
| |
− | | |
− | Acceptance testing a library is practically non-existent. The testing can be a simple code review or can include additional measures, such as negative self tests. If the library is defective or does not meet standards, you must fix it or reject the library. An example of lack of acceptance testing is [http://www.agarri.fr/blog/index.html Adobe's inclusion of a defective Sablotron library], which resulted in [http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2012-1525 CVE-2012-1525]. Another example is the 10's to 100's of millions of vulnerable embedded devices due to defects in <tt>libupnp</tt>. While its popular to lay blame on others, the bottom line is you chose the library so you are responsible for it.
| |
− | | |
− | You must also ensure the library is integrated into your build process. For example, the OpenSSL library should be configured '''without''' SSLv2, SSLv3 and compression since they are defective. That means <tt>config</tt> should be executed with <tt>-no-comp</tt> <tt>-no-sslv2</tt> and <tt>-no-sslv3</tt>. As an additional example, using STLPort your debug configuration should also define <tt>_STLP_DEBUG=1</tt>, <tt>_STLP_USE_DEBUG_LIB=1</tt>, <tt>_STLP_DEBUG_ALLOC=1</tt>, <tt>_STLP_DEBUG_UNINITIALIZED=1</tt> because the library offers the additional diagnostics during development.
| |
− | | |
− | Debug builds also present an opportunity to use additional libraries to help locate problems in the code. For example, you should be using a memory checker such as ''Debug Malloc Library (Dmalloc)'' during development. If you are not using Dmalloc, then ensure you have an equivalent checker, such as GCC 4.8's <tt>-fsanitize=memory</tt>. This is one area where one size clearly does not fit all.
| |
− | | |
− | Using a library properly is always difficult, especially when there is no documentation. Review any hardening documents available for the library, and be sure to visit the library's documentation to ensure proper API usage. If required, you might have to review code or step library code under the debugger to ensure there are no bugs or undocumented features.
| |
− | | |
− | == Static Analysis ==
| |
− | | |
− | Compiler writers do a fantastic job of generating object code from source code. The process creates a lot of additional information useful in analyzing code. Compilers use the analysis to offer programmers warnings to help detect problems in their code, but the catch is you have to ask for them. After you ask for them, you should take time to understand what the underlying issue is when a statement is flagged. For example, compilers will warn you when comparing a signed integer to an unsigned integer because <tt>-1 > 1</tt> after C/C++ promotion. At other times, you will need to back off some warnings to help separate the wheat from the chaff. For example, interface programming is a popular C++ paradigm, so <tt>-Wno-unused-parameter</tt> will probably be helpful with C++ code.
| |
− | | |
− | You should consider a clean compile as a security gate. If you find its painful to turn warnings on, then you have likely been overlooking some of the finer points in the details. In addition, you should strive for multiple compilers and platforms support since each has its own personality (and interpretation of the C/C++ standards). By the time your core modules clean compile under Clang, GCC, ICC, and Visual Studio on the Linux and Windows platforms, your code will have many stability obstacles removed.
| |
− | | |
− | When compiling programs with GCC, you should use the following flags to help detect errors in your programs. The options should be added to <tt>CFLAGS</tt> for a program with C source files, and <tt>CXXFLAGS</tt> for a program with C++ source files. Objective C developers should add their warnings to <tt>CFLAGS</tt>: <tt>-Wall -Wextra -Wconversion (or -Wsign-conversion), -Wcast-align, -Wformat=2 -Wformat-security, -fno-common, -Wmissing-prototypes, -Wmissing-declarations, -Wstrict-prototypes, -Wstrict-overflow, and -Wtrampolines</tt>. C++ presents additional opportunities under GCC, and the flags include <tt>-Woverloaded-virtual, -Wreorder, -Wsign-promo, -Wnon-virtual-dtor</tt> and possibly <tt>-Weffc++</tt>. Finally, Objective C should include <tt>-Wstrict-selector-match</tt> and <tt>-Wundeclared-selector</tt>.
| |
− | | |
− | For a Microsoft platform, you should use: <tt>/W4</tt>, <tt>/Wall</tt>, and <tt>/analyze</tt>. If you don't use <tt>/Wall</tt>, Microsoft recomends using <tt>/W4</tt> and enabling C4191, C4242, C4263, C4264, C4265, C4266, C4302, C4826, C4905, C4906, and C4928. Finally, <tt>/analyze</tt> is Enterprise Code Analysis, which is freely available with the [http://www.microsoft.com/en-us/download/details.aspx?id=24826 Windows SDK for Windows Server 2008 and .NET Framework 3.5 SDK] (you don't need Visual Studio Enterprise edition).
| |
− | | |
− | For additional details on the GCC and Windows options and flags, see ''[http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html GCC Options to Request or Suppress Warnings]'', ''[http://blogs.msdn.com/b/vcblog/archive/2010/12/14/off-by-default-compiler-warnings-in-visual-c.aspx “Off By Default” Compiler Warnings in Visual C++]'', and ''[http://msdn.microsoft.com/en-us/magazine/cc337897.aspx Protecting Your Code with Visual C++ Defenses]''.
| |
− | | |
− | == Platform Security ==
| |
− | | |
− | Integrating with platform security is essential to a defensive posture. Platform security will be your safety umbrella if someone discovers a bug with security implications - and you should always have it with you. For example, if your parser fails, then no-execute stacks and heaps can turn a 0-day into an annoying crash. Not integrating often leaves your users and customers vulnerable to malicious code. While you may not be familiar with some of the flags, you are probably familiar with the effects of omitting them. For example, Android's Gingerbreak overwrote the Global Offset Table (GOT) in the ELF headers, and could have been avoided with <tt>-z,relro</tt>.
| |
− | | |
− | When integrating with platform security on a Linux host, you should use the following flags: <tt>-fPIE</tt> (compiler) and <tt>-pie</tt> (linker), -fstack-protector-all (or -fstack-protector), <tt>-z,noexecstack</tt>, <tt>-z,now</tt>, <tt>-z,relro</tt>. If available, you should also use <tt>_FORTIFY_SOURCES=2</tt> (or <tt>_FORTIFY_SOURCES=1</tt> on Android 4.2), <tt>-fsanitize=address</tt> and <tt>-fsanitize=thread</tt> (the last two should be used in debug configurations). <tt>-z,nodlopen</tt> and <tt>-z,nodump</tt> might help in reducing an attacker's ability to load and manipulate a shared object. On Gentoo and other systems with no-exec heaps, you should also use <tt>-z,noexecheap</tt>.
| |
− | | |
− | Windows programs should include <tt>/dynamicbase</tt>, <tt>/NXCOMPAT</tt>, <tt>/GS</tt>, and <tt>/SafeSEH</tt> to ensure address space layout randomizations (ASLR), data execution prevention (DEP), use of stack cookies, and thwart exception handler overwrites.
| |
− | | |
− | For additional details on the GCC and Windows options and flags, see ''[http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html GCC Options Summary]'' and ''[http://msdn.microsoft.com/en-us/magazine/cc337897.aspx Protecting Your Code with Visual C++ Defenses]''.
| |
− | | |
− | = Authors and Editors =
| |
− | | |
− | * Jeffrey Walton - jeffrey, owasp.org
| |
− | * Jim Manico - jim, owasp.org
| |
− | * Kevin Wall - kevin, owasp.org
| |
− | | |
− | = Other Cheatsheets =
| |
− | | |
− | {{Cheatsheet_Navigation_Body}}
| |
− | | |
− | |}
| |
− | | |
− | [[Category:Cheatsheets]]
| |