From e7b414ce4e8fb2ae9911f0a9fd03c3a661c5e9d6 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Wed, 24 Feb 2021 19:09:46 -0700 Subject: [PATCH] Move env hooks into sudoers_hooks.c. --- MANIFEST | 1 + plugins/sudoers/Makefile.in | 38 ++++++-- plugins/sudoers/env.c | 125 +------------------------- plugins/sudoers/sudoers.h | 3 + plugins/sudoers/sudoers_hooks.c | 155 ++++++++++++++++++++++++++++++++ 5 files changed, 193 insertions(+), 129 deletions(-) create mode 100644 plugins/sudoers/sudoers_hooks.c diff --git a/MANIFEST b/MANIFEST index 214d4c76f..835291684 100644 --- a/MANIFEST +++ b/MANIFEST @@ -960,6 +960,7 @@ plugins/sudoers/sudoers.h plugins/sudoers/sudoers.in plugins/sudoers/sudoers_debug.c plugins/sudoers/sudoers_debug.h +plugins/sudoers/sudoers_hooks.c plugins/sudoers/sudoers_version.h plugins/sudoers/sudoreplay.c plugins/sudoers/testsudoers.c diff --git a/plugins/sudoers/Makefile.in b/plugins/sudoers/Makefile.in index 6fcfe2d0c..2aaafe292 100644 --- a/plugins/sudoers/Makefile.in +++ b/plugins/sudoers/Makefile.in @@ -178,12 +178,12 @@ LIBPARSESUDOERS_OBJS = alias.lo b64_decode.lo defaults.lo digestname.lo \ LIBPARSESUDOERS_IOBJS = $(LIBPARSESUDOERS_OBJS:.lo=.i) passwd.i SUDOERS_OBJS = $(AUTH_OBJS) audit.lo boottime.lo check.lo editor.lo env.lo \ - env_pattern.lo file.lo find_path.lo fmtsudoers.lo gc.lo \ - goodpath.lo group_plugin.lo interfaces.lo iolog.lo \ - iolog_path_escapes.lo locale.lo log_client.lo logging.lo \ - parse.lo policy.lo prompt.lo set_perms.lo starttime.lo \ - strlcpy_unesc.lo strvec_join.lo sudo_nss.lo sudoers.lo \ - timestamp.lo @SUDOERS_OBJS@ + sudoers_hooks.lo env_pattern.lo file.lo find_path.lo \ + fmtsudoers.lo gc.lo goodpath.lo group_plugin.lo interfaces.lo \ + iolog.lo iolog_path_escapes.lo locale.lo log_client.lo \ + logging.lo parse.lo policy.lo prompt.lo set_perms.lo \ + starttime.lo strlcpy_unesc.lo strvec_join.lo sudo_nss.lo \ + sudoers.lo timestamp.lo @SUDOERS_OBJS@ SUDOERS_IOBJS = $(SUDOERS_OBJS:.lo=.i) @@ -244,7 +244,7 @@ CHECK_UNESC_OBJS = check_unesc.o strlcpy_unesc.lo strvec_join.lo sudoers_debug.l FUZZ_POLICY_OBJS = editor.lo env.lo env_pattern.lo fuzz_policy.o fuzz_stubs.o \ gc.lo iolog_path_escapes.lo locale.lo policy.lo \ - strlcpy_unesc.lo strvec_join.lo sudoers.lo + strlcpy_unesc.lo strvec_join.lo sudoers.lo sudoers_hooks.lo FUZZ_POLICY_CORPUS = $(srcdir)/regress/corpus/policy/policy.* @@ -2864,6 +2864,30 @@ sudoers_debug.i: $(srcdir)/sudoers_debug.c $(devdir)/def_data.h \ $(CC) -E -o $@ $(CPPFLAGS) $< sudoers_debug.plog: sudoers_debug.i rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/sudoers_debug.c --i-file $< --output-file $@ +sudoers_hooks.lo: $(srcdir)/sudoers_hooks.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) $(SSP_CFLAGS) $(srcdir)/sudoers_hooks.c +sudoers_hooks.i: $(srcdir)/sudoers_hooks.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) $< +sudoers_hooks.plog: sudoers_hooks.i + rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/sudoers_hooks.c --i-file $< --output-file $@ sudoreplay.o: $(srcdir)/sudoreplay.c $(incdir)/compat/getopt.h \ $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \ $(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \ diff --git a/plugins/sudoers/env.c b/plugins/sudoers/env.c index 0d2a8bcfa..a617408ff 100644 --- a/plugins/sudoers/env.c +++ b/plugins/sudoers/env.c @@ -310,7 +310,7 @@ env_swap_old(void) * Will only overwrite an existing variable if overwrite is set. * Does not include warnings or debugging to avoid recursive calls. */ -static int +int sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite) { char **ep; @@ -462,60 +462,11 @@ sudo_setenv(const char *var, const char *val, int overwrite) return sudo_setenv2(var, val, true, (bool)overwrite); } -/* - * Similar to setenv(3) but operates on a private copy of the environment. - * Does not include warnings or debugging to avoid recursive calls. - */ -static int -sudo_setenv_nodebug(const char *var, const char *val, int overwrite) -{ - char *ep, *estring = NULL; - const char *cp; - size_t esize; - int ret = -1; - - if (var == NULL || *var == '\0') { - errno = EINVAL; - goto done; - } - - /* - * POSIX says a var name with '=' is an error but BSD - * just ignores the '=' and anything after it. - */ - for (cp = var; *cp && *cp != '='; cp++) - continue; - esize = (size_t)(cp - var) + 2; - if (val) { - esize += strlen(val); /* glibc treats a NULL val as "" */ - } - - /* Allocate and fill in estring. */ - if ((estring = ep = malloc(esize)) == NULL) - goto done; - for (cp = var; *cp && *cp != '='; cp++) - *ep++ = *cp; - *ep++ = '='; - if (val) { - for (cp = val; *cp; cp++) - *ep++ = *cp; - } - *ep = '\0'; - - ret = sudo_putenv_nodebug(estring, true, overwrite); -done: - if (ret == -1) - free(estring); - else - sudoers_gc_add(GC_PTR, estring); - return ret; -} - /* * Similar to unsetenv(3) but operates on a private copy of the environment. * Does not include warnings or debugging to avoid recursive calls. */ -static int +int sudo_unsetenv_nodebug(const char *var) { char **ep = env.envp; @@ -562,7 +513,7 @@ sudo_unsetenv(const char *name) * Similar to getenv(3) but operates on a private copy of the environment. * Does not include warnings or debugging to avoid recursive calls. */ -static char * +char * sudo_getenv_nodebug(const char *name) { char **ep, *val = NULL; @@ -1443,73 +1394,3 @@ init_envtables(void) } debug_return_bool(true); } - -int -sudoers_hook_getenv(const char *name, char **value, void *closure) -{ - static bool in_progress = false; /* avoid recursion */ - - if (in_progress || env.envp == NULL) - return SUDO_HOOK_RET_NEXT; - - in_progress = true; - - /* Hack to make GNU gettext() find the sudoers locale when needed. */ - if (*name == 'L' && sudoers_getlocale() == SUDOERS_LOCALE_SUDOERS) { - if (strcmp(name, "LANGUAGE") == 0 || strcmp(name, "LANG") == 0) { - *value = NULL; - goto done; - } - if (strcmp(name, "LC_ALL") == 0 || strcmp(name, "LC_MESSAGES") == 0) { - *value = def_sudoers_locale; - goto done; - } - } - - *value = sudo_getenv_nodebug(name); -done: - in_progress = false; - return SUDO_HOOK_RET_STOP; -} - -int -sudoers_hook_putenv(char *string, void *closure) -{ - static bool in_progress = false; /* avoid recursion */ - - if (in_progress || env.envp == NULL) - return SUDO_HOOK_RET_NEXT; - - in_progress = true; - sudo_putenv_nodebug(string, true, true); - in_progress = false; - return SUDO_HOOK_RET_STOP; -} - -int -sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure) -{ - static bool in_progress = false; /* avoid recursion */ - - if (in_progress || env.envp == NULL) - return SUDO_HOOK_RET_NEXT; - - in_progress = true; - sudo_setenv_nodebug(name, value, overwrite); - in_progress = false; - return SUDO_HOOK_RET_STOP; -} - -int -sudoers_hook_unsetenv(const char *name, void *closure) -{ - static bool in_progress = false; /* avoid recursion */ - - if (in_progress || env.envp == NULL) - return SUDO_HOOK_RET_NEXT; - - in_progress = true; - sudo_unsetenv_nodebug(name); - in_progress = false; - return SUDO_HOOK_RET_STOP; -} diff --git a/plugins/sudoers/sudoers.h b/plugins/sudoers/sudoers.h index 3ec97f428..a0184cf6b 100644 --- a/plugins/sudoers/sudoers.h +++ b/plugins/sudoers/sudoers.h @@ -391,6 +391,9 @@ bool validate_env_vars(char * const envp[]); int sudo_setenv(const char *var, const char *val, int overwrite); int sudo_unsetenv(const char *var); char *sudo_getenv(const char *name); +char *sudo_getenv_nodebug(const char *name); +int sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite); +int sudo_unsetenv_nodebug(const char *var); int sudoers_hook_getenv(const char *name, char **value, void *closure); int sudoers_hook_putenv(char *string, void *closure); int sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure); diff --git a/plugins/sudoers/sudoers_hooks.c b/plugins/sudoers/sudoers_hooks.c new file mode 100644 index 000000000..d0610e0e1 --- /dev/null +++ b/plugins/sudoers/sudoers_hooks.c @@ -0,0 +1,155 @@ +/* + * SPDX-License-Identifier: ISC + * + * Copyright (c) 2000-2005, 2007-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. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ + +/* + * 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 + +#include +#include +#include +#include + +#include "sudoers.h" + +/* + * Similar to setenv(3) but operates on a private copy of the environment. + * Does not include warnings or debugging to avoid recursive calls. + */ +static int +sudo_setenv_nodebug(const char *var, const char *val, int overwrite) +{ + char *ep, *estring = NULL; + const char *cp; + size_t esize; + int ret = -1; + + if (var == NULL || *var == '\0') { + errno = EINVAL; + goto done; + } + + /* + * POSIX says a var name with '=' is an error but BSD + * just ignores the '=' and anything after it. + */ + for (cp = var; *cp && *cp != '='; cp++) + continue; + esize = (size_t)(cp - var) + 2; + if (val) { + esize += strlen(val); /* glibc treats a NULL val as "" */ + } + + /* Allocate and fill in estring. */ + if ((estring = ep = malloc(esize)) == NULL) + goto done; + for (cp = var; *cp && *cp != '='; cp++) + *ep++ = *cp; + *ep++ = '='; + if (val) { + for (cp = val; *cp; cp++) + *ep++ = *cp; + } + *ep = '\0'; + + ret = sudo_putenv_nodebug(estring, true, overwrite); +done: + if (ret == -1) + free(estring); + else + sudoers_gc_add(GC_PTR, estring); + return ret; +} + +int +sudoers_hook_getenv(const char *name, char **value, void *closure) +{ + static bool in_progress = false; /* avoid recursion */ + + if (in_progress || env_get() == NULL) + return SUDO_HOOK_RET_NEXT; + + in_progress = true; + + /* Hack to make GNU gettext() find the sudoers locale when needed. */ + if (*name == 'L' && sudoers_getlocale() == SUDOERS_LOCALE_SUDOERS) { + if (strcmp(name, "LANGUAGE") == 0 || strcmp(name, "LANG") == 0) { + *value = NULL; + goto done; + } + if (strcmp(name, "LC_ALL") == 0 || strcmp(name, "LC_MESSAGES") == 0) { + *value = def_sudoers_locale; + goto done; + } + } + + *value = sudo_getenv_nodebug(name); +done: + in_progress = false; + return SUDO_HOOK_RET_STOP; +} + +int +sudoers_hook_putenv(char *string, void *closure) +{ + static bool in_progress = false; /* avoid recursion */ + + if (in_progress || env_get() == NULL) + return SUDO_HOOK_RET_NEXT; + + in_progress = true; + sudo_putenv_nodebug(string, true, true); + in_progress = false; + return SUDO_HOOK_RET_STOP; +} + +int +sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure) +{ + static bool in_progress = false; /* avoid recursion */ + + if (in_progress || env_get() == NULL) + return SUDO_HOOK_RET_NEXT; + + in_progress = true; + sudo_setenv_nodebug(name, value, overwrite); + in_progress = false; + return SUDO_HOOK_RET_STOP; +} + +int +sudoers_hook_unsetenv(const char *name, void *closure) +{ + static bool in_progress = false; /* avoid recursion */ + + if (in_progress || env_get() == NULL) + return SUDO_HOOK_RET_NEXT; + + in_progress = true; + sudo_unsetenv_nodebug(name); + in_progress = false; + return SUDO_HOOK_RET_STOP; +}