ia64/xen-unstable

changeset 18772:ef202be3cf54

xend, security: Prevent changing of the policy while VMs are migrating

The net changes of this patch are that the reader-side lock is put
into the path of the migration code and the writer lock into the path
of the code that handles the changing of the policy. Simultaneous
migrations of multiple hosts still work after this lock has been added.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Nov 05 10:23:26 2008 +0000 (2008-11-05)
parents 484cf12ba667
children baad1475f2cd
files tools/python/xen/xend/XendDomain.py tools/python/xen/xend/XendDomainInfo.py
line diff
     1.1 --- a/tools/python/xen/xend/XendDomain.py	Wed Nov 05 10:22:19 2008 +0000
     1.2 +++ b/tools/python/xen/xend/XendDomain.py	Wed Nov 05 10:23:26 2008 +0000
     1.3 @@ -50,7 +50,7 @@ from xen.xend.XendAPIConstants import *
     1.4  
     1.5  from xen.xend.xenstore.xstransact import xstransact
     1.6  from xen.xend.xenstore.xswatch import xswatch
     1.7 -from xen.util import mkdir
     1.8 +from xen.util import mkdir, rwlock
     1.9  from xen.xend import uuid
    1.10  
    1.11  xc = xen.lowlevel.xc.xc()
    1.12 @@ -93,6 +93,8 @@ class XendDomain:
    1.13          self.managed_domains = {}
    1.14          self.domains_lock = threading.RLock()
    1.15  
    1.16 +        self.policy_lock = rwlock.RWLock()
    1.17 +
    1.18          # xen api instance vars
    1.19          # TODO: nothing uses this at the moment
    1.20          self._allow_new_domains = True
    1.21 @@ -1139,16 +1141,21 @@ class XendDomain:
    1.22          """
    1.23  
    1.24          try:
    1.25 -            return XendCheckpoint.restore(self, fd, paused=paused, relocating=relocating)
    1.26 -        except XendError, e:
    1.27 -            log.exception("Restore failed")
    1.28 -            raise
    1.29 -        except:
    1.30 -            # I don't really want to log this exception here, but the error
    1.31 -            # handling in the relocation-socket handling code (relocate.py) is
    1.32 -            # poor, so we need to log this for debugging.
    1.33 -            log.exception("Restore failed")
    1.34 -            raise XendError("Restore failed")
    1.35 +            self.policy_lock.acquire_reader()
    1.36 +
    1.37 +            try:
    1.38 +                return XendCheckpoint.restore(self, fd, paused=paused, relocating=relocating)
    1.39 +            except XendError, e:
    1.40 +                log.exception("Restore failed")
    1.41 +                raise
    1.42 +            except:
    1.43 +                # I don't really want to log this exception here, but the error
    1.44 +                # handling in the relocation-socket handling code (relocate.py) is
    1.45 +                # poor, so we need to log this for debugging.
    1.46 +                log.exception("Restore failed")
    1.47 +                raise XendError("Restore failed")
    1.48 +        finally:
    1.49 +            self.policy_lock.release()
    1.50   
    1.51      def domain_unpause(self, domid):
    1.52          """Unpause domain execution.
     2.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Wed Nov 05 10:22:19 2008 +0000
     2.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Wed Nov 05 10:23:26 2008 +0000
     2.3 @@ -3011,64 +3011,69 @@ class XendDomainInfo:
     2.4          if not xspol:
     2.5              xspol = poladmin.get_policy_by_name(policy)
     2.6  
     2.7 -        if state in [ DOM_STATE_RUNNING, DOM_STATE_PAUSED ]:
     2.8 -            #if domain is running or paused try to relabel in hypervisor
     2.9 -            if not xspol:
    2.10 -                return (-xsconstants.XSERR_POLICY_NOT_LOADED, "", "", 0)
    2.11 -
    2.12 -            if typ != xspol.get_type_name() or \
    2.13 -               policy != xspol.get_name():
    2.14 -                return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.15 -
    2.16 -            if typ == xsconstants.ACM_POLICY_ID:
    2.17 -                new_ssidref = xspol.vmlabel_to_ssidref(label)
    2.18 -                if new_ssidref == xsconstants.INVALID_SSIDREF:
    2.19 +        try:
    2.20 +            xen.xend.XendDomain.instance().policy_lock.acquire_writer()
    2.21 +
    2.22 +            if state in [ DOM_STATE_RUNNING, DOM_STATE_PAUSED ]:
    2.23 +                #if domain is running or paused try to relabel in hypervisor
    2.24 +                if not xspol:
    2.25 +                    return (-xsconstants.XSERR_POLICY_NOT_LOADED, "", "", 0)
    2.26 +
    2.27 +                if typ != xspol.get_type_name() or \
    2.28 +                   policy != xspol.get_name():
    2.29                      return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.30  
    2.31 -                # Check that all used resources are accessible under the
    2.32 -                # new label
    2.33 -                if not is_policy_update and \
    2.34 -                   not security.resources_compatible_with_vmlabel(xspol,
    2.35 -                          self, label):
    2.36 -                    return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.37 -
    2.38 -                #Check label against expected one. Can only do this
    2.39 -                # if the policy hasn't changed underneath in the meantime
    2.40 -                if xspol_old == None:
    2.41 -                    old_label = self.get_security_label()
    2.42 -                    if old_label != old_seclab:
    2.43 -                        log.info("old_label != old_seclab: %s != %s" %
    2.44 -                                 (old_label, old_seclab))
    2.45 +                if typ == xsconstants.ACM_POLICY_ID:
    2.46 +                    new_ssidref = xspol.vmlabel_to_ssidref(label)
    2.47 +                    if new_ssidref == xsconstants.INVALID_SSIDREF:
    2.48 +                        return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.49 +
    2.50 +                    # Check that all used resources are accessible under the
    2.51 +                    # new label
    2.52 +                    if not is_policy_update and \
    2.53 +                       not security.resources_compatible_with_vmlabel(xspol,
    2.54 +                              self, label):
    2.55                          return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.56  
    2.57 -                # relabel domain in the hypervisor
    2.58 -                rc, errors = security.relabel_domains([[domid, new_ssidref]])
    2.59 -                log.info("rc from relabeling in HV: %d" % rc)
    2.60 -            else:
    2.61 -                return (-xsconstants.XSERR_POLICY_TYPE_UNSUPPORTED, "", "", 0)
    2.62 -
    2.63 -        if rc == 0:
    2.64 -            # HALTED, RUNNING or PAUSED
    2.65 -            if domid == 0:
    2.66 -                if xspol:
    2.67 -                    self.info['security_label'] = seclab
    2.68 -                    ssidref = poladmin.set_domain0_bootlabel(xspol, label)
    2.69 +                    #Check label against expected one. Can only do this
    2.70 +                    # if the policy hasn't changed underneath in the meantime
    2.71 +                    if xspol_old == None:
    2.72 +                        old_label = self.get_security_label()
    2.73 +                        if old_label != old_seclab:
    2.74 +                            log.info("old_label != old_seclab: %s != %s" %
    2.75 +                                     (old_label, old_seclab))
    2.76 +                            return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.77 +
    2.78 +                    # relabel domain in the hypervisor
    2.79 +                    rc, errors = security.relabel_domains([[domid, new_ssidref]])
    2.80 +                    log.info("rc from relabeling in HV: %d" % rc)
    2.81                  else:
    2.82 -                    return (-xsconstants.XSERR_POLICY_NOT_LOADED, "", "", 0)
    2.83 -            else:
    2.84 -                if self.info.has_key('security_label'):
    2.85 -                    old_label = self.info['security_label']
    2.86 -                    # Check label against expected one, unless wildcard
    2.87 -                    if old_label != old_seclab:
    2.88 -                        return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
    2.89 -
    2.90 -                self.info['security_label'] = seclab
    2.91 -
    2.92 -                try:
    2.93 -                    xen.xend.XendDomain.instance().managed_config_save(self)
    2.94 -                except:
    2.95 -                    pass
    2.96 -        return (rc, errors, old_label, new_ssidref)
    2.97 +                    return (-xsconstants.XSERR_POLICY_TYPE_UNSUPPORTED, "", "", 0)
    2.98 +
    2.99 +            if rc == 0:
   2.100 +                # HALTED, RUNNING or PAUSED
   2.101 +                if domid == 0:
   2.102 +                    if xspol:
   2.103 +                        self.info['security_label'] = seclab
   2.104 +                        ssidref = poladmin.set_domain0_bootlabel(xspol, label)
   2.105 +                    else:
   2.106 +                        return (-xsconstants.XSERR_POLICY_NOT_LOADED, "", "", 0)
   2.107 +                else:
   2.108 +                    if self.info.has_key('security_label'):
   2.109 +                        old_label = self.info['security_label']
   2.110 +                        # Check label against expected one, unless wildcard
   2.111 +                        if old_label != old_seclab:
   2.112 +                            return (-xsconstants.XSERR_BAD_LABEL, "", "", 0)
   2.113 +
   2.114 +                    self.info['security_label'] = seclab
   2.115 +
   2.116 +                    try:
   2.117 +                        xen.xend.XendDomain.instance().managed_config_save(self)
   2.118 +                    except:
   2.119 +                        pass
   2.120 +            return (rc, errors, old_label, new_ssidref)
   2.121 +        finally:
   2.122 +            xen.xend.XendDomain.instance().policy_lock.release()
   2.123  
   2.124      def get_on_shutdown(self):
   2.125          after_shutdown = self.info.get('actions_after_shutdown')