From 3088305166df1532778e88a96a293f88315f9f25 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 21:05:33 +0200 Subject: [PATCH 1/6] list: fix double avaluation in LIST_APPEND() --- src/basic/list.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/basic/list.h b/src/basic/list.h index 643e0bea88..040680c30a 100644 --- a/src/basic/list.h +++ b/src/basic/list.h @@ -38,9 +38,9 @@ /* Append an item to the list */ #define LIST_APPEND(name,head,item) \ do { \ - typeof(*(head)) *_tail; \ - LIST_FIND_TAIL(name,head,_tail); \ - LIST_INSERT_AFTER(name,head,_tail,item); \ + typeof(*(head)) **_hhead = &(head), *_tail; \ + LIST_FIND_TAIL(name, *_hhead, _tail); \ + LIST_INSERT_AFTER(name, *_hhead, _tail, item); \ } while (false) /* Remove an item from the list */ From 656552ebb354447531dc4a293c6a3da75757eb58 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 18:28:14 +0200 Subject: [PATCH 2/6] path-util: add new path_is_valid() helper --- src/basic/path-util.c | 18 +++++++++++++----- src/basic/path-util.h | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/basic/path-util.c b/src/basic/path-util.c index f60cf003e7..2deb176240 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -779,7 +779,18 @@ bool filename_is_valid(const char *p) { if (*e != 0) return false; - if (e - p > FILENAME_MAX) + if (e - p > FILENAME_MAX) /* FILENAME_MAX is counted *without* the trailing NUL byte */ + return false; + + return true; +} + +bool path_is_valid(const char *p) { + + if (isempty(p)) + return false; + + if (strlen(p) >= PATH_MAX) /* PATH_MAX is counted *with* the trailing NUL byte */ return false; return true; @@ -787,7 +798,7 @@ bool filename_is_valid(const char *p) { bool path_is_normalized(const char *p) { - if (isempty(p)) + if (!path_is_valid(p)) return false; if (dot_or_dot_dot(p)) @@ -796,9 +807,6 @@ bool path_is_normalized(const char *p) { if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) return false; - if (strlen(p)+1 > PATH_MAX) - return false; - if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) return false; diff --git a/src/basic/path-util.h b/src/basic/path-util.h index 49604eab80..17d31bbd89 100644 --- a/src/basic/path-util.h +++ b/src/basic/path-util.h @@ -134,6 +134,7 @@ char* dirname_malloc(const char *path); const char *last_path_component(const char *path); bool filename_is_valid(const char *p) _pure_; +bool path_is_valid(const char *p) _pure_; bool path_is_normalized(const char *p) _pure_; char *file_in_same_dir(const char *path, const char *filename); From 6e9c4f450e7c8ba954301ce9f563f667b980b63e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 18:28:43 +0200 Subject: [PATCH 3/6] core: validate socket path with path_is_valid() --- src/core/dbus-socket.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c index 913cc74918..da60cf110a 100644 --- a/src/core/dbus-socket.c +++ b/src/core/dbus-socket.c @@ -360,6 +360,9 @@ static int bus_socket_set_transient_property( return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown Socket type: %s", t); if (p->type != SOCKET_SOCKET) { + if (!path_is_valid(p->path)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid socket path: %s", t); + p->path = strdup(a); path_simplify(p->path, false); From 4ad25546e74d09e910180fada97172d8604d6c36 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 21:00:16 +0200 Subject: [PATCH 4/6] dbus: add missing OOM check --- src/core/dbus-socket.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c index da60cf110a..a1d1b0c6f8 100644 --- a/src/core/dbus-socket.c +++ b/src/core/dbus-socket.c @@ -364,6 +364,9 @@ static int bus_socket_set_transient_property( return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid socket path: %s", t); p->path = strdup(a); + if (!p->path) + return log_oom(); + path_simplify(p->path, false); } else if (streq(t, "Netlink")) { From 1fe84279f7a8bfd656cb371b12f42c3ff125ee5a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 21:06:09 +0200 Subject: [PATCH 5/6] core: shorten list appending a bit, by using better macros --- src/core/dbus-socket.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c index a1d1b0c6f8..dc87e22b29 100644 --- a/src/core/dbus-socket.c +++ b/src/core/dbus-socket.c @@ -395,13 +395,7 @@ static int bus_socket_set_transient_property( empty = false; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { - SocketPort *tail; - - LIST_FIND_TAIL(port, s->ports, tail); - LIST_INSERT_AFTER(port, s->ports, tail, p); - - p = NULL; - + LIST_APPEND(port, s->ports, TAKE_PTR(p)); unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Listen%s=%s", t, a); } } From 1f7817f8e9db100aa5ea599bf4f964ba9c7fc08b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 21:06:23 +0200 Subject: [PATCH 6/6] core: use structured initialization --- src/core/dbus-socket.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c index dc87e22b29..4ea5b6c6e5 100644 --- a/src/core/dbus-socket.c +++ b/src/core/dbus-socket.c @@ -351,10 +351,15 @@ static int bus_socket_set_transient_property( while ((r = sd_bus_message_read(message, "(ss)", &t, &a)) > 0) { _cleanup_free_ SocketPort *p = NULL; - p = new0(SocketPort, 1); + p = new(SocketPort, 1); if (!p) return log_oom(); + *p = (SocketPort) { + .fd = -1, + .socket = s, + }; + p->type = socket_port_type_from_string(t); if (p->type < 0) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown Socket type: %s", t); @@ -387,11 +392,6 @@ static int bus_socket_set_transient_property( return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Address family not supported: %s", a); } - p->fd = -1; - p->auxiliary_fds = NULL; - p->n_auxiliary_fds = 0; - p->socket = s; - empty = false; if (!UNIT_WRITE_FLAGS_NOOP(flags)) {