File:  [NetBSD Developer Wiki] / wikisrc / pkgsrc / hardening.mdwn
Revision 1.29: download - view: text, annotated - select for diffs
Tue Nov 7 02:14:50 2017 UTC (5 years ago) by khorben
Branches: MAIN
CVS tags: HEAD
Add more details for PKGSRC_USE_SSP

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

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