ia64/xen-unstable

changeset 10720:8922c1fbe684

[XM][ACM] Add xm subcommands to work with security resource labels.

This patch adds new xm subcommands to support working with resource
labels. The new subcommands are 'xm resources', 'xm rmlabel', 'xm
getlabel' and 'xm dry-run'. In addition, the 'xm addlabel' subcommand
now uses an updated syntax to support labeling both domains and
resources. See the xm man page for details on each subcommand.

Beyond the new subcommands, this patch allows users to immediately see
when security checks will fail by pushing some basic security checking
into the beginning of 'xm create' and 'xm block-attach'. ACM security
attributes for block devices are added to XenStore in order to support
the final security enforcement, which will be performed in the kernel
and included in a separate patch.

Signed-off-by: Bryan D. Payne <bdpayne@us.ibm.com>
Signed-off-by: Reiner Sailer <sailer@us.ibm.com>
author kfraser@localhost.localdomain
date Mon Jul 10 17:18:07 2006 +0100 (2006-07-10)
parents e05d60be5adb
children 3243ab41b09e
files tools/python/xen/util/dictio.py tools/python/xen/util/security.py tools/python/xen/xend/server/blkif.py tools/python/xen/xm/addlabel.py tools/python/xen/xm/create.py tools/python/xen/xm/dry-run.py tools/python/xen/xm/getlabel.py tools/python/xen/xm/main.py tools/python/xen/xm/resources.py tools/python/xen/xm/rmlabel.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/xen/util/dictio.py	Mon Jul 10 17:18:07 2006 +0100
     1.3 @@ -0,0 +1,50 @@
     1.4 +#===========================================================================
     1.5 +# This library is free software; you can redistribute it and/or
     1.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     1.7 +# License as published by the Free Software Foundation.
     1.8 +#
     1.9 +# This library is distributed in the hope that it will be useful,
    1.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.12 +# Lesser General Public License for more details.
    1.13 +#
    1.14 +# You should have received a copy of the GNU Lesser General Public
    1.15 +# License along with this library; if not, write to the Free Software
    1.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.17 +#============================================================================
    1.18 +# Copyright (C) 2006 International Business Machines Corp.
    1.19 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
    1.20 +#============================================================================
    1.21 +
    1.22 +
    1.23 +def dict_read(dictname, filename):
    1.24 +    """Loads <filename> and returns the dictionary named <dictname> from
    1.25 +       the file.
    1.26 +    """
    1.27 +    dict = {}
    1.28 +
    1.29 +    # read in the config file
    1.30 +    globs = {}
    1.31 +    locs = {}
    1.32 +    execfile(filename, globs, locs)
    1.33 +
    1.34 +    for (k, v) in locs.items():
    1.35 +        if k == dictname:
    1.36 +            dict = v
    1.37 +            break
    1.38 +
    1.39 +    return dict
    1.40 +
    1.41 +def dict_write(dict, dictname, filename):
    1.42 +    """Writes <dict> to <filename> using the name <dictname>.  If the file
    1.43 +       contains any other data, it will be overwritten.
    1.44 +    """
    1.45 +    prefix = dictname + " = {\n"
    1.46 +    suffix = "}\n"
    1.47 +    fd = open(filename, "wb")
    1.48 +    fd.write(prefix)
    1.49 +    for key in dict:
    1.50 +        line = "    '" + str(key) + "': " + str(dict[key]) + ",\n"
    1.51 +        fd.write(line)
    1.52 +    fd.write(suffix)
    1.53 +    fd.close()
     2.1 --- a/tools/python/xen/util/security.py	Mon Jul 10 17:05:44 2006 +0100
     2.2 +++ b/tools/python/xen/util/security.py	Mon Jul 10 17:18:07 2006 +0100
     2.3 @@ -14,6 +14,7 @@
     2.4  #============================================================================
     2.5  # Copyright (C) 2006 International Business Machines Corp.
     2.6  # Author: Reiner Sailer
     2.7 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
     2.8  #============================================================================
     2.9  
    2.10  import commands
    2.11 @@ -23,9 +24,12 @@ import traceback
    2.12  import shutil
    2.13  from xen.lowlevel import acm
    2.14  from xen.xend import sxp
    2.15 +from xen.xend.XendLogging import log
    2.16 +from xen.util import dictio
    2.17  
    2.18  #global directories and tools for security management
    2.19  policy_dir_prefix = "/etc/xen/acm-security/policies"
    2.20 +res_label_filename = policy_dir_prefix + "/resource_labels"
    2.21  boot_filename = "/boot/grub/menu.lst"
    2.22  xensec_xml2bin = "/usr/sbin/xensec_xml2bin"
    2.23  xensec_tool = "/usr/sbin/xensec_tool"
    2.24 @@ -530,3 +534,97 @@ def list_labels(policy_name, condition):
    2.25              if label not in labels:
    2.26                  labels.append(label)
    2.27      return labels
    2.28 +
    2.29 +
    2.30 +def get_res_label(resource):
    2.31 +    """Returns resource label information (label, policy) if it exists.
    2.32 +       Otherwise returns null label and policy.
    2.33 +    """
    2.34 +    def default_res_label():
    2.35 +        ssidref = NULL_SSIDREF
    2.36 +        if on():
    2.37 +            label = ssidref2label(ssidref)
    2.38 +        else:
    2.39 +            label = None
    2.40 +        return (label, 'NULL')
    2.41 +
    2.42 +    (label, policy) = default_res_label()
    2.43 +
    2.44 +    # load the resource label file
    2.45 +    res_label_cache = {}
    2.46 +    try:
    2.47 +        res_label_cache = dictio.dict_read("resources", res_label_filename)
    2.48 +    except:
    2.49 +        log.info("Resource label file not found.")
    2.50 +        return default_res_label()
    2.51 +
    2.52 +    # find the resource information
    2.53 +    if res_label_cache.has_key(resource):
    2.54 +        (policy, label) = res_label_cache[resource]
    2.55 +
    2.56 +    return (label, policy)
    2.57 +
    2.58 +
    2.59 +def get_res_security_details(resource):
    2.60 +    """Returns the (label, ssidref, policy) associated with a given
    2.61 +       resource from the global resource label file.
    2.62 +    """
    2.63 +    def default_security_details():
    2.64 +        ssidref = NULL_SSIDREF
    2.65 +        if on():
    2.66 +            label = ssidref2label(ssidref)
    2.67 +        else:
    2.68 +            label = None
    2.69 +        policy = active_policy
    2.70 +        return (label, ssidref, policy)
    2.71 +
    2.72 +    (label, ssidref, policy) = default_security_details()
    2.73 +
    2.74 +    # find the entry associated with this resource
    2.75 +    (label, policy) = get_res_label(resource)
    2.76 +    if policy == 'NULL':
    2.77 +        log.info("Resource label for "+resource+" not in file, using DEFAULT.")
    2.78 +        return default_security_details()
    2.79 +
    2.80 +    # is this resource label for the running policy?
    2.81 +    if policy == active_policy:
    2.82 +        ssidref = label2ssidref(label, policy, 'res')
    2.83 +    else:
    2.84 +        log.info("Resource label not for active policy, using DEFAULT.")
    2.85 +        return default_security_details()
    2.86 +
    2.87 +    return (label, ssidref, policy)
    2.88 +
    2.89 +
    2.90 +def res_security_check(resource, domain_label):
    2.91 +    """Checks if the given resource can be used by the given domain
    2.92 +       label.  Returns 1 if the resource can be used, otherwise 0.
    2.93 +    """
    2.94 +    rtnval = 1
    2.95 +
    2.96 +    # if security is on, ask the hypervisor for a decision
    2.97 +    if on():
    2.98 +        (label, ssidref, policy) = get_res_security_details(resource)
    2.99 +        domac = ['access_control']
   2.100 +        domac.append(['policy', active_policy])
   2.101 +        domac.append(['label', domain_label])
   2.102 +        domac.append(['type', 'dom'])
   2.103 +        decision = get_decision(domac, ['ssidref', str(ssidref)])
   2.104 +
   2.105 +        # provide descriptive error messages
   2.106 +        if decision == 'DENIED':
   2.107 +            if label == ssidref2label(NULL_SSIDREF):
   2.108 +                raise ACMError("Resource '"+resource+"' is not labeled")
   2.109 +                rtnval = 0
   2.110 +            else:
   2.111 +                raise ACMError("Permission denied for resource '"+resource+"' because label '"+label+"' is not allowed")
   2.112 +                rtnval = 0
   2.113 +
   2.114 +    # security is off, make sure resource isn't labeled
   2.115 +    else:
   2.116 +        (label, policy) = get_res_label(resource)
   2.117 +        if policy != 'NULL':
   2.118 +            raise ACMError("Security is off, but '"+resource+"' is labeled")
   2.119 +            rtnval = 0
   2.120 +
   2.121 +    return rtnval
     3.1 --- a/tools/python/xen/xend/server/blkif.py	Mon Jul 10 17:05:44 2006 +0100
     3.2 +++ b/tools/python/xen/xend/server/blkif.py	Mon Jul 10 17:18:07 2006 +0100
     3.3 @@ -21,6 +21,7 @@ import re
     3.4  import string
     3.5  
     3.6  from xen.util import blkif
     3.7 +from xen.util import security
     3.8  from xen.xend import sxp
     3.9  from xen.xend.XendError import VmError
    3.10  
    3.11 @@ -40,15 +41,22 @@ class BlkifController(DevController):
    3.12  
    3.13      def getDeviceDetails(self, config):
    3.14          """@see DevController.getDeviceDetails"""
    3.15 +        uname = sxp.child_value(config, 'uname')
    3.16  
    3.17          dev = sxp.child_value(config, 'dev')
    3.18  
    3.19 -        (typ, params) = string.split(sxp.child_value(config, 'uname'), ':', 1)
    3.20 +        (typ, params) = string.split(uname, ':', 1)
    3.21          back = { 'dev'    : dev,
    3.22                   'type'   : typ,
    3.23                   'params' : params,
    3.24                   'mode'   : sxp.child_value(config, 'mode', 'r')
    3.25 -                 }
    3.26 +               }
    3.27 +
    3.28 +        if security.on():
    3.29 +            (label, ssidref, policy) = security.get_res_security_details(uname)
    3.30 +            back.update({'acm_label'  : label,
    3.31 +                         'acm_ssidref': str(ssidref),
    3.32 +                         'acm_policy' : policy})
    3.33  
    3.34          if 'ioemu:' in dev:
    3.35              (dummy, dev1) = string.split(dev, ':', 1)
     4.1 --- a/tools/python/xen/xm/addlabel.py	Mon Jul 10 17:05:44 2006 +0100
     4.2 +++ b/tools/python/xen/xm/addlabel.py	Mon Jul 10 17:18:07 2006 +0100
     4.3 @@ -14,59 +14,139 @@
     4.4  #============================================================================
     4.5  # Copyright (C) 2006 International Business Machines Corp.
     4.6  # Author: Reiner Sailer <sailer@us.ibm.com>
     4.7 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
     4.8  #============================================================================
     4.9  
    4.10 -"""Labeling a domain configuration file.
    4.11 +"""Labeling a domain configuration file or a resoruce.
    4.12  """
    4.13  import sys, os
    4.14 +import string
    4.15  import traceback
    4.16 +from xen.util import dictio
    4.17 +from xen.util import security
    4.18 +
    4.19 +def usage():
    4.20 +    print "\nUsage: xm addlabel <label> dom <configfile> [<policy>]"
    4.21 +    print "       xm addlabel <label> res <resource> [<policy>]\n"
    4.22 +    print "  This program adds an acm_label entry into the 'configfile'"
    4.23 +    print "  for a domain or to the global resource label file for a"
    4.24 +    print "  resource. It derives the policy from the running hypervisor"
    4.25 +    print "  if it is not given (optional parameter). If a label already"
    4.26 +    print "  exists for the given domain or resource, then addlabel fails.\n"
    4.27  
    4.28  
    4.29 -from xen.util.security import ACMError, err, active_policy, label2ssidref, on, access_control_re
    4.30 +def validate_config_file(configfile):
    4.31 +    """Performs a simple sanity check on the configuration file passed on
    4.32 +       the command line.  We basically just want to make sure that it's
    4.33 +       not a domain image file so we check for a few configuration values
    4.34 +       and then we are satisfied.  Returned 1 on success, otherwise 0.
    4.35 +    """
    4.36 +    # read in the config file
    4.37 +    globs = {}
    4.38 +    locs = {}
    4.39 +    try:
    4.40 +        execfile(configfile, globs, locs)
    4.41 +    except:
    4.42 +        print "Invalid configuration file."
    4.43 +        return 0
    4.44 +
    4.45 +    # sanity check on the data from the file
    4.46 +    count = 0
    4.47 +    required = ['kernel', 'memory', 'name']
    4.48 +    for (k, v) in locs.items():
    4.49 +        if k in required:
    4.50 +            count += 1
    4.51 +    if count != 3:
    4.52 +        print "Invalid configuration file."
    4.53 +        return 0
    4.54 +    else:
    4.55 +        return 1
    4.56  
    4.57  
    4.58 -def usage():
    4.59 -    print "\nUsage: xm addlabel <configfile> <label> [<policy>]\n"
    4.60 -    print "  This program adds an acm_label entry into the 'configfile'."
    4.61 -    print "  It derives the policy from the running hypervisor if it"
    4.62 -    print "  is not given (optional parameter). If the configfile is"
    4.63 -    print "  already labeled, then addlabel fails.\n"
    4.64 -    err("Usage")
    4.65 +def add_resource_label(label, resource, policyref):
    4.66 +    """Adds a resource label to the global resource label file.
    4.67 +    """
    4.68 +    # sanity check: make sure this label can be instantiated later on
    4.69 +    ssidref = security.label2ssidref(label, policyref, 'res')
    4.70 +
    4.71 +    # sanity check on resource name
    4.72 +    (type, file) = resource.split(":")
    4.73 +    if type == "phy":
    4.74 +        file = "/dev/" + file
    4.75 +    if not os.path.exists(file):
    4.76 +        print "Invalid resource '"+resource+"'"
    4.77 +        return
    4.78 +
    4.79 +    # see if this resource is already in the file
    4.80 +    access_control = {}
    4.81 +    file = security.res_label_filename
    4.82 +    try:
    4.83 +        access_control = dictio.dict_read("resources", file)
    4.84 +    except:
    4.85 +        print "Resource file not found, creating new file at:"
    4.86 +        print "%s" % (file)
    4.87 +
    4.88 +    if access_control.has_key(resource):
    4.89 +        security.err("This resource is already labeled.")
    4.90 +
    4.91 +    # write the data to file
    4.92 +    new_entry = { resource : tuple([policyref, label]) }
    4.93 +    access_control.update(new_entry)
    4.94 +    dictio.dict_write(access_control, "resources", file)
    4.95  
    4.96  
    4.97 -def main(argv):
    4.98 +def add_domain_label(label, configfile, policyref):
    4.99 +    # sanity checks: make sure this label can be instantiated later on
   4.100 +    ssidref = security.label2ssidref(label, policyref, 'dom')
   4.101 +
   4.102 +    new_label = "access_control = ['policy=%s,label=%s']\n" % (policyref, label)
   4.103 +    if not os.path.isfile(configfile):
   4.104 +        security.err("Configuration file \'" + configfile + "\' not found.")
   4.105 +    config_fd = open(configfile, "ra+")
   4.106 +    for line in config_fd:
   4.107 +        if not security.access_control_re.match(line):
   4.108 +            continue
   4.109 +        config_fd.close()
   4.110 +        security.err("Config file \'" + configfile + "\' is already labeled.")
   4.111 +    config_fd.write(new_label)
   4.112 +    config_fd.close()
   4.113 +
   4.114 +
   4.115 +def main (argv):
   4.116      try:
   4.117          policyref = None
   4.118 -        if len(argv) not in [3,4]:
   4.119 +        if len(argv) not in [4,5]:
   4.120              usage()
   4.121 -        configfile = argv[1]
   4.122 -        label = argv[2]
   4.123 -
   4.124 -        if len(argv) == 4:
   4.125 -            policyref = argv[3]
   4.126 -        elif on():
   4.127 -            policyref = active_policy
   4.128 -        else:
   4.129 -            err("No active policy. Policy must be specified in command line.")
   4.130 -
   4.131 -        #sanity checks: make sure this label can be instantiated later on
   4.132 -        ssidref = label2ssidref(label, policyref, 'dom')
   4.133 +            return
   4.134  
   4.135 -        new_label = "access_control = ['policy=%s,label=%s']\n" % (policyref, label)
   4.136 -        if not os.path.isfile(configfile):
   4.137 -            err("Configuration file \'" + configfile + "\' not found.")
   4.138 -        config_fd = open(configfile, "ra+")
   4.139 -        for line in config_fd:
   4.140 -            if not access_control_re.match(line):
   4.141 -                continue
   4.142 -            config_fd.close()
   4.143 -            err("Config file \'" + configfile + "\' is already labeled.")
   4.144 -        config_fd.write(new_label)
   4.145 -        config_fd.close()
   4.146 +        label = argv[1]
   4.147  
   4.148 -    except ACMError:
   4.149 -        pass
   4.150 -    except:
   4.151 +        if len(argv) == 5:
   4.152 +            policyref = argv[4]
   4.153 +        elif security.on():
   4.154 +            policyref = security.active_policy
   4.155 +        else:
   4.156 +            security.err("No active policy. Policy must be specified in command line.")
   4.157 +
   4.158 +        if argv[2].lower() == "dom":
   4.159 +            configfile = argv[3]
   4.160 +            if configfile[0] != '/':
   4.161 +                for prefix in [".", "/etc/xen"]:
   4.162 +                    configfile = prefix + "/" + configfile
   4.163 +                    if os.path.isfile(configfile):
   4.164 +                        fd = open(configfile, "rb")
   4.165 +                        break
   4.166 +            if not validate_config_file(configfile):
   4.167 +                usage()
   4.168 +            else:
   4.169 +                add_domain_label(label, configfile, policyref)
   4.170 +        elif argv[2].lower() == "res":
   4.171 +            resource = argv[3]
   4.172 +            add_resource_label(label, resource, policyref)
   4.173 +        else:
   4.174 +            usage()
   4.175 +
   4.176 +    except security.ACMError:
   4.177          traceback.print_exc(limit=1)
   4.178  
   4.179  
     5.1 --- a/tools/python/xen/xm/create.py	Mon Jul 10 17:05:44 2006 +0100
     5.2 +++ b/tools/python/xen/xm/create.py	Mon Jul 10 17:18:07 2006 +0100
     5.3 @@ -25,6 +25,7 @@ import sys
     5.4  import socket
     5.5  import re
     5.6  import xmlrpclib
     5.7 +import traceback
     5.8  
     5.9  from xen.xend import sxp
    5.10  from xen.xend import PrettyPrint
    5.11 @@ -985,6 +986,117 @@ def parseCommandLine(argv):
    5.12      return (gopts, config)
    5.13  
    5.14  
    5.15 +def check_domain_label(config, verbose):
    5.16 +    """All that we need to check here is that the domain label exists and
    5.17 +       is not null when security is on.  Other error conditions are
    5.18 +       handled when the config file is parsed.
    5.19 +    """
    5.20 +    answer = 0
    5.21 +    default_label = None
    5.22 +    secon = 0
    5.23 +    if security.on():
    5.24 +        default_label = security.ssidref2label(security.NULL_SSIDREF)
    5.25 +        secon = 1
    5.26 +
    5.27 +    # get the domain acm_label
    5.28 +    dom_label = None
    5.29 +    dom_name = None
    5.30 +    for x in sxp.children(config):
    5.31 +        if sxp.name(x) == 'security':
    5.32 +            dom_label = sxp.child_value(sxp.name(sxp.child0(x)), 'label')
    5.33 +        if sxp.name(x) == 'name':
    5.34 +            dom_name = sxp.child0(x)
    5.35 +
    5.36 +    # sanity check on domain label
    5.37 +    if verbose:
    5.38 +        print "Checking domain:"
    5.39 +    if (not secon) and (not dom_label):
    5.40 +        answer = 1
    5.41 +        if verbose:
    5.42 +            print "   %s: PERMITTED" % (dom_name)
    5.43 +    elif (secon) and (dom_label) and (dom_label != default_label):
    5.44 +        answer = 1
    5.45 +        if verbose:
    5.46 +            print "   %s: PERMITTED" % (dom_name)
    5.47 +    else:
    5.48 +        print "   %s: DENIED" % (dom_name)
    5.49 +        if not secon:
    5.50 +            print "   --> Security off, but domain labeled"
    5.51 +        else:
    5.52 +            print "   --> Domain not labeled"
    5.53 +        answer = 0
    5.54 +
    5.55 +    return answer
    5.56 +
    5.57 +
    5.58 +def config_security_check(config, verbose):
    5.59 +    """Checks each resource listed in the config to see if the active
    5.60 +       policy will permit creation of a new domain using the config.
    5.61 +       Returns 1 if the config passes all tests, otherwise 0.
    5.62 +    """
    5.63 +    answer = 1
    5.64 +
    5.65 +    # get the domain acm_label
    5.66 +    domain_label = None
    5.67 +    domain_policy = None
    5.68 +    for x in sxp.children(config):
    5.69 +        if sxp.name(x) == 'security':
    5.70 +            domain_label = sxp.child_value(sxp.name(sxp.child0(x)), 'label')
    5.71 +            domain_policy = sxp.child_value(sxp.name(sxp.child0(x)), 'policy')
    5.72 +
    5.73 +    # if no domain label, use default
    5.74 +    if not domain_label and security.on():
    5.75 +        try:
    5.76 +            domain_label = security.ssidref2label(security.NULL_SSIDREF)
    5.77 +        except:
    5.78 +            traceback.print_exc(limit=1)
    5.79 +            return 0
    5.80 +        domain_policy = 'NULL'
    5.81 +    elif not domain_label:
    5.82 +        domain_label = ""
    5.83 +        domain_policy = 'NULL'
    5.84 +
    5.85 +    if verbose:
    5.86 +        print "Checking resources:"
    5.87 +
    5.88 +    # build a list of all resources in the config file
    5.89 +    resources = []
    5.90 +    for x in sxp.children(config):
    5.91 +        if sxp.name(x) == 'device':
    5.92 +            if sxp.name(sxp.child0(x)) == 'vbd':
    5.93 +                resources.append(sxp.child_value(sxp.child0(x), 'uname'))
    5.94 +
    5.95 +    # perform a security check on each resource
    5.96 +    for resource in resources:
    5.97 +        try:
    5.98 +            security.res_security_check(resource, domain_label)
    5.99 +            if verbose:
   5.100 +                print "   %s: PERMITTED" % (resource)
   5.101 +
   5.102 +        except security.ACMError:
   5.103 +            print "   %s: DENIED" % (resource)
   5.104 +            (res_label, res_policy) = security.get_res_label(resource)
   5.105 +            print "   --> res:"+res_label+" ("+res_policy+")"
   5.106 +            print "   --> dom:"+domain_label+" ("+domain_policy+")"
   5.107 +            answer = 0
   5.108 +
   5.109 +    return answer
   5.110 +
   5.111 +
   5.112 +def create_security_check(config):
   5.113 +    passed = 0
   5.114 +    try:
   5.115 +        if check_domain_label(config, verbose=0):
   5.116 +            if config_security_check(config, verbose=0):
   5.117 +                passed = 1
   5.118 +        else:
   5.119 +            print "Checking resources: (skipped)"
   5.120 +    except security.ACMError:
   5.121 +        traceback.print_exc(limit=1)
   5.122 +
   5.123 +    return passed
   5.124 +
   5.125 +
   5.126  def main(argv):
   5.127      try:
   5.128          (opts, config) = parseCommandLine(argv)
   5.129 @@ -997,9 +1109,12 @@ def main(argv):
   5.130      if opts.vals.dryrun:
   5.131          PrettyPrint.prettyprint(config)
   5.132      else:
   5.133 -        dom = make_domain(opts, config)
   5.134 -        if opts.vals.console_autoconnect:
   5.135 -            console.execConsole(dom)
   5.136 +        if not create_security_check(config):
   5.137 +            print "Security configuration prevents domain from starting"
   5.138 +        else:
   5.139 +            dom = make_domain(opts, config)
   5.140 +            if opts.vals.console_autoconnect:
   5.141 +                console.execConsole(dom)
   5.142          
   5.143  if __name__ == '__main__':
   5.144      main(sys.argv)
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/tools/python/xen/xm/dry-run.py	Mon Jul 10 17:18:07 2006 +0100
     6.3 @@ -0,0 +1,56 @@
     6.4 +#============================================================================
     6.5 +# This library is free software; you can redistribute it and/or
     6.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     6.7 +# License as published by the Free Software Foundation.
     6.8 +#
     6.9 +# This library is distributed in the hope that it will be useful,
    6.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    6.12 +# Lesser General Public License for more details.
    6.13 +#
    6.14 +# You should have received a copy of the GNU Lesser General Public
    6.15 +# License along with this library; if not, write to the Free Software
    6.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    6.17 +#============================================================================
    6.18 +# Copyright (C) 2006 International Business Machines Corp.
    6.19 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
    6.20 +#============================================================================
    6.21 +
    6.22 +"""Tests the security settings for a domain and its resources.
    6.23 +"""
    6.24 +from xen.util import security
    6.25 +from xen.xm import create
    6.26 +from xen.xend import sxp
    6.27 +
    6.28 +def usage():
    6.29 +    print "\nUsage: xm dry-run <configfile>\n"
    6.30 +    print "This program checks each resource listed in the configfile"
    6.31 +    print "to see if the domain created by the configfile can access"
    6.32 +    print "the resources.  The status of each resource is listed"
    6.33 +    print "individually along with the final security decision.\n"
    6.34 +
    6.35 +
    6.36 +def main (argv):
    6.37 +    if len(argv) != 2:
    6.38 +        usage()
    6.39 +        return
    6.40 +
    6.41 +    try:
    6.42 +        passed = 0
    6.43 +        (opts, config) = create.parseCommandLine(argv)
    6.44 +        if create.check_domain_label(config, verbose=1):
    6.45 +            if create.config_security_check(config, verbose=1):
    6.46 +                passed = 1
    6.47 +        else:
    6.48 +            print "Checking resources: (skipped)"
    6.49 +
    6.50 +        if passed:
    6.51 +            print "Dry Run: PASSED"
    6.52 +        else:
    6.53 +            print "Dry Run: FAILED"
    6.54 +    except security.ACMError:
    6.55 +        pass
    6.56 +
    6.57 +
    6.58 +if __name__ == '__main__':
    6.59 +    main(sys.argv)
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/tools/python/xen/xm/getlabel.py	Mon Jul 10 17:18:07 2006 +0100
     7.3 @@ -0,0 +1,114 @@
     7.4 +#============================================================================
     7.5 +# This library is free software; you can redistribute it and/or
     7.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     7.7 +# License as published by the Free Software Foundation.
     7.8 +#
     7.9 +# This library is distributed in the hope that it will be useful,
    7.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    7.12 +# Lesser General Public License for more details.
    7.13 +#
    7.14 +# You should have received a copy of the GNU Lesser General Public
    7.15 +# License along with this library; if not, write to the Free Software
    7.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    7.17 +#============================================================================
    7.18 +# Copyright (C) 2006 International Business Machines Corp.
    7.19 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
    7.20 +#============================================================================
    7.21 +
    7.22 +"""Show the label for a domain or resoruce.
    7.23 +"""
    7.24 +import sys, os, re
    7.25 +import string
    7.26 +import traceback
    7.27 +from xen.util import dictio
    7.28 +from xen.util import security
    7.29 +
    7.30 +def usage():
    7.31 +    print "\nUsage: xm getlabel dom <configfile>"
    7.32 +    print "       xm getlabel res <resource>\n"
    7.33 +    print "  This program shows the label for a domain or resource.\n"
    7.34 +
    7.35 +
    7.36 +def get_resource_label(resource):
    7.37 +    """Gets the resource label
    7.38 +    """
    7.39 +    # read in the resource file
    7.40 +    file = security.res_label_filename
    7.41 +    try:
    7.42 +        access_control = dictio.dict_read("resources", file)
    7.43 +    except:
    7.44 +        print "Resource label file not found"
    7.45 +        return
    7.46 +
    7.47 +    # get the entry and print label
    7.48 +    if access_control.has_key(resource):
    7.49 +        policy = access_control[resource][0]
    7.50 +        label = access_control[resource][1]
    7.51 +        print "policy="+policy+",label="+label
    7.52 +    else:
    7.53 +        print "Resource not labeled"
    7.54 +
    7.55 +
    7.56 +def get_domain_label(configfile):
    7.57 +    # open the domain config file
    7.58 +    fd = None
    7.59 +    file = None
    7.60 +    if configfile[0] == '/':
    7.61 +        fd = open(configfile, "rb")
    7.62 +    else:
    7.63 +        for prefix in [".", "/etc/xen"]:
    7.64 +            file = prefix + "/" + configfile
    7.65 +            if os.path.isfile(file):
    7.66 +                fd = open(file, "rb")
    7.67 +                break
    7.68 +    if not fd:
    7.69 +        print "Configuration file '"+configfile+"' not found."
    7.70 +        return
    7.71 +
    7.72 +    # read in the domain config file, finding the label line
    7.73 +    ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
    7.74 +    ac_exit_re = re.compile(".*'\].*")
    7.75 +    acline = ""
    7.76 +    record = 0
    7.77 +    for line in fd.readlines():
    7.78 +        if ac_entry_re.match(line):
    7.79 +            record = 1
    7.80 +        if record:
    7.81 +            acline = acline + line
    7.82 +        if record and ac_exit_re.match(line):
    7.83 +            record = 0
    7.84 +    fd.close()
    7.85 +
    7.86 +    # send error message if we didn't find anything
    7.87 +    if acline == "":
    7.88 +        print "Label does not exist in domain configuration file."
    7.89 +        return
    7.90 +
    7.91 +    # print out the label
    7.92 +    (title, data) = acline.split("=", 1)
    7.93 +    data = data.strip()
    7.94 +    data = data.lstrip("[\'")
    7.95 +    data = data.rstrip("\']")
    7.96 +    print data
    7.97 +
    7.98 +
    7.99 +def main (argv):
   7.100 +    if len(argv) != 3:
   7.101 +        usage()
   7.102 +        return
   7.103 +
   7.104 +    if argv[1].lower() == "dom":
   7.105 +        configfile = argv[2]
   7.106 +        get_domain_label(configfile)
   7.107 +    elif argv[1].lower() == "res":
   7.108 +        resource = argv[2]
   7.109 +        get_resource_label(resource)
   7.110 +    else:
   7.111 +        usage()
   7.112 +
   7.113 +
   7.114 +if __name__ == '__main__':
   7.115 +    main(sys.argv)
   7.116 +
   7.117 +
     8.1 --- a/tools/python/xen/xm/main.py	Mon Jul 10 17:05:44 2006 +0100
     8.2 +++ b/tools/python/xen/xm/main.py	Mon Jul 10 17:18:07 2006 +0100
     8.3 @@ -30,6 +30,7 @@ import socket
     8.4  import warnings
     8.5  warnings.filterwarnings('ignore', category=FutureWarning)
     8.6  import xmlrpclib
     8.7 +import traceback
     8.8  
     8.9  import xen.xend.XendProtocol
    8.10  
    8.11 @@ -119,7 +120,11 @@ vnet_list_help = "vnet-list [-l|--long] 
    8.12  vnet_create_help = "vnet-create <config>             create a vnet from a config file"
    8.13  vnet_delete_help = "vnet-delete <vnetid>             delete a vnet"
    8.14  vtpm_list_help = "vtpm-list <DomId> [--long]       list virtual TPM devices"
    8.15 -addlabel_help =  "addlabel <ConfigFile> <label>    Add security label to ConfigFile"
    8.16 +addlabel_help =  "addlabel <label> dom <configfile> Add security label to domain\n            <label> res <resource>   or resource"
    8.17 +rmlabel_help =  "rmlabel dom <configfile>         Remove security label from domain\n           res <resource>           or resource"
    8.18 +getlabel_help =  "getlabel dom <configfile>        Show security label for domain\n            res <resource>          or resource"
    8.19 +dry_run_help =  "dry-run <configfile>             Tests if domain can access its resources"
    8.20 +resources_help =  "resources                        Show info for each labeled resource"
    8.21  cfgbootpolicy_help = "cfgbootpolicy <policy>           Add policy to boot configuration "
    8.22  dumppolicy_help = "dumppolicy                       Print hypervisor ACM state information"
    8.23  loadpolicy_help = "loadpolicy <policy>              Load binary policy into hypervisor"
    8.24 @@ -203,6 +208,10 @@ vnet_commands = [
    8.25  acm_commands = [
    8.26      "labels",
    8.27      "addlabel",
    8.28 +    "rmlabel",
    8.29 +    "getlabel",
    8.30 +    "dry-run",
    8.31 +    "resources",
    8.32      "makepolicy",
    8.33      "loadpolicy",
    8.34      "cfgbootpolicy",
    8.35 @@ -992,6 +1001,21 @@ def xm_block_attach(args):
    8.36      if len(args) == 5:
    8.37          vbd.append(['backend', args[4]])
    8.38  
    8.39 +    # verify that policy permits attaching this resource
    8.40 +    try:
    8.41 +        if security.on():
    8.42 +            dominfo = server.xend.domain(dom)
    8.43 +            label = security.get_security_printlabel(dominfo)
    8.44 +        else:
    8.45 +            label = None
    8.46 +        security.res_security_check(args[1], label)
    8.47 +    except security.ACMError, e:
    8.48 +        print e.value
    8.49 +        sys.exit(1)
    8.50 +    except:
    8.51 +        traceback.print_exc(limit=1)
    8.52 +        sys.exit(1)
    8.53 +
    8.54      server.xend.domain.device_create(dom, vbd)
    8.55  
    8.56  
    8.57 @@ -1124,6 +1148,10 @@ subcommands = [
    8.58      'shutdown',
    8.59      'labels',
    8.60      'addlabel',
    8.61 +    'rmlabel',
    8.62 +    'getlabel',
    8.63 +    'dry-run',
    8.64 +    'resources',
    8.65      'cfgbootpolicy',
    8.66      'makepolicy',
    8.67      'loadpolicy',
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/tools/python/xen/xm/resources.py	Mon Jul 10 17:18:07 2006 +0100
     9.3 @@ -0,0 +1,56 @@
     9.4 +#============================================================================
     9.5 +# This library is free software; you can redistribute it and/or
     9.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     9.7 +# License as published by the Free Software Foundation.
     9.8 +#
     9.9 +# This library is distributed in the hope that it will be useful,
    9.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    9.12 +# Lesser General Public License for more details.
    9.13 +#
    9.14 +# You should have received a copy of the GNU Lesser General Public
    9.15 +# License along with this library; if not, write to the Free Software
    9.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    9.17 +#============================================================================
    9.18 +# Copyright (C) 2006 International Business Machines Corp.
    9.19 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
    9.20 +#============================================================================
    9.21 +
    9.22 +"""List the resource label information from the global resource label file
    9.23 +"""
    9.24 +import sys, os
    9.25 +import string
    9.26 +from xen.util import dictio
    9.27 +from xen.util import security
    9.28 +
    9.29 +def usage():
    9.30 +    print "\nUsage: xm resource\n"
    9.31 +    print "  This program lists information for each resource in the"
    9.32 +    print "  global resource label file\n"
    9.33 +
    9.34 +
    9.35 +def print_resource_data(access_control):
    9.36 +    """Prints out a resource dictionary to stdout
    9.37 +    """
    9.38 +    for resource in access_control:
    9.39 +        (policy, label) = access_control[resource]
    9.40 +        print resource
    9.41 +        print "    policy: "+policy
    9.42 +        print "    label:  "+label
    9.43 +
    9.44 +
    9.45 +def main (argv):
    9.46 +    try:
    9.47 +        file = security.res_label_filename
    9.48 +        access_control = dictio.dict_read("resources", file)
    9.49 +    except:
    9.50 +        print "Resource file not found."
    9.51 +        return
    9.52 +
    9.53 +    print_resource_data(access_control)
    9.54 +
    9.55 +
    9.56 +if __name__ == '__main__':
    9.57 +    main(sys.argv)
    9.58 +
    9.59 +
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/tools/python/xen/xm/rmlabel.py	Mon Jul 10 17:18:07 2006 +0100
    10.3 @@ -0,0 +1,118 @@
    10.4 +#============================================================================
    10.5 +# This library is free software; you can redistribute it and/or
    10.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    10.7 +# License as published by the Free Software Foundation.
    10.8 +#
    10.9 +# This library is distributed in the hope that it will be useful,
   10.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   10.12 +# Lesser General Public License for more details.
   10.13 +#
   10.14 +# You should have received a copy of the GNU Lesser General Public
   10.15 +# License along with this library; if not, write to the Free Software
   10.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   10.17 +#============================================================================
   10.18 +# Copyright (C) 2006 International Business Machines Corp.
   10.19 +# Author: Bryan D. Payne <bdpayne@us.ibm.com>
   10.20 +#============================================================================
   10.21 +
   10.22 +"""Remove a label from a domain configuration file or a resoruce.
   10.23 +"""
   10.24 +import sys, os, re
   10.25 +import string
   10.26 +import traceback
   10.27 +from xen.util import dictio
   10.28 +from xen.util import security
   10.29 +
   10.30 +def usage():
   10.31 +    print "\nUsage: xm rmlabel dom <configfile>"
   10.32 +    print "       xm rmlabel res <resource>\n"
   10.33 +    print "  This program removes an acm_label entry from the 'configfile'"
   10.34 +    print "  for a domain or from the global resource label file for a"
   10.35 +    print "  resource. If the label does not exist for the given domain or"
   10.36 +    print "  resource, then rmlabel fails.\n"
   10.37 +
   10.38 +
   10.39 +def rm_resource_label(resource):
   10.40 +    """Removes a resource label from the global resource label file.
   10.41 +    """
   10.42 +    # read in the resource file
   10.43 +    file = security.res_label_filename
   10.44 +    try:
   10.45 +        access_control = dictio.dict_read("resources", file)
   10.46 +    except:
   10.47 +        security.err("Resource file not found, cannot remove label!")
   10.48 +
   10.49 +    # remove the entry and update file
   10.50 +    if access_control.has_key(resource):
   10.51 +        del access_control[resource]
   10.52 +        dictio.dict_write(access_control, "resources", file)
   10.53 +    else:
   10.54 +        security.err("Label does not exist in resource label file.")
   10.55 +
   10.56 +
   10.57 +def rm_domain_label(configfile):
   10.58 +    # open the domain config file
   10.59 +    fd = None
   10.60 +    file = None
   10.61 +    if configfile[0] == '/':
   10.62 +        fd = open(configfile, "rb")
   10.63 +    else:
   10.64 +        for prefix in [".", "/etc/xen"]:
   10.65 +            file = prefix + "/" + configfile
   10.66 +            if os.path.isfile(file):
   10.67 +                fd = open(file, "rb")
   10.68 +                break
   10.69 +    if not fd:
   10.70 +        security.err("Configuration file '"+configfile+"' not found.")
   10.71 +
   10.72 +    # read in the domain config file, removing label
   10.73 +    ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
   10.74 +    ac_exit_re = re.compile(".*'\].*")
   10.75 +    file_contents = ""
   10.76 +    comment = 0
   10.77 +    removed = 0
   10.78 +    for line in fd.readlines():
   10.79 +        if ac_entry_re.match(line):
   10.80 +            comment = 1
   10.81 +        if comment:
   10.82 +            removed = 1
   10.83 +            line = "#"+line
   10.84 +        if comment and ac_exit_re.match(line):
   10.85 +            comment = 0
   10.86 +        file_contents = file_contents + line
   10.87 +    fd.close()
   10.88 +
   10.89 +    # send error message if we didn't find anything to remove
   10.90 +    if not removed:
   10.91 +        security.err("Label does not exist in domain configuration file.")
   10.92 +
   10.93 +    # write the data back out to the file
   10.94 +    fd = open(file, "wb")
   10.95 +    fd.writelines(file_contents)
   10.96 +    fd.close()
   10.97 +
   10.98 +
   10.99 +def main (argv):
  10.100 +    try:
  10.101 +        if len(argv) != 3:
  10.102 +            usage()
  10.103 +            return
  10.104 +
  10.105 +        if argv[1].lower() == "dom":
  10.106 +            configfile = argv[2]
  10.107 +            rm_domain_label(configfile)
  10.108 +        elif argv[1].lower() == "res":
  10.109 +            resource = argv[2]
  10.110 +            rm_resource_label(resource)
  10.111 +        else:
  10.112 +            usage()
  10.113 +
  10.114 +    except security.ACMError:
  10.115 +        traceback.print_exc(limit=1)
  10.116 +
  10.117 +
  10.118 +if __name__ == '__main__':
  10.119 +    main(sys.argv)
  10.120 +
  10.121 +