From owner-trustedbsd-cvs@FreeBSD.ORG Thu Feb 23 19:14:14 2006 Return-Path: X-Original-To: trustedbsd-cvs@freebsd.org Delivered-To: trustedbsd-cvs@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3E42016A420 for ; Thu, 23 Feb 2006 19:14:14 +0000 (GMT) (envelope-from owner-perforce@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [209.31.154.42]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2C6F843D49 for ; Thu, 23 Feb 2006 19:14:13 +0000 (GMT) (envelope-from owner-perforce@freebsd.org) Received: from mx2.freebsd.org (mx2.freebsd.org [216.136.204.119]) by cyrus.watson.org (Postfix) with ESMTP id ADDAF46BAE for ; Thu, 23 Feb 2006 14:13:55 -0500 (EST) Received: from hub.freebsd.org (hub.freebsd.org [216.136.204.18]) by mx2.freebsd.org (Postfix) with ESMTP id D4B4B56AF7; Thu, 23 Feb 2006 19:14:07 +0000 (GMT) (envelope-from owner-perforce@freebsd.org) Received: by hub.freebsd.org (Postfix, from userid 32767) id C61A716A423; Thu, 23 Feb 2006 19:14:07 +0000 (GMT) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6A32216A420 for ; Thu, 23 Feb 2006 19:14:07 +0000 (GMT) (envelope-from millert@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id B1E7B43D4C for ; Thu, 23 Feb 2006 19:14:06 +0000 (GMT) (envelope-from millert@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id k1NJE6Dj014779 for ; Thu, 23 Feb 2006 19:14:06 GMT (envelope-from millert@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id k1NJE6eb014776 for perforce@freebsd.org; Thu, 23 Feb 2006 19:14:06 GMT (envelope-from millert@freebsd.org) Date: Thu, 23 Feb 2006 19:14:06 GMT Message-Id: <200602231914.k1NJE6eb014776@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to millert@freebsd.org using -f From: Todd Miller To: Perforce Change Reviews Cc: Subject: PERFORCE change 92281 for review X-BeenThere: trustedbsd-cvs@FreeBSD.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: TrustedBSD CVS and Perforce commit message list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Feb 2006 19:14:14 -0000 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