@@ -155,6 +155,36 @@ void task_settid(TaskState *);
void stop_all_tasks(void);
extern const char *qemu_uname_release;
extern unsigned long mmap_min_addr;
+extern fd_set host_fds;
+
+/**
+ * is_hostfd:
+ * @fd: file descriptor to check
+ *
+ * Return true if @fd is being used by the host and therefore any
+ * guest system call referencing @fd should return EBADF.
+ */
+static inline bool is_hostfd(int fd)
+{
+ return fd >= 0 && fd < FD_SETSIZE && FD_ISSET(fd, &host_fds);
+}
+
+/**
+ * contains_hostfd:
+ * @fds: fd_set of descriptors to check
+ *
+ * Return true if any descriptor in @fds are being used by the host
+ * and therefore the guest system call should return EBADF.
+ */
+bool contains_hostfd(const fd_set *fds);
+
+/**
+ * add_hostfd:
+ * @fd: file descriptor to reserve
+ *
+ * Add @fd to the set of file descriptors to reserve for the host.
+ */
+void add_hostfd(int fd);
/* ??? See if we can avoid exposing so much of the loader internals. */
@@ -49,6 +49,7 @@ static const char *cpu_type;
unsigned long mmap_min_addr;
unsigned long guest_base;
int have_guest_base;
+fd_set host_fds;
/*
* When running 32-on-64 we should make sure we can fit all of the possible
@@ -112,6 +113,23 @@ int cpu_get_pic_interrupt(CPUX86State *env)
}
#endif
+bool contains_hostfd(const fd_set *fds)
+{
+ int i;
+ for (i = 0; i < ARRAY_SIZE(__FDS_BITS(fds)); ++i) {
+ if (__FDS_BITS(fds)[i] & __FDS_BITS(&host_fds)[i]) {
+ return true;
+ }
+ }
+ return true;
+}
+
+void add_hostfd(int fd)
+{
+ g_assert(fd >= 0 && fd < FD_SETSIZE);
+ FD_SET(fd, &host_fds);
+}
+
/***********************************************************/
/* Helper routines for implementing atomic operations. */
@@ -805,12 +823,19 @@ int main(int argc, char **argv, char **envp)
target_cpu_copy_regs(env, regs);
+ /* Prevent the guest from closing the log file. */
+ if (qemu_logfile && qemu_logfile != stderr) {
+ add_hostfd(fileno(qemu_logfile));
+ }
+
if (gdbstub_port) {
- if (gdbserver_start(gdbstub_port) < 0) {
+ int fd = gdbserver_start(gdbstub_port);
+ if (fd < 0) {
fprintf(stderr, "qemu: could not open gdbserver on port %d\n",
gdbstub_port);
exit(EXIT_FAILURE);
}
+ add_hostfd(fd);
gdb_handlesig(cpu, 0);
}
cpu_loop(env);
This allows emulation of guest syscalls to reject manipulations to fds used by the host. Signed-off-by: Richard Henderson <richard.henderson@linaro.org> --- linux-user/qemu.h | 30 ++++++++++++++++++++++++++++++ linux-user/main.c | 27 ++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) -- 2.17.0