File:  [NetBSD Developer Wiki] / wikisrc / pkgsrc / hardening.mdwn
Revision 1.31: download - view: text, annotated - select for diffs
Tue Nov 7 02:22:50 2017 UTC (5 years ago) by khorben
Branches: MAIN
CVS tags: HEAD
Clarify support for PIE

    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
   93: mitigation.
   94: 
   95: # Caveats
   96: 
   97: ## Problems with `PKGSRC_MKPIE`
   98: 
   99: ### Recent support for cwrappers
  100: 
  101: `PKGSRC_MKPIE` is only supported by `pkgtools/cwrappers` from the 2017Q3
  102: release on (`USE_CWRAPPERS` in `mk.conf`).
  103: 
  104: ### Packages failing to build
  105: 
  106: A number of packages may fail to build with this option enabled. The failures
  107: are often related to the absence of the `-fPIC` compilation flag when building
  108: libraries or executables (or ideally `-fPIE` in the latter case). This flag is
  109: added to the `CFLAGS` already, but requires the package to actually support it.
  110: 
  111: #### How to fix
  112: 
  113: These instructions are meant as a reference only; they likely need to be adapted
  114: for many packages individually.
  115: 
  116: For packages using `Makefiles`:
  117: 
  118:     MAKE_FLAGS+=	CFLAGS=${CFLAGS:Q}
  119:     MAKE_FLAGS+=	LDFLAGS=${LDFLAGS:Q}
  120: 
  121: For packages using `Imakefiles`:
  122: 
  123:     MAKE_FLAGS+=	CCOPTIONS=${CFLAGS:Q}
  124:     MAKE_FLAGS+=	LOCAL_LDFLAGS=${LDFLAGS:Q}
  125: 
  126: ### Run-time crashes
  127: 
  128: Some programs may fail to run, or crash at random times once built as PIE. Two
  129: scenarios are essentially possible:
  130: 
  131: * actual bug in the program crashing, exposed thanks to ASLR/mprotect;
  132: * bug in the implementation of ASLR/mprotect in the Operating System.
  133: 
  134: ## Problems with `PKGSRC_USE_FORTIFY`
  135: 
  136: ### Packages failing to build
  137: 
  138: This feature makes use of pre-processing directives to look for hardened,
  139: alternative implementations of essential library calls. Some programs may fail
  140: to build as a result; this usually happens for those trying too hard to be
  141: portable, or otherwise abusing definitions in the standard library.
  142: 
  143: This will require a modification to the program, or disabling this feature for
  144: part or all of the build.
  145: 
  146: ### Run-time crashes
  147: 
  148: Just like with `PKGSRC_MKPIE` above, this feature may cause some programs to
  149: crash, usually indicating an actual bug in the program. The fix will typically
  150: involve patching the original program.
  151: 
  152: ### Optimization is required
  153: 
  154: At least in the case of GCC, FORTIFY will only be applied if optimization is
  155: applied while compiling. This means that the CFLAGS should also contain -O, -O2
  156: or another optimization level. This cannot easily be applied globally, as some
  157: packages may require specific optimization levels.
  158: 
  159: ## Problems with `PKGSRC_USE_RELRO`
  160: 
  161: ### Performance impact
  162: 
  163: For better protection, full RELRO requires every symbol to be resolved when the
  164: program starts, rather than simply when required at run-time. This will have
  165: more impact on programs using a lot of symbols, or linked to libraries exposing
  166: a lot of symbols. Therefore, daemons or programs otherwise running in
  167: background are affected only when started. Programs loading plug-ins at
  168: run-time are affected when loading the plug-ins.
  169: 
  170: The impact is not expected to be noticeable on modern hardware, except in some
  171: cases for big programs.
  172: 
  173: ### Run-time crashes
  174: 
  175: Some programs handle plug-ins and dependencies in a way that conflicts with
  176: RELRO: for instance, with an initialization routine listing any other plug-in
  177: required. With full RELRO, the missing symbols are resolved before the
  178: initialization routine can run, and the dynamic loader will not be able to find
  179: them directly and abort as a result. Unfortunately, this is how Xorg loads its
  180: drivers. Partial RELRO can be applied instead in this case.
  181: 
  182: ## Problems with `PKGSRC_USE_SSP`
  183: 
  184: ### Packages failing to build
  185: 
  186: The stack-smashing protection provided by this option does not work for some
  187: programs. The two most common situations in which this happens are:
  188: 
  189: * the program makes use of the `alloca(3)` library call (memory allocator on the
  190:   stack)
  191: * the program allocates variables on the stack, with the size determined at
  192:   run-time.
  193: 
  194: Both cases will require a modification to the program, or disabling this feature
  195: for part or all of the build.
  196: 
  197: ### Run-time crashes
  198: 
  199: Again, this feature may cause some programs to crash, usually indicating an
  200: actual bug in the program. Patching the original program is then required.
  201: 
  202: ### Performance impact
  203: 
  204: The compiler emits extra code when using this feature: a check for buffer
  205: overflows is performed when entering and exiting functions, requiring an extra
  206: variable on the stack. The level of protection can otherwise be adjusted to
  207: affect only those functions considered more sensitive by the compiler (with
  208: `-fstack-protector` instead of `-fstack-protector-all`).
  209: 
  210: The impact is not expected to be noticeable on modern hardware. However,
  211: programs with a hard requirement to run at the fastest possible speed should
  212: avoid using this feature, or using libraries built with this feature.
  213: 
  214: # Auditing the system
  215: 
  216: The illusion of security is worse than having no security at all. This section
  217: lists a number of ways to ensure the security features requested are actually
  218: effective.
  219: 
  220: _These instructions were obtained and tested on a system derived from NetBSD 7
  221: (amd64). YMMV._
  222: 
  223: ## Checking for PIE
  224: 
  225: The ELF executable type in use changes for binaries built as PIE; without:
  226: 
  227:     $ file /path/to/bin/ary
  228:     /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
  229: 
  230: as opposed to the following binary, built as PIE:
  231: 
  232:     $ file /path/to/pie/bin/ary
  233:     /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
  234: 
  235: The latter result is then what is expected.
  236: 
  237: ## Checking for partial RELRO
  238: 
  239: The following command should list a section called `RELRO`:
  240: 
  241:     $ objdump -p /path/to/bin/ary
  242: 
  243:     /path/to/bin/ary:     file format elf64-x86-64
  244: 
  245:     Program Header:
  246:     [...]
  247:        RELRO off    0x0000000000000d78 vaddr 0x0000000000600d78 paddr 0x0000000000600d78 align 2**0
  248: 
  249: This check is now performed automatically if `PKG_DEVELOPER` is set and `RELRO`
  250: is enabled.
  251: 
  252: ## Checking for full RELRO
  253: 
  254: The dynamic loader will apply RELRO immediately when detecting the presence of
  255: the `BIND_NOW` flag:
  256: 
  257:     $ objdump -x /path/to/bin/ary
  258: 
  259:     /path/to/bin/ary:     file format elf64-x86-64
  260: 
  261:     Dynamic Section:
  262:     [...]
  263:       BIND_NOW             0x0000000000000000
  264: 
  265: This has to be combined with partial RELRO (see above) to be fully efficient.
  266: 
  267: ## Checking for SSP
  268: 
  269: Building objects, binaries and libraries with SSP will affect the presence of
  270: additional symbols in the resulting file:
  271: 
  272:     $ nm /path/to/bin/ary
  273:     [...]
  274:                      U __stack_chk_fail
  275:     0000000000600ea0 B __stack_chk_guard
  276: 
  277: This is an indicator that the program was indeed built with support for SSP.
  278: 
  279: # References
  280: 
  281: * <http://tk-blog.blogspot.co.at/2009/02/relro-not-so-well-known-memory.html>
  282: 

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