|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// TASK-068: portable secure-zero primitive. |
| 22 | +// |
| 23 | +// CWE-14 mitigation: the per-connection arena clearing path needs a write |
| 24 | +// that the optimizer is forbidden to elide as a dead store, since the |
| 25 | +// bytes being cleared are credentials (basic-auth username/password, |
| 26 | +// digest-auth digested_user) that linger in the arena across keep-alive |
| 27 | +// requests until the next allocation overwrites them. A plain |
| 28 | +// std::memset(p, 0, n) followed by no observable read is exactly the |
| 29 | +// shape that GCC / clang dead-store elimination targets at -O2 and above. |
| 30 | +// |
| 31 | +// This header centralizes a `secure_zero(void*, size_t)` helper that |
| 32 | +// dispatches to: |
| 33 | +// - explicit_bzero on glibc / musl / FreeBSD / OpenBSD / *BSD |
| 34 | +// (POSIX-ish lanes; declared in <string.h> when |
| 35 | +// the libc supports it) |
| 36 | +// - RtlSecureZeroMemory on Windows (the canonical kernel-grade |
| 37 | +// primitive; declared in <winnt.h> / <windows.h>) |
| 38 | +// - a portable fallback that walks the buffer through a `volatile` |
| 39 | +// unsigned-char pointer and follows the loop |
| 40 | +// with an inline-asm memory clobber. This is |
| 41 | +// the same pattern used by OpenSSL |
| 42 | +// OPENSSL_cleanse and by the Linux kernel's |
| 43 | +// barrier_data(); see also CERT C MSC06-C. |
| 44 | +// |
| 45 | +// Selection happens at compile time via HAVE_EXPLICIT_BZERO emitted by |
| 46 | +// configure.ac. The macOS path intentionally takes the portable |
| 47 | +// fallback: although Apple libc exposes memset_s when |
| 48 | +// __STDC_WANT_LIB_EXT1__ is defined before <string.h>, this header is |
| 49 | +// included transitively from many TUs whose include order we cannot |
| 50 | +// control, so the macro might be set after a sibling header already |
| 51 | +// pulled in <string.h>. The volatile + asm-memory-clobber fallback has |
| 52 | +// the same security guarantee with no preprocessor-order coupling, so |
| 53 | +// the trade-off is worth it. |
| 54 | + |
| 55 | +// Internal detail header. Strict gate: reachable only from libhttpserver |
| 56 | +// translation units, never from the public umbrella. |
| 57 | +#if !defined(HTTPSERVER_COMPILATION) |
| 58 | +#error "secure_zero.hpp is internal; only reachable when compiling libhttpserver." |
| 59 | +#endif |
| 60 | + |
| 61 | +#ifndef SRC_HTTPSERVER_DETAIL_SECURE_ZERO_HPP_ |
| 62 | +#define SRC_HTTPSERVER_DETAIL_SECURE_ZERO_HPP_ |
| 63 | + |
| 64 | +#include <cstddef> |
| 65 | + |
| 66 | +// HAVE_EXPLICIT_BZERO and HAVE_MEMSET_S are emitted as -D flags by |
| 67 | +// configure.ac (pushed into AM_CXXFLAGS, mirroring the HAVE_GNUTLS / |
| 68 | +// HAVE_BAUTH propagation pattern). This header therefore consults them |
| 69 | +// without an explicit config.h include. |
| 70 | + |
| 71 | +#if defined(_WIN32) |
| 72 | +// RtlSecureZeroMemory lives in <windows.h>. We forward-declare it instead |
| 73 | +// of pulling the full Win32 header here to keep this internal helper |
| 74 | +// self-contained: the broader codebase already includes <winsock2.h> in |
| 75 | +// the network path, and the two declarations are ODR-compatible (both |
| 76 | +// are `extern "C" void* __stdcall RtlSecureZeroMemory(void*, size_t)` |
| 77 | +// in <winnt.h>). |
| 78 | +extern "C" void* __stdcall RtlSecureZeroMemory(void*, std::size_t); |
| 79 | +#elif defined(HAVE_EXPLICIT_BZERO) |
| 80 | +#include <string.h> |
| 81 | +#endif |
| 82 | + |
| 83 | +namespace httpserver { |
| 84 | +namespace detail { |
| 85 | + |
| 86 | +// secure_zero(p, n): zero `n` bytes starting at `p` with a write the |
| 87 | +// optimizer is forbidden to eliminate as a dead store. |
| 88 | +// |
| 89 | +// Contract: |
| 90 | +// - secure_zero(nullptr, 0) is a no-op (precondition for callers that |
| 91 | +// hold a possibly-empty buffer). |
| 92 | +// - For n > 0, p must point at a writable region of at least n bytes. |
| 93 | +// - The function is noexcept. |
| 94 | +// |
| 95 | +// Trade-off: relative to plain std::memset, the fallback walks the |
| 96 | +// buffer byte-by-byte through a volatile pointer and emits a memory |
| 97 | +// clobber. For the ARENA_INITIAL_BYTES = 8192 case in |
| 98 | +// connection_state::reset_arena() this is a few thousand extra cycles |
| 99 | +// per keep-alive request. The CWE-226 mitigation is worth it because |
| 100 | +// the alternative is credential residue across requests. (Profiling |
| 101 | +// has not surfaced this as a hot-path concern for the bench workloads.) |
| 102 | +inline void secure_zero(void* p, std::size_t n) noexcept { |
| 103 | + if (p == nullptr || n == 0) { |
| 104 | + return; |
| 105 | + } |
| 106 | +#if defined(_WIN32) |
| 107 | + ::RtlSecureZeroMemory(p, n); |
| 108 | +#elif defined(HAVE_EXPLICIT_BZERO) |
| 109 | + ::explicit_bzero(p, n); |
| 110 | +#else |
| 111 | + // Portable fallback. The two ingredients: |
| 112 | + // (1) Walk through a volatile pointer so every store is a |
| 113 | + // visible side effect [intro.execution]. |
| 114 | + // (2) Follow the loop with an inline-asm memory clobber so the |
| 115 | + // compiler cannot prove the writes are dead even under LTO. |
| 116 | + volatile unsigned char* q = static_cast<volatile unsigned char*>(p); |
| 117 | + for (std::size_t i = 0; i < n; ++i) { |
| 118 | + q[i] = 0; |
| 119 | + } |
| 120 | +#if defined(__GNUC__) || defined(__clang__) |
| 121 | + __asm__ __volatile__("" : : "r"(p) : "memory"); |
| 122 | +#endif |
| 123 | +#endif |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace detail |
| 127 | +} // namespace httpserver |
| 128 | + |
| 129 | +#endif // SRC_HTTPSERVER_DETAIL_SECURE_ZERO_HPP_ |
0 commit comments