Skip to content

Commit d0c1927

Browse files
committed
expand_buf: Allocate "newsize" bytes not "needed" bytes
Sync the implementation of expand_buf() with the version in logsrv_util.c. We compute "newsize" by rounding up the number of needed bytes to the nearest power of 2, but the malloc() call used the unrounded size. This is only used for messages coming from sudo_logsrvd, which are not going to be larger than the initial buffer, so there is no possibility of an overflow. Thanks to Joshua Rogers for finding this.
1 parent 7fe2dd2 commit d0c1927

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

plugins/sudoers/log_client.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2019-2022 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2019-2025 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above
@@ -1671,21 +1671,23 @@ expand_buf(struct connection_buffer *buf, size_t needed)
16711671
if (buf->size < needed) {
16721672
/* Expand buffer. */
16731673
const size_t newsize = sudo_pow2_roundup(needed);
1674+
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
1675+
"expanding buffer from %zu to %zu", buf->size, newsize);
16741676
if (newsize < needed) {
16751677
/* overflow */
16761678
errno = ENOMEM;
16771679
goto oom;
16781680
}
1679-
if ((newdata = malloc(needed)) == NULL)
1681+
if ((newdata = malloc(newsize)) == NULL)
16801682
goto oom;
1681-
if (buf->off > 0)
1683+
if (buf->len != buf->off)
16821684
memcpy(newdata, buf->data + buf->off, buf->len - buf->off);
16831685
free(buf->data);
16841686
buf->data = newdata;
16851687
buf->size = newsize;
16861688
} else {
16871689
/* Just reset existing buffer. */
1688-
if (buf->off > 0) {
1690+
if (buf->len != buf->off) {
16891691
memmove(buf->data, buf->data + buf->off,
16901692
buf->len - buf->off);
16911693
}

0 commit comments

Comments
 (0)