eBPF-based security platform
log: send a message to the journal once
Daniel Borkmann3fac85061fd3
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex d70957d6..e9da0698 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -127,6 +127,10 @@ signed config image.options from ``--list-opts``, a value by its placeholder, and acommand without subcommands that answers ``--list-cmds`` withnothing and logs nothing+ * - ``t1012-messages-once``+ - A message goes to standard error and to syslog, and to syslog+ alone where standard error is the journal stream+ ``JOURNAL_STREAM`` names``signing`` (2)~~~~~~~~~~~~~~~@@ -320,8 +324,9 @@ signed config image.``body_mismatch``; with the libtpms simulator built, the capturescript rebuilds a file that verifies* - ``t4101-initrd``- - The production configuration: ``init`` from the initrd, adoption,- PCR 11 after the binding, pins under ``/run``+ - The production configuration: ``init`` from the initrd, its+ messages in the journal once, adoption, PCR 11 after the binding,+ pins under ``/run``* - ``t4102-tpm-chain``- Replay, measured load and unload, evidence from the VM's TPM,challenges with ECC and RSA endorsement keys@@ -847,14 +852,15 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover 36 of the modules in+The unit suites in :src:`testing/unit/` cover 37 of the modules in:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,``eventlog``, ``format``, ``hex``, ``io``, ``java_agent``, ``json``,``kernel_abi``,``keepalive``, ``keepalive_quote``, ``kernel_build_id``,-``kernel_prereq``, ``kernel_release``, ``keys``, ``measured``, ``options``,+``kernel_prereq``, ``kernel_release``, ``keys``, ``log``, ``measured``,+``options``,``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and``overflow`` the arithmetic of@@ -1060,9 +1066,9 @@ Coverage by area``t6120``, ``t6121``, ``t6124``, ``t6126``, ``t6135``,``t6137``* - CLI- - ``t1001``, ``t1002``, ``t1011``, ``t2001``, ``t3005``, ``t3106``,- ``t3108``, ``t5004``, ``t5103``, ``t6007``, ``t6114``,- ``t7001``, ``t8001``+ - ``t1001``, ``t1002``, ``t1011``, ``t1012``, ``t2001``, ``t3005``,+ ``t3106``, ``t3108``, ``t5004``, ``t5103``, ``t6007``, ``t6114``,+ ``t7001``, ``t8001``. Unit: ``log``* - Build and packaging- ``t0005``, ``t0007``, ``t1003``, ``t1004``, ``t1005``,``t7004``, ``t8003``diff --git a/Documentation/reference/cli/shirudo.rst b/Documentation/reference/cli/shirudo.rstindex 6cdbda67..179ad3c9 100644--- a/Documentation/reference/cli/shirudo.rst+++ b/Documentation/reference/cli/shirudo.rst@@ -98,7 +98,10 @@ Every invocation goes through the same steps before the command runs:Progress, warning and error messages go to standard error and to syslogwith the identifier ``shirudo`` and the facility ``LOG_AUTHPRIV``, where-journald picks them up. Results go to standard output. Help text goes to+journald picks them up. Where standard error is the journal's own+stream, as under the systemd units, they go to syslog alone, so that the+journal holds each message once; systemd names that stream in+``JOURNAL_STREAM``. Results go to standard output. Help text goes tostandard error, except the help of the generator scripts, which goes tostandard output.diff --git a/Documentation/reference/environment.rst b/Documentation/reference/environment.rstindex 4ddb05ea..67498ab4 100644--- a/Documentation/reference/environment.rst+++ b/Documentation/reference/environment.rst@@ -88,6 +88,12 @@ shirudo and shirudo-build``--check-only`` and ``--json``. The runtime drop-in clears``ExecStart=`` and ``EnvironmentFile=``, so the variable has noeffect with it.+ * - ``JOURNAL_STREAM``+ - ``shirudo`` and ``shirudo-build``, for their messages+ - Set by systemd to the device and inode of the journal stream a+ service's output is connected to. When it names the file that is+ standard error, and ``/dev/log`` exists, messages go to syslog+ alone, so that the journal holds each once. Honored on a target.* - ``TMPDIR``- ``shirudo-build bpm build`` (staging directory), the generatorscripts (``mktemp -d``)diff --git a/Documentation/reference/systemd-units.rst b/Documentation/reference/systemd-units.rstindex fe35690e..e7b21588 100644--- a/Documentation/reference/systemd-units.rst+++ b/Documentation/reference/systemd-units.rst@@ -11,8 +11,10 @@ systemd units and timersThe units the target package installs, from ``systemd/*.in`` and``systemd/*.timer``. Every service is ``Type=oneshot``: shirudo runs nodaemon. Every service sets ``SyslogIdentifier=shirudo``, so what it writes-to standard error reaches the journal under the same identifier as what-it sends to syslog, whatever the name of the process that runs the binary.+to standard output and standard error reaches the journal under the same+identifier as the messages it sends to syslog, whatever the name of the+process that runs the binary. A message is in the journal once: under a+unit shirudo sends it to syslog and not to standard error as well.Units-----diff --git a/testing/functional/build/t1012-messages-once.sh b/testing/functional/build/t1012-messages-once.shnew file mode 100755index 00000000..7ad975a5--- /dev/null+++ b/testing/functional/build/t1012-messages-once.sh@@ -0,0 +1,52 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1012 -- a message goes to standard error and to syslog, and to syslog+# alone where standard error is the journal stream systemd names in+# JOURNAL_STREAM, so that the journal of a unit holds it once.++cd "$(dirname "$0")/../.."+. ./test-lib.sh++SHIRUDO=../shirudo+W=$(mktemp -d) || exit 1+trap 'rm -rf "$W"' EXIT+MESSAGE="upatch info: $W/none.upatch: No such file or directory"++# The lines of standard error that are the message, with JOURNAL_STREAM+# set to $1 when it is not empty. The command fails, as it must.+copies() {+ : >"$W/err"+ if [ -n "$1" ]; then+ JOURNAL_STREAM=$1 $SHIRUDO upatch info "$W/none.upatch" \+ 2>>"$W/err" || true+ else+ $SHIRUDO upatch info "$W/none.upatch" 2>>"$W/err" || true+ fi+ grep -cFx "$MESSAGE" "$W/err" || true+}++test_expect_success 'a message goes to standard error' '+ [ "$(copies "")" = 1 ]+'++# Without /dev/log syslog drops the message, and the copy stays.+if [ -e /dev/log ]; then+ test_expect_success 'a standard error that is the journal stream gets no copy' '+ : >"$W/err" &&+ [ "$(copies "$(stat -c %d:%i "$W/err")")" = 0 ]+ '+else+ test_expect_success 'without /dev/log the journal stream keeps its copy' '+ : >"$W/err" &&+ [ "$(copies "$(stat -c %d:%i "$W/err")")" = 1 ]+ '+fi++test_expect_success 'another stream in JOURNAL_STREAM leaves standard error its copy' '+ [ "$(copies 1:2)" = 1 ] &&+ [ "$(copies garbage)" = 1 ]+'++test_donediff --git a/testing/functional/tpm/t4101-initrd.sh b/testing/functional/tpm/t4101-initrd.shindex bdfe47cc..f6269f78 100755--- a/testing/functional/tpm/t4101-initrd.sh+++ b/testing/functional/tpm/t4101-initrd.sh@@ -44,6 +44,12 @@ test_expect_success 'the initrd unit ran and completed init' 'grep -q "init: complete"'+test_expect_success 'the journal holds a message of the initrd run once' '+ n=$(journalctl -b -u shirudo-initrd.service --no-pager 2>/dev/null |+ grep -c "init: complete") &&+ [ "$n" = 1 ]+'+test_expect_success 'the root adopted the initrd record into the eventlog' 'test ! -e /run/shirudo/eventlog.pending &&grep -q "\"bound_in\":\"initrd\"" /var/lib/shirudo/eventlogdiff --git a/testing/unit/log.c b/testing/unit/log.cnew file mode 100644index 00000000..fd819b00--- /dev/null+++ b/testing/unit/log.c@@ -0,0 +1,47 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <stddef.h>++#include "log.h"+#include "unit.h"++static void stream_names_its_file(void)+{+ EXPECT_TRUE(log_stream_is("8:1234", 8, 1234));+ EXPECT_TRUE(log_stream_is("0:0", 0, 0));+ EXPECT_TRUE(log_stream_is("18446744073709551615:1",+ 18446744073709551615ULL, 1));+}++static void stream_of_another_file(void)+{+ EXPECT_FALSE(log_stream_is("8:1234", 8, 1235));+ EXPECT_FALSE(log_stream_is("8:1234", 9, 1234));+ EXPECT_FALSE(log_stream_is("1234:8", 8, 1234));+}++static void stream_that_is_no_pair_of_numbers(void)+{+ EXPECT_FALSE(log_stream_is(NULL, 0, 0));+ EXPECT_FALSE(log_stream_is("", 0, 0));+ EXPECT_FALSE(log_stream_is(":", 0, 0));+ EXPECT_FALSE(log_stream_is("8", 8, 0));+ EXPECT_FALSE(log_stream_is("8:", 8, 0));+ EXPECT_FALSE(log_stream_is(":1234", 0, 1234));+ EXPECT_FALSE(log_stream_is("8:1234 ", 8, 1234));+ EXPECT_FALSE(log_stream_is(" 8:1234", 8, 1234));+ EXPECT_FALSE(log_stream_is("8:1234:5", 8, 1234));+ EXPECT_FALSE(log_stream_is("0x8:1234", 8, 1234));+ EXPECT_FALSE(log_stream_is("-8:1234", 8, 1234));+ EXPECT_FALSE(log_stream_is("18446744073709551616:1", 0, 1));+}++static const struct unit_case cases[] = {+ UNIT_CASE(stream_names_its_file),+ UNIT_CASE(stream_of_another_file),+ UNIT_CASE(stream_that_is_no_pair_of_numbers),+ {}+};++UNIT_SUITE(log, cases);diff --git a/util/log.c b/util/log.cindex 3fdd2ee0..d4720a0c 100644--- a/util/log.c+++ b/util/log.c@@ -7,8 +7,13 @@#include <errno.h>#include <stdarg.h>+#include <stdbool.h>#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>#include <syslog.h>+#include <unistd.h>#include <shirudo/misc.h>#include <shirudo/string.h>@@ -17,9 +22,42 @@#define LOG_MSG_MAX 8192+static bool to_stderr = true;++bool log_stream_is(const char *stream, __u64 dev, __u64 ino)+{+ const char *colon;+ __u64 d, i;++ if (!stream)+ return false;+ colon = strchr(stream, ':');+ if (!colon)+ return false;+ return !parse_u64(stream, colon - stream, ~0ULL, &d) &&+ !parse_u64(colon + 1, strlen(colon + 1), ~0ULL, &i) &&+ d == dev && i == ino;+}++/*+ * systemd connects a service's standard error to the journal and names+ * that stream in JOURNAL_STREAM. A message sent to syslog and written+ * there too is in the journal twice. Without /dev/log syslog(3) drops+ * the message, so the copy on standard error stays then.+ */+static bool stderr_repeats_syslog(void)+{+ struct stat st;++ return !fstat(STDERR_FILENO, &st) &&+ log_stream_is(getenv("JOURNAL_STREAM"), st.st_dev, st.st_ino) &&+ !access("/dev/log", F_OK);+}+void log_init(void){openlog("shirudo", LOG_PID | LOG_NDELAY, LOG_AUTHPRIV);+ to_stderr = !stderr_repeats_syslog();}/*@@ -64,7 +102,8 @@ static __printf(2, 0) void emit(int priority, const char *fmt, va_list ap)strscpy(msg, fmt, sizeof(msg));escape(out, sizeof(out), msg);syslog(priority, "%s", out);- fprintf(stderr, "%s\n", out);+ if (to_stderr)+ fprintf(stderr, "%s\n", out);errno = saved_errno;}diff --git a/util/log.h b/util/log.hindex 8824a449..8778ae8c 100644--- a/util/log.h+++ b/util/log.h@@ -9,11 +9,24 @@#define SHIRUDO_LOG_H#include <stdarg.h>+#include <stdbool.h>#include <shirudo/compiler.h>+#include <shirudo/types.h>+/*+ * Opens syslog. From then on a message goes to standard error as well,+ * unless that is the journal stream of a systemd service, where the+ * syslog record is the one copy.+ */void log_init(void);+/*+ * Whether stream, the value of systemd's JOURNAL_STREAM, names the file+ * of device dev and inode ino: "<device>:<inode>" in decimal.+ */+bool log_stream_is(const char *stream, __u64 dev, __u64 ino);+void pr_err(const char *fmt, ...)__printf(1, 2);void pr_warn(const char *fmt, ...)