2005-05-04 10:39:15 +00:00
|
|
|
/*-
|
2007-10-24 19:04:04 +00:00
|
|
|
* Copyright (c) 2003-2006 SPARTA, Inc.
|
2009-03-08 00:50:37 +00:00
|
|
|
* Copyright (c) 2009 Robert N. M. Watson
|
2005-05-04 10:39:15 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project in part by Network
|
|
|
|
* Associates Laboratories, the Security Research Division of Network
|
|
|
|
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
|
|
|
|
* as part of the DARPA CHATS research program.
|
|
|
|
*
|
2007-10-24 19:04:04 +00:00
|
|
|
* This software was enhanced by SPARTA ISSO under SPAWAR contract
|
|
|
|
* N66001-04-C-6019 ("SEFOS").
|
|
|
|
*
|
2009-03-08 00:50:37 +00:00
|
|
|
* This software was developed at the University of Cambridge Computer
|
|
|
|
* Laboratory with support from a grant from Google, Inc.
|
|
|
|
*
|
2005-05-04 10:39:15 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
#include "opt_kdtrace.h"
|
2005-05-04 10:39:15 +00:00
|
|
|
#include "opt_mac.h"
|
|
|
|
#include "opt_posix.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
2006-11-11 16:26:58 +00:00
|
|
|
#include <sys/ksem.h>
|
2005-05-04 10:39:15 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
2009-03-08 00:50:37 +00:00
|
|
|
#include <sys/sdt.h>
|
2005-05-04 10:39:15 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
2006-10-22 11:52:19 +00:00
|
|
|
#include <security/mac/mac_framework.h>
|
2005-05-04 10:39:15 +00:00
|
|
|
#include <security/mac/mac_internal.h>
|
2006-12-22 23:34:47 +00:00
|
|
|
#include <security/mac/mac_policy.h>
|
2005-05-04 10:39:15 +00:00
|
|
|
|
|
|
|
static struct label *
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_label_alloc(void)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
|
|
|
label = mac_labelzone_alloc(M_WAITOK);
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM(posixsem_init_label, label);
|
2005-05-04 10:39:15 +00:00
|
|
|
return (label);
|
|
|
|
}
|
|
|
|
|
2006-12-20 23:17:34 +00:00
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_init(struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
|
Introduce two related changes to the TrustedBSD MAC Framework:
(1) Abstract interpreter vnode labeling in execve(2) and mac_execve(2)
so that the general exec code isn't aware of the details of
allocating, copying, and freeing labels, rather, simply passes in
a void pointer to start and stop functions that will be used by
the framework. This change will be MFC'd.
(2) Introduce a new flags field to the MAC_POLICY_SET(9) interface
allowing policies to declare which types of objects require label
allocation, initialization, and destruction, and define a set of
flags covering various supported object types (MPC_OBJECT_PROC,
MPC_OBJECT_VNODE, MPC_OBJECT_INPCB, ...). This change reduces the
overhead of compiling the MAC Framework into the kernel if policies
aren't loaded, or if policies require labels on only a small number
or even no object types. Each time a policy is loaded or unloaded,
we recalculate a mask of labeled object types across all policies
present in the system. Eliminate MAC_ALWAYS_LABEL_MBUF option as it
is no longer required.
MFC after: 1 week ((1) only)
Reviewed by: csjp
Obtained from: TrustedBSD Project
Sponsored by: Apple, Inc.
2008-08-23 15:26:36 +00:00
|
|
|
if (mac_labeled & MPC_OBJECT_POSIXSEM)
|
|
|
|
ks->ks_label = mac_posixsem_label_alloc();
|
|
|
|
else
|
|
|
|
ks->ks_label = NULL;
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_label_free(struct label *label)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(posixsem_destroy_label, label);
|
2007-12-17 17:26:32 +00:00
|
|
|
mac_labelzone_free(label);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_destroy(struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
|
Introduce two related changes to the TrustedBSD MAC Framework:
(1) Abstract interpreter vnode labeling in execve(2) and mac_execve(2)
so that the general exec code isn't aware of the details of
allocating, copying, and freeing labels, rather, simply passes in
a void pointer to start and stop functions that will be used by
the framework. This change will be MFC'd.
(2) Introduce a new flags field to the MAC_POLICY_SET(9) interface
allowing policies to declare which types of objects require label
allocation, initialization, and destruction, and define a set of
flags covering various supported object types (MPC_OBJECT_PROC,
MPC_OBJECT_VNODE, MPC_OBJECT_INPCB, ...). This change reduces the
overhead of compiling the MAC Framework into the kernel if policies
aren't loaded, or if policies require labels on only a small number
or even no object types. Each time a policy is loaded or unloaded,
we recalculate a mask of labeled object types across all policies
present in the system. Eliminate MAC_ALWAYS_LABEL_MBUF option as it
is no longer required.
MFC after: 1 week ((1) only)
Reviewed by: csjp
Obtained from: TrustedBSD Project
Sponsored by: Apple, Inc.
2008-08-23 15:26:36 +00:00
|
|
|
if (ks->ks_label != NULL) {
|
|
|
|
mac_posixsem_label_free(ks->ks_label);
|
|
|
|
ks->ks_label = NULL;
|
|
|
|
}
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2006-12-20 23:17:34 +00:00
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_create(struct ucred *cred, struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(posixsem_create, cred, ks, ks->ks_label);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE2(posixsem_check_open, "struct ucred *",
|
|
|
|
"struct ksem *");
|
|
|
|
|
2005-05-04 10:39:15 +00:00
|
|
|
int
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_check_open(struct ucred *cred, struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(posixsem_check_open, cred, ks,
|
|
|
|
ks->ks_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE2(posixsem_check_open, error, cred, ks);
|
2005-05-04 10:39:15 +00:00
|
|
|
|
2006-12-21 09:58:02 +00:00
|
|
|
return (error);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE3(posixsem_check_getvalue, "struct ucred *",
|
|
|
|
"struct ucred *", "struct ksem *");
|
|
|
|
|
2005-05-04 10:39:15 +00:00
|
|
|
int
|
Rework the lifetime management of the kernel implementation of POSIX
semaphores. Specifically, semaphores are now represented as new file
descriptor type that is set to close on exec. This removes the need for
all of the manual process reference counting (and fork, exec, and exit
event handlers) as the normal file descriptor operations handle all of
that for us nicely. It is also suggested as one possible implementation
in the spec and at least one other OS (OS X) uses this approach.
Some bugs that were fixed as a result include:
- References to a named semaphore whose name is removed still work after
the sem_unlink() operation. Prior to this patch, if a semaphore's name
was removed, valid handles from sem_open() would get EINVAL errors from
sem_getvalue(), sem_post(), etc. This fixes that.
- Unnamed semaphores created with sem_init() were not cleaned up when a
process exited or exec'd. They were only cleaned up if the process
did an explicit sem_destroy(). This could result in a leak of semaphore
objects that could never be cleaned up.
- On the other hand, if another process guessed the id (kernel pointer to
'struct ksem' of an unnamed semaphore (created via sem_init)) and had
write access to the semaphore based on UID/GID checks, then that other
process could manipulate the semaphore via sem_destroy(), sem_post(),
sem_wait(), etc.
- As part of the permission check (UID/GID), the umask of the proces
creating the semaphore was not honored. Thus if your umask denied group
read/write access but the explicit mode in the sem_init() call allowed
it, the semaphore would be readable/writable by other users in the
same group, for example. This includes access via the previous bug.
- If the module refused to unload because there were active semaphores,
then it might have deregistered one or more of the semaphore system
calls before it noticed that there was a problem. I'm not sure if
this actually happened as the order that modules are discovered by the
kernel linker depends on how the actual .ko file is linked. One can
make the order deterministic by using a single module with a mod_event
handler that explicitly registers syscalls (and deregisters during
unload after any checks). This also fixes a race where even if the
sem_module unloaded first it would have destroyed locks that the
syscalls might be trying to access if they are still executing when
they are unloaded.
XXX: By the way, deregistering system calls doesn't do any blocking
to drain any threads from the calls.
- Some minor fixes to errno values on error. For example, sem_init()
isn't documented to return ENFILE or EMFILE if we run out of semaphores
the way that sem_open() can. Instead, it should return ENOSPC in that
case.
Other changes:
- Kernel semaphores now use a hash table to manage the namespace of
named semaphores nearly in a similar fashion to the POSIX shared memory
object file descriptors. Kernel semaphores can now also have names
longer than 14 chars (up to MAXPATHLEN) and can include subdirectories
in their pathname.
- The UID/GID permission checks for access to a named semaphore are now
done via vaccess() rather than a home-rolled set of checks.
- Now that kernel semaphores have an associated file object, the various
MAC checks for POSIX semaphores accept both a file credential and an
active credential. There is also a new posixsem_check_stat() since it
is possible to fstat() a semaphore file descriptor.
- A small set of regression tests (using the ksem API directly) is present
in src/tools/regression/posixsem.
Reported by: kris (1)
Tested by: kris
Reviewed by: rwatson (lightly)
MFC after: 1 month
2008-06-27 05:39:04 +00:00
|
|
|
mac_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
|
|
|
|
struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(posixsem_check_getvalue, active_cred,
|
|
|
|
file_cred, ks, ks->ks_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE3(posixsem_check_getvalue, error, active_cred,
|
|
|
|
file_cred, ks);
|
2005-05-04 10:39:15 +00:00
|
|
|
|
2006-12-21 09:58:02 +00:00
|
|
|
return (error);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE3(posixsem_check_post, "struct ucred *",
|
|
|
|
"struct ucred *", "struct ksem *");
|
|
|
|
|
2005-05-04 10:39:15 +00:00
|
|
|
int
|
Rework the lifetime management of the kernel implementation of POSIX
semaphores. Specifically, semaphores are now represented as new file
descriptor type that is set to close on exec. This removes the need for
all of the manual process reference counting (and fork, exec, and exit
event handlers) as the normal file descriptor operations handle all of
that for us nicely. It is also suggested as one possible implementation
in the spec and at least one other OS (OS X) uses this approach.
Some bugs that were fixed as a result include:
- References to a named semaphore whose name is removed still work after
the sem_unlink() operation. Prior to this patch, if a semaphore's name
was removed, valid handles from sem_open() would get EINVAL errors from
sem_getvalue(), sem_post(), etc. This fixes that.
- Unnamed semaphores created with sem_init() were not cleaned up when a
process exited or exec'd. They were only cleaned up if the process
did an explicit sem_destroy(). This could result in a leak of semaphore
objects that could never be cleaned up.
- On the other hand, if another process guessed the id (kernel pointer to
'struct ksem' of an unnamed semaphore (created via sem_init)) and had
write access to the semaphore based on UID/GID checks, then that other
process could manipulate the semaphore via sem_destroy(), sem_post(),
sem_wait(), etc.
- As part of the permission check (UID/GID), the umask of the proces
creating the semaphore was not honored. Thus if your umask denied group
read/write access but the explicit mode in the sem_init() call allowed
it, the semaphore would be readable/writable by other users in the
same group, for example. This includes access via the previous bug.
- If the module refused to unload because there were active semaphores,
then it might have deregistered one or more of the semaphore system
calls before it noticed that there was a problem. I'm not sure if
this actually happened as the order that modules are discovered by the
kernel linker depends on how the actual .ko file is linked. One can
make the order deterministic by using a single module with a mod_event
handler that explicitly registers syscalls (and deregisters during
unload after any checks). This also fixes a race where even if the
sem_module unloaded first it would have destroyed locks that the
syscalls might be trying to access if they are still executing when
they are unloaded.
XXX: By the way, deregistering system calls doesn't do any blocking
to drain any threads from the calls.
- Some minor fixes to errno values on error. For example, sem_init()
isn't documented to return ENFILE or EMFILE if we run out of semaphores
the way that sem_open() can. Instead, it should return ENOSPC in that
case.
Other changes:
- Kernel semaphores now use a hash table to manage the namespace of
named semaphores nearly in a similar fashion to the POSIX shared memory
object file descriptors. Kernel semaphores can now also have names
longer than 14 chars (up to MAXPATHLEN) and can include subdirectories
in their pathname.
- The UID/GID permission checks for access to a named semaphore are now
done via vaccess() rather than a home-rolled set of checks.
- Now that kernel semaphores have an associated file object, the various
MAC checks for POSIX semaphores accept both a file credential and an
active credential. There is also a new posixsem_check_stat() since it
is possible to fstat() a semaphore file descriptor.
- A small set of regression tests (using the ksem API directly) is present
in src/tools/regression/posixsem.
Reported by: kris (1)
Tested by: kris
Reviewed by: rwatson (lightly)
MFC after: 1 month
2008-06-27 05:39:04 +00:00
|
|
|
mac_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
|
|
|
|
struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(posixsem_check_post, active_cred, file_cred,
|
|
|
|
ks, ks->ks_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE3(posixsem_check_post, error, active_cred, file_cred,
|
|
|
|
ks);
|
Rework the lifetime management of the kernel implementation of POSIX
semaphores. Specifically, semaphores are now represented as new file
descriptor type that is set to close on exec. This removes the need for
all of the manual process reference counting (and fork, exec, and exit
event handlers) as the normal file descriptor operations handle all of
that for us nicely. It is also suggested as one possible implementation
in the spec and at least one other OS (OS X) uses this approach.
Some bugs that were fixed as a result include:
- References to a named semaphore whose name is removed still work after
the sem_unlink() operation. Prior to this patch, if a semaphore's name
was removed, valid handles from sem_open() would get EINVAL errors from
sem_getvalue(), sem_post(), etc. This fixes that.
- Unnamed semaphores created with sem_init() were not cleaned up when a
process exited or exec'd. They were only cleaned up if the process
did an explicit sem_destroy(). This could result in a leak of semaphore
objects that could never be cleaned up.
- On the other hand, if another process guessed the id (kernel pointer to
'struct ksem' of an unnamed semaphore (created via sem_init)) and had
write access to the semaphore based on UID/GID checks, then that other
process could manipulate the semaphore via sem_destroy(), sem_post(),
sem_wait(), etc.
- As part of the permission check (UID/GID), the umask of the proces
creating the semaphore was not honored. Thus if your umask denied group
read/write access but the explicit mode in the sem_init() call allowed
it, the semaphore would be readable/writable by other users in the
same group, for example. This includes access via the previous bug.
- If the module refused to unload because there were active semaphores,
then it might have deregistered one or more of the semaphore system
calls before it noticed that there was a problem. I'm not sure if
this actually happened as the order that modules are discovered by the
kernel linker depends on how the actual .ko file is linked. One can
make the order deterministic by using a single module with a mod_event
handler that explicitly registers syscalls (and deregisters during
unload after any checks). This also fixes a race where even if the
sem_module unloaded first it would have destroyed locks that the
syscalls might be trying to access if they are still executing when
they are unloaded.
XXX: By the way, deregistering system calls doesn't do any blocking
to drain any threads from the calls.
- Some minor fixes to errno values on error. For example, sem_init()
isn't documented to return ENFILE or EMFILE if we run out of semaphores
the way that sem_open() can. Instead, it should return ENOSPC in that
case.
Other changes:
- Kernel semaphores now use a hash table to manage the namespace of
named semaphores nearly in a similar fashion to the POSIX shared memory
object file descriptors. Kernel semaphores can now also have names
longer than 14 chars (up to MAXPATHLEN) and can include subdirectories
in their pathname.
- The UID/GID permission checks for access to a named semaphore are now
done via vaccess() rather than a home-rolled set of checks.
- Now that kernel semaphores have an associated file object, the various
MAC checks for POSIX semaphores accept both a file credential and an
active credential. There is also a new posixsem_check_stat() since it
is possible to fstat() a semaphore file descriptor.
- A small set of regression tests (using the ksem API directly) is present
in src/tools/regression/posixsem.
Reported by: kris (1)
Tested by: kris
Reviewed by: rwatson (lightly)
MFC after: 1 month
2008-06-27 05:39:04 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE3(posixsem_check_stat, "struct ucred *",
|
|
|
|
"struct ucred *", "struct ksem *");
|
|
|
|
|
Rework the lifetime management of the kernel implementation of POSIX
semaphores. Specifically, semaphores are now represented as new file
descriptor type that is set to close on exec. This removes the need for
all of the manual process reference counting (and fork, exec, and exit
event handlers) as the normal file descriptor operations handle all of
that for us nicely. It is also suggested as one possible implementation
in the spec and at least one other OS (OS X) uses this approach.
Some bugs that were fixed as a result include:
- References to a named semaphore whose name is removed still work after
the sem_unlink() operation. Prior to this patch, if a semaphore's name
was removed, valid handles from sem_open() would get EINVAL errors from
sem_getvalue(), sem_post(), etc. This fixes that.
- Unnamed semaphores created with sem_init() were not cleaned up when a
process exited or exec'd. They were only cleaned up if the process
did an explicit sem_destroy(). This could result in a leak of semaphore
objects that could never be cleaned up.
- On the other hand, if another process guessed the id (kernel pointer to
'struct ksem' of an unnamed semaphore (created via sem_init)) and had
write access to the semaphore based on UID/GID checks, then that other
process could manipulate the semaphore via sem_destroy(), sem_post(),
sem_wait(), etc.
- As part of the permission check (UID/GID), the umask of the proces
creating the semaphore was not honored. Thus if your umask denied group
read/write access but the explicit mode in the sem_init() call allowed
it, the semaphore would be readable/writable by other users in the
same group, for example. This includes access via the previous bug.
- If the module refused to unload because there were active semaphores,
then it might have deregistered one or more of the semaphore system
calls before it noticed that there was a problem. I'm not sure if
this actually happened as the order that modules are discovered by the
kernel linker depends on how the actual .ko file is linked. One can
make the order deterministic by using a single module with a mod_event
handler that explicitly registers syscalls (and deregisters during
unload after any checks). This also fixes a race where even if the
sem_module unloaded first it would have destroyed locks that the
syscalls might be trying to access if they are still executing when
they are unloaded.
XXX: By the way, deregistering system calls doesn't do any blocking
to drain any threads from the calls.
- Some minor fixes to errno values on error. For example, sem_init()
isn't documented to return ENFILE or EMFILE if we run out of semaphores
the way that sem_open() can. Instead, it should return ENOSPC in that
case.
Other changes:
- Kernel semaphores now use a hash table to manage the namespace of
named semaphores nearly in a similar fashion to the POSIX shared memory
object file descriptors. Kernel semaphores can now also have names
longer than 14 chars (up to MAXPATHLEN) and can include subdirectories
in their pathname.
- The UID/GID permission checks for access to a named semaphore are now
done via vaccess() rather than a home-rolled set of checks.
- Now that kernel semaphores have an associated file object, the various
MAC checks for POSIX semaphores accept both a file credential and an
active credential. There is also a new posixsem_check_stat() since it
is possible to fstat() a semaphore file descriptor.
- A small set of regression tests (using the ksem API directly) is present
in src/tools/regression/posixsem.
Reported by: kris (1)
Tested by: kris
Reviewed by: rwatson (lightly)
MFC after: 1 month
2008-06-27 05:39:04 +00:00
|
|
|
int
|
|
|
|
mac_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
|
|
|
|
struct ksem *ks)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(posixsem_check_stat, active_cred, file_cred,
|
|
|
|
ks, ks->ks_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE3(posixsem_check_stat, error, active_cred, file_cred,
|
|
|
|
ks);
|
2005-05-04 10:39:15 +00:00
|
|
|
|
2006-12-21 09:58:02 +00:00
|
|
|
return (error);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE2(posixsem_check_unlink, "struct ucred *",
|
|
|
|
"struct ksem *");
|
|
|
|
|
2005-05-04 10:39:15 +00:00
|
|
|
int
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(posixsem_check_unlink, cred, ks,
|
|
|
|
ks->ks_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE2(posixsem_check_unlink, error, cred, ks);
|
2005-05-04 10:39:15 +00:00
|
|
|
|
2006-12-21 09:58:02 +00:00
|
|
|
return (error);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE3(posixsem_check_wait, "struct ucred *",
|
|
|
|
"struct ucred *", "struct ksem *");
|
|
|
|
|
2005-05-04 10:39:15 +00:00
|
|
|
int
|
Rework the lifetime management of the kernel implementation of POSIX
semaphores. Specifically, semaphores are now represented as new file
descriptor type that is set to close on exec. This removes the need for
all of the manual process reference counting (and fork, exec, and exit
event handlers) as the normal file descriptor operations handle all of
that for us nicely. It is also suggested as one possible implementation
in the spec and at least one other OS (OS X) uses this approach.
Some bugs that were fixed as a result include:
- References to a named semaphore whose name is removed still work after
the sem_unlink() operation. Prior to this patch, if a semaphore's name
was removed, valid handles from sem_open() would get EINVAL errors from
sem_getvalue(), sem_post(), etc. This fixes that.
- Unnamed semaphores created with sem_init() were not cleaned up when a
process exited or exec'd. They were only cleaned up if the process
did an explicit sem_destroy(). This could result in a leak of semaphore
objects that could never be cleaned up.
- On the other hand, if another process guessed the id (kernel pointer to
'struct ksem' of an unnamed semaphore (created via sem_init)) and had
write access to the semaphore based on UID/GID checks, then that other
process could manipulate the semaphore via sem_destroy(), sem_post(),
sem_wait(), etc.
- As part of the permission check (UID/GID), the umask of the proces
creating the semaphore was not honored. Thus if your umask denied group
read/write access but the explicit mode in the sem_init() call allowed
it, the semaphore would be readable/writable by other users in the
same group, for example. This includes access via the previous bug.
- If the module refused to unload because there were active semaphores,
then it might have deregistered one or more of the semaphore system
calls before it noticed that there was a problem. I'm not sure if
this actually happened as the order that modules are discovered by the
kernel linker depends on how the actual .ko file is linked. One can
make the order deterministic by using a single module with a mod_event
handler that explicitly registers syscalls (and deregisters during
unload after any checks). This also fixes a race where even if the
sem_module unloaded first it would have destroyed locks that the
syscalls might be trying to access if they are still executing when
they are unloaded.
XXX: By the way, deregistering system calls doesn't do any blocking
to drain any threads from the calls.
- Some minor fixes to errno values on error. For example, sem_init()
isn't documented to return ENFILE or EMFILE if we run out of semaphores
the way that sem_open() can. Instead, it should return ENOSPC in that
case.
Other changes:
- Kernel semaphores now use a hash table to manage the namespace of
named semaphores nearly in a similar fashion to the POSIX shared memory
object file descriptors. Kernel semaphores can now also have names
longer than 14 chars (up to MAXPATHLEN) and can include subdirectories
in their pathname.
- The UID/GID permission checks for access to a named semaphore are now
done via vaccess() rather than a home-rolled set of checks.
- Now that kernel semaphores have an associated file object, the various
MAC checks for POSIX semaphores accept both a file credential and an
active credential. There is also a new posixsem_check_stat() since it
is possible to fstat() a semaphore file descriptor.
- A small set of regression tests (using the ksem API directly) is present
in src/tools/regression/posixsem.
Reported by: kris (1)
Tested by: kris
Reviewed by: rwatson (lightly)
MFC after: 1 month
2008-06-27 05:39:04 +00:00
|
|
|
mac_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
|
|
|
|
struct ksem *ks)
|
2005-05-04 10:39:15 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(posixsem_check_wait, active_cred, file_cred,
|
|
|
|
ks, ks->ks_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE3(posixsem_check_wait, error, active_cred, file_cred,
|
|
|
|
ks);
|
2005-05-04 10:39:15 +00:00
|
|
|
|
2006-12-21 09:58:02 +00:00
|
|
|
return (error);
|
2005-05-04 10:39:15 +00:00
|
|
|
}
|