From 868258cf38fb081c7766989fb92a91d2b167147e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 12 Sep 2024 17:39:33 +0200 Subject: [PATCH 1/4] basic: split ifname related calls from format-util.h into format-ifname.h This way we don't have to pull in net/if.h into format-util.h. This is supposed to address https://github.com/systemd/systemd/pull/32212#discussion_r1755639881 No actual code changes, just a .c/.h file split-up. --- src/basic/format-ifname.c | 37 +++++++++++++++++++++++++ src/basic/format-ifname.h | 27 ++++++++++++++++++ src/basic/format-util.c | 32 --------------------- src/basic/format-util.h | 24 ---------------- src/basic/meson.build | 1 + src/basic/socket-util.c | 2 +- src/libsystemd-network/network-common.c | 2 +- src/machine/machinectl.c | 1 + src/network/networkctl-dump-util.c | 2 +- src/network/networkctl-misc.c | 2 +- src/network/networkd-link.c | 2 +- src/network/wait-online/link.c | 2 +- src/nsresourced/nsresourcework.c | 1 + src/resolve/resolvectl.c | 2 +- src/resolve/resolved-dns-cache.c | 2 +- src/shared/format-table.c | 1 + src/test/test-nss-hosts.c | 2 +- 17 files changed, 77 insertions(+), 65 deletions(-) create mode 100644 src/basic/format-ifname.c create mode 100644 src/basic/format-ifname.h diff --git a/src/basic/format-ifname.c b/src/basic/format-ifname.c new file mode 100644 index 0000000000..ce4933c57d --- /dev/null +++ b/src/basic/format-ifname.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "format-ifname.h" +#include "string-util.h" + +assert_cc(STRLEN("%") + DECIMAL_STR_MAX(int) <= IF_NAMESIZE); + +int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { + if (ifindex <= 0) + return -EINVAL; + + if (if_indextoname(ifindex, buf)) + return 0; + + if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX)) + return -errno; + + if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) + assert_se(snprintf_ok(buf, IF_NAMESIZE, "%%%d", ifindex)); + else + assert_se(snprintf_ok(buf, IF_NAMESIZE, "%d", ifindex)); + + return 0; +} + +int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) { + char buf[IF_NAMESIZE]; + int r; + + assert(ret); + + r = format_ifname_full(ifindex, flag, buf); + if (r < 0) + return r; + + return strdup_to(ret, buf); +} diff --git a/src/basic/format-ifname.h b/src/basic/format-ifname.h new file mode 100644 index 0000000000..84415b1388 --- /dev/null +++ b/src/basic/format-ifname.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +typedef enum { + FORMAT_IFNAME_IFINDEX = 1 << 0, + FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX, +} FormatIfnameFlag; + +int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]); +int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret); + +static inline int format_ifname(int ifindex, char buf[static IF_NAMESIZE]) { + return format_ifname_full(ifindex, 0, buf); +} +static inline int format_ifname_alloc(int ifindex, char **ret) { + return format_ifname_full_alloc(ifindex, 0, ret); +} + +static inline char* _format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { + (void) format_ifname_full(ifindex, flag, buf); + return buf; +} + +#define FORMAT_IFNAME_FULL(index, flag) _format_ifname_full(index, flag, (char[IF_NAMESIZE]){}) +#define FORMAT_IFNAME(index) _format_ifname_full(index, 0, (char[IF_NAMESIZE]){}) diff --git a/src/basic/format-util.c b/src/basic/format-util.c index 7098bf6a99..09cb716e7d 100644 --- a/src/basic/format-util.c +++ b/src/basic/format-util.c @@ -5,38 +5,6 @@ #include "stdio-util.h" #include "strxcpyx.h" -assert_cc(STRLEN("%") + DECIMAL_STR_MAX(int) <= IF_NAMESIZE); -int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { - if (ifindex <= 0) - return -EINVAL; - - if (if_indextoname(ifindex, buf)) - return 0; - - if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX)) - return -errno; - - if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) - assert(snprintf_ok(buf, IF_NAMESIZE, "%%%d", ifindex)); - else - assert(snprintf_ok(buf, IF_NAMESIZE, "%d", ifindex)); - - return 0; -} - -int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) { - char buf[IF_NAMESIZE]; - int r; - - assert(ret); - - r = format_ifname_full(ifindex, flag, buf); - if (r < 0) - return r; - - return strdup_to(ret, buf); -} - char* format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { typedef struct { const char *suffix; diff --git a/src/basic/format-util.h b/src/basic/format-util.h index 4a030b7557..b528c005ca 100644 --- a/src/basic/format-util.h +++ b/src/basic/format-util.h @@ -2,7 +2,6 @@ #pragma once #include -#include #include #include "cgroup-util.h" @@ -66,29 +65,6 @@ assert_cc(sizeof(gid_t) == sizeof(uint32_t)); # error Unknown ino_t size #endif -typedef enum { - FORMAT_IFNAME_IFINDEX = 1 << 0, - FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX, -} FormatIfnameFlag; - -int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]); -int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret); - -static inline int format_ifname(int ifindex, char buf[static IF_NAMESIZE]) { - return format_ifname_full(ifindex, 0, buf); -} -static inline int format_ifname_alloc(int ifindex, char **ret) { - return format_ifname_full_alloc(ifindex, 0, ret); -} - -static inline char* _format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { - (void) format_ifname_full(ifindex, flag, buf); - return buf; -} - -#define FORMAT_IFNAME_FULL(index, flag) _format_ifname_full(index, flag, (char[IF_NAMESIZE]){}) -#define FORMAT_IFNAME(index) _format_ifname_full(index, 0, (char[IF_NAMESIZE]){}) - typedef enum { FORMAT_BYTES_USE_IEC = 1 << 0, FORMAT_BYTES_BELOW_POINT = 1 << 1, diff --git a/src/basic/meson.build b/src/basic/meson.build index 5b62fe90bc..4f83eb67a0 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -33,6 +33,7 @@ basic_sources = files( 'fd-util.c', 'fileio.c', 'filesystems.c', + 'format-ifname.c', 'format-util.c', 'fs-util.c', 'gcrypt-util.c', diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index e4fb5a280c..94089323b4 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -21,7 +21,7 @@ #include "escape.h" #include "fd-util.h" #include "fileio.h" -#include "format-util.h" +#include "format-ifname.h" #include "io-util.h" #include "log.h" #include "memory-util.h" diff --git a/src/libsystemd-network/network-common.c b/src/libsystemd-network/network-common.c index b639e9ca5a..8a0abe8fe8 100644 --- a/src/libsystemd-network/network-common.c +++ b/src/libsystemd-network/network-common.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "env-util.h" -#include "format-util.h" +#include "format-ifname.h" #include "network-common.h" #include "socket-util.h" #include "unaligned.h" diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 934cfb2774..92d8826f3a 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -32,6 +32,7 @@ #include "edit-util.h" #include "env-util.h" #include "fd-util.h" +#include "format-ifname.h" #include "format-table.h" #include "hostname-util.h" #include "import-util.h" diff --git a/src/network/networkctl-dump-util.c b/src/network/networkctl-dump-util.c index d8022eb3f0..1e497a5747 100644 --- a/src/network/networkctl-dump-util.c +++ b/src/network/networkctl-dump-util.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include "format-util.h" +#include "format-ifname.h" #include "in-addr-util.h" #include "local-addresses.h" #include "networkctl-dump-util.h" diff --git a/src/network/networkctl-misc.c b/src/network/networkctl-misc.c index 7a14bffc5b..8e1fbd8843 100644 --- a/src/network/networkctl-misc.c +++ b/src/network/networkctl-misc.c @@ -5,7 +5,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "fd-util.h" -#include "format-util.h" +#include "format-ifname.h" #include "netlink-util.h" #include "networkctl.h" #include "networkctl-misc.h" diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 303007a9de..b464ea8405 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -24,7 +24,7 @@ #include "event-util.h" #include "fd-util.h" #include "fileio.h" -#include "format-util.h" +#include "format-ifname.h" #include "fs-util.h" #include "glyph-util.h" #include "logarithm.h" diff --git a/src/network/wait-online/link.c b/src/network/wait-online/link.c index a8ab7f53de..3097611d98 100644 --- a/src/network/wait-online/link.c +++ b/src/network/wait-online/link.c @@ -3,7 +3,7 @@ #include "sd-network.h" #include "alloc-util.h" -#include "format-util.h" +#include "format-ifname.h" #include "hashmap.h" #include "link.h" #include "manager.h" diff --git a/src/nsresourced/nsresourcework.c b/src/nsresourced/nsresourcework.c index ac088e6c7f..892eaf3b57 100644 --- a/src/nsresourced/nsresourcework.c +++ b/src/nsresourced/nsresourcework.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 81b5a193f7..1fa62a4df7 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -20,8 +20,8 @@ #include "dns-domain.h" #include "errno-list.h" #include "escape.h" +#include "format-ifname.h" #include "format-table.h" -#include "format-util.h" #include "gcrypt-util.h" #include "hostname-util.h" #include "json-util.h" diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index f80f9dff8a..1310bd5f8b 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -5,7 +5,7 @@ #include "af-list.h" #include "alloc-util.h" #include "dns-domain.h" -#include "format-util.h" +#include "format-ifname.h" #include "resolved-dns-answer.h" #include "resolved-dns-cache.h" #include "resolved-dns-packet.h" diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 4cf4b67f6d..f5ef7175a3 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -10,6 +10,7 @@ #include "devnum-util.h" #include "fd-util.h" #include "fileio.h" +#include "format-ifname.h" #include "format-table.h" #include "format-util.h" #include "fs-util.h" diff --git a/src/test/test-nss-hosts.c b/src/test/test-nss-hosts.c index 2f1810d93b..6744d7068a 100644 --- a/src/test/test-nss-hosts.c +++ b/src/test/test-nss-hosts.c @@ -9,7 +9,7 @@ #include "dlfcn-util.h" #include "env-util.h" #include "errno-list.h" -#include "format-util.h" +#include "format-ifname.h" #include "hexdecoct.h" #include "hostname-util.h" #include "in-addr-util.h" From a44fa55e267aa7b075d096b0333714af7c11a485 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 12 Sep 2024 17:46:12 +0200 Subject: [PATCH 2/4] networkd: move sysctl code to use PID_FMT Now that format-util.h doesn't pull in net/if.h anymore, we can use it to format PIDs in the networkd-sysctl.c code. --- src/network/networkd-sysctl.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/network/networkd-sysctl.c b/src/network/networkd-sysctl.c index d61c208f84..4c83a6c9a9 100644 --- a/src/network/networkd-sysctl.c +++ b/src/network/networkd-sysctl.c @@ -9,6 +9,7 @@ #include "af-list.h" #include "cgroup-util.h" #include "fd-util.h" +#include "format-util.h" #include "missing_network.h" #include "networkd-link.h" #include "networkd-lldp-tx.h" @@ -72,14 +73,14 @@ static int sysctl_event_handler(void *ctx, void *data, size_t data_sz) { if (!strneq(value, we->newvalue, sizeof(we->newvalue))) log_struct(LOG_WARNING, "MESSAGE_ID=" SD_MESSAGE_SYSCTL_CHANGED_STR, - "OBJECT_PID=%d", we->pid, + "OBJECT_PID=" PID_FMT, we->pid, "OBJECT_COMM=%s", we->comm, - "SYSCTL=/proc/sys/%s", we->path, + "SYSCTL=%s", path, "OLDVALUE=%s", we->current, "NEWVALUE=%s", we->newvalue, "OURVALUE=%s", value, - LOG_MESSAGE("Foreign process '%s[%d]' changed sysctl '/proc/sys/%s' from '%s' to '%s', conflicting with our setting to '%s'", - we->comm, we->pid, we->path, we->current, we->newvalue, value)); + LOG_MESSAGE("Foreign process '%s[" PID_FMT "]' changed sysctl '%s' from '%s' to '%s', conflicting with our setting to '%s'.", + we->comm, we->pid, path, we->current, we->newvalue, value)); return 0; } From 9d63491f25e4ec525f701c2817b58ee1fe09aa5b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 12 Sep 2024 17:51:24 +0200 Subject: [PATCH 3/4] catalog: rebreak catalog entry --- catalog/systemd.catalog.in | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/catalog/systemd.catalog.in b/catalog/systemd.catalog.in index 0a12b7c3f7..acc6a22094 100644 --- a/catalog/systemd.catalog.in +++ b/catalog/systemd.catalog.in @@ -788,12 +788,13 @@ Defined-By: systemd Support: %SUPPORT_URL% Documentation: man:systemd-tpm2-setup.service(8) -An authorization failure occurred while attempting to enroll a Storage Root Key (SRK) on the Trusted Platform -Module (TPM). Most likely this means that a PIN/Password (authValue) has been set on the Owner hierarchy of -the TPM. +An authorization failure occurred while attempting to enroll a Storage Root Key +(SRK) on the Trusted Platform Module (TPM). Most likely this means that a +PIN/Password (authValue) has been set on the Owner hierarchy of the TPM. -Automatic SRK enrollment on TPMs in such scenarios is not supported. In order to unset the PIN/password -protection on the owner hierarchy issue a command like the following: 'tpm2_changeauth -c o -p ""'. +Automatic SRK enrollment on TPMs in such scenarios is not supported. In order +to unset the PIN/password protection on the owner hierarchy issue a command +like the following: 'tpm2_changeauth -c o -p ""'. -- 9cf56b8baf9546cf9478783a8de42113 Subject: A foreign process changed a sysctl we manage From e1f9d3c84b66d1b86adaae4c7aebf89514685d51 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 12 Sep 2024 17:51:28 +0200 Subject: [PATCH 4/4] catalog: beef up new sysctl message Let's make use of the templating logic, to make the entry more useful. --- catalog/systemd.catalog.in | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/catalog/systemd.catalog.in b/catalog/systemd.catalog.in index acc6a22094..eab4afd0cd 100644 --- a/catalog/systemd.catalog.in +++ b/catalog/systemd.catalog.in @@ -797,10 +797,13 @@ to unset the PIN/password protection on the owner hierarchy issue a command like the following: 'tpm2_changeauth -c o -p ""'. -- 9cf56b8baf9546cf9478783a8de42113 -Subject: A foreign process changed a sysctl we manage +Subject: A foreign process changed a sysctl systemd-networkd manages Defined-By: systemd Support: %SUPPORT_URL% -A sysctl handle under /proc/sys/net, which is managed by systemd-networkd, has been changed by another process. -The event is raised only if the written value differs from the current one. -The program name, the written value, the previous value, and the value initially set by networkd have been logged. +The sysctl configuration setting @SYSCTL@, which is managed by +systemd-networkd, has been changed by another, unrelated process +("@OBJECT_COMM@", PID @OBJECT_PID@). This represents a conflict of ownership +and will likely result in problems later on. + +Value changed to "@NEWVALUE@", which should be "@OURVALUE@".