From nobody Tue Feb 10 13:17:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 152780707722168.4513739741758; Thu, 31 May 2018 15:51:17 -0700 (PDT) Received: from localhost ([::1]:46502 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOWPY-0006Mf-TY for importer@patchew.org; Thu, 31 May 2018 18:51:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38492) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOWNi-00056K-O0 for qemu-devel@nongnu.org; Thu, 31 May 2018 18:49:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fOWNh-0000lA-O2 for qemu-devel@nongnu.org; Thu, 31 May 2018 18:49:18 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:37862) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fOWNh-0000ke-Hp for qemu-devel@nongnu.org; Thu, 31 May 2018 18:49:17 -0400 Received: by mail-pf0-x243.google.com with SMTP id e9-v6so11490070pfi.4 for ; Thu, 31 May 2018 15:49:17 -0700 (PDT) Received: from cloudburst.twiddle.net (97-126-112-211.tukw.qwest.net. [97.126.112.211]) by smtp.gmail.com with ESMTPSA id t3-v6sm33385584pgs.91.2018.05.31.15.49.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 May 2018 15:49:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4kx9iWqrZaAigsaP3mi/Tl0rT4kzKW4FmGtRPBRUajU=; b=IHPHY/PnNxiLGBmD/SOzbwnkkztdegq25rSlB7OPUunzCK6SbcTfqBBM66R4+/3pdY q2202u4GyvLsds4bLhwObFfgA0cHNBP8/xPjjp5fA+MsIfY6UlJfMhMHQqUSeDDaIN2b XkK/t3gK72wOX8Q72u3+o7CQoKNmBhZPvVPrk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4kx9iWqrZaAigsaP3mi/Tl0rT4kzKW4FmGtRPBRUajU=; b=nui0YBdu4vQKIfJ4EYvRMqAV3S1DBIKEntiW9RRjzYoBGKTZG/SAZBmmDHhFCNfCUf jR4n/D9AW50yiFhg9MVRLJYyHKwTDS3m/rRmQARLYo9f3+t396Rf4ZdmaptyhpflYCy3 4eQC2Vi+qMTY6fHUHo7Acrecc14pGSfAYSn1yEsp7OwnTw56X1A5JFnKYuoZ1UXfz5Yj AhUnv0uJ7J3MAU49dHeGH+jE7FoaDyCOFSOLxPjABTfy7cP8lpEktCiCv5NQ+CNaJM+R t+Xn04VJ/01KOCwte3aes2SHrB+vFqNkOXRK6VPGQbfTlHPCtsWmo37AyIUEiJ7a8H9H Jnog== X-Gm-Message-State: ALKqPwfqq35wazyVInARj3bei/QR4W6GuwCLOTEvjLDRvNkkKUArVwpO zsXdIDm1ouMY0N882hhEgsPvWy4kM5c= X-Google-Smtp-Source: ADUXVKLC67cbojy9eXH/cRd5t/t+H7Uct5AFMTsE2WbFg5XvsIySNGJ/3AA6ClGwbm75AzB2Xzgthw== X-Received: by 2002:a62:1411:: with SMTP id 17-v6mr8512872pfu.3.1527806956101; Thu, 31 May 2018 15:49:16 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 31 May 2018 15:49:07 -0700 Message-Id: <20180531224911.23725-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180531224911.23725-1-richard.henderson@linaro.org> References: <20180531224911.23725-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH 2/6] linux-user: Add host_fds and manipulators X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, laurent@vivier.eu, evgreen@chromium.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This allows emulation of guest syscalls to reject manipulations to fds used by the host. Signed-off-by: Richard Henderson Reviewed-by: Laurent Vivier --- 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 >=3D 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); =20 /* ??? See if we can avoid exposing so much of the loader internals. */ =20 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; =20 /* * 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 =20 +bool contains_hostfd(const fd_set *fds) +{ + int i; + for (i =3D 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 >=3D 0 && fd < FD_SETSIZE); + FD_SET(fd, &host_fds); +} + /***********************************************************/ /* Helper routines for implementing atomic operations. */ =20 @@ -805,12 +823,19 @@ int main(int argc, char **argv, char **envp) =20 target_cpu_copy_regs(env, regs); =20 + /* Prevent the guest from closing the log file. */ + if (qemu_logfile && qemu_logfile !=3D stderr) { + add_hostfd(fileno(qemu_logfile)); + } + if (gdbstub_port) { - if (gdbserver_start(gdbstub_port) < 0) { + int fd =3D 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); --=20 2.17.0