From patchwork Thu May 31 22:49:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 137446 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp209397lji; Thu, 31 May 2018 15:51:57 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJSeD6IM3/z68yf6zFfMiR7mVeZYqKpq3H2YawyDQu0+zUHZIW2GLQ+cf5AaBIQMOhWIz2G X-Received: by 2002:ac8:65c6:: with SMTP id t6-v6mr8884075qto.347.1527807117076; Thu, 31 May 2018 15:51:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527807117; cv=none; d=google.com; s=arc-20160816; b=m/FAiYlJJBtFabHcl5NqYZsbTiOPTFy4NZYPcZ6/m4WudPbouFEJ8TYPethYoc5kPO XFzZsBXE9sTHzCk0Z2+BgYrEpMPl+h/sNsJGGfY+GOOQ5YSoWRh2Yuw1hGlqVF7x29x+ MmEorcuVgYN2s9T0G8RYueWjNCmfwRf4VCf32xrK0s1CNPgm4HOOITxJGZtfP4LRX3ci NBeFwp2Jd9+7ZxrpXWnOYr8HCzxoRQlJaN57/g5Gj/5+yAglYYDt6VF+A3kGmEDW3ln8 bsqEmhoiaWzfMzTYUY0rX+s9+jIlGASrc9UavTRmE15rE3C7DOFoUAB1qKkYOS73H3lX dlbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=4kx9iWqrZaAigsaP3mi/Tl0rT4kzKW4FmGtRPBRUajU=; b=U/rmskKO74jd7zanND/6+PJ4XtDOMuKJ/pvmKdX2MDHrYRGLYLkPzkntDdMBObImY8 8jA4EfLQOA8AjI48IJeBG18qQ164byJV5E/AokVu2NKb2uz9M7HRAyGf6oPodwOjL/8c mf7u9s2JJX1dNd7AEMumCDEe7lKh9NwVOsAsBaAfj9QUkieHvwLG/iUdXvJau7gKoorq BCnM7se04hTq/A5waYZAqLtrpxjNIVXgHmPTeixNc8vTpm4M2tckRyK70tEGOp6NWPHb V+c/dyXLW5y8odJ43Z2E9MjReECoDrChxhuy+n4GX3S2jZeIWXqQtntmtmeq4eTMYMfh 3bwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=IHPHY/Pn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id e10-v6si5919676qvm.131.2018.05.31.15.51.56 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 31 May 2018 15:51:57 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=IHPHY/Pn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:46508 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOWQG-0007Tp-He for patch@linaro.org; Thu, 31 May 2018 18:51:56 -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) 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-Received: by 2002:a62:1411:: with SMTP id 17-v6mr8512872pfu.3.1527806956101; Thu, 31 May 2018 15:49:16 -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) 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This allows emulation of guest syscalls to reject manipulations to fds used by the host. Signed-off-by: Richard Henderson --- linux-user/qemu.h | 30 ++++++++++++++++++++++++++++++ linux-user/main.c | 27 ++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) -- 2.17.0 Reviewed-by: Laurent Vivier 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);