From: Lennart Poettering Date: Mon, 11 Oct 2021 13:25:14 +0000 (+0200) Subject: util: define initializer for 'struct ucred' that properly invalidates all fields X-Git-Tag: v250-rc1~531^2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a995ce4768928843b4d37ecf46f32fe9e635e38a;p=thirdparty%2Fsystemd.git util: define initializer for 'struct ucred' that properly invalidates all fields i.e. let's make sure to invalid uid/gid to UID_INVAID + GID_INVALID instead of zero. --- diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 1e66f8700bc..94ae90929a1 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -551,7 +551,7 @@ int getpeername_pretty(int fd, bool include_port, char **ret) { return -errno; if (sa.sa.sa_family == AF_UNIX) { - struct ucred ucred = {}; + struct ucred ucred = UCRED_INVALID; /* UNIX connection sockets are anonymous, so let's use * PID/UID as pretty credentials instead */ diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index cb4a92236fa..c4fafa084b7 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -327,3 +327,6 @@ static inline int socket_set_recvfragsize(int fd, int af, bool b) { } int socket_get_mtu(int fd, int af, size_t *ret); + +/* an initializer for struct ucred that initialized all fields to the invalid value appropriate for each */ +#define UCRED_INVALID { .pid = 0, .uid = UID_INVALID, .gid = GID_INVALID } diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index 0a90091a863..cbff5036a42 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -36,6 +36,7 @@ #include "syslog-util.h" #include "tmpfile-util.h" #include "unit-name.h" +#include "user-util.h" #define STDOUT_STREAMS_MAX 4096 @@ -663,6 +664,7 @@ int stdout_stream_install(Server *s, int fd, StdoutStream **ret) { *stream = (StdoutStream) { .fd = -1, .priority = LOG_INFO, + .ucred = UCRED_INVALID, }; xsprintf(stream->id_field, "_STREAM_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id)); @@ -727,9 +729,9 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent } if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) { - struct ucred u; + struct ucred u = UCRED_INVALID; - r = getpeercred(fd, &u); + (void) getpeercred(fd, &u); /* By closing fd here we make sure that the client won't wait too long for journald to * gather all the data it adds to the error message to find out that the connection has @@ -737,7 +739,7 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent */ fd = safe_close(fd); - server_driver_message(s, r < 0 ? 0 : u.pid, NULL, LOG_MESSAGE("Too many stdout streams, refusing connection."), NULL); + server_driver_message(s, u.pid, NULL, LOG_MESSAGE("Too many stdout streams, refusing connection."), NULL); return 0; } diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 80f2bdd87f3..d8a7c817648 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -249,6 +249,7 @@ _public_ int sd_bus_new(sd_bus **ret) { .original_pid = getpid_cached(), .n_groups = SIZE_MAX, .close_on_exit = true, + .ucred = UCRED_INVALID, }; /* We guarantee that wqueue always has space for at least one entry */ diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index d127443c4c0..4a35e614257 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -136,7 +136,7 @@ _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) { } _public_ int sd_peer_get_session(int fd, char **session) { - struct ucred ucred = {}; + struct ucred ucred = UCRED_INVALID; int r; assert_return(fd >= 0, -EBADF); diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 07a1b96f601..984dea1ff56 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -258,8 +258,7 @@ static int varlink_new(Varlink **ret) { .state = _VARLINK_STATE_INVALID, - .ucred.uid = UID_INVALID, - .ucred.gid = GID_INVALID, + .ucred = UCRED_INVALID, .timestamp = USEC_INFINITY, .timeout = VARLINK_DEFAULT_TIMEOUT_USEC @@ -2106,8 +2105,8 @@ static int count_connection(VarlinkServer *server, struct ucred *ucred) { int varlink_server_add_connection(VarlinkServer *server, int fd, Varlink **ret) { _cleanup_(varlink_unrefp) Varlink *v = NULL; + struct ucred ucred = UCRED_INVALID; bool ucred_acquired; - struct ucred ucred; int r; assert_return(server, -EINVAL);