qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, laurent@vivier.eu, evgreen@chromium.org
Subject: [Qemu-devel] [PATCH 2/6] linux-user: Add host_fds and manipulators
Date: Thu, 31 May 2018 15:49:07 -0700	[thread overview]
Message-ID: <20180531224911.23725-3-richard.henderson@linaro.org> (raw)
In-Reply-To: <20180531224911.23725-1-richard.henderson@linaro.org>

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(-)

diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index c55c8e294b..33dafbe0e4 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -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.  */
 
diff --git a/linux-user/main.c b/linux-user/main.c
index 78d6d3e7eb..ee3f323c08 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -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);
-- 
2.17.0

  parent reply	other threads:[~2018-05-31 22:49 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-31 22:49 [Qemu-devel] [PATCH v5 0/6] linux-user: Reorg interp_prefix handling Richard Henderson
2018-05-31 22:49 ` [Qemu-devel] [PATCH 1/6] gdbstub: Return the fd from gdbserver_start Richard Henderson
2018-05-31 23:15   ` Philippe Mathieu-Daudé
2018-06-01  0:16     ` Richard Henderson
2018-06-01 12:42       ` Philippe Mathieu-Daudé
2018-06-01  8:59   ` Peter Maydell
2018-06-01 16:42     ` Richard Henderson
2018-06-01 20:00   ` Laurent Vivier
2018-05-31 22:49 ` Richard Henderson [this message]
2018-06-01 20:05   ` [Qemu-devel] [PATCH 2/6] linux-user: Add host_fds and manipulators Laurent Vivier
2018-05-31 22:49 ` [Qemu-devel] [PATCH 3/6] linux-user: Check is_hostfd in do_syscall Richard Henderson
2018-06-01 20:52   ` Laurent Vivier
2018-05-31 22:49 ` [Qemu-devel] [PATCH 4/6] linux-user: Check contains_hostfd in select syscalls Richard Henderson
2018-06-01 20:54   ` Laurent Vivier
2018-05-31 22:49 ` [Qemu-devel] [PATCH 5/6] linux-user: Check is_hostfd in mmap syscalls Richard Henderson
2018-06-01 20:57   ` Laurent Vivier
2018-05-31 22:49 ` [Qemu-devel] [PATCH 6/6] linux-user: Use *at functions to implement interp_prefix Richard Henderson
2018-06-04  1:04   ` Laurent Vivier
2018-06-05  5:27     ` Richard Henderson
2018-06-05  6:33       ` Laurent Vivier
2018-06-05 14:18         ` Richard Henderson
2018-06-05 14:27           ` Laurent Vivier
2018-06-05 10:52       ` Peter Maydell
2018-06-05 12:05         ` Laurent Vivier
2018-06-05 12:14           ` Peter Maydell
2018-06-05 12:23             ` Laurent Vivier
2018-06-05 12:27               ` Daniel P. Berrangé
2018-06-05 12:33               ` Peter Maydell
2018-06-05 12:37                 ` Laurent Vivier
2018-06-05 13:45         ` Richard Henderson
2018-06-05 14:14           ` Peter Maydell
2018-06-07  8:01       ` Laurent Vivier
2018-06-07 16:43         ` Richard Henderson
2018-06-15 22:51           ` Evan Green
2018-05-31 23:01 ` [Qemu-devel] [PATCH v5 0/6] linux-user: Reorg interp_prefix handling no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180531224911.23725-3-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=evgreen@chromium.org \
    --cc=laurent@vivier.eu \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).