From: Daan De Meyer Date: Fri, 15 Jul 2022 00:26:52 +0000 (+0200) Subject: mkosi: Changes to allow booting with sanitizers in mkosi X-Git-Tag: v252-rc1~618^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=69d638e67e5bfc5fedcae4072f144a4f7d798c9a;p=thirdparty%2Fsystemd.git mkosi: Changes to allow booting with sanitizers in mkosi - Extra memory because ASAN needs it - The environment variables to make the sanitizers more useful - LD_PRELOAD because the ASAN DSO needs to be the first in the list - The sanitizer library packages - Disable syscall filters because they interfere with ASAN - Disable systemd-hwdb-update because it's super slow when systemd-hwdb is built with sanitizers - Take the value for meson's b_sanitize option from the SANITIZERS environment variable --- diff --git a/docs/HACKING.md b/docs/HACKING.md index 9e5313e07a2..a74d0468e5e 100644 --- a/docs/HACKING.md +++ b/docs/HACKING.md @@ -140,6 +140,11 @@ enabled that are suitable when hacking on systemd (such as internal documentation consistency checks). Those are not useful when compiling for distribution and can be disabled by setting `-Dmode=release`. +## Sanitizers in mkosi + +See [Testing systemd using sanitizers](TESTING_WITH_SANITIZERS.md) for more information +on how to build with sanitizers enabled in mkosi. + ## Fuzzers systemd includes fuzzers in `src/fuzz/` that use libFuzzer and are automatically diff --git a/docs/TESTING_WITH_SANITIZERS.md b/docs/TESTING_WITH_SANITIZERS.md index 4f965c96178..642e1f19c97 100644 --- a/docs/TESTING_WITH_SANITIZERS.md +++ b/docs/TESTING_WITH_SANITIZERS.md @@ -13,6 +13,22 @@ This is mostly done automagically by various CI systems for each PR, but you may want to do it locally as well. The process slightly varies depending on the compiler you want to use and which part of the test suite you want to run. +## mkosi + +To build with sanitizers in mkosi, create a file 20-local.conf in mkosi.default.d/ and add the following +contents: + +``` +[Content] +Environment=SANITIZERS=address,undefined +``` + +The value of `SANITIZERS` is passed directly to meson's `b_sanitize` option, See +https://mesonbuild.com/Builtin-options.html#base-options for the format expected by the option. Currently, +only the sanitizers supported by gcc can be used, which are `address` and `undefined`. + +Note that this will only work with a recent version of mkosi (>= 14 or by running mkosi directly from source). + ## gcc gcc compiles in sanitizer libraries dynamically by default, so you need to get the shared libraries first - on Fedora these are shipped as a separate packages diff --git a/mkosi.build b/mkosi.build index 2be8fdbda18..27e5b1c65c5 100755 --- a/mkosi.build +++ b/mkosi.build @@ -5,6 +5,9 @@ set -e # This is a build script for OS image generation using mkosi (https://github.com/systemd/mkosi). # Simply invoke "mkosi" in the project directory to build an OS image. +ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:disable_coredump=0:use_madv_dontdump=1 +UBSAN_OPTIONS=print_stacktrace=1:print_summary=1:halt_on_error=1 + # On Fedora "ld" is (unfortunately — if you ask me) managed via # "alternatives". Since we'd like to support building images in environments # with only /usr/ around (e.g. mkosi's UsrOnly=1 option), we have the problem @@ -61,7 +64,8 @@ if [ ! -f "$BUILDDIR"/build.ninja ] ; then -D man=false \ -D translations=false \ -D version-tag="${VERSION_TAG}" \ - -D mode=developer + -D mode=developer \ + -D b_sanitize="${SANITIZERS:-none}" fi cd "$BUILDDIR" @@ -71,7 +75,15 @@ if [ "$WITH_TESTS" = 1 ] ; then getent group $id >/dev/null || groupadd -g $id testgroup$id done - ninja test + if [ -n "$SANITIZERS" ]; then + export ASAN_OPTIONS="$ASAN_OPTIONS" + export UBSAN_OPTIONS="$UBSAN_OPTIONS" + TIMEOUT_MULTIPLIER=3 + else + TIMEOUT_MULTIPLIER=1 + fi + + meson test --timeout-multiplier=$TIMEOUT_MULTIPLIER fi cd "$SRCDIR" @@ -120,3 +132,42 @@ if [ -n "$CI_BUILD" ]; then cp -v "$SRCDIR/test/mkosi-check-and-shutdown.sh" "$DESTDIR/usr/lib/systemd/mkosi-check-and-shutdown.sh" chmod +x "$DESTDIR/usr/lib/systemd/mkosi-check-and-shutdown.sh" fi + +if [ -n "$SANITIZERS" ]; then + LD_PRELOAD=$(ldd $BUILDDIR/systemd | grep libasan.so | awk '{print $3}') + + mkdir -p "$DESTDIR/etc/systemd/system.conf.d" + + cat > "$DESTDIR/etc/systemd/system.conf.d/10-asan.conf" < "$DESTDIR/etc/systemd/system/systemd-journald.service.d/10-stdout-tty.conf" < "$DESTDIR/etc/systemd/system/console-getty.service.d/10-no-vhangup.conf" < /dev/null && [ -d "/efi" ]; then - bootctl install +if [ "$1" = "final" ]; then + if command -v bootctl > /dev/null && [ -d "/efi" ]; then + bootctl install + fi + + if [ -n "$SANITIZERS" ]; then + # ASAN and syscall filters aren't compatible with each other. + find / -name '*.service' -type f -exec sed -i 's/^\(MemoryDeny\|SystemCall\)/# \1/' {} + + + # `systemd-hwdb update` takes > 50s when built with sanitizers so let's not run it by default. + systemctl mask systemd-hwdb-update.service + fi fi # Temporary workaround until https://github.com/openSUSE/suse-module-tools/commit/158643414ddb8d8208016a5f03a4484d58944d7a