From: "Jan Beulich" Subject: [PATCH] domctl: relax getdomaininfo permissions Date: Thu, 04 Aug 2016 02:41:19 -0600 Message-ID: <57A31BCF0200007800102980@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartDDEB25BF.3__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bVEDT-0006D7-LG for xen-devel@lists.xenproject.org; Thu, 04 Aug 2016 08:41:23 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , dgdegra@tycho.nsa.gov List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartDDEB25BF.3__= Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Content-Disposition: inline Qemu needs access to this for the domain it controls, both due to it being used by xc_domain_memory_mapping() (which qemu calls) and the explicit use in hw/xenpv/xen_domainbuild.c:xen_domain_poll(). This at once avoids a for_each_domain() loop when the ID of an existing domain gets passed in. Reported-by: Marek Marczykowski-Górecki Signed-off-by: Jan Beulich --- I know there had been an alternative patch suggestion, but that one doesn't seem have seen a formal submission so far, so here is my original proposal. I wonder what good the duplication of the returned domain ID does: I'm tempted to remove the one in the command-specific structure. Does anyone have insight into why it was done that way? I further wonder why we have XSM_OTHER: The respective conversion into other XSM_* values in xsm/dummy.h could as well move into the callers, making intentions more obvious when looking at the actual code. --- a/tools/flask/policy/modules/xen.if +++ b/tools/flask/policy/modules/xen.if @@ -149,7 +149,7 @@ define(`device_model', ` create_channel($2, $1, $2_channel) allow $1 $2_channel:event create; - allow $1 $2_target:domain shutdown; + allow $1 $2_target:domain { getdomaininfo shutdown }; allow $1 $2_target:mmu { map_read map_write adjust physmap target_hack }; allow $1 $2_target:hvm { getparam setparam trackdirtyvram hvmctl irqlevel pciroute pcilevel cacheattr send_irq }; ') --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -396,14 +396,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe switch ( op->cmd ) { case XEN_DOMCTL_createdomain: - case XEN_DOMCTL_getdomaininfo: case XEN_DOMCTL_test_assign_device: case XEN_DOMCTL_gdbsx_guestmemio: d = NULL; break; default: d = rcu_lock_domain_by_id(op->domain); - if ( d == NULL ) + if ( !d && op->cmd != XEN_DOMCTL_getdomaininfo ) return -ESRCH; } @@ -817,14 +816,22 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe case XEN_DOMCTL_getdomaininfo: { - domid_t dom = op->domain; - - rcu_read_lock(&domlist_read_lock); + domid_t dom = DOMID_INVALID; - for_each_domain ( d ) - if ( d->domain_id >= dom ) + if ( !d ) + { + ret = -EINVAL; + if ( op->domain >= DOMID_FIRST_RESERVED ) break; + rcu_read_lock(&domlist_read_lock); + + dom = op->domain; + for_each_domain ( d ) + if ( d->domain_id >= dom ) + break; + } + ret = -ESRCH; if ( d == NULL ) goto getdomaininfo_out; @@ -839,6 +846,9 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe copyback = 1; getdomaininfo_out: + if ( dom == DOMID_INVALID ) + break; + rcu_read_unlock(&domlist_read_lock); d = NULL; break; --- a/xen/include/xsm/dummy.h +++ b/xen/include/xsm/dummy.h @@ -61,7 +61,12 @@ static always_inline int xsm_default_act return 0; case XSM_TARGET: if ( src == target ) + { return 0; + case XSM_XS_PRIV: + if ( src->is_xenstore ) + return 0; + } /* fall through */ case XSM_DM_PRIV: if ( target && src->target == target ) @@ -71,10 +76,6 @@ static always_inline int xsm_default_act if ( src->is_privileged ) return 0; return -EPERM; - case XSM_XS_PRIV: - if ( src->is_xenstore || src->is_privileged ) - return 0; - return -EPERM; default: LINKER_BUG_ON(1); return -EPERM; --=__PartDDEB25BF.3__= Content-Type: text/plain; name="domctl-relax-getdomaininfo.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="domctl-relax-getdomaininfo.patch" domctl: relax getdomaininfo permissions=0A=0AQemu needs access to this for = the domain it controls, both due to it=0Abeing used by xc_domain_memory_map= ping() (which qemu calls) and the=0Aexplicit use in hw/xenpv/xen_domainbuil= d.c:xen_domain_poll().=0A=0AThis at once avoids a for_each_domain() loop = when the ID of an=0Aexisting domain gets passed in.=0A=0AReported-by: = Marek Marczykowski-G=C3=B3recki =0ASigned-= off-by: Jan Beulich =0A---=0AI know there had been an = alternative patch suggestion, but that one=0Adoesn't seem have seen a = formal submission so far, so here is my=0Aoriginal proposal.=0A=0AI wonder = what good the duplication of the returned domain ID does: I'm=0Atempted to = remove the one in the command-specific structure. Does=0Aanyone have = insight into why it was done that way?=0A=0AI further wonder why we have = XSM_OTHER: The respective conversion into=0Aother XSM_* values in = xsm/dummy.h could as well move into the callers,=0Amaking intentions more = obvious when looking at the actual code.=0A=0A--- a/tools/flask/policy/modu= les/xen.if=0A+++ b/tools/flask/policy/modules/xen.if=0A@@ -149,7 +149,7 @@ = define(`device_model', `=0A create_channel($2, $1, $2_channel)=0A = allow $1 $2_channel:event create;=0A =0A- allow $1 $2_target:domain = shutdown;=0A+ allow $1 $2_target:domain { getdomaininfo shutdown };=0A = allow $1 $2_target:mmu { map_read map_write adjust physmap target_hack = };=0A allow $1 $2_target:hvm { getparam setparam trackdirtyvram hvmctl = irqlevel pciroute pcilevel cacheattr send_irq };=0A ')=0A--- a/xen/common/d= omctl.c=0A+++ b/xen/common/domctl.c=0A@@ -396,14 +396,13 @@ long do_domctl(= XEN_GUEST_HANDLE_PARAM(xe=0A switch ( op->cmd )=0A {=0A case = XEN_DOMCTL_createdomain:=0A- case XEN_DOMCTL_getdomaininfo:=0A case = XEN_DOMCTL_test_assign_device:=0A case XEN_DOMCTL_gdbsx_guestmemio:=0A = d =3D NULL;=0A break;=0A default:=0A d =3D = rcu_lock_domain_by_id(op->domain);=0A- if ( d =3D=3D NULL )=0A+ = if ( !d && op->cmd !=3D XEN_DOMCTL_getdomaininfo )=0A = return -ESRCH;=0A }=0A =0A@@ -817,14 +816,22 @@ long do_domctl(XEN_GUES= T_HANDLE_PARAM(xe=0A =0A case XEN_DOMCTL_getdomaininfo:=0A {=0A- = domid_t dom =3D op->domain;=0A-=0A- rcu_read_lock(&domlist_read= _lock);=0A+ domid_t dom =3D DOMID_INVALID;=0A =0A- for_each_d= omain ( d )=0A- if ( d->domain_id >=3D dom )=0A+ if ( !d = )=0A+ {=0A+ ret =3D -EINVAL;=0A+ if ( = op->domain >=3D DOMID_FIRST_RESERVED )=0A break;=0A =0A+ = rcu_read_lock(&domlist_read_lock);=0A+=0A+ dom =3D = op->domain;=0A+ for_each_domain ( d )=0A+ if ( = d->domain_id >=3D dom )=0A+ break;=0A+ }=0A+=0A = ret =3D -ESRCH;=0A if ( d =3D=3D NULL )=0A goto = getdomaininfo_out;=0A@@ -839,6 +846,9 @@ long do_domctl(XEN_GUEST_HANDLE_PA= RAM(xe=0A copyback =3D 1;=0A =0A getdomaininfo_out:=0A+ = if ( dom =3D=3D DOMID_INVALID )=0A+ break;=0A+=0A = rcu_read_unlock(&domlist_read_lock);=0A d =3D NULL;=0A = break;=0A--- a/xen/include/xsm/dummy.h=0A+++ b/xen/include/xsm/dummy.h=0A@@= -61,7 +61,12 @@ static always_inline int xsm_default_act=0A = return 0;=0A case XSM_TARGET:=0A if ( src =3D=3D target )=0A+ = {=0A return 0;=0A+ case XSM_XS_PRIV:=0A+ = if ( src->is_xenstore )=0A+ return 0;=0A+ }=0A = /* fall through */=0A case XSM_DM_PRIV:=0A if ( target && = src->target =3D=3D target )=0A@@ -71,10 +76,6 @@ static always_inline int = xsm_default_act=0A if ( src->is_privileged )=0A return = 0;=0A return -EPERM;=0A- case XSM_XS_PRIV:=0A- if ( = src->is_xenstore || src->is_privileged )=0A- return 0;=0A- = return -EPERM;=0A default:=0A LINKER_BUG_ON(1);=0A = return -EPERM;=0A --=__PartDDEB25BF.3__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartDDEB25BF.3__=--