#include <mach_debug.h>
#include <mach_rt.h>
#include <mach/port.h>
#include <mach/kern_return.h>
#include <mach/notify.h>
#include <mach/mach_param.h>
#include <mach/vm_param.h>
#include <mach/vm_prot.h>
#include <mach/vm_map.h>
#include <kern/task.h>
#include <kern/counters.h>
#include <kern/thread.h>
#include <kern/kalloc.h>
#include <mach/mach_port_server.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <ipc/ipc_entry.h>
#include <ipc/ipc_space.h>
#include <ipc/ipc_object.h>
#include <ipc/ipc_notify.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_pset.h>
#include <ipc/ipc_right.h>
#include <ipc/ipc_kmsg.h>
#include <ipc/ipc_labelh.h>
#include <kern/misc_protos.h>
#include <security/mac_mach_internal.h>
#include <mach/security_server.h>
void mach_port_names_helper(
ipc_port_timestamp_t timestamp,
ipc_entry_t entry,
mach_port_name_t name,
mach_port_name_t *names,
mach_port_type_t *types,
ipc_entry_num_t *actualp);
void mach_port_gst_helper(
ipc_pset_t pset,
ipc_port_t port,
ipc_entry_num_t maxnames,
mach_port_name_t *names,
ipc_entry_num_t *actualp);
static mach_port_qos_t qos_template;
void
mach_port_names_helper(
ipc_port_timestamp_t timestamp,
ipc_entry_t entry,
mach_port_name_t name,
mach_port_name_t *names,
mach_port_type_t *types,
ipc_entry_num_t *actualp)
{
ipc_entry_bits_t bits;
ipc_port_request_index_t request;
mach_port_type_t type = 0;
ipc_entry_num_t actual;
ipc_port_t port;
bits = entry->ie_bits;
request = entry->ie_request;
port = (ipc_port_t) entry->ie_object;
if (bits & MACH_PORT_TYPE_RECEIVE) {
assert(IP_VALID(port));
if (request != IE_REQ_NONE) {
ip_lock(port);
assert(ip_active(port));
type |= ipc_port_request_type(port, name, request);
ip_unlock(port);
}
} else if (bits & MACH_PORT_TYPE_SEND_RIGHTS) {
mach_port_type_t reqtype;
assert(IP_VALID(port));
ip_lock(port);
reqtype = (request != IE_REQ_NONE) ?
ipc_port_request_type(port, name, request) : 0;
if (ip_active(port) || IP_TIMESTAMP_ORDER(timestamp, port->ip_timestamp)) {
type |= reqtype;
} else {
bits &= ~(IE_BITS_TYPE_MASK);
bits |= MACH_PORT_TYPE_DEAD_NAME;
if (reqtype != 0)
bits++;
}
ip_unlock(port);
}
type |= IE_BITS_TYPE(bits);
actual = *actualp;
names[actual] = name;
types[actual] = type;
*actualp = actual+1;
}
kern_return_t
mach_port_names(
ipc_space_t space,
mach_port_name_t **namesp,
mach_msg_type_number_t *namesCnt,
mach_port_type_t **typesp,
mach_msg_type_number_t *typesCnt)
{
ipc_entry_t table;
ipc_entry_num_t tsize;
mach_port_index_t index;
ipc_entry_num_t actual;
ipc_port_timestamp_t timestamp;
mach_port_name_t *names;
mach_port_type_t *types;
kern_return_t kr;
vm_size_t size;
vm_offset_t addr1;
vm_offset_t addr2;
vm_map_copy_t memory1;
vm_map_copy_t memory2;
assert_static(sizeof(mach_port_name_t) == sizeof(mach_port_type_t));
if (space == IS_NULL)
return KERN_INVALID_TASK;
size = 0;
for (;;) {
ipc_entry_num_t bound;
vm_size_t size_needed;
is_read_lock(space);
if (!is_active(space)) {
is_read_unlock(space);
if (size != 0) {
kmem_free(ipc_kernel_map, addr1, size);
kmem_free(ipc_kernel_map, addr2, size);
}
return KERN_INVALID_TASK;
}
bound = space->is_table_size;
size_needed = round_page(bound * sizeof(mach_port_name_t));
if (size_needed <= size)
break;
is_read_unlock(space);
if (size != 0) {
kmem_free(ipc_kernel_map, addr1, size);
kmem_free(ipc_kernel_map, addr2, size);
}
size = size_needed;
kr = vm_allocate(ipc_kernel_map, &addr1, size, VM_FLAGS_ANYWHERE);
if (kr != KERN_SUCCESS)
return KERN_RESOURCE_SHORTAGE;
kr = vm_allocate(ipc_kernel_map, &addr2, size, VM_FLAGS_ANYWHERE);
if (kr != KERN_SUCCESS) {
kmem_free(ipc_kernel_map, addr1, size);
return KERN_RESOURCE_SHORTAGE;
}
kr = vm_map_wire(ipc_kernel_map, vm_map_trunc_page(addr1),
vm_map_round_page(addr1 + size),
VM_PROT_READ|VM_PROT_WRITE, FALSE);
if (kr != KERN_SUCCESS) {
kmem_free(ipc_kernel_map, addr1, size);
kmem_free(ipc_kernel_map, addr2, size);
return KERN_RESOURCE_SHORTAGE;
}
kr = vm_map_wire(ipc_kernel_map, vm_map_trunc_page(addr2),
vm_map_round_page(addr2 + size),
VM_PROT_READ|VM_PROT_WRITE, FALSE);
if (kr != KERN_SUCCESS) {
kmem_free(ipc_kernel_map, addr1, size);
kmem_free(ipc_kernel_map, addr2, size);
return KERN_RESOURCE_SHORTAGE;
}
}
names = (mach_port_name_t *) addr1;
types = (mach_port_type_t *) addr2;
actual = 0;
timestamp = ipc_port_timestamp();
table = space->is_table;
tsize = space->is_table_size;
for (index = 0; index < tsize; index++) {
ipc_entry_t entry = &table[index];
ipc_entry_bits_t bits = entry->ie_bits;
if (IE_BITS_TYPE(bits) != MACH_PORT_TYPE_NONE) {
mach_port_name_t name;
name = MACH_PORT_MAKE(index, IE_BITS_GEN(bits));
mach_port_names_helper(timestamp, entry, name, names,
types, &actual);
}
}
is_read_unlock(space);
if (actual == 0) {
memory1 = VM_MAP_COPY_NULL;
memory2 = VM_MAP_COPY_NULL;
if (size != 0) {
kmem_free(ipc_kernel_map, addr1, size);
kmem_free(ipc_kernel_map, addr2, size);
}
} else {
vm_size_t size_used;
vm_size_t vm_size_used;
size_used = actual * sizeof(mach_port_name_t);
vm_size_used = round_page(size_used);
kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr1),
vm_map_round_page(addr1 + vm_size_used), FALSE);
assert(kr == KERN_SUCCESS);
kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr2),
vm_map_round_page(addr2 + vm_size_used), FALSE);
assert(kr == KERN_SUCCESS);
kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr1,
(vm_map_size_t)size_used, TRUE, &memory1);
assert(kr == KERN_SUCCESS);
kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr2,
(vm_map_size_t)size_used, TRUE, &memory2);
assert(kr == KERN_SUCCESS);
if (vm_size_used != size) {
kmem_free(ipc_kernel_map,
addr1 + vm_size_used, size - vm_size_used);
kmem_free(ipc_kernel_map,
addr2 + vm_size_used, size - vm_size_used);
}
}
*namesp = (mach_port_name_t *) memory1;
*namesCnt = actual;
*typesp = (mach_port_type_t *) memory2;
*typesCnt = actual;
return KERN_SUCCESS;
}
kern_return_t
mach_port_type(
ipc_space_t space,
mach_port_name_t name,
mach_port_type_t *typep)
{
mach_port_urefs_t urefs;
ipc_entry_t entry;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (name == MACH_PORT_NULL)
return KERN_INVALID_NAME;
if (name == MACH_PORT_DEAD) {
*typep = MACH_PORT_TYPE_DEAD_NAME;
return KERN_SUCCESS;
}
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
kr = ipc_right_info(space, name, entry, typep, &urefs);
#if 1
*typep &= ~(MACH_PORT_TYPE_SPREQUEST | MACH_PORT_TYPE_SPREQUEST_DELAYED);
#endif
return kr;
}
kern_return_t
mach_port_rename(
__unused ipc_space_t space,
__unused mach_port_name_t oname,
__unused mach_port_name_t nname)
{
return KERN_NOT_SUPPORTED;
}
kern_return_t
mach_port_allocate_name(
ipc_space_t space,
mach_port_right_t right,
mach_port_name_t name)
{
kern_return_t kr;
mach_port_qos_t qos = qos_template;
qos.name = TRUE;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_VALUE;
kr = mach_port_allocate_full (space, right, MACH_PORT_NULL,
&qos, &name);
return (kr);
}
kern_return_t
mach_port_allocate(
ipc_space_t space,
mach_port_right_t right,
mach_port_name_t *namep)
{
kern_return_t kr;
mach_port_qos_t qos = qos_template;
kr = mach_port_allocate_full (space, right, MACH_PORT_NULL,
&qos, namep);
return (kr);
}
kern_return_t
mach_port_allocate_qos(
ipc_space_t space,
mach_port_right_t right,
mach_port_qos_t *qosp,
mach_port_name_t *namep)
{
kern_return_t kr;
if (qosp->name)
return KERN_INVALID_ARGUMENT;
kr = mach_port_allocate_full (space, right, MACH_PORT_NULL,
qosp, namep);
return (kr);
}
kern_return_t
mach_port_allocate_full(
ipc_space_t space,
mach_port_right_t right,
mach_port_t proto,
mach_port_qos_t *qosp,
mach_port_name_t *namep)
{
ipc_kmsg_t kmsg = IKM_NULL;
kern_return_t kr;
if (space == IS_NULL)
return (KERN_INVALID_TASK);
if (proto != MACH_PORT_NULL)
return (KERN_INVALID_VALUE);
if (qosp->name) {
if (!MACH_PORT_VALID (*namep))
return (KERN_INVALID_VALUE);
}
if (qosp->prealloc) {
if (qosp->len > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE) {
return KERN_RESOURCE_SHORTAGE;
} else {
mach_msg_size_t size = qosp->len + MAX_TRAILER_SIZE;
if (right != MACH_PORT_RIGHT_RECEIVE)
return (KERN_INVALID_VALUE);
kmsg = (ipc_kmsg_t)ipc_kmsg_prealloc(size);
if (kmsg == IKM_NULL)
return (KERN_RESOURCE_SHORTAGE);
}
}
switch (right) {
case MACH_PORT_RIGHT_RECEIVE:
{
ipc_port_t port;
if (qosp->name)
kr = ipc_port_alloc_name(space, *namep, &port);
else
kr = ipc_port_alloc(space, namep, &port);
if (kr == KERN_SUCCESS) {
if (kmsg != IKM_NULL)
ipc_kmsg_set_prealloc(kmsg, port);
ip_unlock(port);
} else if (kmsg != IKM_NULL)
ipc_kmsg_free(kmsg);
break;
}
case MACH_PORT_RIGHT_PORT_SET:
{
ipc_pset_t pset;
if (qosp->name)
kr = ipc_pset_alloc_name(space, *namep, &pset);
else
kr = ipc_pset_alloc(space, namep, &pset);
if (kr == KERN_SUCCESS)
ips_unlock(pset);
break;
}
case MACH_PORT_RIGHT_DEAD_NAME:
kr = ipc_object_alloc_dead(space, namep);
break;
default:
kr = KERN_INVALID_VALUE;
break;
}
return (kr);
}
kern_return_t
mach_port_destroy(
ipc_space_t space,
mach_port_name_t name)
{
ipc_entry_t entry;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_SUCCESS;
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
kr = ipc_right_destroy(space, name, entry);
return kr;
}
kern_return_t
mach_port_deallocate(
ipc_space_t space,
mach_port_name_t name)
{
ipc_entry_t entry;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_SUCCESS;
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
kr = ipc_right_dealloc(space, name, entry);
return kr;
}
kern_return_t
mach_port_get_refs(
ipc_space_t space,
mach_port_name_t name,
mach_port_right_t right,
mach_port_urefs_t *urefsp)
{
mach_port_type_t type;
mach_port_urefs_t urefs;
ipc_entry_t entry;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (right >= MACH_PORT_RIGHT_NUMBER)
return KERN_INVALID_VALUE;
if (!MACH_PORT_VALID(name)) {
if (right == MACH_PORT_RIGHT_SEND ||
right == MACH_PORT_RIGHT_SEND_ONCE) {
*urefsp = 1;
return KERN_SUCCESS;
}
return KERN_INVALID_NAME;
}
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
kr = ipc_right_info(space, name, entry, &type, &urefs);
if (kr != KERN_SUCCESS)
return kr;
if (type & MACH_PORT_TYPE(right))
switch (right) {
case MACH_PORT_RIGHT_SEND_ONCE:
assert(urefs == 1);
case MACH_PORT_RIGHT_PORT_SET:
case MACH_PORT_RIGHT_RECEIVE:
*urefsp = 1;
break;
case MACH_PORT_RIGHT_DEAD_NAME:
case MACH_PORT_RIGHT_SEND:
assert(urefs > 0);
*urefsp = urefs;
break;
default:
panic("mach_port_get_refs: strange rights");
}
else
*urefsp = 0;
return kr;
}
kern_return_t
mach_port_mod_refs(
ipc_space_t space,
mach_port_name_t name,
mach_port_right_t right,
mach_port_delta_t delta)
{
ipc_entry_t entry;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (right >= MACH_PORT_RIGHT_NUMBER)
return KERN_INVALID_VALUE;
if (!MACH_PORT_VALID(name)) {
if (right == MACH_PORT_RIGHT_SEND ||
right == MACH_PORT_RIGHT_SEND_ONCE)
return KERN_SUCCESS;
return KERN_INVALID_NAME;
}
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
kr = ipc_right_delta(space, name, entry, right, delta);
return kr;
}
kern_return_t
mach_port_set_mscount(
ipc_space_t space,
mach_port_name_t name,
mach_port_mscount_t mscount)
{
ipc_port_t port;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
ipc_port_set_mscount(port, mscount);
ip_unlock(port);
return KERN_SUCCESS;
}
kern_return_t
mach_port_set_seqno(
ipc_space_t space,
mach_port_name_t name,
mach_port_seqno_t seqno)
{
ipc_port_t port;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
ipc_mqueue_set_seqno(&port->ip_messages, seqno);
ip_unlock(port);
return KERN_SUCCESS;
}
kern_return_t
mach_port_get_context(
ipc_space_t space,
mach_port_name_t name,
mach_vm_address_t *context)
{
ipc_port_t port;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
*context = port->ip_context;
ip_unlock(port);
return KERN_SUCCESS;
}
kern_return_t
mach_port_set_context(
ipc_space_t space,
mach_port_name_t name,
mach_vm_address_t context)
{
ipc_port_t port;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
port->ip_context = context;
ip_unlock(port);
return KERN_SUCCESS;
}
void
mach_port_gst_helper(
ipc_pset_t pset,
ipc_port_t port,
ipc_entry_num_t maxnames,
mach_port_name_t *names,
ipc_entry_num_t *actualp)
{
mach_port_name_t name;
assert(port != IP_NULL);
assert(ip_active(port));
name = port->ip_receiver_name;
assert(name != MACH_PORT_NULL);
if (ipc_pset_member(pset, port)) {
ipc_entry_num_t actual = *actualp;
if (actual < maxnames)
names[actual] = name;
*actualp = actual+1;
}
}
kern_return_t
mach_port_get_set_status(
ipc_space_t space,
mach_port_name_t name,
mach_port_name_t **members,
mach_msg_type_number_t *membersCnt)
{
ipc_entry_num_t actual;
ipc_entry_num_t maxnames;
kern_return_t kr;
vm_size_t size;
vm_offset_t addr;
vm_map_copy_t memory;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
size = PAGE_SIZE;
for (;;) {
ipc_entry_t entry, table;
ipc_entry_num_t tsize;
mach_port_index_t index;
mach_port_name_t *names;
ipc_pset_t pset;
kr = vm_allocate(ipc_kernel_map, &addr, size, VM_FLAGS_ANYWHERE);
if (kr != KERN_SUCCESS)
return KERN_RESOURCE_SHORTAGE;
kr = vm_map_wire(ipc_kernel_map, addr, addr + size,
VM_PROT_READ|VM_PROT_WRITE, FALSE);
assert(kr == KERN_SUCCESS);
kr = ipc_right_lookup_read(space, name, &entry);
if (kr != KERN_SUCCESS) {
kmem_free(ipc_kernel_map, addr, size);
return kr;
}
if (IE_BITS_TYPE(entry->ie_bits) != MACH_PORT_TYPE_PORT_SET) {
is_read_unlock(space);
kmem_free(ipc_kernel_map, addr, size);
return KERN_INVALID_RIGHT;
}
pset = (ipc_pset_t) entry->ie_object;
assert(pset != IPS_NULL);
names = (mach_port_name_t *) addr;
maxnames = (ipc_entry_num_t)(size / sizeof(mach_port_name_t));
actual = 0;
table = space->is_table;
tsize = space->is_table_size;
for (index = 0; index < tsize; index++) {
ipc_entry_t ientry = &table[index];
if (ientry->ie_bits & MACH_PORT_TYPE_RECEIVE) {
ipc_port_t port =
(ipc_port_t) ientry->ie_object;
mach_port_gst_helper(pset, port,
maxnames, names, &actual);
}
}
is_read_unlock(space);
if (actual <= maxnames)
break;
kmem_free(ipc_kernel_map, addr, size);
size = round_page(actual * sizeof(mach_port_name_t)) + PAGE_SIZE;
}
if (actual == 0) {
memory = VM_MAP_COPY_NULL;
kmem_free(ipc_kernel_map, addr, size);
} else {
vm_size_t size_used;
vm_size_t vm_size_used;
size_used = actual * sizeof(mach_port_name_t);
vm_size_used = round_page(size_used);
kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr),
vm_map_round_page(addr + vm_size_used), FALSE);
assert(kr == KERN_SUCCESS);
kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
(vm_map_size_t)size_used, TRUE, &memory);
assert(kr == KERN_SUCCESS);
if (vm_size_used != size)
kmem_free(ipc_kernel_map,
addr + vm_size_used, size - vm_size_used);
}
*members = (mach_port_name_t *) memory;
*membersCnt = actual;
return KERN_SUCCESS;
}
kern_return_t
mach_port_move_member(
ipc_space_t space,
mach_port_name_t member,
mach_port_name_t after)
{
ipc_entry_t entry;
ipc_port_t port;
ipc_pset_t nset;
kern_return_t kr;
wait_queue_link_t wql;
queue_head_t links_data;
queue_t links = &links_data;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(member))
return KERN_INVALID_RIGHT;
if (after == MACH_PORT_DEAD)
return KERN_INVALID_RIGHT;
else if (after == MACH_PORT_NULL)
wql = WAIT_QUEUE_LINK_NULL;
else
wql = wait_queue_link_allocate();
queue_init(links);
kr = ipc_right_lookup_read(space, member, &entry);
if (kr != KERN_SUCCESS)
goto done;
if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
is_read_unlock(space);
kr = KERN_INVALID_RIGHT;
goto done;
}
port = (ipc_port_t) entry->ie_object;
assert(port != IP_NULL);
if (after == MACH_PORT_NULL)
nset = IPS_NULL;
else {
entry = ipc_entry_lookup(space, after);
if (entry == IE_NULL) {
is_read_unlock(space);
kr = KERN_INVALID_NAME;
goto done;
}
if ((entry->ie_bits & MACH_PORT_TYPE_PORT_SET) == 0) {
is_read_unlock(space);
kr = KERN_INVALID_RIGHT;
goto done;
}
nset = (ipc_pset_t) entry->ie_object;
assert(nset != IPS_NULL);
}
ip_lock(port);
ipc_pset_remove_from_all(port, links);
if (nset != IPS_NULL) {
ips_lock(nset);
kr = ipc_pset_add(nset, port, wql);
ips_unlock(nset);
}
ip_unlock(port);
is_read_unlock(space);
done:
if (kr != KERN_SUCCESS && wql != WAIT_QUEUE_LINK_NULL)
wait_queue_link_free(wql);
while(!queue_empty(links)) {
wql = (wait_queue_link_t) dequeue(links);
wait_queue_link_free(wql);
}
return kr;
}
kern_return_t
mach_port_request_notification(
ipc_space_t space,
mach_port_name_t name,
mach_msg_id_t id,
mach_port_mscount_t sync,
ipc_port_t notify,
ipc_port_t *previousp)
{
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (notify == IP_DEAD)
return KERN_INVALID_CAPABILITY;
#if NOTYET
{
ipc_port_t port;
ipc_entry_t entry;
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
port = (ipc_port_t) entry->ie_object;
if (port->ip_subsystem != NULL) {
is_write_unlock(space);
panic("mach_port_request_notification: on RPC port!!");
return KERN_INVALID_CAPABILITY;
}
is_write_unlock(space);
}
#endif
switch (id) {
case MACH_NOTIFY_PORT_DESTROYED: {
ipc_port_t port, previous;
if (sync != 0)
return KERN_INVALID_VALUE;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
ipc_port_pdrequest(port, notify, &previous);
*previousp = previous;
break;
}
case MACH_NOTIFY_NO_SENDERS: {
ipc_port_t port;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
ipc_port_nsrequest(port, sync, notify, previousp);
break;
}
case MACH_NOTIFY_SEND_POSSIBLE:
if (!MACH_PORT_VALID(name)) {
return KERN_INVALID_ARGUMENT;
}
kr = ipc_right_request_alloc(space, name, sync != 0,
TRUE, notify, previousp);
if (kr != KERN_SUCCESS)
return kr;
break;
case MACH_NOTIFY_DEAD_NAME:
if (!MACH_PORT_VALID(name)) {
return KERN_INVALID_ARGUMENT;
}
kr = ipc_right_request_alloc(space, name, sync != 0,
FALSE, notify, previousp);
if (kr != KERN_SUCCESS)
return kr;
break;
default:
return KERN_INVALID_VALUE;
}
return KERN_SUCCESS;
}
kern_return_t
mach_port_insert_right(
ipc_space_t space,
mach_port_name_t name,
ipc_port_t poly,
mach_msg_type_name_t polyPoly)
{
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name) ||
!MACH_MSG_TYPE_PORT_ANY_RIGHT(polyPoly))
return KERN_INVALID_VALUE;
if (!IO_VALID((ipc_object_t) poly))
return KERN_INVALID_CAPABILITY;
return ipc_object_copyout_name(space, (ipc_object_t) poly,
polyPoly, FALSE, name);
}
kern_return_t
mach_port_extract_right(
ipc_space_t space,
mach_port_name_t name,
mach_msg_type_name_t msgt_name,
ipc_port_t *poly,
mach_msg_type_name_t *polyPoly)
{
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_MSG_TYPE_PORT_ANY(msgt_name))
return KERN_INVALID_VALUE;
if (!MACH_PORT_VALID(name)) {
return KERN_INVALID_RIGHT;
}
kr = ipc_object_copyin(space, name, msgt_name, (ipc_object_t *) poly);
if (kr == KERN_SUCCESS)
*polyPoly = ipc_object_copyin_type(msgt_name);
return kr;
}
kern_return_t
mach_port_get_attributes(
ipc_space_t space,
mach_port_name_t name,
int flavor,
mach_port_info_t info,
mach_msg_type_number_t *count)
{
ipc_port_t port;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
switch (flavor) {
case MACH_PORT_LIMITS_INFO: {
mach_port_limits_t *lp = (mach_port_limits_t *)info;
if (*count < MACH_PORT_LIMITS_INFO_COUNT)
return KERN_FAILURE;
if (!MACH_PORT_VALID(name)) {
*count = 0;
break;
}
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
lp->mpl_qlimit = port->ip_messages.imq_qlimit;
*count = MACH_PORT_LIMITS_INFO_COUNT;
ip_unlock(port);
break;
}
case MACH_PORT_RECEIVE_STATUS: {
mach_port_status_t *statusp = (mach_port_status_t *)info;
spl_t s;
if (*count < MACH_PORT_RECEIVE_STATUS_COUNT)
return KERN_FAILURE;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
statusp->mps_pset = port->ip_pset_count;
s = splsched();
imq_lock(&port->ip_messages);
statusp->mps_seqno = port->ip_messages.imq_seqno;
statusp->mps_qlimit = port->ip_messages.imq_qlimit;
statusp->mps_msgcount = port->ip_messages.imq_msgcount;
imq_unlock(&port->ip_messages);
splx(s);
statusp->mps_mscount = port->ip_mscount;
statusp->mps_sorights = port->ip_sorights;
statusp->mps_srights = port->ip_srights > 0;
statusp->mps_pdrequest = port->ip_pdrequest != IP_NULL;
statusp->mps_nsrequest = port->ip_nsrequest != IP_NULL;
statusp->mps_flags = 0;
*count = MACH_PORT_RECEIVE_STATUS_COUNT;
ip_unlock(port);
break;
}
case MACH_PORT_DNREQUESTS_SIZE: {
ipc_port_request_t table;
if (*count < MACH_PORT_DNREQUESTS_SIZE_COUNT)
return KERN_FAILURE;
if (!MACH_PORT_VALID(name)) {
*(int *)info = 0;
break;
}
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
table = port->ip_requests;
if (table == IPR_NULL)
*(int *)info = 0;
else
*(int *)info = table->ipr_size->its_size;
*count = MACH_PORT_DNREQUESTS_SIZE_COUNT;
ip_unlock(port);
break;
}
default:
return KERN_INVALID_ARGUMENT;
}
return KERN_SUCCESS;
}
kern_return_t
mach_port_set_attributes(
ipc_space_t space,
mach_port_name_t name,
int flavor,
mach_port_info_t info,
mach_msg_type_number_t count)
{
ipc_port_t port;
kern_return_t kr;
if (space == IS_NULL)
return KERN_INVALID_TASK;
switch (flavor) {
case MACH_PORT_LIMITS_INFO: {
mach_port_limits_t *mplp = (mach_port_limits_t *)info;
if (count < MACH_PORT_LIMITS_INFO_COUNT)
return KERN_FAILURE;
if (mplp->mpl_qlimit > MACH_PORT_QLIMIT_MAX)
return KERN_INVALID_VALUE;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
ipc_mqueue_set_qlimit(&port->ip_messages, mplp->mpl_qlimit);
ip_unlock(port);
break;
}
case MACH_PORT_DNREQUESTS_SIZE: {
if (count < MACH_PORT_DNREQUESTS_SIZE_COUNT)
return KERN_FAILURE;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_RIGHT;
kr = ipc_port_translate_receive(space, name, &port);
if (kr != KERN_SUCCESS)
return kr;
kr = ipc_port_request_grow(port, *(int *)info);
if (kr != KERN_SUCCESS)
return kr;
break;
}
default:
return KERN_INVALID_ARGUMENT;
}
return KERN_SUCCESS;
}
kern_return_t
mach_port_insert_member(
ipc_space_t space,
mach_port_name_t name,
mach_port_name_t psname)
{
ipc_object_t obj;
ipc_object_t psobj;
kern_return_t kr;
wait_queue_link_t wql;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name) || !MACH_PORT_VALID(psname))
return KERN_INVALID_RIGHT;
wql = wait_queue_link_allocate();
kr = ipc_object_translate_two(space,
name, MACH_PORT_RIGHT_RECEIVE, &obj,
psname, MACH_PORT_RIGHT_PORT_SET, &psobj);
if (kr != KERN_SUCCESS)
goto done;
assert(psobj != IO_NULL);
assert(obj != IO_NULL);
kr = ipc_pset_add((ipc_pset_t)psobj, (ipc_port_t)obj, wql);
io_unlock(psobj);
io_unlock(obj);
done:
if (kr != KERN_SUCCESS)
wait_queue_link_free(wql);
return kr;
}
kern_return_t
mach_port_extract_member(
ipc_space_t space,
mach_port_name_t name,
mach_port_name_t psname)
{
ipc_object_t psobj;
ipc_object_t obj;
kern_return_t kr;
wait_queue_link_t wql = WAIT_QUEUE_LINK_NULL;
if (space == IS_NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name) || !MACH_PORT_VALID(psname))
return KERN_INVALID_RIGHT;
kr = ipc_object_translate_two(space,
name, MACH_PORT_RIGHT_RECEIVE, &obj,
psname, MACH_PORT_RIGHT_PORT_SET, &psobj);
if (kr != KERN_SUCCESS)
return kr;
assert(psobj != IO_NULL);
assert(obj != IO_NULL);
kr = ipc_pset_remove((ipc_pset_t)psobj, (ipc_port_t)obj, &wql);
io_unlock(psobj);
io_unlock(obj);
if (wql != WAIT_QUEUE_LINK_NULL)
wait_queue_link_free(wql);
return kr;
}
kern_return_t
task_set_port_space(
ipc_space_t space,
int table_entries)
{
kern_return_t kr;
is_write_lock(space);
if (!is_active(space)) {
is_write_unlock(space);
return KERN_INVALID_TASK;
}
kr = ipc_entry_grow_table(space, table_entries);
if (kr == KERN_SUCCESS)
is_write_unlock(space);
return kr;
}
#if CONFIG_MACF_MACH
kern_return_t
mach_get_label(
ipc_space_t space,
mach_port_name_t name,
mach_port_name_t *outlabel)
{
ipc_entry_t entry;
ipc_port_t port;
struct label outl;
kern_return_t kr;
int dead;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_NAME;
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
port = (ipc_port_t) entry->ie_object;
dead = ipc_right_check(space, port, name, entry);
if (dead) {
is_write_unlock(space);
ip_release(port);
return KERN_INVALID_RIGHT;
}
is_write_unlock(space);
if (ip_kotype(port) == IKOT_LABELH) {
ip_unlock(port);
return KERN_INVALID_ARGUMENT;
}
mac_port_label_init(&outl);
mac_port_label_copy(&port->ip_label, &outl);
kr = labelh_new_user(space, &outl, outlabel);
ip_unlock(port);
return KERN_SUCCESS;
}
#else
kern_return_t
mach_get_label(
__unused ipc_space_t space,
__unused mach_port_name_t name,
__unused mach_port_name_t *outlabel)
{
return KERN_INVALID_ARGUMENT;
}
#endif
#if CONFIG_MACF_MACH
kern_return_t
mach_get_label_text(
ipc_space_t space,
mach_port_name_t name,
labelstr_t policies,
labelstr_t outlabel)
{
ipc_entry_t entry;
ipc_port_t port;
kern_return_t kr;
struct label *l;
int dead;
if (space == IS_NULL || space->is_task == NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_NAME;
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
port = (ipc_port_t)entry->ie_object;
dead = ipc_right_check(space, port, name, entry);
if (dead) {
is_write_unlock(space);
ip_release(port);
return KERN_INVALID_RIGHT;
}
is_write_unlock (space);
l = io_getlabel(entry->ie_object);
mac_port_label_externalize(l, policies, outlabel, 512, 0);
io_unlocklabel(entry->ie_object);
io_unlock(entry->ie_object);
return KERN_SUCCESS;
}
#else
kern_return_t
mach_get_label_text(
__unused ipc_space_t space,
__unused mach_port_name_t name,
__unused labelstr_t policies,
__unused labelstr_t outlabel)
{
return KERN_INVALID_ARGUMENT;
}
#endif
#if CONFIG_MACF_MACH
kern_return_t
mach_set_port_label(
ipc_space_t space,
mach_port_name_t name,
labelstr_t labelstr)
{
ipc_entry_t entry;
kern_return_t kr;
struct label inl;
ipc_port_t port;
int rc;
if (space == IS_NULL || space->is_task == NULL)
return KERN_INVALID_TASK;
if (!MACH_PORT_VALID(name))
return KERN_INVALID_NAME;
mac_port_label_init(&inl);
rc = mac_port_label_internalize(&inl, labelstr);
if (rc)
return KERN_INVALID_ARGUMENT;
kr = ipc_right_lookup_write(space, name, &entry);
if (kr != KERN_SUCCESS)
return kr;
if (io_otype(entMACry->ie_object) != IOT_PORT) {
is_write_unlock(space);
return KERN_INVALID_RIGHT;
}
port = (ipc_port_t) entry->ie_object;
ip_lock(port);
tasklabel_lock(space->is_task);
rc = mac_port_check_label_update(&space->is_task->maclabel,
&port->ip_label, &inl);
tasklabel_unlock(space->is_task);
if (rc)
kr = KERN_NO_ACCESS;
else
mac_port_label_copy(&inl, &port->ip_label);
ip_unlock(port);
is_write_unlock(space);
return kr;
}
#else
kern_return_t
mach_set_port_label(
ipc_space_t space __unused,
mach_port_name_t name __unused,
labelstr_t labelstr __unused)
{
return KERN_INVALID_ARGUMENT;
}
#endif