Move hexchar() from the sudoers plugin to lib/util.

This commit is contained in:
Todd C. Miller
2023-01-03 15:50:42 -07:00
parent 0cb3835111
commit e5d98da014
10 changed files with 61 additions and 68 deletions

View File

@@ -159,7 +159,7 @@ PROGS = sudoers.la visudo sudoreplay cvtsudoers testsudoers
# Regression tests
TEST_PROGS = check_addr check_base64 check_digest check_editor \
check_env_pattern check_exptilde check_fill check_gentime \
check_hexchar check_iolog_plugin check_serialize_list \
check_iolog_plugin check_serialize_list \
check_starttime check_unesc @SUDOERS_TEST_PROGS@
TEST_VERBOSE =
HARNESS = $(SHELL) regress/harness $(TEST_VERBOSE)
@@ -178,7 +178,7 @@ AUTH_OBJS = sudo_auth.lo @AUTH_OBJS@
LIBPARSESUDOERS_OBJS = alias.lo b64_decode.lo defaults.lo digestname.lo \
exptilde.lo filedigest.lo gentime.lo gram.lo \
hexchar.lo match.lo match_addr.lo match_command.lo \
match.lo match_addr.lo match_command.lo \
match_digest.lo pwutil.lo pwutil_impl.lo \
redblack.lo strlist.lo sudoers_debug.lo timeout.lo \
timestr.lo toke.lo toke_util.lo
@@ -237,12 +237,10 @@ CHECK_ENV_MATCH_OBJS = check_env_pattern.o env_pattern.lo sudoers_debug.lo
CHECK_EXPTILDE_OBJS = check_exptilde.o exptilde.lo pwutil.lo pwutil_impl.lo redblack.lo sudoers_debug.lo
CHECK_FILL_OBJS = check_fill.o hexchar.lo toke_util.lo sudoers_debug.lo
CHECK_FILL_OBJS = check_fill.o toke_util.lo sudoers_debug.lo
CHECK_GENTIME_OBJS = check_gentime.o gentime.lo sudoers_debug.lo
CHECK_HEXCHAR_OBJS = check_hexchar.o hexchar.lo sudoers_debug.lo
CHECK_IOLOG_PLUGIN_OBJS = check_iolog_plugin.o iolog.lo log_client.lo \
locale.lo pwutil.lo pwutil_impl.lo redblack.lo \
strlist.lo sudoers_debug.lo unesc_str.lo
@@ -393,9 +391,6 @@ check_fill: $(CHECK_FILL_OBJS) $(LIBUTIL)
check_gentime: $(CHECK_GENTIME_OBJS) $(LIBUTIL)
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CHECK_GENTIME_OBJS) $(LDFLAGS) $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(LIBS)
check_hexchar: $(CHECK_HEXCHAR_OBJS) $(LIBUTIL)
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CHECK_HEXCHAR_OBJS) $(LDFLAGS) $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(LIBS)
check_iolog_plugin: $(CHECK_IOLOG_PLUGIN_OBJS) $(LIBUTIL) $(LIBIOLOG) $(LIBLOGSRV)
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CHECK_IOLOG_PLUGIN_OBJS) $(LDFLAGS) $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(LIBIOLOG) $(LIBLOGSRV) @LIBTLS@
@@ -667,7 +662,6 @@ check: $(TEST_PROGS) visudo testsudoers cvtsudoers check-fuzzer
./check_exptilde || rval=`expr $$rval + $$?`; \
./check_fill || rval=`expr $$rval + $$?`; \
./check_gentime || rval=`expr $$rval + $$?`; \
./check_hexchar || rval=`expr $$rval + $$?`; \
mkdir -p regress/iolog_plugin; \
./check_iolog_plugin regress/iolog_plugin/iolog || rval=`expr $$rval + $$?`; \
./check_serialize_list || rval=`expr $$rval + $$?`; \
@@ -1118,16 +1112,6 @@ check_gentime.i: $(srcdir)/regress/parser/check_gentime.c \
$(CC) -E -o $@ $(CPPFLAGS) $<
check_gentime.plog: check_gentime.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/regress/parser/check_gentime.c --i-file $< --output-file $@
check_hexchar.o: $(srcdir)/regress/parser/check_hexchar.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/regress/parser/check_hexchar.c
check_hexchar.i: $(srcdir)/regress/parser/check_hexchar.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
check_hexchar.plog: check_hexchar.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/regress/parser/check_hexchar.c --i-file $< --output-file $@
check_iolog_plugin.o: $(srcdir)/regress/iolog_plugin/check_iolog_plugin.c \
$(devdir)/def_data.c $(devdir)/def_data.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
@@ -1924,28 +1908,6 @@ group_plugin.i: $(srcdir)/group_plugin.c $(devdir)/def_data.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
group_plugin.plog: group_plugin.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/group_plugin.c --i-file $< --output-file $@
hexchar.lo: $(srcdir)/hexchar.c $(devdir)/def_data.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_eventlog.h $(incdir)/sudo_fatal.h \
$(incdir)/sudo_gettext.h $(incdir)/sudo_plugin.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(srcdir)/defaults.h \
$(srcdir)/logging.h $(srcdir)/parse.h $(srcdir)/sudo_nss.h \
$(srcdir)/sudoers.h $(srcdir)/sudoers_debug.h \
$(top_builddir)/config.h $(top_builddir)/pathnames.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/hexchar.c
hexchar.i: $(srcdir)/hexchar.c $(devdir)/def_data.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_eventlog.h $(incdir)/sudo_fatal.h \
$(incdir)/sudo_gettext.h $(incdir)/sudo_plugin.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(srcdir)/defaults.h \
$(srcdir)/logging.h $(srcdir)/parse.h $(srcdir)/sudo_nss.h \
$(srcdir)/sudoers.h $(srcdir)/sudoers_debug.h \
$(top_builddir)/config.h $(top_builddir)/pathnames.h
$(CC) -E -o $@ $(CPPFLAGS) $<
hexchar.plog: hexchar.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/hexchar.c --i-file $< --output-file $@
interfaces.lo: $(srcdir)/interfaces.c $(devdir)/def_data.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \

View File

@@ -1,101 +0,0 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws>
*
* 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.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#include <config.h>
#include "sudoers.h"
/*
* Converts a two-byte hex string to decimal.
* Returns the decimal value or -1 for invalid input.
*/
int
hexchar(const char *s)
{
unsigned char result[2];
int i;
debug_decl(hexchar, SUDOERS_DEBUG_UTIL);
for (i = 0; i < 2; i++) {
switch (s[i]) {
case '0':
result[i] = 0;
break;
case '1':
result[i] = 1;
break;
case '2':
result[i] = 2;
break;
case '3':
result[i] = 3;
break;
case '4':
result[i] = 4;
break;
case '5':
result[i] = 5;
break;
case '6':
result[i] = 6;
break;
case '7':
result[i] = 7;
break;
case '8':
result[i] = 8;
break;
case '9':
result[i] = 9;
break;
case 'A':
case 'a':
result[i] = 10;
break;
case 'B':
case 'b':
result[i] = 11;
break;
case 'C':
case 'c':
result[i] = 12;
break;
case 'D':
case 'd':
result[i] = 13;
break;
case 'E':
case 'e':
result[i] = 14;
break;
case 'F':
case 'f':
result[i] = 15;
break;
default:
/* Invalid input. */
debug_return_int(-1);
}
}
debug_return_int((result[0] << 4) | result[1]);
}

View File

@@ -97,7 +97,7 @@ digest_matches(int fd, const char *path, const char *runchroot,
/* Convert ascii hex to binary. */
unsigned int i;
for (i = 0; i < digest_len; i++) {
const int h = hexchar(&digest->digest_str[i + i]);
const int h = sudo_hexchar(&digest->digest_str[i + i]);
if (h == -1)
goto bad_format;
sudoers_digest[i] = (unsigned char)h;

View File

@@ -417,9 +417,6 @@ struct gid_list *runas_getgroups(void);
/* toke.c */
void init_lexer(void);
/* hexchar.c */
int hexchar(const char *s);
/* base64.c */
size_t base64_decode(const char *str, unsigned char *dst, size_t dsize);
size_t base64_encode(const unsigned char *in, size_t in_len, char *out, size_t out_len);

View File

@@ -1,83 +0,0 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2014-2015 Todd C. Miller <Todd.Miller@sudo.ws>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUDO_ERROR_WRAP 0
#include "sudo_compat.h"
#include "sudo_util.h"
int hexchar(const char *s);
sudo_dso_public int main(int argc, char *argv[]);
struct hexchar_test {
char hex[3];
int value;
};
int
main(int argc, char *argv[])
{
struct hexchar_test *test_data;
int i, ntests, result, errors = 0;
static const char xdigs_lower[] = "0123456789abcdef";
static const char xdigs_upper[] = "0123456789ABCDEF";
initprogname(argc > 0 ? argv[0] : "check_hexchar");
/* Build up test data. */
ntests = 256 + 256 + 3;
test_data = calloc(sizeof(*test_data), ntests);
for (i = 0; i < 256; i++) {
/* lower case */
test_data[i].value = i;
test_data[i].hex[1] = xdigs_lower[ (i & 0x0f)];
test_data[i].hex[0] = xdigs_lower[((i & 0xf0) >> 4)];
/* upper case */
test_data[i + 256].value = i;
test_data[i + 256].hex[1] = xdigs_upper[ (i & 0x0f)];
test_data[i + 256].hex[0] = xdigs_upper[((i & 0xf0) >> 4)];
}
/* Also test invalid data */
test_data[ntests - 3].hex[0] = '\0';
test_data[ntests - 3].value = -1;
strlcpy(test_data[ntests - 2].hex, "AG", sizeof(test_data[ntests - 2].hex));
test_data[ntests - 2].value = -1;
strlcpy(test_data[ntests - 1].hex, "-1", sizeof(test_data[ntests - 1].hex));
test_data[ntests - 1].value = -1;
for (i = 0; i < ntests; i++) {
result = hexchar(test_data[i].hex);
if (result != test_data[i].value) {
fprintf(stderr, "check_hexchar: expected %d, got %d\n",
test_data[i].value, result);
errors++;
}
}
if (ntests != 0) {
printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
}
exit(errors);
}

View File

@@ -51,7 +51,7 @@ copy_string(char *dst, const char *src, size_t len)
while (len--) {
if (*src == '\\' && len) {
if (src[1] == 'x' && len >= 3 && (h = hexchar(src + 2)) != -1) {
if (src[1] == 'x' && len >= 3 && (h = sudo_hexchar(src + 2)) != -1) {
*dst++ = h;
src += 4;
len -= 3;