File:  [NetBSD Developer Wiki] / wikisrc / pkgsrc / hardening.mdwn
Revision 1.32: download - view: text, annotated - select for diffs
Tue Nov 7 02:26:54 2017 UTC (5 years ago) by khorben
Branches: MAIN
CVS tags: HEAD
Add some flesh to PKGSRC_USE_STACK_CHECK

    1: [[!meta title="Hardening pkgsrc"]]
    2: 
    3: A number of mechanisms are available in
    4: [pkgsrc](https://www.pkgsrc.org/) to improve the security of the
    5: resulting system. This page describes the mechanisms, and gives hints
    6: about detecting and fixing problems.
    7: 
    8: # Mechanisms
    9: 
   10: Mechanisms can be enabled individually in `mk.conf`, and are
   11: individually described below. They are sorted by whether they are
   12: enabled by default, and then by their ordering in `mk/defaults/mk.conf`.
   13: 
   14: Typically, a feature will cause some programs to fail to build or work
   15: when first enabled. This can be due to latent problems in the
   16: program, and can be due to other reasons. After enough testing to
   17: have confidence that user problems will be quite rare, individual
   18: mechanisms will be enabled by default.
   19: 
   20: For each mechanism, see the Caveats section below for an explanation
   21: of what might go wrong at compile time and at run time, and how to
   22: notice and address these problems.
   23: 
   24: ## Enabled by default in the stable branch
   25: 
   26: ### PKGSRC_USE_FORTIFY
   27: 
   28: This allows substitute wrappers to be used for some commonly used
   29: library functions that do not have built-in bounds checking - but
   30: could in some cases.
   31: 
   32: TODO: Explain FORTIFY_SOURCE 1 vs 2, and which is used. Give a link
   33: to a good explanation of the technique. Explain if this is gcc specific.
   34: 
   35: It has been enabled by default since pkgsrc-2017Q3.
   36: 
   37: ### PKGSRC_USE_SSP
   38: 
   39: This enables a stack-smashing protection mitigation. It is done by adding a
   40: guard variable to functions with vulnerable objects. The guards are initialized
   41: when a function is entered and then checked when the function exits. The guard
   42: check will fail and the program forcibly exited if the variable was modified in
   43: the meantime. This can happen in case of buffer overflows or memory corruption,
   44: and therefore exposing these bugs.
   45: 
   46: Different mitigation levels are available:
   47: * the default ("yes"), which will only protect functions considered vulnerable
   48:   by the compiler;
   49: * "all", which will protect every function;
   50: * "strong", which will apply a better balance between the two settings above.
   51: 
   52: This mitigation is supported by both GCC and clang. It may be supported in
   53: additional compilers, possibly under a different name. It is particularly useful
   54: for unsafe programming languages, such as C/C++.
   55: 
   56: It is enabled by default where known supported since pkgsrc-2017Q3.
   57: 
   58: * <https://en.wikipedia.org/wiki/Buffer_overflow_protection>
   59: 
   60: ## Enabled by default in pkgsrc HEAD
   61: 
   62: ## Not enabled by default
   63: 
   64: ### PKGSRC_MKPIE
   65: 
   66: This requests the the creation of PIE (Position Independent
   67: Executables) for all executables. The PIE mechanism is normally used
   68: for shared libraries so that they can be loaded at differing addresses
   69: at runtime. PIE itself does not have useful security properties.
   70: However, some operating systems support Address Space Layout
   71: Randomization (ASLR), which causes different addresses to be used each
   72: time a program is run. This makes it more difficult for an attacker
   73: to guess addresses and thus makes exploits harder to construct.
   74: 
   75: PIE executables will only be built for toolchains that are known to support PIE.
   76: Currently, this means NetBSD on amd64 and i386.
   77: 
   78: ### PKGSRC_USE_RELRO
   79: 
   80: This also makes the exploitation of some security vulnerabilities more
   81: difficult in some cases.
   82: 
   83: TODO: Explain gcc vs clang, and whether this has broad support or just
   84: a few platforms.
   85: 
   86: TODO: Address "partial" vs "full"; which is this?
   87: 
   88: TODO: Give a link to a comprehensive explanation.
   89: 
   90: ### PKGSRC_USE_STACK_CHECK
   91: 
   92: This uses `-fstack-check` with GCC for another stack protection mitigation.
   93: 
   94: It asks the compiler to generate code verifying that it does not corrupt the
   95: stack. According to GCC's manual page, this is really only useful for
   96: multi-threaded programs.
   97: 
   98: # Caveats
   99: 
  100: ## Problems with `PKGSRC_MKPIE`
  101: 
  102: ### Recent support for cwrappers
  103: 
  104: `PKGSRC_MKPIE` is only supported by `pkgtools/cwrappers` from the 2017Q3
  105: release on (`USE_CWRAPPERS` in `mk.conf`).
  106: 
  107: ### Packages failing to build
  108: 
  109: A number of packages may fail to build with this option enabled. The failures
  110: are often related to the absence of the `-fPIC` compilation flag when building
  111: libraries or executables (or ideally `-fPIE` in the latter case). This flag is
  112: added to the `CFLAGS` already, but requires the package to actually support it.
  113: 
  114: #### How to fix
  115: 
  116: These instructions are meant as a reference only; they likely need to be adapted
  117: for many packages individually.
  118: 
  119: For packages using `Makefiles`:
  120: 
  121:     MAKE_FLAGS+=	CFLAGS=${CFLAGS:Q}
  122:     MAKE_FLAGS+=	LDFLAGS=${LDFLAGS:Q}
  123: 
  124: For packages using `Imakefiles`:
  125: 
  126:     MAKE_FLAGS+=	CCOPTIONS=${CFLAGS:Q}
  127:     MAKE_FLAGS+=	LOCAL_LDFLAGS=${LDFLAGS:Q}
  128: 
  129: ### Run-time crashes
  130: 
  131: Some programs may fail to run, or crash at random times once built as PIE. Two
  132: scenarios are essentially possible:
  133: 
  134: * actual bug in the program crashing, exposed thanks to ASLR/mprotect;
  135: * bug in the implementation of ASLR/mprotect in the Operating System.
  136: 
  137: ## Problems with `PKGSRC_USE_FORTIFY`
  138: 
  139: ### Packages failing to build
  140: 
  141: This feature makes use of pre-processing directives to look for hardened,
  142: alternative implementations of essential library calls. Some programs may fail
  143: to build as a result; this usually happens for those trying too hard to be
  144: portable, or otherwise abusing definitions in the standard library.
  145: 
  146: This will require a modification to the program, or disabling this feature for
  147: part or all of the build.
  148: 
  149: ### Run-time crashes
  150: 
  151: Just like with `PKGSRC_MKPIE` above, this feature may cause some programs to
  152: crash, usually indicating an actual bug in the program. The fix will typically
  153: involve patching the original program.
  154: 
  155: ### Optimization is required
  156: 
  157: At least in the case of GCC, FORTIFY will only be applied if optimization is
  158: applied while compiling. This means that the CFLAGS should also contain -O, -O2
  159: or another optimization level. This cannot easily be applied globally, as some
  160: packages may require specific optimization levels.
  161: 
  162: ## Problems with `PKGSRC_USE_RELRO`
  163: 
  164: ### Performance impact
  165: 
  166: For better protection, full RELRO requires every symbol to be resolved when the
  167: program starts, rather than simply when required at run-time. This will have
  168: more impact on programs using a lot of symbols, or linked to libraries exposing
  169: a lot of symbols. Therefore, daemons or programs otherwise running in
  170: background are affected only when started. Programs loading plug-ins at
  171: run-time are affected when loading the plug-ins.
  172: 
  173: The impact is not expected to be noticeable on modern hardware, except in some
  174: cases for big programs.
  175: 
  176: ### Run-time crashes
  177: 
  178: Some programs handle plug-ins and dependencies in a way that conflicts with
  179: RELRO: for instance, with an initialization routine listing any other plug-in
  180: required. With full RELRO, the missing symbols are resolved before the
  181: initialization routine can run, and the dynamic loader will not be able to find
  182: them directly and abort as a result. Unfortunately, this is how Xorg loads its
  183: drivers. Partial RELRO can be applied instead in this case.
  184: 
  185: ## Problems with `PKGSRC_USE_SSP`
  186: 
  187: ### Packages failing to build
  188: 
  189: The stack-smashing protection provided by this option does not work for some
  190: programs. The two most common situations in which this happens are:
  191: 
  192: * the program makes use of the `alloca(3)` library call (memory allocator on the
  193:   stack)
  194: * the program allocates variables on the stack, with the size determined at
  195:   run-time.
  196: 
  197: Both cases will require a modification to the program, or disabling this feature
  198: for part or all of the build.
  199: 
  200: ### Run-time crashes
  201: 
  202: Again, this feature may cause some programs to crash, usually indicating an
  203: actual bug in the program. Patching the original program is then required.
  204: 
  205: ### Performance impact
  206: 
  207: The compiler emits extra code when using this feature: a check for buffer
  208: overflows is performed when entering and exiting functions, requiring an extra
  209: variable on the stack. The level of protection can otherwise be adjusted to
  210: affect only those functions considered more sensitive by the compiler (with
  211: `-fstack-protector` instead of `-fstack-protector-all`).
  212: 
  213: The impact is not expected to be noticeable on modern hardware. However,
  214: programs with a hard requirement to run at the fastest possible speed should
  215: avoid using this feature, or using libraries built with this feature.
  216: 
  217: # Auditing the system
  218: 
  219: The illusion of security is worse than having no security at all. This section
  220: lists a number of ways to ensure the security features requested are actually
  221: effective.
  222: 
  223: _These instructions were obtained and tested on a system derived from NetBSD 7
  224: (amd64). YMMV._
  225: 
  226: ## Checking for PIE
  227: 
  228: The ELF executable type in use changes for binaries built as PIE; without:
  229: 
  230:     $ file /path/to/bin/ary
  231:     /path/to/bin/ary: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for NetBSD 7.0, not stripped
  232: 
  233: as opposed to the following binary, built as PIE:
  234: 
  235:     $ file /path/to/pie/bin/ary
  236:     /path/to/pie/bin/ary: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for NetBSD 7.0, not stripped
  237: 
  238: The latter result is then what is expected.
  239: 
  240: ## Checking for partial RELRO
  241: 
  242: The following command should list a section called `RELRO`:
  243: 
  244:     $ objdump -p /path/to/bin/ary
  245: 
  246:     /path/to/bin/ary:     file format elf64-x86-64
  247: 
  248:     Program Header:
  249:     [...]
  250:        RELRO off    0x0000000000000d78 vaddr 0x0000000000600d78 paddr 0x0000000000600d78 align 2**0
  251: 
  252: This check is now performed automatically if `PKG_DEVELOPER` is set and `RELRO`
  253: is enabled.
  254: 
  255: ## Checking for full RELRO
  256: 
  257: The dynamic loader will apply RELRO immediately when detecting the presence of
  258: the `BIND_NOW` flag:
  259: 
  260:     $ objdump -x /path/to/bin/ary
  261: 
  262:     /path/to/bin/ary:     file format elf64-x86-64
  263: 
  264:     Dynamic Section:
  265:     [...]
  266:       BIND_NOW             0x0000000000000000
  267: 
  268: This has to be combined with partial RELRO (see above) to be fully efficient.
  269: 
  270: ## Checking for SSP
  271: 
  272: Building objects, binaries and libraries with SSP will affect the presence of
  273: additional symbols in the resulting file:
  274: 
  275:     $ nm /path/to/bin/ary
  276:     [...]
  277:                      U __stack_chk_fail
  278:     0000000000600ea0 B __stack_chk_guard
  279: 
  280: This is an indicator that the program was indeed built with support for SSP.
  281: 
  282: # References
  283: 
  284: * <http://tk-blog.blogspot.co.at/2009/02/relro-not-so-well-known-memory.html>
  285: 

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb