2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Add dup3() emulation.

This commit is contained in:
Todd C. Miller 2019-11-02 10:52:55 -06:00
parent 352212c7bf
commit 43df086186
8 changed files with 128 additions and 5 deletions

View File

@ -107,6 +107,7 @@ lib/util/closefrom.c
lib/util/digest.c lib/util/digest.c
lib/util/digest_gcrypt.c lib/util/digest_gcrypt.c
lib/util/digest_openssl.c lib/util/digest_openssl.c
lib/util/dup3.c
lib/util/event.c lib/util/event.c
lib/util/event_poll.c lib/util/event_poll.c
lib/util/event_select.c lib/util/event_select.c

View File

@ -227,6 +227,9 @@
/* Define to 1 if the compiler supports the __visibility__ attribute. */ /* Define to 1 if the compiler supports the __visibility__ attribute. */
#undef HAVE_DSO_VISIBILITY #undef HAVE_DSO_VISIBILITY
/* Define to 1 if you have the `dup3' function. */
#undef HAVE_DUP3
/* Define to 1 if you have the <endian.h> header file. */ /* Define to 1 if you have the <endian.h> header file. */
#undef HAVE_ENDIAN_H #undef HAVE_ENDIAN_H

26
configure vendored
View File

@ -20560,6 +20560,32 @@ esac
done done
fi
done
for ac_func in dup3
do :
ac_fn_c_check_func "$LINENO" "dup3" "ac_cv_func_dup3"
if test "x$ac_cv_func_dup3" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_DUP3 1
_ACEOF
else
case " $LIBOBJS " in
*" dup3.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS dup3.$ac_objext"
;;
esac
for _sym in sudo_dup3; do
COMPAT_EXP="${COMPAT_EXP}${_sym}
"
done
fi fi
done done

View File

@ -2740,6 +2740,10 @@ AC_CHECK_FUNCS([fstatat], [], [
AC_LIBOBJ(fstatat) AC_LIBOBJ(fstatat)
SUDO_APPEND_COMPAT_EXP(sudo_fstatat) SUDO_APPEND_COMPAT_EXP(sudo_fstatat)
]) ])
AC_CHECK_FUNCS([dup3], [], [
AC_LIBOBJ(dup3)
SUDO_APPEND_COMPAT_EXP(sudo_dup3)
])
AC_CHECK_FUNCS([pipe2], [], [ AC_CHECK_FUNCS([pipe2], [], [
AC_LIBOBJ(pipe2) AC_LIBOBJ(pipe2)
SUDO_APPEND_COMPAT_EXP(sudo_pipe2) SUDO_APPEND_COMPAT_EXP(sudo_pipe2)

View File

@ -221,10 +221,12 @@
# endif # endif
#endif #endif
/* For pipe2() emulation. */ /* For dup3() and pipe2() emulation. */
#if !defined(HAVE_PIPE2) && defined(O_NONBLOCK) && (!defined(O_CLOEXEC) || O_CLOEXEC > 0xffffffff) #if (!defined(HAVE_PIPE2) || !defined(HAVE_DUP3)) && defined(O_NONBLOCK)
# undef O_CLOEXEC # if !defined(O_CLOEXEC) || O_CLOEXEC > 0xffffffff
# define O_CLOEXEC 0x80000000 # undef O_CLOEXEC
# define O_CLOEXEC 0x80000000
# endif
#endif #endif
/* /*
@ -544,6 +546,11 @@ __dso_public void sudo_vsyslog(int pri, const char *fmt, va_list ap);
# undef vsyslog # undef vsyslog
# define vsyslog(_a, _b, _c) sudo_vsyslog((_a), (_b), (_c)) # define vsyslog(_a, _b, _c) sudo_vsyslog((_a), (_b), (_c))
#endif /* HAVE_VSYSLOG */ #endif /* HAVE_VSYSLOG */
#ifndef HAVE_DUP3
__dso_public int sudo_dup3(int oldd, int newd, int flags);
# undef dup3
# define dup3(_a, _b, _c) sudo_dup3((_a), (_b), (_c))
#endif /* HAVE_DUP3 */
#ifndef HAVE_PIPE2 #ifndef HAVE_PIPE2
__dso_public int sudo_pipe2(int fildes[2], int flags); __dso_public int sudo_pipe2(int fildes[2], int flags);
# undef pipe2 # undef pipe2

View File

@ -510,6 +510,12 @@ digest_openssl.i: $(srcdir)/digest_openssl.c $(incdir)/compat/stdbool.h \
$(CC) -E -o $@ $(CPPFLAGS) $< $(CC) -E -o $@ $(CPPFLAGS) $<
digest_openssl.plog: digest_openssl.i digest_openssl.plog: digest_openssl.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/digest_openssl.c --i-file $< --output-file $@ rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/digest_openssl.c --i-file $< --output-file $@
dup3.lo: $(srcdir)/dup3.c $(incdir)/sudo_compat.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/dup3.c
dup3.i: $(srcdir)/dup3.c $(incdir)/sudo_compat.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
dup3.plog: dup3.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/dup3.c --i-file $< --output-file $@
event.lo: $(srcdir)/event.c $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \ event.lo: $(srcdir)/event.c $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_debug.h $(incdir)/sudo_event.h $(incdir)/sudo_fatal.h \ $(incdir)/sudo_debug.h $(incdir)/sudo_event.h $(incdir)/sudo_fatal.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(top_builddir)/config.h $(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(top_builddir)/config.h

76
lib/util/dup3.c Normal file
View File

@ -0,0 +1,76 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2013 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>
#ifndef HAVE_DUP3
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "sudo_compat.h"
int
sudo_dup3(int oldd, int newd, int flags)
{
int oflags;
if (oldd == newd) {
errno = EINVAL;
return -1;
}
if (dup2(oldd, newd) == -1)
return -1;
oflags = fcntl(newd, F_GETFL, 0);
if (oflags == -1)
goto bad;
if (ISSET(flags, O_NONBLOCK)) {
if (!ISSET(oflags, O_NONBLOCK)) {
SET(oflags, O_NONBLOCK);
if (fcntl(newd, F_SETFL, oflags) == -1)
goto bad;
}
} else {
if (ISSET(oflags, O_NONBLOCK)) {
CLR(oflags, O_NONBLOCK);
if (fcntl(newd, F_SETFL, oflags) == -1)
goto bad;
}
}
if (ISSET(flags, O_CLOEXEC)) {
if (fcntl(newd, F_SETFD, FD_CLOEXEC) == -1)
goto bad;
}
return 0;
bad:
close(newd);
return -1;
}
#endif /* HAVE_DUP3 */

View File

@ -116,7 +116,7 @@ sub mkdep {
# XXX - fill in AUTH_OBJS from contents of the auth dir instead # XXX - fill in AUTH_OBJS from contents of the auth dir instead
$makefile =~ s:\@AUTH_OBJS\@:afs.lo aix_auth.lo bsdauth.lo dce.lo fwtk.lo getspwuid.lo kerb5.lo pam.lo passwd.lo rfc1938.lo secureware.lo securid5.lo sia.lo:; $makefile =~ s:\@AUTH_OBJS\@:afs.lo aix_auth.lo bsdauth.lo dce.lo fwtk.lo getspwuid.lo kerb5.lo pam.lo passwd.lo rfc1938.lo secureware.lo securid5.lo sia.lo:;
$makefile =~ s:\@DIGEST\@:digest.lo digest_openssl.lo digest_gcrypt.lo:; $makefile =~ s:\@DIGEST\@:digest.lo digest_openssl.lo digest_gcrypt.lo:;
$makefile =~ s:\@LTLIBOBJS\@:arc4random.lo arc4random_uniform.lo closefrom.lo fchmodat.lo fstatat.lo fnmatch.lo getaddrinfo.lo getcwd.lo getentropy.lo getgrouplist.lo getdelim.lo getopt_long.lo glob.lo inet_ntop_lo inet_pton.lo isblank.lo memrchr.lo memset_s.lo mksiglist.lo mksigname.lo mktemp.lo nanosleep.lo openat.lo pipe2.lo pw_dup.lo reallocarray.lo sha2.lo sig2str.lo siglist.lo signame.lo snprintf.lo str2sig.lo strlcat.lo strlcpy.lo strndup.lo strnlen.lo strsignal.lo unlinkat.lo utimens.lo vsyslog.lo:; $makefile =~ s:\@LTLIBOBJS\@:arc4random.lo arc4random_uniform.lo closefrom.lo dup3.lo fchmodat.lo fstatat.lo fnmatch.lo getaddrinfo.lo getcwd.lo getentropy.lo getgrouplist.lo getdelim.lo getopt_long.lo glob.lo inet_ntop_lo inet_pton.lo isblank.lo memrchr.lo memset_s.lo mksiglist.lo mksigname.lo mktemp.lo nanosleep.lo openat.lo pipe2.lo pw_dup.lo reallocarray.lo sha2.lo sig2str.lo siglist.lo signame.lo snprintf.lo str2sig.lo strlcat.lo strlcpy.lo strndup.lo strnlen.lo strsignal.lo unlinkat.lo utimens.lo vsyslog.lo:;
# Parse OBJS lines # Parse OBJS lines
my %objs; my %objs;