From patchwork Sat Mar 25 01:21:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 9644333 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 7D68260327 for ; Sat, 25 Mar 2017 01:24:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 64B5F269A3 for ; Sat, 25 Mar 2017 01:24:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 592E226E4D; Sat, 25 Mar 2017 01:24:47 +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 A0C57269A3 for ; Sat, 25 Mar 2017 01:24:46 +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 1craOx-0007N4-Uu; Sat, 25 Mar 2017 01:21:55 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1craOx-0007My-Fk for xen-devel@lists.xenproject.org; Sat, 25 Mar 2017 01:21:55 +0000 Received: from [193.109.254.147] by server-2.bemta-6.messagelabs.com id C7/6C-19731-236C5D85; Sat, 25 Mar 2017 01:21:54 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrMIsWRWlGSWpSXmKPExsVybKJsh67Rsas RBv83cll83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBnty/+wFBx3qTjR38/WwNhn3sXIxSEkMJVR Ys3eRywQzjYmiaOf9rF3MXJysAhoS1w+2ckKYrMJGEr8fbKJrYuRg0MCyF7ymQMkLCKgJXFk3 RImkF5mgTmMEq3HLrCBJIQFvCXmdjwB6+UUcJO4uf4tM4jNK+AlMe/8b7D5QgKuEnv63zOC2K ICuhKH/v1hg6gRlDg58wkLiM0MtGD59G1gtoRAhsS8njmsELaXxKIbl6BsNYmr5zYxT2AUnIW kfRaS9gWMTKsYNYpTi8pSi3QNLfWSijLTM0pyEzNzdA0NzPRyU4uLE9NTcxKTivWS83M3MQID lAEIdjD+WBZwiFGSg0lJlPdMy9UIIb6k/JTKjMTijPii0pzU4kOMMhwcShK8748A5QSLUtNTK 9Iyc4CxApOW4OBREuHVOQqU5i0uSMwtzkyHSJ1i1OWYM3v3GyYhlrz8vFQpcd6/IDMEQIoySv PgRsDi9hKjrJQwLyPQUUI8BalFuZklqPKvGMU5GJWEea+CTOHJzCuB2/QK6AgmoCNmb7gCckR JIkJKqoFxgqSjcMU3ceGJjjN3bHs7d4+/0CNj9k0fXjBwir9+cWX6snShoKRDlk+Y61ZKyO3e 1n7jsNq7OT4rKlNK66as+bdui9TUubIXkswP5C9M8pguaJDNqfni8KSfKz8Kt+4JStkl/Wf19 ngeRk++y7tjItfmWTJzOieG9b7e37x1M9dL/933NnZKK7EUZyQaajEXFScCAKbBGfzWAgAA X-Env-Sender: sstabellini@kernel.org X-Msg-Ref: server-10.tower-27.messagelabs.com!1490404912!70400297!1 X-Originating-IP: [198.145.29.136] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 52034 invoked from network); 25 Mar 2017 01:21:53 -0000 Received: from mail.kernel.org (HELO mail.kernel.org) (198.145.29.136) by server-10.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 25 Mar 2017 01:21:53 -0000 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CC4A42024D; Sat, 25 Mar 2017 01:21:50 +0000 (UTC) Received: from sstabellini-ThinkPad-X260.attlocal.net (104-6-25-238.lightspeed.sntcca.sbcglobal.net [104.6.25.238]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F252720225; Sat, 25 Mar 2017 01:21:48 +0000 (UTC) Date: Fri, 24 Mar 2017 18:21:48 -0700 (PDT) From: Stefano Stabellini X-X-Sender: sstabellini@sstabellini-ThinkPad-X260 To: Paul Durrant In-Reply-To: <1490374688-488-1-git-send-email-paul.durrant@citrix.com> Message-ID: References: <1490374688-488-1-git-send-email-paul.durrant@citrix.com> User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Cc: Anthony Perard , xen-devel@lists.xenproject.org, Stefano Stabellini , qemu-devel@nongnu.org Subject: Re: [Xen-devel] [PATCH] xen: additionally restrict xenforeignmemory operations 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 On Fri, 24 Mar 2017, Paul Durrant wrote: > Commit f0f272baf3a7 "xen: use libxendevice model to restrict operations" > added a command-line option (-xen-domid-restrict) to limit operations > using the libxendevicemodel API to a specified domid. The commit also > noted that the restriction would be extended to cover operations issued > via other xen libraries by subsequent patches. > > My recent Xen patch [1] added a call to the xenforeignmemory API to allow > it to be restricted. This patch now makes use of that new call when the > -xen-domid-restrict option is passed. > > [1] http://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=5823d6eb > > Signed-off-by: Paul Durrant > --- > Cc: Stefano Stabellini > Cc: Anthony Perard > --- > include/hw/xen/xen_common.h | 52 +++++++++++++++++++++++++++++++++------------ > 1 file changed, 39 insertions(+), 13 deletions(-) > > diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h > index 4f3bd35..6f13e8c 100644 > --- a/include/hw/xen/xen_common.h > +++ b/include/hw/xen/xen_common.h > @@ -213,19 +213,6 @@ static inline int xen_modified_memory(domid_t domid, uint64_t first_pfn, > return xendevicemodel_modified_memory(xen_dmod, domid, first_pfn, nr); > } > > -static inline int xen_restrict(domid_t domid) > -{ > - int rc = xendevicemodel_restrict(xen_dmod, domid); > - > - trace_xen_domid_restrict(errno); > - > - if (errno == ENOTTY) { > - return 0; > - } > - > - return rc; > -} > - > /* Xen 4.2 through 4.6 */ > #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 40701 > > @@ -276,8 +263,47 @@ static inline void *xenforeignmemory_map(xc_interface *h, uint32_t dom, > > #endif > > +#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 40900 > + > +static inline int xenforeignmemory_restrict( > + xenforeignmemory_handle *fmem, domid_t domid) > +{ > + errno = ENOTTY; > + return -1; > +} > + > +#endif > + > extern xenforeignmemory_handle *xen_fmem; > > +static inline int xen_restrict(domid_t domid) > +{ > + int rc; > + > + /* Attempt to restrict devicemodel operations */ > + rc = xendevicemodel_restrict(xen_dmod, domid); > + trace_xen_domid_restrict(rc ? errno : 0); > + > + if (rc < 0) { > + /* > + * If errno is ENOTTY then restriction is not implemented so > + * there's no point in trying to restrict other types of > + * operation, but it should not be treated as a failure. > + */ > + if (errno == ENOTTY) { > + return 0; > + } > + > + return rc; > + } > + > + /* Restrict foreignmemory operations */ > + rc = xenforeignmemory_restrict(xen_fmem, domid); > + trace_xen_domid_restrict(rc ? errno : 0); > + > + return rc; > +} > + > void destroy_hvm_domain(bool reboot); > > /* shutdown/destroy current domain because of an error */ This is OK but the file is growing too entangled. What do you think of the following, which moves the if CONFIG_XEN_CTRL_INTERFACE_VERSION < 40701 at the top? This way we don't have to add yet another ifdef. diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h index 4f3bd35..624fb86 100644 --- a/include/hw/xen/xen_common.h +++ b/include/hw/xen/xen_common.h @@ -27,6 +27,58 @@ extern xc_interface *xen_xc; * We don't support Xen prior to 4.2.0. */ +/* Xen 4.2 through 4.6 */ +#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 40701 + +typedef xc_interface xenforeignmemory_handle; +typedef xc_evtchn xenevtchn_handle; +typedef xc_gnttab xengnttab_handle; + +#define xenevtchn_open(l, f) xc_evtchn_open(l, f); +#define xenevtchn_close(h) xc_evtchn_close(h) +#define xenevtchn_fd(h) xc_evtchn_fd(h) +#define xenevtchn_pending(h) xc_evtchn_pending(h) +#define xenevtchn_notify(h, p) xc_evtchn_notify(h, p) +#define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(h, d, p) +#define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p) +#define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p) + +#define xengnttab_open(l, f) xc_gnttab_open(l, f) +#define xengnttab_close(h) xc_gnttab_close(h) +#define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(h, n) +#define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(h, d, r, p) +#define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n) +#define xengnttab_map_grant_refs(h, c, d, r, p) \ + xc_gnttab_map_grant_refs(h, c, d, r, p) +#define xengnttab_map_domain_grant_refs(h, c, d, r, p) \ + xc_gnttab_map_domain_grant_refs(h, c, d, r, p) + +#define xenforeignmemory_open(l, f) xen_xc +#define xenforeignmemory_close(h) + +static inline void *xenforeignmemory_map(xc_interface *h, uint32_t dom, + int prot, size_t pages, + const xen_pfn_t arr[/*pages*/], + int err[/*pages*/]) +{ + if (err) + return xc_map_foreign_bulk(h, dom, prot, arr, err, pages); + else + return xc_map_foreign_pages(h, dom, prot, arr, pages); +} + +#define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE) + +#else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 40701 */ + +#include +#include +#include + +#endif + +extern xenforeignmemory_handle *xen_fmem; + #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 40900 typedef xc_interface xendevicemodel_handle; @@ -159,6 +211,13 @@ static inline int xendevicemodel_restrict( return -1; } +static inline int xenforeignmemory_restrict( + xenforeignmemory_handle *fmem, domid_t domid) +{ + errno = ENOTTY; + return -1; +} + #else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 40900 */ #include @@ -215,69 +274,32 @@ static inline int xen_modified_memory(domid_t domid, uint64_t first_pfn, static inline int xen_restrict(domid_t domid) { - int rc = xendevicemodel_restrict(xen_dmod, domid); + int rc; - trace_xen_domid_restrict(errno); + /* Attempt to restrict devicemodel operations */ + rc = xendevicemodel_restrict(xen_dmod, domid); + trace_xen_domid_restrict(rc ? errno : 0); - if (errno == ENOTTY) { - return 0; + if (rc < 0) { + /* + * If errno is ENOTTY then restriction is not implemented so + * there's no point in trying to restrict other types of + * operation, but it should not be treated as a failure. + */ + if (errno == ENOTTY) { + return 0; + } + + return rc; } - return rc; -} - -/* Xen 4.2 through 4.6 */ -#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 40701 - -typedef xc_interface xenforeignmemory_handle; -typedef xc_evtchn xenevtchn_handle; -typedef xc_gnttab xengnttab_handle; - -#define xenevtchn_open(l, f) xc_evtchn_open(l, f); -#define xenevtchn_close(h) xc_evtchn_close(h) -#define xenevtchn_fd(h) xc_evtchn_fd(h) -#define xenevtchn_pending(h) xc_evtchn_pending(h) -#define xenevtchn_notify(h, p) xc_evtchn_notify(h, p) -#define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(h, d, p) -#define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p) -#define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p) - -#define xengnttab_open(l, f) xc_gnttab_open(l, f) -#define xengnttab_close(h) xc_gnttab_close(h) -#define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(h, n) -#define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(h, d, r, p) -#define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n) -#define xengnttab_map_grant_refs(h, c, d, r, p) \ - xc_gnttab_map_grant_refs(h, c, d, r, p) -#define xengnttab_map_domain_grant_refs(h, c, d, r, p) \ - xc_gnttab_map_domain_grant_refs(h, c, d, r, p) - -#define xenforeignmemory_open(l, f) xen_xc -#define xenforeignmemory_close(h) + /* Restrict foreignmemory operations */ + rc = xenforeignmemory_restrict(xen_fmem, domid); + trace_xen_domid_restrict(rc ? errno : 0); -static inline void *xenforeignmemory_map(xc_interface *h, uint32_t dom, - int prot, size_t pages, - const xen_pfn_t arr[/*pages*/], - int err[/*pages*/]) -{ - if (err) - return xc_map_foreign_bulk(h, dom, prot, arr, err, pages); - else - return xc_map_foreign_pages(h, dom, prot, arr, pages); + return rc; } -#define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE) - -#else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 40701 */ - -#include -#include -#include - -#endif - -extern xenforeignmemory_handle *xen_fmem; - void destroy_hvm_domain(bool reboot); /* shutdown/destroy current domain because of an error */