diff --git a/logsrvd/logsrvd.c b/logsrvd/logsrvd.c index 250a7ba4e..fe1a922cb 100644 --- a/logsrvd/logsrvd.c +++ b/logsrvd/logsrvd.c @@ -142,7 +142,7 @@ connection_closure_free(struct connection_closure *closure) } struct connection_buffer * -get_free_buf(struct connection_closure *closure) +get_free_buf(size_t len, struct connection_closure *closure) { struct connection_buffer *buf; debug_decl(get_free_buf, SUDO_DEBUG_UTIL); @@ -153,24 +153,29 @@ get_free_buf(struct connection_closure *closure) else buf = calloc(1, sizeof(*buf)); + if (len > buf->size) { + free(buf->data); + buf->size = sudo_pow2_roundup(len); + if ((buf->data = malloc(buf->size)) == NULL) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "unable to malloc %u", buf->size); + free(buf); + buf = NULL; + } + } + debug_return_ptr(buf); } bool fmt_server_message(struct connection_closure *closure, ServerMessage *msg) { - struct connection_buffer *buf; + struct connection_buffer *buf = NULL; uint32_t msg_len; bool ret = false; size_t len; debug_decl(fmt_server_message, SUDO_DEBUG_UTIL); - if ((buf = get_free_buf(closure)) == NULL) { - sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, - "unable to allocate connection_buffer"); - goto done; - } - len = server_message__get_packed_size(msg); if (len > MESSAGE_SIZE_MAX) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, @@ -182,20 +187,14 @@ fmt_server_message(struct connection_closure *closure, ServerMessage *msg) msg_len = htonl((uint32_t)len); len += sizeof(msg_len); - /* Resize buffer as needed. */ - if (len > buf->size) { - free(buf->data); - buf->size = sudo_pow2_roundup(len); - if ((buf->data = malloc(buf->size)) == NULL) { - sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, - "unable to malloc %u", buf->size); - buf->size = 0; - goto done; - } - } sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO, "size + server message %zu bytes", len); + if ((buf = get_free_buf(len, closure)) == NULL) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "unable to allocate connection_buffer"); + goto done; + } memcpy(buf->data, &msg_len, sizeof(msg_len)); server_message__pack(msg, buf->data + sizeof(msg_len)); buf->len = len; diff --git a/logsrvd/logsrvd.h b/logsrvd/logsrvd.h index d3048ad28..c80de00bb 100644 --- a/logsrvd/logsrvd.h +++ b/logsrvd/logsrvd.h @@ -159,7 +159,7 @@ void connection_closure_free(struct connection_closure *closure); bool schedule_commit_point(TimeSpec *commit_point, struct connection_closure *closure); bool fmt_log_id_message(const char *id, struct connection_closure *closure); bool fmt_error_message(const char *errstr, struct connection_closure *closure); -struct connection_buffer *get_free_buf(struct connection_closure *closure); +struct connection_buffer *get_free_buf(size_t, struct connection_closure *closure); /* logsrvd_conf.c */ bool logsrvd_conf_read(const char *path); diff --git a/logsrvd/logsrvd_relay.c b/logsrvd/logsrvd_relay.c index 26a0b09f6..5fdf7d91c 100644 --- a/logsrvd/logsrvd_relay.c +++ b/logsrvd/logsrvd_relay.c @@ -148,18 +148,12 @@ static bool fmt_client_message(struct connection_closure *closure, ClientMessage *msg) { struct relay_closure *relay_closure = closure->relay_closure; - struct connection_buffer *buf; + struct connection_buffer *buf = NULL; uint32_t msg_len; bool ret = false; size_t len; debug_decl(fmt_client_message, SUDO_DEBUG_UTIL); - if ((buf = get_free_buf(closure)) == NULL) { - sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, - "unable to allocate connection_buffer"); - goto done; - } - len = client_message__get_packed_size(msg); if (len > MESSAGE_SIZE_MAX) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, @@ -171,20 +165,14 @@ fmt_client_message(struct connection_closure *closure, ClientMessage *msg) msg_len = htonl((uint32_t)len); len += sizeof(msg_len); - /* Resize buffer as needed. */ - if (len > buf->size) { - free(buf->data); - buf->size = sudo_pow2_roundup(len); - if ((buf->data = malloc(buf->size)) == NULL) { - sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, - "unable to malloc %u", buf->size); - buf->size = 0; - goto done; - } - } sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO, "size + client message %zu bytes", len); + if ((buf = get_free_buf(len, closure)) == NULL) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "unable to allocate connection_buffer"); + goto done; + } memcpy(buf->data, &msg_len, sizeof(msg_len)); client_message__pack(msg, buf->data + sizeof(msg_len)); buf->len = len;