]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ipc/ipc_object.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_object.c
index df9081bef58c7c8f7408615c01121fc86eb6c3b1..176e80ec827375fef40e9aca3739f29b9af2043d 100644 (file)
@@ -1,16 +1,19 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
@@ -20,7 +23,7 @@
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_COPYRIGHT@
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
+/*
+ * NOTICE: This file was modified by McAfee Research in 2004 to introduce
+ * support for mandatory and extensible security protections.  This notice
+ * is included in support of clause 2.2 (b) of the Apple Public License,
+ * Version 2.0.
+ * Copyright (c) 2005-2006 SPARTA, Inc.
+ */
 /*
  */
 /*
 
 #include <mach_rt.h>
 
+#include <mach/mach_types.h>
 #include <mach/boolean.h>
 #include <mach/kern_return.h>
 #include <mach/port.h>
 #include <mach/message.h>
+
+#include <kern/kern_types.h>
 #include <kern/misc_protos.h>
+#include <kern/ipc_kobject.h>
+
+#include <ipc/ipc_types.h>
 #include <ipc/port.h>
 #include <ipc/ipc_space.h>
 #include <ipc/ipc_entry.h>
 #include <ipc/ipc_hash.h>
 #include <ipc/ipc_right.h>
 #include <ipc/ipc_notify.h>
+#include <ipc/ipc_port.h>
 #include <ipc/ipc_pset.h>
+#include <ipc/ipc_labelh.h>
+
+#include <security/mac_mach_internal.h>
 
 zone_t ipc_object_zones[IOT_NUMBER];
 
@@ -234,9 +254,6 @@ ipc_object_alloc_dead(
        ipc_entry_t entry;
        kern_return_t kr;
 
-       int i;
-
-
        kr = ipc_entry_alloc(space, namep, &entry);
        if (kr != KERN_SUCCESS)
                return kr;
@@ -272,9 +289,6 @@ ipc_object_alloc_dead_name(
        ipc_entry_t entry;
        kern_return_t kr;
 
-       int i;
-
-
        kr = ipc_entry_alloc_name(space, name, &entry);
        if (kr != KERN_SUCCESS)
                return kr;
@@ -332,6 +346,9 @@ ipc_object_alloc(
                ipc_port_t port = (ipc_port_t)object;
 
                bzero((char *)port, sizeof(*port));
+#if CONFIG_MACF_MACH
+               mac_port_label_init(&port->ip_label);
+#endif
        } else if (otype == IOT_PORT_SET) {
                ipc_pset_t pset = (ipc_pset_t)object;
 
@@ -339,7 +356,7 @@ ipc_object_alloc(
        }
 
        io_lock_init(object);
-       *namep = (mach_port_name_t)object;
+       *namep = CAST_MACH_PORT_TO_NAME(object);
        kr = ipc_entry_alloc(space, namep, &entry);
        if (kr != KERN_SUCCESS) {
                io_free(otype, object);
@@ -400,6 +417,9 @@ ipc_object_alloc_name(
                ipc_port_t port = (ipc_port_t)object;
 
                bzero((char *)port, sizeof(*port));
+#if CONFIG_MACF_MACH
+               mac_port_label_init(&port->ip_label);
+#endif
        } else if (otype == IOT_PORT_SET) {
                ipc_pset_t pset = (ipc_pset_t)object;
 
@@ -488,8 +508,6 @@ ipc_object_copyin(
        ipc_port_t soright;
        kern_return_t kr;
 
-       int i;
-
        /*
         *      Could first try a read lock when doing
         *      MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND,
@@ -520,7 +538,8 @@ ipc_object_copyin(
  *             Copyin a naked capability from the kernel.
  *
  *             MACH_MSG_TYPE_MOVE_RECEIVE
- *                     The receiver must be ipc_space_kernel.
+ *                     The receiver must be ipc_space_kernel
+ *                     or the receive right must already be in limbo.
  *                     Consumes the naked receive right.
  *             MACH_MSG_TYPE_COPY_SEND
  *                     A naked send right must be supplied.
@@ -554,14 +573,15 @@ ipc_object_copyin_from_kernel(
 
                ip_lock(port);
                assert(ip_active(port));
-               assert(port->ip_receiver_name != MACH_PORT_NULL);
-               assert(port->ip_receiver == ipc_space_kernel);
+               if (port->ip_destination != IP_NULL) {
+                       assert(port->ip_receiver == ipc_space_kernel);
 
-               /* relevant part of ipc_port_clear_receiver */
-               ipc_port_set_mscount(port, 0);
+                       /* relevant part of ipc_port_clear_receiver */
+                       ipc_port_set_mscount(port, 0);
 
-               port->ip_receiver_name = MACH_PORT_NULL;
-               port->ip_destination = IP_NULL;
+                       port->ip_receiver_name = MACH_PORT_NULL;
+                       port->ip_destination = IP_NULL;
+               }
                ip_unlock(port);
                break;
            }
@@ -594,9 +614,11 @@ ipc_object_copyin_from_kernel(
                break;
            }
 
-           case MACH_MSG_TYPE_MOVE_SEND:
+           case MACH_MSG_TYPE_MOVE_SEND: {
                /* move naked send right into the message */
+               assert(((ipc_port_t)object)->ip_srights);
                break;
+           }
 
            case MACH_MSG_TYPE_MAKE_SEND_ONCE: {
                ipc_port_t port = (ipc_port_t) object;
@@ -611,9 +633,11 @@ ipc_object_copyin_from_kernel(
                break;
            }
 
-           case MACH_MSG_TYPE_MOVE_SEND_ONCE:
+           case MACH_MSG_TYPE_MOVE_SEND_ONCE: {
                /* move naked send-once right into the message */
+               assert(((ipc_port_t)object)->ip_sorights);
                break;
+           }
 
            default:
                panic("ipc_object_copyin_from_kernel: strange rights");
@@ -657,6 +681,42 @@ ipc_object_destroy(
        }
 }
 
+/*
+ *     Routine:        ipc_object_destroy_dest
+ *     Purpose:
+ *             Destroys a naked capability for the destination of
+ *             of a message. Consumes a ref for the object.
+ *
+ *     Conditions:
+ *             Nothing locked.
+ */
+
+void
+ipc_object_destroy_dest(
+       ipc_object_t            object,
+       mach_msg_type_name_t    msgt_name)
+{
+       assert(IO_VALID(object));
+       assert(io_otype(object) == IOT_PORT);
+
+       switch (msgt_name) {
+           case MACH_MSG_TYPE_PORT_SEND:
+               ipc_port_release_send((ipc_port_t) object);
+               break;
+
+           case MACH_MSG_TYPE_PORT_SEND_ONCE:
+               if (io_active(object) && 
+                   !ip_full_kernel((ipc_port_t) object))
+                       ipc_notify_send_once((ipc_port_t) object);
+               else
+                       ipc_port_release_sonce((ipc_port_t) object);
+               break;
+
+           default:
+               panic("ipc_object_destroy_dest: strange rights");
+       }
+}
+
 /*
  *     Routine:        ipc_object_copyout
  *     Purpose:
@@ -705,7 +765,7 @@ ipc_object_copyout(
                        break;
                }
 
-               name = (mach_port_name_t)object;
+               name = CAST_MACH_PORT_TO_NAME(object);
                kr = ipc_entry_get(space, &name, &entry);
                if (kr != KERN_SUCCESS) {
                        /* unlocks/locks space, so must start again */
@@ -776,8 +836,6 @@ ipc_object_copyout_name(
        ipc_entry_t entry;
        kern_return_t kr;
 
-       int i;
-
        assert(IO_VALID(object));
        assert(io_otype(object) == IOT_PORT);
 
@@ -920,6 +978,7 @@ ipc_object_copyout_dest(
 
            default:
                panic("ipc_object_copyout_dest: strange rights");
+               name = MACH_PORT_DEAD;
        }
 
        *namep = name;
@@ -948,8 +1007,6 @@ ipc_object_rename(
        ipc_entry_t oentry, nentry;
        kern_return_t kr;
        
-       int i;
-
        kr = ipc_entry_alloc_name(space, nname, &nentry);
        if (kr != KERN_SUCCESS)
                return kr;
@@ -975,7 +1032,31 @@ ipc_object_rename(
        return kr;
 }
 
-#if    MACH_ASSERT
+/*
+ * Get a label out of a port, to be used by a kernel call
+ * that takes a security label as a parameter. In this case, we want
+ * to use the label stored in the label handle and not the label on its
+ * port.
+ *
+ * The port should be locked for this call. The lock protecting
+ * label handle contents should not be necessary, as they can only
+ * be modified when a label handle with one reference is a task label.
+ * User allocated label handles can never be modified.
+ */
+#if CONFIG_MACF_MACH
+struct label *io_getlabel (ipc_object_t objp)
+{
+       ipc_port_t port = (ipc_port_t)objp;
+
+       assert(io_otype(objp) == IOT_PORT);
+
+       if (ip_kotype(port) == IKOT_LABELH)
+               return &((ipc_labelh_t) port->ip_kobject)->lh_label;
+       else
+               return &port->ip_label;
+}
+#endif
+
 /*
  *     Check whether the object is a port if so, free it.  But
  *     keep track of that fact.
@@ -989,13 +1070,11 @@ io_free(
 
        if (otype == IOT_PORT) {
                port = (ipc_port_t) object;
-#if    MACH_ASSERT
-               ipc_port_track_dealloc(port);
-#endif /* MACH_ASSERT */
+               ipc_port_finalize(port);
        }
-       zfree(ipc_object_zones[otype], (vm_offset_t) object);
+       io_lock_destroy(object);
+       zfree(ipc_object_zones[otype], object);
 }
-#endif /* MACH_ASSERT */
 
 #include <mach_kdb.h>
 #if    MACH_KDB
@@ -1010,7 +1089,7 @@ io_free(
  *             Pretty-print an object for kdb.
  */
 
-char *ikot_print_array[IKOT_MAX_TYPE] = {
+const char *ikot_print_array[IKOT_MAX_TYPE] = {
        "(NONE)             ",
        "(THREAD)           ",
        "(TASK)             ",
@@ -1043,10 +1122,18 @@ char *ikot_print_array[IKOT_MAX_TYPE] = {
        "(IOKIT_CONNECT)    ",
        "(IOKIT_OBJECT)     ",  /* 30 */
        "(UPL)              ",
-                                               /* << new entries here  */
+       "(MEM_OBJ_CONTROL)  ",
+       "(AU_SESSIONPORT)   ",  /* 33 */
+       "(FILEPORT)", /* 34 */
+#if CONFIG_MACF_MACH
+       "(LABELH)           ",
+#endif
+/*
+ * Add new entries here.
+ * Please keep in sync with kern/ipc_kobject.h
+ */
        "(UNKNOWN)          "   /* magic catchall       */
 };
-/* Please keep in sync with kern/ipc_kobject.h */
 
 void
 ipc_object_print(