Skip to contentSkip to conversation

Extension of the Xen-API for managing Xen Security Policies

PATCH New Stefan Berger 1 participant 1 message Jun 6, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Extension of the Xen-API for managing Xen Security Policies

This patch implements extensions for managing security policies in xend. The XSPolicy and ACMPolicy classes provide the interface for the Xen-API and implement functionality for setting, updating and activating of a Xen security policy as well as labeling of virtual machines and resources such as block devices. Labeling of network devices will follow.

The acmpolicy class implements a compiler for translating an XML policy into their binary format and provides functionality for comparing a current policy against a new one when changing/updating a policy.

The xspolicyadmin class administers the policy of the system.

Some of the xend-internal code deals with transforming the labeling information from the S-Expression format into the new Xen-API format. This is similar to much of the other code that is already there.

---
 xen-unstable.hg/tools/python/xen/util/acmpolicy.py          | 1124 ++++++++++++
 xen-unstable.hg/tools/python/xen/util/bootloader.py         |  386 ++++
 xen-unstable.hg/tools/python/xen/util/security.py           |  781 +++++++-
 xen-unstable.hg/tools/python/xen/util/xsconstants.py        |  102 +
 xen-unstable.hg/tools/python/xen/util/xspolicy.py           |   66 
 xen-unstable.hg/tools/python/xen/xend/XendAPI.py            |   66 
 xen-unstable.hg/tools/python/xen/xend/XendConfig.py         |   27 
 xen-unstable.hg/tools/python/xen/xend/XendDomainInfo.py     |  180 +
 xen-unstable.hg/tools/python/xen/xend/XendError.py          |   12 
 xen-unstable.hg/tools/python/xen/xend/XendVDI.py            |   11 
 xen-unstable.hg/tools/python/xen/xend/XendXSPolicy.py       |  222 ++
 xen-unstable.hg/tools/python/xen/xend/XendXSPolicyAdmin.py  |  316 +++
 xen-unstable.hg/tools/python/xen/xend/server/blkif.py       |   15 
 xen-unstable.hg/tools/security/policies/security_policy.xsd |   29 
 14 files changed, 3208 insertions(+), 129 deletions(-)
xen-unstable.hg/tools/python/xen/xend/XendAPI.py +59 -7
Index: root/xen-unstable.hg/tools/python/xen/xend/XendAPI.py
===================================================================
@@ -40,11 +40,13 @@ from XendPIFMetrics import XendPIFMetric
 from XendVMMetrics import XendVMMetrics
 from XendPIF import XendPIF
 from XendPBD import XendPBD
+from XendXSPolicy import XendXSPolicy, XendACMPolicy
 
 from XendAPIConstants import *
 from xen.util.xmlrpclib2 import stringify
 
 from xen.util.blkif import blkdev_name_to_number
+from xen.util import xsconstants
 
 
 AUTH_NONE = 'none'
@@ -467,6 +469,8 @@ classes = {
     'console'      : valid_console,
     'SR'           : valid_sr,
     'task'         : valid_task,
+    'XSPolicy'     : valid_object("XSPolicy"),
+    'ACMPolicy'    : valid_object("ACMPolicy"),
     'debug'        : valid_debug,
     'network'      : valid_object("network"),
     'PIF'          : valid_object("PIF"),
@@ -481,6 +485,8 @@ autoplug_classes = {
     'VM_metrics'  : XendVMMetrics,
     'PBD'         : XendPBD,
     'PIF_metrics' : XendPIFMetrics,
+    'XSPolicy'    : XendXSPolicy,
+    'ACMPolicy'   : XendACMPolicy,
 }
 
 class XendAPI(object):
@@ -1170,7 +1176,8 @@ class XendAPI(object):
                   'HVM_boot_params',
                   'platform',
                   'PCI_bus',
-                  'other_config']
+                  'other_config',
+                  'security_label']
 
     VM_methods = [('clone', 'VM'),
                   ('start', None),
@@ -1230,7 +1237,8 @@ class XendAPI(object):
         'HVM_boot_params',
         'platform',
         'PCI_bus',
-        'other_config']
+        'other_config',
+        'security_label']
         
     def VM_get(self, name, session, vm_ref):
         return xen_api_success(
@@ -1595,7 +1603,22 @@ class XendAPI(object):
         if dom:
             return xen_api_success([dom.get_uuid()])
         return xen_api_success([])
-    
+
+    def VM_get_security_label(self, session, vm_ref):
+        dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
+        label = dom.get_security_label()
+        return xen_api_success(label)
+
+    def VM_set_security_label(self, session, vm_ref, sec_label, old_label):
+        dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
+        (rc, errors, oldlabel, new_ssidref) = \
+                                 dom.set_security_label(sec_label, old_label)
+        if rc != xsconstants.XSERR_SUCCESS:
+            return xen_api_error(['SECURITY_ERROR', rc])
+        if rc == 0:
+            rc = new_ssidref
+        return xen_api_success(rc)
+
     def VM_create(self, session, vm_struct):
         xendom = XendDomain.instance()
         domuuid = XendTask.log_progress(0, 100,
@@ -1649,6 +1672,7 @@ class XendAPI(object):
             'domid': domid is None and -1 or domid,
             'is_control_domain': xeninfo.info['is_control_domain'],
             'metrics': xeninfo.get_metrics(),
+            'security_label': xeninfo.get_security_label(),
             'crash_dumps': []
         }
         return xen_api_success(record)
@@ -1935,7 +1959,8 @@ class XendAPI(object):
                    'runtime_properties']
     VIF_attr_rw = ['device',
                    'MAC',
-                   'MTU']
+                   'MTU',
+                   'security_label']
 
     VIF_attr_inst = VIF_attr_rw
 
@@ -2037,7 +2062,10 @@ class XendAPI(object):
         except Exception, exn:
             log.exception(exn)
             return xen_api_success({})
-    
+
+    def VIF_get_security_label(self, session, vif_ref):
+        return self._VIF_get(vif_ref, 'security_label')
+
     # Xen API: Class VIF_metrics
     # ----------------------------------------------------------------
 
@@ -2081,7 +2109,8 @@ class XendAPI(object):
                    'virtual_size',
                    'sharable',
                    'read_only',
-                   'other_config']
+                   'other_config',
+                   'security_label']
     VDI_attr_inst = VDI_attr_ro + VDI_attr_rw
 
     VDI_methods = [('destroy', None)]
@@ -2189,13 +2218,24 @@ class XendAPI(object):
         xennode = XendNode.instance()
         return xen_api_success(xennode.get_vdi_by_name_label(name))
 
+    def VDI_set_security_label(self, session, vdi_ref, sec_lab, old_lab):
+        vdi = XendNode.instance().get_vdi_by_uuid(vdi_ref)
+        rc = vdi.set_security_label(sec_lab, old_lab)
+        if rc < 0:
+            return xen_api_error(['SECURITY_ERROR', rc])
+        return xen_api_success(rc)
+
+    def VDI_get_security_label(self, session, vdi_ref):
+        vdi = XendNode.instance().get_vdi_by_uuid(vdi_ref)
+        return xen_api_success(vdi.get_security_label())
 
     # Xen API: Class VTPM
     # ----------------------------------------------------------------
 
     VTPM_attr_rw = [ ]
     VTPM_attr_ro = ['VM',
-                    'backend']
+                    'backend',
+                    'runtime_properties' ]
 
     VTPM_attr_inst = VTPM_attr_rw
 
@@ -2273,6 +2313,18 @@ class XendAPI(object):
         vtpms = reduce(lambda x, y: x + y, vtpms)
         return xen_api_success(vtpms)
 
+    def VTPM_get_runtime_properties(self, _, vtpm_ref):
+        xendom = XendDomain.instance()
+        dominfo = xendom.get_vm_with_dev_uuid('vtpm', vtpm_ref)
+        device = dominfo.get_dev_config_by_uuid('vtpm', vtpm_ref)
+
+        try:
+            device_sxps = dominfo.getDeviceSxprs('vtpm')
+            device_dict = dict(device_sxps[0][1])
+            return xen_api_success(device_dict)
+        except:
+            return xen_api_success({})
+
     # Xen API: Class console
     # ----------------------------------------------------------------
 
xen-unstable.hg/tools/python/xen/xend/XendConfig.py +23 -4
Index: root/xen-unstable.hg/tools/python/xen/xend/XendConfig.py
===================================================================
@@ -157,6 +157,7 @@ XENAPI_CFG_TYPES = {
     'platform': dict,
     'tools_version': dict,
     'other_config': dict,
+    'security_label': str,
 }
 
 # List of legacy configuration keys that have no equivalent in the
@@ -419,9 +420,9 @@ class XendConfig(dict):
             self._memory_sanity_check()
 
         self['cpu_time'] = dominfo['cpu_time']/1e9
-        # TODO: i don't know what the security stuff expects here
         if dominfo.get('ssidref'):
             self['security'] = [['ssidref', dominfo['ssidref']]]
+
         self['shutdown_reason'] = dominfo['shutdown_reason']
 
         # parse state into Xen API states
@@ -628,8 +629,27 @@ class XendConfig(dict):
                 except ValueError, e:
                     raise XendConfigError('cpus = %s: %s' % (cfg['cpus'], e))
 
-        if 'security' in cfg and isinstance(cfg['security'], str):
-            cfg['security'] = sxp.from_string(cfg['security'])
+        if 'security' in cfg and not cfg.get('security_label'):
+            secinfo = cfg['security']
+            if isinstance(secinfo, list):
+                # This list is formatted like this:
+                # [['access_control', ['policy', 'xm-test'],['label', 'red']],
+                #                     ['ssidref', 196611]]
+                policy = ""
+                label = ""
+                from xen.util import xsconstants
+                policytype = xsconstants.ACM_POLICY_ID
+                for idx in range(0, len(secinfo)):
+                    if secinfo[idx][0] == "access_control":
+                        for aidx in range(1, len(secinfo[idx])):
+                            if secinfo[idx][aidx][0] == "policy":
+                                policy = secinfo[idx][aidx][1]
+                            if secinfo[idx][aidx][0] == "label":
+                                label  = secinfo[idx][aidx][1]
+                if label != "" and policy != "":
+                    cfg['security_label'] = "%s:%s:%s" % \
+                            (policytype, policy, label)
+                    del cfg['security']
 
         old_state = sxp.child_value(sxp_cfg, 'state')
         if old_state:
@@ -772,7 +792,6 @@ class XendConfig(dict):
                     self[sxp_arg] = val
 
         _set_cfg_if_exists('shadow_memory')
-        _set_cfg_if_exists('security')
         _set_cfg_if_exists('features')
         _set_cfg_if_exists('on_xend_stop')
         _set_cfg_if_exists('on_xend_start')
xen-unstable.hg/tools/python/xen/xend/XendDomainInfo.py +165 -15

211 lines not on this page · raw message

xen-unstable.hg/tools/python/xen/xend/server/blkif.py +11 -4
Index: root/xen-unstable.hg/tools/python/xen/xend/server/blkif.py
===================================================================
@@ -73,10 +73,17 @@ class BlkifController(DevController):
             back['uuid'] = uuid
 
         if security.on():
-            (label, ssidref, policy) = security.get_res_security_details(uname)
-            back.update({'acm_label'  : label,
-                         'acm_ssidref': str(ssidref),
-                         'acm_policy' : policy})
+            (label, ssidref, policy) = \
+                                 security.get_res_security_details(uname)
+            domain_label = self.vm.get_security_label()
+            if domain_label:
+                rc = security.res_security_check_xapi(label, ssidref, policy,
+                                                      domain_label)
+                if rc == 0:
+                    raise VmError("VM's access to block device '%s' denied." %
+                                  uname)
+            else:
+                raise VmError("VM must have a security label.")
 
         devid = blkif.blkdev_name_to_number(dev)
         if devid is None:
xen-unstable.hg/tools/python/xen/xend/XendError.py +12 -0
Index: root/xen-unstable.hg/tools/python/xen/xend/XendError.py
===================================================================
@@ -174,6 +174,17 @@ class NetworkError(XendAPIError):
 
     def __str__(self):
         return 'NETWORK_ERROR: %s %s' % (self.error, self.network)
+
+class SecurityError(XendAPIError):
+    def __init__(self, error):
+        XendAPIError.__init__(self)
+        self.error = error
+
+    def get_api_error(self):
+        return ['SECURITY_ERROR', self.error]
+
+    def __str__(self):
+        return 'SECURITY_ERROR: %s' % (self.error)
     
 XEND_ERROR_AUTHENTICATION_FAILED = ('ELUSER', 'Authentication Failed')
 XEND_ERROR_SESSION_INVALID       = ('EPERMDENIED', 'Session Invalid')
@@ -188,4 +199,5 @@ XEND_ERROR_VIF_INVALID           = ('EVI
 XEND_ERROR_VTPM_INVALID          = ('EVTPMINVALID', 'VTPM Invalid')
 XEND_ERROR_VDI_INVALID           = ('EVDIINVALID', 'VDI Invalid')
 XEND_ERROR_SR_INVALID           = ('ESRINVALID', 'SR Invalid')
+XEND_ERROR_XSPOLICY_INVALID      = ('EXSPOLICYINVALID', 'XS Invalid')
 XEND_ERROR_TODO                  = ('ETODO', 'Lazy Programmer Error')
xen-unstable.hg/tools/python/xen/util/xsconstants.py new file +102 -0

102 lines not on this page · raw message

xen-unstable.hg/tools/python/xen/xend/XendVDI.py +11 -0
Index: root/xen-unstable.hg/tools/python/xen/xend/XendVDI.py
===================================================================
@@ -23,6 +23,7 @@ import os
 
 from xen.util.xmlrpclib2 import stringify
 from xmlrpclib import dumps, loads
+from xen.util import security
 
 KB = 1024
 MB = 1024 * 1024
@@ -160,6 +161,16 @@ class XendVDI(AutoSaveObject):
 
     def get_location(self):
         raise NotImplementedError()
+
+    def set_security_label(self, sec_lab, old_lab):
+        image = self.get_location()
+        rc = security.set_resource_label_xapi(image, sec_lab, old_lab)
+        if rc != xsconstants.XSERR_SUCCESS:
+            raise SecurityError(rc)
+
+    def get_security_label(self):
+        image = self.get_location()
+        return security.get_resource_label_xapi(image)
                 
 
 class XendQCoWVDI(XendVDI):
xen-unstable.hg/tools/python/xen/util/security.py +688 -93

923 lines not on this page · raw message

xen-unstable.hg/tools/python/xen/util/bootloader.py new file +386 -0

386 lines not on this page · raw message

xen-unstable.hg/tools/python/xen/util/xspolicy.py new file +66 -0

66 lines not on this page · raw message

xen-unstable.hg/tools/python/xen/util/acmpolicy.py new file +1124 -0

1,044 lines not on this page · raw message

xen-unstable.hg/tools/security/policies/security_policy.xsd +23 -6
Index: root/xen-unstable.hg/tools/security/policies/security_policy.xsd
===================================================================

Lines of this file are not shown. View the raw message.

xen-unstable.hg/tools/python/xen/xend/XendXSPolicy.py new file +222 -0

222 lines not on this page · raw message

xen-unstable.hg/tools/python/xen/xend/XendXSPolicyAdmin.py new file +312 -0

312 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

+ global poladmin
+ if poladmin == None:
+ poladmin = XSPolicyAdmin(maxpolicies)
+ return poladmin

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Part of the diff is not shown here. View the raw message.