Fork open file descriptors

* Add O_CLOEXEC to all open(...) calls
  * Add "e" to all fopen(...) calls
  * Prevents passing open file descriptors to child
    processes.  Leaving psid's available to the child
    could cause unwanted i/o problems and possibly a
    security leak.
pull/1/head
David Freese 2012-06-01 10:26:30 -05:00
rodzic f47119c7f5
commit 9b3dabd853
8 zmienionych plików z 80 dodań i 12 usunięć

Wyświetl plik

@ -123,6 +123,8 @@ AC_FUNC_STRFTIME
AC_FUNC_STRTOD AC_FUNC_STRTOD
AC_CHECK_FUNCS([getaddrinfo gethostbyname hstrerror gmtime_r localtime_r memmove memset mkdir select setenv snprintf socket socketpair strcasecmp strcasestr strchr strdup strerror strlcpy strncasecmp strrchr strstr strtol uname unsetenv vsnprintf]) AC_CHECK_FUNCS([getaddrinfo gethostbyname hstrerror gmtime_r localtime_r memmove memset mkdir select setenv snprintf socket socketpair strcasecmp strcasestr strchr strdup strerror strlcpy strncasecmp strrchr strstr strtol uname unsetenv vsnprintf])
# Check for O_CLOEXEC
AC_FCNTL_FLAGS
AC_PRESERVE_HELP_ORDER AC_PRESERVE_HELP_ORDER

Wyświetl plik

@ -13,3 +13,25 @@ AC_DEFINE_UNQUOTED([HAVE_]FUNC_NAME_UC, $ac_cv_have_func_[]$1, [Define to 1 if w
LIBS="$LIBS_search_libs_save" LIBS="$LIBS_search_libs_save"
]) ])
# ---------------------------------------------------------------------------
# Macro: FCNTL_FLAGS
# ---------------------------------------------------------------------------
AC_DEFUN([AC_FCNTL_FLAGS],
[
AC_CACHE_CHECK([for O_CLOEXEC], [ac_cv_o_cloexec], [
AC_LANG_PUSH([C])
save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -I${srcdir}"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <fcntl.h>], [ int flags= O_CLOEXEC])], [ac_cv_o_cloexec="yes"], [ac_cv_o_cloexec="no"])
AC_LANG_POP
])
AS_IF([test "x$ac_cv_o_cloexec" = "xyes"],[ AC_DEFINE(HAVE_O_CLOEXEC, 1, [Define to 1 if you have O_CLOEXEC defined])])
])
# ---------------------------------------------------------------------------
# End Macro: FCNTL_FLAGS
# ---------------------------------------------------------------------------

Wyświetl plik

@ -886,7 +886,7 @@ static bool open_serial(const char* dev)
{ {
bool ret = false; bool ret = false;
#ifdef __CYGWIN__ #ifdef __CYGWIN__
int fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); int fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY | O_CLOEXEC);
if (fd != -1) { if (fd != -1) {
close(fd); close(fd);
ret = true; ret = true;

Wyświetl plik

@ -204,7 +204,12 @@ void PTT::open_tty(void)
com_to_tty(pttdevName); com_to_tty(pttdevName);
# endif # endif
if ((pttfd = open(pttdevName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY)) < 0) { int oflags = O_RDWR | O_NOCTTY | O_NDELAY;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
if ((pttfd = open(pttdevName.c_str(), oflags)) < 0) {
LOG_ERROR("Could not open \"%s\": %s", pttdevName.c_str(), strerror(errno)); LOG_ERROR("Could not open \"%s\": %s", pttdevName.c_str(), strerror(errno));
return; return;
} }
@ -295,8 +300,13 @@ void PTT::set_tty(bool ptt)
void PTT::open_parport(void) void PTT::open_parport(void)
{ {
if (progdefaults.PTTdev.find("tty") != string::npos) return; if (progdefaults.PTTdev.find("tty") != string::npos) return;
if ((pttfd = open(progdefaults.PTTdev.c_str(), O_RDWR | O_NDELAY)) == -1) { int oflags = O_RDWR | O_NDELAY;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
if ((pttfd = open(progdefaults.PTTdev.c_str(), oflags)) == -1) {
LOG_ERROR("Could not open %s: %s", progdefaults.PTTdev.c_str(), strerror(errno)); LOG_ERROR("Could not open %s: %s", progdefaults.PTTdev.c_str(), strerror(errno));
return; return;
} }
@ -417,7 +427,13 @@ static bool open_fifos(const char* base, int fd[2])
LOG_ERROR("%s is not a fifo", fifo.c_str()); LOG_ERROR("%s is not a fifo", fifo.c_str());
return false; return false;
} }
if ((fd[0] = open(fifo.c_str(), O_RDONLY | O_NONBLOCK)) == -1) {
int oflags = O_RDONLY | O_NONBLOCK;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
if ((fd[0] = open(fifo.c_str(), oflags)) == -1) {
LOG_ERROR("Could not open %s: %s", fifo.c_str(), strerror(errno)); LOG_ERROR("Could not open %s: %s", fifo.c_str(), strerror(errno));
return false; return false;
} }
@ -428,7 +444,13 @@ static bool open_fifos(const char* base, int fd[2])
LOG_ERROR("%s is not a fifo", fifo.c_str()); LOG_ERROR("%s is not a fifo", fifo.c_str());
return false; return false;
} }
if ((fd[1] = open(fifo.c_str(), O_WRONLY | O_NONBLOCK)) == -1) { oflags = O_WRONLY | O_NONBLOCK;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
if ((fd[1] = open(fifo.c_str(), oflags)) == -1) {
LOG_ERROR("Could not open %s: %s", fifo.c_str(), strerror(errno)); LOG_ERROR("Could not open %s: %s", fifo.c_str(), strerror(errno));
return false; return false;
} }

Wyświetl plik

@ -301,7 +301,7 @@ static void *rigMEM_loop(void *args)
break; break;
if (TogglePTT || rig_qsy || change_mode) { if (TogglePTT || rig_qsy || change_mode) {
IOout = fopen("c:/RIGCTL/ptt", "w"); IOout = fopen("c:/RIGCTL/ptt", "we");
if (IOout) { if (IOout) {
LOG_VERBOSE("sent %d, %c, %s", LOG_VERBOSE("sent %d, %c, %s",
(int)qsy_f, (int)qsy_f,
@ -318,7 +318,7 @@ static void *rigMEM_loop(void *args)
} }
} }
IOin = fopen("c:/RIGCTL/rig", "r"); IOin = fopen("c:/RIGCTL/rig", "re");
if (IOin) { if (IOin) {
fscanf(IOin, "%ld\n", &IOfreq); fscanf(IOin, "%ld\n", &IOfreq);
fscanf(IOin, "%s", szmode); fscanf(IOin, "%s", szmode);

Wyświetl plik

@ -71,7 +71,13 @@ bool Cserial::OpenPort() {
#ifdef __CYGWIN__ #ifdef __CYGWIN__
com_to_tty(device); com_to_tty(device);
#endif #endif
if ((fd = open( device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
int oflags = O_RDWR | O_NOCTTY | O_NDELAY;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
if ((fd = open( device.c_str(), oflags)) < 0)
return false; return false;
// save current port settings // save current port settings
tcflush (fd, TCIFLUSH); tcflush (fd, TCIFLUSH);

Wyświetl plik

@ -73,7 +73,13 @@ void MixerOSS::openMixer(const char *dev)
if (mixer_fd != -1) closeMixer(); if (mixer_fd != -1) closeMixer();
mixer = dev; mixer = dev;
try { try {
mixer_fd = open(mixer.c_str(), O_RDWR);
int oflags = O_RDWR;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
mixer_fd = open(mixer.c_str(), oflags);
if (mixer_fd == -1) if (mixer_fd == -1)
throw MixerException(errno); throw MixerException(errno);
if ((err = initMask()) != 0) if ((err = initMask()) != 0)
@ -144,7 +150,12 @@ void MixerOSS::findNumMixers()
szDevice[10] = 0; szDevice[10] = 0;
else else
szDevice[10] = '0'+(i-1); szDevice[10] = '0'+(i-1);
fd = open(szDevice, O_RDWR); int oflags = O_RDWR;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
fd = open(szDevice, oflags);
if (fd >= 0) { if (fd >= 0) {
Devices[NumMixers] = i; Devices[NumMixers] = i;
NumMixers++; NumMixers++;

Wyświetl plik

@ -399,7 +399,12 @@ int SoundOSS::Open(int md, int freq)
mode = md; mode = md;
try { try {
device_fd = open(device.c_str(), mode, 0); int oflags = md;
# ifdef HAVE_O_CLOEXEC
oflags = oflags | O_CLOEXEC;
# endif
device_fd = open(device.c_str(), oflags, 0);
if (device_fd == -1) if (device_fd == -1)
throw SndException(errno); throw SndException(errno);
Format(AFMT_S16_LE); // default: 16 bit little endian Format(AFMT_S16_LE); // default: 16 bit little endian