From patchwork Wed Oct 4 15:57:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Jackson X-Patchwork-Id: 9985107 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E0C446028E for ; Wed, 4 Oct 2017 16:09:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D3817286B1 for ; Wed, 4 Oct 2017 16:09:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C867A286DE; Wed, 4 Oct 2017 16:09:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6FC95286B1 for ; Wed, 4 Oct 2017 16:09:22 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dzmD4-0003lf-4u; Wed, 04 Oct 2017 16:07:46 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dzmD1-0003fv-PC for xen-devel@lists.xensource.com; Wed, 04 Oct 2017 16:07:43 +0000 Received: from [85.158.139.211] by server-12.bemta-5.messagelabs.com id 1D/A9-30935-F4705D95; Wed, 04 Oct 2017 16:07:43 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrOIsWRWlGSWpSXmKPExsXitHSDva4f+9V IgyNzZC3uTXnP7sDosb1vF3sAYxRrZl5SfkUCa8bUdS9YCg7IVXz9cJSxgbFZsouRk0NCwF9i 5/IWJgjbQ+L62RNgNpuArkTTlr9sILaIgLLE8aYvrF2MXBzMAucYJVqnbGAGSQgLREr0r2pmA bFZBFQktm17D9bAK+AlceLPDUaIoXIS54//BKvnBIo/3dkBZHNwCAl4ShxbawphqknMXR8P0S kocXLmE7CJzAISEgdfvGCGmGIp8W39U+YJjPyzkJTNQlK2gJFpFaN6cWpRWWqRrpFeUlFmekZ JbmJmjq6hgalebmpxcWJ6ak5iUrFecn7uJkZgoDEAwQ7G73+cDjFKcjApifL++nklUogvKT+l MiOxOCO+qDQntfgQowwHh5IEry3b1UghwaLU9NSKtMwcYMjDpCU4eJREeK+wAqV5iwsSc4sz0 yFSpxh1OTpu3v3DJMSSl5+XKiXOOwFkhgBIUUZpHtwIWPxdYpSVEuZlBDpKiKcgtSg3swRV/h WjOAejkjBvFMgUnsy8ErhNr4COYAI6Yk7TFZAjShIRUlINjO0xKyTXGe+3XqdoNMnlxt/jRnv Wzthyc9qWBk4jn5xdRhwVLl5vJQ65i0oa/U3bddfTXqjj8Ix193+8LLg6bfWZqLWdbXfqZYre 23dOE3t58MZCoxqep9adp9OW+XsoKSzQ0T2TV130uOnXrok1Z/h078pzRb/Osn/WVfhXyihxq +NJtlX7BJVYijMSDbWYi4oTAWhviM26AgAA X-Env-Sender: prvs=4438c69fc=Ian.Jackson@citrix.com X-Msg-Ref: server-3.tower-206.messagelabs.com!1507133256!110999150!5 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 17602 invoked from network); 4 Oct 2017 16:07:42 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-3.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 4 Oct 2017 16:07:42 -0000 X-IronPort-AV: E=Sophos;i="5.42,477,1500940800"; d="scan'208";a="450475323" X-CrossPremisesHeadersFilteredBySendConnector: FTLPEX02AMS01.citrite.net From: Ian Jackson To: Date: Wed, 4 Oct 2017 16:57:17 +0100 Message-ID: <1507132650-25376-12-git-send-email-ian.jackson@eu.citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1507132650-25376-1-git-send-email-ian.jackson@eu.citrix.com> References: <1507132650-25376-1-git-send-email-ian.jackson@eu.citrix.com> MIME-Version: 1.0 X-OrganizationHeadersPreserved: FTLPEX02AMS01.citrite.net Cc: Ross Lagerwall , Stefano Stabellini , Wei Liu , Ian Jackson Subject: [Xen-devel] [PATCH 11/24] xentoolcore_restrict: Break out xentoolcore__restrict_by_dup2_null X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Ian Jackson Acked-by: Wei Liu --- tools/libs/call/core.c | 30 +--------------------- tools/libs/toolcore/handlereg.c | 26 +++++++++++++++++++ tools/libs/toolcore/include/xentoolcore_internal.h | 12 +++++++++ tools/libs/toolcore/libxentoolcore.map | 1 + 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/tools/libs/call/core.c b/tools/libs/call/core.c index 8d1b11b..d6ce73d 100644 --- a/tools/libs/call/core.c +++ b/tools/libs/call/core.c @@ -15,39 +15,11 @@ #include -#include -#include -#include -#include - #include "private.h" static int all_restrict_cb(Xentoolcore__Active_Handle *ah, uint32_t domid) { xencall_handle *xcall = CONTAINER_OF(ah, *xcall, tc_ah); - int nullfd = -1, r; - - if (xcall->fd < 0) - /* just in case */ - return 0; - - /* - * We don't implement a restrict function. We neuter the fd by - * dup'ing /dev/null onto it. This is better than closing it, - * because it does not involve locking against concurrent uses - * of xencall in other threads. - */ - nullfd = open("/dev/null", O_RDONLY); - if (nullfd < 0) goto err; - - r = dup2(nullfd, xcall->fd); - if (r < 0) goto err; - - close(nullfd); - return 0; - -err: - if (nullfd >= 0) close(nullfd); - return -1; + return xentoolcore__restrict_by_dup2_null(xcall->fd); } xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags) diff --git a/tools/libs/toolcore/handlereg.c b/tools/libs/toolcore/handlereg.c index efeffb1..1252bed 100644 --- a/tools/libs/toolcore/handlereg.c +++ b/tools/libs/toolcore/handlereg.c @@ -22,6 +22,11 @@ #include "xentoolcore_internal.h" +#include +#include +#include +#include + #include #include @@ -66,6 +71,27 @@ int xentoolcore_restrict_all(uint32_t domid) { return r; } +int xentoolcore__restrict_by_dup2_null(int fd) { + int nullfd = -1, r; + + if (fd < 0) + /* just in case */ + return 0; + + nullfd = open("/dev/null", O_RDONLY); + if (nullfd < 0) goto err; + + r = dup2(nullfd, fd); + if (r < 0) goto err; + + close(nullfd); + return 0; + +err: + if (nullfd >= 0) close(nullfd); + return -1; +} + /* * Local variables: * mode: C diff --git a/tools/libs/toolcore/include/xentoolcore_internal.h b/tools/libs/toolcore/include/xentoolcore_internal.h index 27497d6..7e96a48 100644 --- a/tools/libs/toolcore/include/xentoolcore_internal.h +++ b/tools/libs/toolcore/include/xentoolcore_internal.h @@ -91,6 +91,18 @@ struct Xentoolcore__Active_Handle { void xentoolcore__register_active_handle(Xentoolcore__Active_Handle*); void xentoolcore__deregister_active_handle(Xentoolcore__Active_Handle*); +/* + * Utility function for use in restrict_callback in libraries whose + * handles don't have a useful restrict function. We neuter the fd by + * dup'ing /dev/null onto it. This is better than closing it, because + * it does not involve locking against concurrent uses of in other + * threads. + * + * Returns the value that restrict_callback should return. + * fd may be < 0. + */ +int xentoolcore__restrict_by_dup2_null(int fd); + /* ---------- convenient stuff ---------- */ /* diff --git a/tools/libs/toolcore/libxentoolcore.map b/tools/libs/toolcore/libxentoolcore.map index eb5d251..0b7d925 100644 --- a/tools/libs/toolcore/libxentoolcore.map +++ b/tools/libs/toolcore/libxentoolcore.map @@ -3,5 +3,6 @@ VERS_1.0 { xentoolcore_restrict_all; xentoolcore__register_active_handle; xentoolcore__deregister_active_handle; + xentoolcore__restrict_by_dup2_null; local: *; /* Do not expose anything by default */ };