From patchwork Mon Oct 9 15:57:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Jackson X-Patchwork-Id: 9993793 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 B559360216 for ; Mon, 9 Oct 2017 16:00:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A7D0E285E8 for ; Mon, 9 Oct 2017 16:00:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9CC032879A; Mon, 9 Oct 2017 16:00:16 +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 1437A2870E for ; Mon, 9 Oct 2017 16:00:15 +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 1e1aRC-0003d0-8K; Mon, 09 Oct 2017 15:57:50 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e1aRA-0003ZO-JY for xen-devel@lists.xensource.com; Mon, 09 Oct 2017 15:57:48 +0000 Received: from [85.158.137.68] by server-8.bemta-3.messagelabs.com id AD/F6-02228-B7C9BD95; Mon, 09 Oct 2017 15:57:47 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrJIsWRWlGSWpSXmKPExsXitHRDpG71nNu RBn+28lncm/Ke3YHRY3vfLvYAxijWzLyk/IoE1oyp616wFByQq/j64ShjA2OzZBcjJ4eEgL9E c9s2JgjbQ+L62RNgNpuArkTTlr9sILaIgLLE8aYvrF2MXBzMAucYJVqnbGAGSQgLREp0H9wL1 sAioCLxef1VRhCbV8BT4vrkl4wQQ+Ukzh//CVbPCRRfMGMJWL0Q0LKzHbeAajiAbDWJuevjIV oFJU7OfMICYjMLSEgcfPGCGWKMpcS39U+ZJzDyz0JSNgtJ2QJGplWMGsWpRWWpRbpGpnpJRZn pGSW5iZk5uoYGxnq5qcXFiempOYlJxXrJ+bmbGIHBVs/AwLiDsfWE3yFGSQ4mJVHeeTNuRwrx JeWnVGYkFmfEF5XmpBYfYpTh4FCS4C2bDZQTLEpNT61Iy8wBhj1MWoKDR0mENxEkzVtckJhbn JkOkTrFqMvRcfPuHyYhlrz8vFQpcd5Ts4CKBECKMkrz4EbAYvASo6yUMC8jAwODEE9BalFuZg mq/CtGcQ5GJWHedJBVPJl5JXCbXgEdwQR0BGPxDZAjShIRUlINjIumf/zqoPbjzMoHCi/bq49 xcXtq+UXtOhrkcjQkYI7Y3FKPzkxH5wOLdqRfWMJRxSbJ/PNdYP2K1wWinKu+vzj86uUBppaG gHOLRMTP/npqoVvtmPLVsYAlkePd0S28PziSLFW33mZaYXa89uTnhbyWrz4/+eZww5nnzZsfy w58eWTyf+X+nwuVWIozEg21mIuKEwFD77NxvAIAAA== X-Env-Sender: prvs=4484f2008=Ian.Jackson@citrix.com X-Msg-Ref: server-10.tower-31.messagelabs.com!1507564658!108752994!5 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 43707 invoked from network); 9 Oct 2017 15:57:46 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-10.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 9 Oct 2017 15:57:46 -0000 X-IronPort-AV: E=Sophos;i="5.42,500,1500940800"; d="scan'208";a="443012899" X-CrossPremisesHeadersFilteredBySendConnector: FTLPEX02AMS01.citrite.net From: Ian Jackson To: Date: Mon, 9 Oct 2017 16:57:14 +0100 Message-ID: <1507564648-7580-13-git-send-email-ian.jackson@eu.citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1507564648-7580-1-git-send-email-ian.jackson@eu.citrix.com> References: <1507564648-7580-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 12/26] 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 */ };