Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Feb 2006 19:14:06 GMT
From:      Todd Miller <millert@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 92281 for review
Message-ID:  <200602231914.k1NJE6eb014776@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=92281

Change 92281 by millert@millert_g4tower on 2006/02/23 19:13:15

	Style fixes
	
	Add labelh_new_user() function that allocates a label handle
	and a normal ipc port in the specified task's address space.
	The specified label is inserted into the label handle and 
	a send/receive right for the label handle's port is inserted
	into the task's space.  The port name is passed back in the 
	namep parameter.  Unlike labelh_new() the specified task
	now holds the receive right for the port which means that
	when the task dies (or calls mach_port_destroy()), the port
	and label handle will be garbage collected.
	
	Use labelh_new_user() in mac_label_new().
	
	Use labelh_new() in labelh_duplicate().

Affected files ...

.. //depot/projects/trustedbsd/sedarwin7/src/darwin/xnu/osfmk/ipc/ipc_labelh.c#6 edit
.. //depot/projects/trustedbsd/sedarwin7/src/darwin/xnu/osfmk/ipc/ipc_labelh.h#6 edit

Differences ...

==== //depot/projects/trustedbsd/sedarwin7/src/darwin/xnu/osfmk/ipc/ipc_labelh.c#6 (text+ko) ====

@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2005 SPARTA, Inc.
+ * Copyright (c) 2005, 2006 SPARTA, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -31,105 +31,162 @@
 
 zone_t ipc_labelh_zone;
 
-kern_return_t mac_label_new (ipc_space_t task, mach_port_name_t *name,
-			     vm_offset_t labelstr)
+/*
+ * Create a new label handle in the task described by the specified space.
+ * The specified label is used in the label handle.  The associated port
+ * name is copied out to namep and the task is granted send and receive rights.
+ */
+kern_return_t
+labelh_new_user(ipc_space_t space, struct label *inl, mach_port_name_t *namep)
 {
-  ipc_labelh_t lh;
-  struct label inl;
-  ipc_port_t   port, sport;
-  kern_return_t kr;
+	kern_return_t kr;
+	ipc_labelh_t lh;
+	ipc_entry_t entry;
+	ipc_port_t port;
+
+	if (space == IS_NULL || space->is_task == NULL)
+		return (KERN_INVALID_TASK);
+
+	/* XXX - perform entrypoint check here */
 
-  if (task == IS_NULL)
-    return (KERN_INVALID_TASK);
+	/*
+	 * Note: the calling task will have a receive right for the port.
+	 * This is different from label handles that reference tasks
+	 * where the kernel holds the receive right and the caller only
+	 * gets a send right.
+	 */
+	kr = ipc_port_alloc(space, namep, &port);
+	if (kr != KERN_SUCCESS)
+		return (kr);
+	ip_reference(port);	/* ipc_port_alloc() does not add a reference */
 
-  mac_init_port_label (&inl);
-  if (mac_internalize_port_label (&inl, labelstr))
-    return KERN_INVALID_ARGUMENT;
+	/* Convert right to MACH_PORT_TYPE_SEND_RECEIVE */
+	port->ip_mscount++;
+	port->ip_srights++;
+	is_write_lock(space);
+	entry = ipc_entry_lookup(space, *namep);
+	if (entry != IE_NULL)
+		entry->ie_bits |= MACH_PORT_TYPE_SEND;
+	is_write_unlock(space);
 
-  port = ipc_port_alloc_kernel();
+	/* Allocate new label handle, insert port and label. */
+	lh = (ipc_labelh_t)zalloc(ipc_labelh_zone);
+	io_lock_init(lh);
+	lh->lh_port = port;
+	lh->lh_label = *inl;
+	lh->lh_type = 0;
+	lh->lh_references = 1;
 
-  lh = (ipc_labelh_t) zalloc(ipc_labelh_zone);
-  io_lock_init(lh);
-  lh->lh_port = port;
-  lh->lh_type = 0;
-  lh->lh_references = 1;
-  lh->lh_label = inl;
-  ipc_kobject_set(port, (ipc_kobject_t)lh, IKOT_LABELH);
+	/* Must call ipc_kobject_set() with port unlocked. */
+	ip_unlock(lh->lh_port);
+	ipc_kobject_set(lh->lh_port, (ipc_kobject_t)lh, IKOT_LABELH);
 
-  sport = ipc_port_make_send_locked(port);
-  ip_release(port);
-  ip_unlock(port);
-  *name = ipc_port_copyout_send (port,task);
-  return 0;
+	return (KERN_SUCCESS);
 }
 
-/* This function should be used to allocate label handles
-   that are stored in other kernel objects, such as tasks.
-   They must be released along with that object.
-   The caller gets one reference, which can be applied to either the
-   port or the ipc_label_t structure itself.
-*/
-ipc_labelh_t labelh_new ()
+kern_return_t
+mac_label_new(ipc_space_t space, mach_port_name_t *namep, vm_offset_t labelstr)
 {
-  ipc_labelh_t lh = (ipc_labelh_t) zalloc(ipc_labelh_zone);
-  io_lock_init(lh);
-  lh->lh_port = ipc_port_alloc_kernel();
-  lh->lh_type = 0;
-  lh->lh_references = 1;
-  ipc_kobject_set(lh->lh_port, (ipc_kobject_t)lh, IKOT_LABELH);
-  ip_unlock(lh->lh_port);
-  return lh;
+	struct label inl;
+	kern_return_t kr;
+
+	mac_init_port_label(&inl);
+	if (mac_internalize_port_label(&inl, labelstr))
+		return (KERN_INVALID_ARGUMENT);
+
+	kr = labelh_new_user(space, &inl, namep);
+	if (kr != KERN_SUCCESS) {
+		mac_destroy_port_label(&inl);
+		return (kr);
+	}
+
+	return (KERN_SUCCESS);
 }
 
-/* call with old locked; returned object is unlocked */
+/*
+ * This function should be used to allocate label handles
+ * that are stored in other kernel objects, such as tasks.
+ * They must be released along with that object.
+ * The caller gets one reference, which can be applied to either the
+ * port or the ipc_label_t structure itself.
+ */
+ipc_labelh_t
+labelh_new(void)
+{
+	ipc_labelh_t lh;
+
+	lh = (ipc_labelh_t)zalloc(ipc_labelh_zone);
+	io_lock_init(lh);
+	lh->lh_port = ipc_port_alloc_kernel();
+	lh->lh_type = 0;
+	lh->lh_references = 1;
+	ip_unlock(lh->lh_port);
+
+	/* Must call ipc_kobject_set() with port unlocked. */
+	ipc_kobject_set(lh->lh_port, (ipc_kobject_t)lh, IKOT_LABELH);
+
+	return (lh);
+}
 
-ipc_labelh_t labelh_duplicate (ipc_labelh_t old)
+/*
+ * Call with old label handle locked.
+ * Returned label handle is unlocked.
+ */
+ipc_labelh_t
+labelh_duplicate(ipc_labelh_t old)
 {
-  ipc_labelh_t lh = (ipc_labelh_t) zalloc(ipc_labelh_zone);
-  io_lock_init(lh);
-  lh->lh_port = ipc_port_alloc_kernel();
-  lh->lh_type = 0;
-  lh->lh_references = 1;
-  ipc_kobject_set(lh->lh_port, (ipc_kobject_t)lh, IKOT_LABELH);
-  mac_init_port_label (&lh->lh_label);
-  mac_copy_port_label (&old->lh_label, &lh->lh_label);
-  ip_unlock(lh->lh_port);
-  return lh;
+	ipc_labelh_t lh;
+
+	lh = labelh_new();
+	ip_lock(lh->lh_port);
+	mac_init_port_label(&lh->lh_label);
+	mac_copy_port_label(&old->lh_label, &lh->lh_label);
+	ip_unlock(lh->lh_port);
+	return (lh);
 }
 
-/* call with old locked; returns a locked object */
+/*
+ * Call with old label handle locked.
+ * Returned label handle is locked.
+ */
+ipc_labelh_t
+labelh_modify(ipc_labelh_t old)
+{
+	ipc_labelh_t lh;
 
-ipc_labelh_t labelh_modify (ipc_labelh_t old)
-{
-  if (old->lh_references == 1)
-    return old;
-  ipc_labelh_t lh = labelh_duplicate (old);
-  lh_release(old);
-  lh_check_unlock (old);
-  lh_lock (lh);
-  return lh;
+	if (old->lh_references == 1)
+		return (old);
+	lh = labelh_duplicate(old);
+	lh_release(old);
+	lh_check_unlock(old);
+	lh_lock(lh);
+	return (lh);
 }
 
-/* add or drop a reference on a label handle; not locked */
-
-ipc_labelh_t labelh_reference (ipc_labelh_t lh)
+/*
+ * Add or drop a reference on an (unlocked) label handle.
+ */
+ipc_labelh_t
+labelh_reference(ipc_labelh_t lh)
 {
-  lh_lock(lh);
-  lh_reference(lh);
-  lh_unlock(lh);
-  return lh;
+	lh_lock(lh);
+	lh_reference(lh);
+	lh_unlock(lh);
+	return (lh);
 }
 
-void labelh_release(ipc_labelh_t lh)
+void
+labelh_release(ipc_labelh_t lh)
 {
-  lh_lock(lh);
-  lh_release(lh);
-  lh_check_unlock(lh);
+	lh_lock(lh);
+	lh_release(lh);
+	lh_check_unlock(lh);
 }
 
-void lh_free (ipc_labelh_t lh)
+void
+lh_free(ipc_labelh_t lh)
 {
-    ipc_object_release(&lh->lh_port->ip_object);
-    mac_destroy_port_label (&lh->lh_label);
-    zfree(ipc_labelh_zone, (vm_offset_t)lh);
+	ipc_object_release(&lh->lh_port->ip_object);
+	mac_destroy_port_label(&lh->lh_label);
+	zfree(ipc_labelh_zone, (vm_offset_t)lh);
 }

==== //depot/projects/trustedbsd/sedarwin7/src/darwin/xnu/osfmk/ipc/ipc_labelh.h#6 (text+ko) ====

@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2005 SPARTA, Inc.
+ * Copyright (c) 2005, 2006 SPARTA, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -44,16 +44,18 @@
 #endif
 } *ipc_labelh_t;
 
-ipc_labelh_t labelh_duplicate (ipc_labelh_t old);
-ipc_labelh_t labelh_modify (ipc_labelh_t old);
-void labelh_release (ipc_labelh_t lh);
-ipc_labelh_t labelh_reference (ipc_labelh_t lh);
-void lh_free (ipc_labelh_t lh);
+ipc_labelh_t labelh_duplicate(ipc_labelh_t old);
+ipc_labelh_t labelh_modify(ipc_labelh_t old);
+ipc_labelh_t labelh_new(void);
+kern_return_t labelh_new_user(ipc_space_t, struct label *, mach_port_name_t *);
+void labelh_release(ipc_labelh_t lh);
+ipc_labelh_t labelh_reference(ipc_labelh_t lh);
+void lh_free(ipc_labelh_t lh);
 
-#define lh_reference(lh) ((lh)->lh_references++)
+#define lh_reference(lh)	((lh)->lh_references++)
 #define lh_release(lh)					\
 MACRO_BEGIN						\
-  assert ((lh)->lh_references > 0);			\
+  assert((lh)->lh_references > 0);			\
 	(lh)->lh_references--;				\
 MACRO_END
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200602231914.k1NJE6eb014776>