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

@@ -1,7 +1,7 @@
#
# SPDX-License-Identifier: ISC
#
# Copyright (c) 2011-2022 Todd C. Miller <Todd.Miller@sudo.ws>
# Copyright (c) 2011-2023 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
@@ -111,8 +111,8 @@ PVS_IGNORE = 'V707,V011,V002,V536'
PVS_LOG_OPTS = -a 'GA:1,2' -e -t errorfile -d $(PVS_IGNORE)
# Regression tests
TEST_PROGS = conf_test getgids getgrouplist_test hltq_test json_test \
multiarch_test open_parent_dir_test parse_gids_test \
TEST_PROGS = conf_test getgids getgrouplist_test hexchar_test hltq_test \
json_test multiarch_test open_parent_dir_test parse_gids_test \
parseln_test progname_test regex_test strsplit_test \
strtobool_test strtoid_test strtomode_test strtonum_test \
uuid_test @COMPAT_TEST_PROGS@
@@ -145,12 +145,13 @@ DEVEL = @DEVEL@
SHELL = @SHELL@
LTOBJS = basename.lo @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \
gettime.lo getgrouplist.lo gidlist.lo json.lo lbuf.lo locking.lo \
logfac.lo logpri.lo mkdir_parents.lo mmap_alloc.lo multiarch.lo \
parseln.lo progname.lo rcstr.lo regex.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 uuid.lo @COMMON_OBJS@ @LTLIBOBJS@
gettime.lo getgrouplist.lo gidlist.lo hexchar.lo json.lo lbuf.lo \
locking.lo logfac.lo logpri.lo mkdir_parents.lo mmap_alloc.lo \
multiarch.lo parseln.lo progname.lo rcstr.lo regex.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 uuid.lo \
@COMMON_OBJS@ @LTLIBOBJS@
IOBJS = $(LTOBJS:.lo=.i)
@@ -174,6 +175,8 @@ GETDELIM_TEST_OBJS = getdelim_test.lo getdelim.lo
HLTQ_TEST_OBJS = hltq_test.lo
HEXCHAR_TEST_OBJS = hexchar_test.lo hexchar.lo
JSON_TEST_OBJS = json_test.lo json.lo
MULTIARCH_TEST_OBJS = multiarch_test.lo multiarch.lo
@@ -290,6 +293,9 @@ getdelim_test: $(GETDELIM_TEST_OBJS) libsudo_util.la
hltq_test: $(HLTQ_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(HLTQ_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
hexchar_test: $(HEXCHAR_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(HEXCHAR_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
json_test: $(JSON_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(JSON_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
@@ -476,6 +482,7 @@ check: $(TEST_PROGS) check-fuzzer
./strtonum_test || rval=`expr $$rval + $$?`; \
./uuid_test || rval=`expr $$rval + $$?`; \
./hltq_test || rval=`expr $$rval + $$?`; \
./hexchar_test || rval=`expr $$rval + $$?`; \
./json_test || rval=`expr $$rval + $$?`; \
./progname_test || rval=`expr $$rval + $$?`; \
rm -f ./progname_test2; ln -s ./progname_test ./progname_test2; \
@@ -945,6 +952,28 @@ gmtime_r.i: $(srcdir)/gmtime_r.c $(incdir)/compat/stdbool.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
gmtime_r.plog: gmtime_r.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/gmtime_r.c --i-file $< --output-file $@
hexchar.lo: $(srcdir)/hexchar.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) $(HARDENING_CFLAGS) $(srcdir)/hexchar.c
hexchar.i: $(srcdir)/hexchar.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) $<
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 $@
hexchar_test.lo: $(srcdir)/regress/hexchar/hexchar_test.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/regress/hexchar/hexchar_test.c
hexchar_test.i: $(srcdir)/regress/hexchar/hexchar_test.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
hexchar_test.plog: hexchar_test.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/regress/hexchar/hexchar_test.c --i-file $< --output-file $@
hltq_test.lo: $(srcdir)/regress/tailq/hltq_test.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_fatal.h \
$(incdir)/sudo_plugin.h $(incdir)/sudo_queue.h \

103
lib/util/hexchar.c Normal file
View File

@@ -0,0 +1,103 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2013-2015, 2023 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 "sudo_compat.h"
#include "sudo_debug.h"
#include "sudo_util.h"
/*
* Converts a two-byte hex string to decimal.
* Returns a value 0-255 on success or -1 for invalid input.
*/
int
sudo_hexchar_v1(const char *s)
{
unsigned char result[2];
int i;
debug_decl(sudo_hexchar, SUDO_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

@@ -0,0 +1,81 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2014-2015, 2023 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"
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] : "hexchar_test");
/* 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 = sudo_hexchar(test_data[i].hex);
if (result != test_data[i].value) {
fprintf(stderr, "%s: expected %d, got %d\n", getprogname(),
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

@@ -88,6 +88,7 @@ sudo_gethostname_v1
sudo_gettime_awake_v1
sudo_gettime_mono_v1
sudo_gettime_real_v1
sudo_hexchar_v1
sudo_json_add_value_as_object_v1
sudo_json_add_value_v1
sudo_json_close_array_v1