diff options
author | Ian Campbell <ian.campbell@citrix.com> | 2011-05-24 16:18:19 +0100 |
---|---|---|
committer | Ian Campbell <ian.campbell@citrix.com> | 2011-05-24 16:18:19 +0100 |
commit | bdf5d459953372a6e0347258ebc15ec58af0bf7d (patch) | |
tree | b3cb51d950f3c4c2dcf94d0ff99f3be76a329f51 /tools/libxl/libxl_bootloader.c | |
parent | 306fa5ad641e49f1232d3ff2d224995e2ecb237f (diff) | |
download | xen-bdf5d459953372a6e0347258ebc15ec58af0bf7d.tar.gz xen-bdf5d459953372a6e0347258ebc15ec58af0bf7d.tar.bz2 xen-bdf5d459953372a6e0347258ebc15ec58af0bf7d.zip |
libxl: don't close file descriptors 4..255 in libxl__exec
It prevents callers from deliberately passing file descriptors to the child,
hides any callers who erroneously do so and doesn't deal with all file
descriptors in any case.
Rather than remove it all together replace it with some debug code
which checks for open file handles which do not have either O_CLOEXEC
or FD_CLOEXEC set. To enable the debug set _LIBXL_DEBUG_EXEC_FDS=1 to
print any open and non-CLOEXEC non-stdio FDs just before libxl__exec
actualy calls exec. Set _LIBXL_DEBUG_EXEC_FDS=2 to abort if any of
these exist.
On the basis of this debugging fix some leaked filehandles:
* The read end of the pipe used to wake the parent from the
intermediate process during libxl__spawn_spawn was leaked into the
intermediate process.
* The file descriptor representing the xl lock was not marked
O_CLOEXEC (the lock itself is already specified to not be
inherited across a fork).
* The file descriptors passed to libxl__exec to be dup'd as
std{in,out,err} were leaked at their original number. They are
harmless (an attacker can just as easily use fd 0..2) but close
anyway since it removes a case which a person evaluating open fd's
needs to consider.
* libxl_run_bootloader was leaking the xenconsole pty master into
the bootloader child process.
* If the bootloader fails to get as far as opening its end of the
FIFO then we can also hang, check that the process has not exited
as part of that loop. (we actually block opening the FIFO too so
this is only a partial fix for the case where the bootlader has
crashed quickly).
With these fixes I have tested that device models, bootloaders
(pygrub) and vncviewers which are spawned via libxl__exec with no
unexpected file descriptors open, at least in my configuration.
Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
Diffstat (limited to 'tools/libxl/libxl_bootloader.c')
-rw-r--r-- | tools/libxl/libxl_bootloader.c | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/tools/libxl/libxl_bootloader.c b/tools/libxl/libxl_bootloader.c index 3eb0343bf9..0d0b8d7652 100644 --- a/tools/libxl/libxl_bootloader.c +++ b/tools/libxl/libxl_bootloader.c @@ -115,6 +115,7 @@ static int open_xenconsoled_pty(int *master, int *slave, char *slave_path, size_ #endif fcntl(*master, F_SETFL, O_NDELAY); + fcntl(*master, F_SETFD, FD_CLOEXEC); return 0; } @@ -393,6 +394,9 @@ int libxl_run_bootloader(libxl_ctx *ctx, } while (1) { + if (waitpid(pid, &blrc, WNOHANG) == pid) + goto out_close; + fifo_fd = open(fifo, O_RDONLY); if (fifo_fd > -1) break; |