Close all file descriptors for the external program

... except for stdin, stdout and stderr.

Besides the obvious security issue, this avoids an issue where output
processing would hang with more than one pipe vdp:

When we see a VDP_END, we close the external program's stdin. Yet before
we closed all file descriptors, a second external progam would also hold
the stdin pipe's write end open.
parent 2b1c5d9a
......@@ -75,6 +75,8 @@ AC_SUBST([VARNISH_LIBRARY_PATH],
AC_CHECK_FUNCS([dup2])
AC_CHECK_FUNCS([memchr])
AC_CHECK_FUNCS([strdup])
AC_CHECK_FUNCS([closefrom])
AC_CHECK_HEADERS([limits.h])
AC_C_INLINE
AC_FUNC_FORK
......
......@@ -5,9 +5,10 @@ AM_LDFLAGS = $(VARNISHAPI_LIBS) -ldl -lrt
vmod_LTLIBRARIES = libvmod_pipe.la
libvmod_pipe_la_SOURCES = \
vdfp_pipe.c \
from_varnish/vfil.c
libvmod_pipe_la_SOURCES = \
vdfp_pipe.c \
from_varnish/vfil.c \
from_varnish/vsub.c
nodist_libvmod_pipe_la_SOURCES = \
vcc_if.c \
......
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2011 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Run stuff in a child process
*/
#include "config.h"
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef HAVE_CLOSEFROM
# include <dirent.h>
#endif
#include "vdef.h"
#include "vas.h"
#include "vsub.h"
void
VSUB_closefrom(int fd)
{
assert(fd >= 0);
#ifdef HAVE_CLOSEFROM
closefrom(fd);
return;
#else
char buf[128];
int i, maxfd = 0;
DIR *d;
struct dirent *de;
char *p;
bprintf(buf, "/proc/%d/fd/", getpid());
d = opendir(buf);
if (d != NULL) {
while (1) {
de = readdir(d);
if (de == NULL)
break;
i = strtoul(de->d_name, &p, 10);
if (*p != '\0')
continue;
if (i > maxfd)
maxfd = i;
}
AZ(closedir(d));
}
if (maxfd == 0)
maxfd = sysconf(_SC_OPEN_MAX);
assert(maxfd > 0);
for (; maxfd > fd; maxfd--)
(void)close(maxfd);
#endif
}
void VSUB_closefrom(int);
......@@ -47,6 +47,7 @@
#include "vtree.h"
#include "from_varnish/vfil.h"
#include "from_varnish/vsub.h"
#include "vcc_if.h"
......@@ -265,9 +266,6 @@ vdp_init(struct vdp_ctx *ctx, void **priv, struct objcore *objcore)
if (state->chldpid == 0) {
struct setenv_entry *setenv_entry;
closefd(&in[1]);
closefd(&out[0]);
closefd(&err[0]);
if (mk_dup(err[1], STDERR_FILENO) != 0)
exit(EXIT_FAILURE);
if (mk_dup(in[0], STDIN_FILENO) != 0)
......@@ -275,6 +273,8 @@ vdp_init(struct vdp_ctx *ctx, void **priv, struct objcore *objcore)
if (mk_dup(out[1], STDOUT_FILENO) != 0)
exit(EXIT_FAILURE);
VSUB_closefrom(STDERR_FILENO + 1);
if (setenv_head != NULL)
VSTAILQ_FOREACH(setenv_entry, setenv_head, list) {
CHECK_OBJ_NOTNULL(setenv_entry,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment