Skip to contentSkip to conversation

Restructuring ACM-related code in do_domctl

PATCH In review Stefan Berger 3 participants 7 messages Apr 21, 2007 – Apr 23, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Restructuring ACM-related code in do_domctl

This patch restructures the ACM-related code in the do_domctl() function so that the lock to the policy can be fetched in the individual operations, i.e., XEN_DOMCTL_createdomain, and the pair of locking functions acm_rlock_policy()/acm_runlock_policy() don't take the function parameter anymore.

xen-unstable.hg/xen/common/domctl.c +60 -46
Index: root/xen-unstable.hg/xen/common/domctl.c
===================================================================
@@ -176,7 +176,6 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
 {
     long ret = 0;
     struct xen_domctl curop, *op = &curop;
-    void *ssid = NULL; /* save security ptr between pre and post/fail hooks */
     static DEFINE_SPINLOCK(domctl_lock);
 
     if ( !IS_PRIV(current->domain) )
@@ -188,13 +187,6 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
     if ( op->interface_version != XEN_DOMCTL_INTERFACE_VERSION )
         return -EACCES;
 
-    acm_rlock_policy(op->cmd);
-
-    if ( acm_pre_domctl(op, &ssid) ) {
-        acm_runlock_policy(op->cmd);
-        return -EPERM;
-    }
-
     spin_lock(&domctl_lock);
 
     switch ( op->cmd )
@@ -304,52 +296,69 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
         static domid_t rover = 0;
         unsigned int domcr_flags;
 
-        if ( supervisor_mode_kernel ||
-             (op->u.createdomain.flags & ~XEN_DOMCTL_CDF_hvm_guest) ) {
-            ret = -EINVAL;
+        acm_rlock_policy();
+
+        if ( acm_pre_domctl_createdomain(op) ) {
+            acm_runlock_policy();
+            ret = -EPERM;
             break;
         }
 
-        dom = op->domain;
-        if ( (dom > 0) && (dom < DOMID_FIRST_RESERVED) )
-        {
-            ret = -EINVAL;
-            if ( !is_free_domid(dom) )
+        do {
+            if ( supervisor_mode_kernel ||
+                 (op->u.createdomain.flags & ~XEN_DOMCTL_CDF_hvm_guest) ) {
+                ret = -EINVAL;
                 break;
-        }
-        else
-        {
-            for ( dom = rover + 1; dom != rover; dom++ )
+            }
+
+            dom = op->domain;
+            if ( (dom > 0) && (dom < DOMID_FIRST_RESERVED) )
+            {
+                ret = -EINVAL;
+                if ( !is_free_domid(dom) )
+                    break;
+            }
+            else
             {
-                if ( dom == DOMID_FIRST_RESERVED )
-                    dom = 0;
-                if ( is_free_domid(dom) )
+                for ( dom = rover + 1; dom != rover; dom++ )
+                {
+                    if ( dom == DOMID_FIRST_RESERVED )
+                        dom = 0;
+                    if ( is_free_domid(dom) )
+                        break;
+                }
+
+                ret = -ENOMEM;
+                if ( dom == rover )
                     break;
+
+                rover = dom;
             }
 
+            domcr_flags = 0;
+            if ( op->u.createdomain.flags & XEN_DOMCTL_CDF_hvm_guest )
+                domcr_flags |= DOMCRF_hvm;
+
             ret = -ENOMEM;
-            if ( dom == rover )
+            if ( (d = domain_create(dom, domcr_flags)) == NULL )
                 break;
 
-            rover = dom;
-        }
-
-        domcr_flags = 0;
-        if ( op->u.createdomain.flags & XEN_DOMCTL_CDF_hvm_guest )
-            domcr_flags |= DOMCRF_hvm;
+            ret = 0;
 
-        ret = -ENOMEM;
-        if ( (d = domain_create(dom, domcr_flags)) == NULL )
-            break;
+            memcpy(d->handle, op->u.createdomain.handle,
+                   sizeof(xen_domain_handle_t));
 
-        ret = 0;
+            op->domain = d->domain_id;
+            if ( copy_to_guest(u_domctl, op, 1) )
+                ret = -EFAULT;
+        } while (0);
 
-        memcpy(d->handle, op->u.createdomain.handle,
-               sizeof(xen_domain_handle_t));
+        if ( ret == 0 )
+            acm_post_domctl_createdomain(op);
+        else
+            acm_fail_domctl_createdomain(op);
 
-        op->domain = d->domain_id;
-        if ( copy_to_guest(u_domctl, op, 1) )
-            ret = -EFAULT;
+        acm_runlock_policy();
     }
     break;
 
@@ -399,6 +408,13 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
     case XEN_DOMCTL_destroydomain:
     {
         struct domain *d = rcu_lock_domain_by_id(op->domain);
+        void *ssid; /* save security ptr between pre and post/fail hooks */
+
+        if ( acm_pre_domctl_destroydomain(op, &ssid) ) {
+            ret = -EPERM;
+            break;
+        }
+
         ret = -ESRCH;
         if ( d != NULL )
         {
@@ -410,6 +426,11 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
             }
             rcu_unlock_domain(d);
         }
+
+        if ( ret == 0 )
+            acm_post_domctl_destroydomain(op, &ssid);
+        else
+            acm_fail_domctl_destroydomain(op, &ssid);
     }
     break;
 
@@ -722,13 +743,6 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
 
     spin_unlock(&domctl_lock);
 
-    if ( ret == 0 )
-        acm_post_domctl(op, &ssid);
-    else
-        acm_fail_domctl(op, &ssid);
-
-    acm_runlock_policy(op->cmd);
-
     return ret;
 }
 
xen-unstable.hg/xen/include/acm/acm_hooks.h +72 -76

196 lines not on this page · raw message

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

#1 lore raw

I think that relying on domctl wrapper functions to do necessary label bookkeeping on domain create/destroy is making things more complicated than they need to be. Wrapper functions are obviously the right answer for most ACM functions (since they are allow/deny checks, external to the core logic of the operation being checked), but in the case of domain create/destroy ACM is more part of the process.

So, how about having functions: domain_acm_create() and domain_acm_destroy()? The former would be called from domain_create(), the latter from the error path of domain_create() and from domain_destroy().

You can then have appropriate critical regions *within* those functions (not across them) that you synchronise against when relabelling the world. And you shouldn't then need to modify do_domctl() at all?

-- Keir

Quoted 12 lines · Stefan Berger

This patch restructures the ACM-related code in the do_domctl() function so that the lock to the policy can be fetched in the individual operations, i.e., XEN_DOMCTL_createdomain, and the pair of locking functions acm_rlock_policy()/acm_runlock_policy() don't take the function parameter anymore.

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

#2 lore raw

[email protected] wrote on 04/22/2007 06:07:05 AM:

Quoted 2 lines

I think that relying on domctl wrapper functions to do necessary label bookkeeping on domain create/destroy is making things more complicated

than

Quoted 1 line

they need to be. Wrapper functions are obviously the right answer for

most

Quoted 1 line

ACM functions (since they are allow/deny checks, external to the core

logic

Quoted 1 line

of the operation being checked), but in the case of domain

create/destroy

Quoted 4 lines

ACM is more part of the process.

So, how about having functions: domain_acm_create() and domain_acm_destroy()? The former would be called from domain_create(),

the

Quoted 3 lines

latter from the error path of domain_create() and from domain_destroy().

You can then have appropriate critical regions *within* those functions

(not

Quoted 1 line

across them) that you synchronise against when relabelling the world.

And

Quoted 1 line

you shouldn't then need to modify do_domctl() at all?

At the beginning of do_domctl() there's the call to acm_pre_domctl, which ends up in its callpath in chwall_pre_domain_create to check whether under the current policy the domain is allowed to be created and it grabs the lock to the policy before doing that.
At the end of the do_domctl() there's the call to acm_post_domctl, in case everything went fine with creating the domain for example. Here it ends up in its callpath in chwall_post_domain_create where it again grabs the lock to the policy and under the assumption that the policy hasn't changed modifies a counter array (running_types).
If the policy is changed in between those calls, i.e. the chinese wall part is changed such that the domain would not be allowed to exist under the new policy, the post-domain-create call would still go through. That's what I want to prevent with a continously-held lock that spans the evaluation at the beginning and the modification of that counter array at the end.

Stefan

Quoted 6 lines

-- Keir

Quoted 1 line · Stefan Berger

This patch restructures the ACM-related code in the do_domctl()

function

Quoted 14 lines
Quoted 11 lines

so that the lock to the policy can be fetched in the individual operations, i.e., XEN_DOMCTL_createdomain, and the pair of locking functions acm_rlock_policy()/acm_runlock_policy() don't take the function parameter anymore.

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

Quoted 4 lines

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

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

#3 lore raw
Quoted 6 lines · Stefan Berger

At the beginning of do_domctl() there's the call to acm_pre_domctl, which ends up in its callpath in chwall_pre_domain_create to check whether under the current policy the domain is allowed to be created and it grabs the lock to the policy before doing that.
At the end of the do_domctl() there's the call to acm_post_domctl, in case everything went fine with creating the domain for example. Here it ends up in

Quoted 8 lines

its callpath in chwall_post_domain_create where it again grabs the lock to the policy and under the assumption that the policy hasn't changed modifies a counter array (running_types).
If the policy is changed in between those calls, i.e. the chinese wall part is changed such that the domain would not be allowed to exist under the new policy, the post-domain-create call would still go through. That's what I want to prevent with a continously-held lock that spans the evaluation at the beginning and the modification of that counter array at the end.

If you did this with a straightforward domain_create() hook, you could update state at the same time as doing the policy check. Your domain_destroy() hook would be called if the creation subsequently failed to commit. And if the policy changes at any time after the call to your domain_create() hook, you¹ve already updated your ACM state so you can see the new domain via some internal structure you presumably maintains, and hence can re-evaluate the decision under the new policy.

In general, keeping checks and state updates together is nice compared with pushing them to pre/post hooks with locks implicitly held across the two. That¹s just plain gross imo.

That is, an architecture where you have a Œpre-doing-stuff¹ hook and a Œpre-destroying-stuff¹ hook, where the latter is also called when the doing-stuff action turns out to fail, is nicer than pre/post hooks.

-- Keir

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

#4 lore raw

Keir Fraser <[email protected]> wrote on 04/22/2007 02:34:00 PM:

Quoted 18 lines · Stefan Berger

At the beginning of do_domctl() there's the call to acm_pre_domctl, which ends up in its callpath in chwall_pre_domain_create to check whether under the current policy the domain is allowed to be created and it grabs the lock to the policy before doing that.
At the end of the do_domctl() there's the call to acm_post_domctl, in case everything went fine with creating the domain for example. Here it ends up in its callpath in chwall_post_domain_create where it again grabs the lock to the policy and under the assumption that the policy hasn't changed modifies a counter array (running_types). If the policy is changed in between those calls, i.e. the chinese wall part is changed such that the domain would not be allowed to exist under the new policy, the post-domain-create call would still go through. That's what I want to prevent with a continously-held lock that spans the evaluation at the beginning and the modification of that counter array at the end.

Quoted 8 lines

If you did this with a straightforward domain_create() hook, you could update state at the same time as doing the policy check. Your domain_destroy() hook would be called if the creation subsequently failed to commit. And if the policy changes at any time after the call to your domain_create() hook, you?ve already updated your ACM state so you can see the new domain via some internal structure you presumably maintains, and hence can re-evaluate the decision under the new policy.

The 'internal structure' that represents the label of the domain is maintained through a pointer connected to the domain structure. For that reason I would intend to hold the lock on the policy until the domain has been added to the list of domains.

Quoted 8 lines

In general, keeping checks and state updates together is nice compared with pushing them to pre/post hooks with locks implicitly held across the two. That?s just plain gross imo.

That is, an architecture where you have a ?pre-doing-stuff? hook and a ?pre-destroying-stuff? hook, where the latter is also called when the doing-stuff action turns out to fail, is nicer than pre/post hooks.

I'll change that in do_domctl().

Stefan

Quoted 2 lines

-- Keir

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

#5 lore raw
Quoted 5 lines · Stefan Berger

The 'internal structure' that represents the label of the domain is maintained through a pointer connected to the domain structure. For that reason I would intend to hold the lock on the policy until the domain has been added to the list of domains.

In this case it probably makes sense to maintain your own domain list within your ACM subsystem, so that you can always find any domain that has been registered with you via your domain_create hook. I suggest a policy-specific structure pointed at by struct domain (void *security ?). You can dynamically allocate this struct on domain_create() and it can contain a list_head to chain domains through. You can also hide SSID in there (it¹s going to need to move out of generic struct domain when XSM comes along, as sHype won¹t be the only game in town any longer).

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

That is, an architecture where you have a Œpre-doing-stuff¹ hook and a Œpre-destroying-stuff¹ hook, where the latter is also called when the doing-stuff action turns out to fail, is nicer than pre/post hooks.

I'll change that in do_domctl().

If you do it in domain_create() itself you¹ll also get your hook called for creation of dom0. That will probably avoid some nasty hacking around (like you have right now in chwall_post_domain_create, because the pre-hook is missed out for dom0).

-- Keir

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

#6 lore raw
In reply to Keir Fraser
Quoted 7 lines · Keir Fraser

I think that relying on domctl wrapper functions to do necessary label bookkeeping on domain create/destroy is making things more complicated than they need to be. Wrapper functions are obviously the right answer for most ACM functions (since they are allow/deny checks, external to the core logic of the operation being checked), but in the case of domain create/destroy ACM is more part of the process.

Quoted 8 lines

So, how about having functions: domain_acm_create() and domain_acm_destroy()? The former would be called from domain_create(), the latter from the error path of domain_create() and from domain_destroy().

You can then have appropriate critical regions *within* those functions (not across them) that you synchronise against when relabelling the world. And you shouldn't then need to modify do_domctl() at all?

I'll chime in here, although my response applies to later parts of the thread.

XSM provides hooks in domain_create and domain_destroy so that modules can do these kinds of operations. There are some side issues that need to be considered in the creation of these hooks, such as when domain_create is first called and when any security module is initialized. In my experience it has been straightforward to factor the existing sHype(ACM) module to take advantage of these additional hooks.

A nice side effect (for sHype(ACM)) of the domain_destroy hook is we can clean up the destroy domain operation in do_domctl by removing the local ssid variable as well ensure that the security object is destroyed/freed last.

I'll be posting some updated and cleaned up XSM patches later today/tomorrow. I realize many may be distracted with the 3.0.5 release candidate, but this should help me get XSM into good shape for when 3.0.6 opens up for patches.

George