35.1. fortify-headers-3.0.2
Standalone _FORTIFY_SOURCE implementation for musl, as overlay headers
| Version | 3.0.2, release 2 |
| Shelf | toolchain — The native compilers, the linker, the C library, and the LLVM family |
| Phase | 20_selfhost (Self-Hosting Bootstrap), 1 of 15 in build order |
| Licence | 0BSD |
| Homepage | https://github.com/jvoisin/fortify-headers |
| Build time | 0.1 SBU |
| Installed size | 34.8 KiB in 11 files |
| Recipe | ports/core/toolchain/fortify-headers/kpkgbuild |
About
fortify-headers is a _FORTIFY_SOURCE implementation that needs no help from the C library. glibc carries its own; musl has none. These headers sit in front of musl’s: each one, such as string.h, first includes musl’s header of the same name with #include_next, then wraps the functions it checks (memcpy, strcpy, read, snprintf and the rest) in inline versions that compare the length against the size of the destination the compiler can see. A call that would overrun such an object runs a trap instruction and the program dies with SIGILL instead of writing past the end.
KDOS turns it on for every port: from 20_selfhost on, script/env/chroot.env compiles with -D_FORTIFY_SOURCE=2 -isystem /usr/include/fortify. That is why this port is the first one 20_selfhost installs, ahead even of musl: a port compiled before the headers exist is compiled unchecked. What the checks cover, what they cost, and which builds escape them is in The release flags.
The port compiles nothing. build.sh applies the two patches and copies include/*.h and include/sys/*.h into /usr/include/fortify, the same files upstream’s make install would put under its own prefix.
Sources
fortify-headers-3.0.2.tar.gz(15.9 KiB)
sha256a4aab14c56eb00239cbd61ac65b3e778cf29ec82f06116ba343a50552882f587
upstream: https://github.com/jvoisin/fortify-headers/archive/refs/tags/3.0.2.tar.gz
archive copy: sources-4/toolchain--fortify-headers-3.0.2.tar.gz
Dependencies
Depends on: nothing a recipe names.
Required by: no port names it.
Patches
build.sh applies these; kpkg applies nothing by itself.
late-feature-macro.patch— adds a plain prototype in front of each wrapper whose function a feature-test macro gates:realpathinstdlib.h,mempcpy,strlcatandstrlcpyinstring.h,getdomainnameandswabinunistd.h. The wrapper then never defines a function that musl’s header, read under different feature macros, left undeclared, and a source that defines_GNU_SOURCEafter its first system header still compiles.lto-no-always-inline.patch— drops__always_inline__from the GCC definition of_FORTIFY_INLINE, the attribute set every wrapper is declared with, and keeps__gnu_inline__and__artificial__; the clang definitions are untouched. When a program takes the address of a fortified function and link-time optimisation later turns that into a direct call, GCC cannot inline analways_inlinebody there and stops the link. Without the attribute that one call goes to musl’s function unchecked, and every direct call in an optimised build is still inlined and checked. The case and the example, libstdc++’sstd::to_stringpassingvsnprintf, are in The release flags.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/20_selfhost/phase.env && kpkg install fortify-headers'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/toolchain/fortify-headers.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/toolchain/fortify-headers
echo 'a4aab14c56eb00239cbd61ac65b3e778cf29ec82f06116ba343a50552882f587 fortify-headers-3.0.2.tar.gz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/fortify-headers
SRC=$SRC_ROOT/fortify-headers-3.0.2
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf fortify-headers-3.0.2.tar.gz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
patch -p1 -i "$PORT_SRC/late-feature-macro.patch"
patch -p1 -i "$PORT_SRC/lto-no-always-inline.patch"
install -d "$PKG/usr/include/fortify/sys"
install -m644 include/*.h "$PKG/usr/include/fortify/"
install -m644 include/sys/*.h "$PKG/usr/include/fortify/sys/"
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/fortify-headers, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf fortify-headers-3.0.2-2.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/fortify-headers, and runs the triggers its paths call for.
Contents
Installed directories: /usr/include/fortify, /usr/share/licenses/fortify-headers.
Notes
The checks work only in optimised code, since they read object sizes the optimiser works out. A makefile that replaces CFLAGS with a line of its own drops -isystem /usr/include/fortify with it and builds unchecked, without an error.
The programs 10_bootstrap compiles are built before this port is installed and carry no fortification. Later phases replace most of them with their ports; kpkg, which no port builds, stays the copy 10_bootstrap compiled.