2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Allocate the data buffer in get_free_buf() too.

We always know the size of the data buffer we need at allocation time.
This commit is contained in:
Todd C. Miller 2021-04-18 17:10:53 -06:00
parent 3dce67ec10
commit 935daf6b7e
3 changed files with 25 additions and 38 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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;