From f1d0c99e03cb49b41d91dc92833ac2bc97639bf0 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sat, 2 Nov 2019 12:03:44 -0600 Subject: [PATCH] Move bufsize_roundup() -> sudo_pow2_roundup() in libsudo_util. --- MANIFEST | 1 + include/sudo_util.h | 4 ++++ lib/util/Makefile.in | 14 +++++++++++++- lib/util/roundup.c | 43 +++++++++++++++++++++++++++++++++++++++++++ logsrvd/logsrv_util.c | 22 +--------------------- logsrvd/logsrv_util.h | 1 - logsrvd/logsrvd.c | 2 +- logsrvd/sendlog.c | 4 ++-- 8 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 lib/util/roundup.c diff --git a/MANIFEST b/MANIFEST index 3e4908528..b74b40821 100644 --- a/MANIFEST +++ b/MANIFEST @@ -196,6 +196,7 @@ lib/util/regress/sudo_parseln/test6.in lib/util/regress/sudo_parseln/test6.out.ok lib/util/regress/tailq/hltq_test.c lib/util/regress/vsyslog/vsyslog_test.c +lib/util/roundup.c lib/util/secure_path.c lib/util/setgroups.c lib/util/sha2.c diff --git a/include/sudo_util.h b/include/sudo_util.h index d6ee26261..c10094651 100644 --- a/include/sudo_util.h +++ b/include/sudo_util.h @@ -244,6 +244,10 @@ __dso_public ssize_t sudo_parseln_v2(char **buf, size_t *bufsize, unsigned int * /* progname.c */ __dso_public void initprogname(const char *); +/* roundup.c */ +__dso_public unsigned int sudo_pow2_roundup_v1(unsigned int len); +#define sudo_pow2_roundup(_a) sudo_pow2_roundup_v1((_a)) + /* secure_path.c */ #define SUDO_PATH_SECURE 0 #define SUDO_PATH_MISSING -1 diff --git a/lib/util/Makefile.in b/lib/util/Makefile.in index d53218391..f1f6d72a5 100644 --- a/lib/util/Makefile.in +++ b/lib/util/Makefile.in @@ -118,7 +118,7 @@ SHELL = @SHELL@ LTOBJS = @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \ gettime.lo getgrouplist.lo gidlist.lo lbuf.lo locking.lo \ logfac.lo logpri.lo mkdir_parents.lo parseln.lo progname.lo \ - secure_path.lo setgroups.lo strsplit.lo strtobool.lo \ + roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \ strtoid.lo strtomode.lo strtonum.lo sudo_conf.lo \ sudo_debug.lo sudo_dso.lo term.lo ttyname_dev.lo \ ttysize.lo @COMMON_OBJS@ @LTLIBOBJS@ @@ -966,6 +966,18 @@ reallocarray.i: $(srcdir)/reallocarray.c $(incdir)/sudo_compat.h \ $(CC) -E -o $@ $(CPPFLAGS) $< reallocarray.plog: reallocarray.i rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/reallocarray.c --i-file $< --output-file $@ +roundup.lo: $(srcdir)/roundup.c $(incdir)/compat/stdbool.h \ + $(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \ + $(incdir)/sudo_queue.h $(incdir)/sudo_util.h \ + $(top_builddir)/config.h + $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/roundup.c +roundup.i: $(srcdir)/roundup.c $(incdir)/compat/stdbool.h \ + $(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \ + $(incdir)/sudo_queue.h $(incdir)/sudo_util.h \ + $(top_builddir)/config.h + $(CC) -E -o $@ $(CPPFLAGS) $< +roundup.plog: roundup.i + rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/roundup.c --i-file $< --output-file $@ secure_path.lo: $(srcdir)/secure_path.c $(incdir)/compat/stdbool.h \ $(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \ $(incdir)/sudo_queue.h $(incdir)/sudo_util.h \ diff --git a/lib/util/roundup.c b/lib/util/roundup.c new file mode 100644 index 000000000..de3b9b80b --- /dev/null +++ b/lib/util/roundup.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "config.h" + +#include + +#include "sudo_compat.h" +#include "sudo_debug.h" +#include "sudo_util.h" + +/* + * Round 32-bit unsigned length to the next highest power of two. + * Always returns at least 64. + * Algorithm from bit twiddling hacks. + */ +unsigned int +sudo_pow2_roundup_v1(unsigned int len) +{ + if (len < 64) + return 64; + len--; + len |= len >> 1; + len |= len >> 2; + len |= len >> 4; + len |= len >> 8; + len |= len >> 16; + len++; + return len; +} diff --git a/logsrvd/logsrv_util.c b/logsrvd/logsrv_util.c index 806c5c40a..84ea662fb 100644 --- a/logsrvd/logsrv_util.c +++ b/logsrvd/logsrv_util.c @@ -43,26 +43,6 @@ #include "sudo_iolog.h" #include "logsrv_util.h" -/* - * Round 32-bit unsigned length to the next highest power of two. - * Always returns at least 64. - * Algorithm from bit twiddling hacks. - */ -unsigned int -bufsize_roundup(unsigned int len) -{ - if (len < 64) - return 64; - len--; - len |= len >> 1; - len |= len >> 2; - len |= len >> 4; - len |= len >> 8; - len |= len >> 16; - len++; - return len; -} - /* * Expand buf as needed or just reset it. */ @@ -74,7 +54,7 @@ expand_buf(struct connection_buffer *buf, unsigned int needed) if (buf->size < needed) { /* Expand buffer. */ - needed = bufsize_roundup(needed); + needed = sudo_pow2_roundup(needed); if ((newdata = malloc(needed)) == NULL) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, "%s: unable to malloc %u", __func__, needed); diff --git a/logsrvd/logsrv_util.h b/logsrvd/logsrv_util.h index adaaa498f..c226d3630 100644 --- a/logsrvd/logsrv_util.h +++ b/logsrvd/logsrv_util.h @@ -35,7 +35,6 @@ struct iolog_file; bool expand_buf(struct connection_buffer *buf, unsigned int needed); bool iolog_open_all(int dfd, const char *iolog_dir, struct iolog_file *iolog_files, const char *mode); bool iolog_seekto(int iolog_dir_fd, const char *iolog_path, struct iolog_file *iolog_files, struct timespec *elapsed_time, const struct timespec *target); -unsigned int bufsize_roundup(unsigned int len); #endif /* SUDO_LOGSRV_UTIL_H */ diff --git a/logsrvd/logsrvd.c b/logsrvd/logsrvd.c index 3d52c7d0b..006911c18 100644 --- a/logsrvd/logsrvd.c +++ b/logsrvd/logsrvd.c @@ -126,7 +126,7 @@ fmt_server_message(struct connection_buffer *buf, ServerMessage *msg) /* Resize buffer as needed. */ if (len > buf->size) { free(buf->data); - buf->size = bufsize_roundup(len); + 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); diff --git a/logsrvd/sendlog.c b/logsrvd/sendlog.c index a9c143971..f6c44d887 100644 --- a/logsrvd/sendlog.c +++ b/logsrvd/sendlog.c @@ -186,7 +186,7 @@ read_io_buf(struct client_closure *closure) /* Expand buf as needed. */ if (timing->u.nbytes > closure->bufsize) { free(closure->buf); - closure->bufsize = bufsize_roundup(timing->u.nbytes); + closure->bufsize = sudo_pow2_roundup(timing->u.nbytes); if ((closure->buf = malloc(closure->bufsize)) == NULL) { sudo_warn(NULL); timing->u.nbytes = 0; @@ -228,7 +228,7 @@ fmt_client_message(struct connection_buffer *buf, ClientMessage *msg) /* Resize buffer as needed. */ if (len > buf->size) { free(buf->data); - buf->size = bufsize_roundup(len); + 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);