2004-02-26 03:51:04 +00:00
|
|
|
/*-
|
2009-03-08 00:50:37 +00:00
|
|
|
* Copyright (c) 1999-2002, 2007, 2009 Robert N. M. Watson
|
2004-02-26 03:51:04 +00:00
|
|
|
* Copyright (c) 2001 Ilmar S. Habibulin
|
|
|
|
* Copyright (c) 2001-2004 Networks Associates Technology, Inc.
|
2007-10-24 19:04:04 +00:00
|
|
|
* Copyright (c) 2006 SPARTA, Inc.
|
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
|
|
|
* Copyright (c) 2008 Apple Inc.
|
2004-02-26 03:51:04 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Robert Watson and Ilmar Habibulin for the
|
|
|
|
* TrustedBSD Project.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2004-02-26 03:51:04 +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$");
|
|
|
|
|
|
|
|
#include "opt_mac.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/sbuf.h>
|
2009-03-08 00:50:37 +00:00
|
|
|
#include <sys/sdt.h>
|
2004-02-26 03:51:04 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_var.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
|
2006-10-22 11:52:19 +00:00
|
|
|
#include <security/mac/mac_framework.h>
|
2004-02-26 03:51:04 +00:00
|
|
|
#include <security/mac/mac_internal.h>
|
2006-12-22 23:34:47 +00:00
|
|
|
#include <security/mac/mac_policy.h>
|
2004-02-26 03:51:04 +00:00
|
|
|
|
|
|
|
static struct label *
|
|
|
|
mac_inpcb_label_alloc(int flag)
|
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
label = mac_labelzone_alloc(flag);
|
|
|
|
if (label == NULL)
|
|
|
|
return (NULL);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
if (flag & M_WAITOK)
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK(inpcb_init_label, label, flag);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
else
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(inpcb_init_label, label, flag);
|
2004-02-26 03:51:04 +00:00
|
|
|
if (error) {
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
|
2004-02-26 03:51:04 +00:00
|
|
|
mac_labelzone_free(label);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (label);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_inpcb_init(struct inpcb *inp, int flag)
|
2004-02-26 03:51:04 +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_INPCB) {
|
|
|
|
inp->inp_label = mac_inpcb_label_alloc(flag);
|
|
|
|
if (inp->inp_label == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
} else
|
|
|
|
inp->inp_label = NULL;
|
2004-02-26 03:51:04 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct label *
|
|
|
|
mac_ipq_label_alloc(int flag)
|
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
label = mac_labelzone_alloc(flag);
|
|
|
|
if (label == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
if (flag & M_WAITOK)
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK(ipq_init_label, label, flag);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
else
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(ipq_init_label, label, flag);
|
2004-02-26 03:51:04 +00:00
|
|
|
if (error) {
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
|
2004-02-26 03:51:04 +00:00
|
|
|
mac_labelzone_free(label);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (label);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-06-13 22:14:15 +00:00
|
|
|
mac_ipq_init(struct ipq *q, int flag)
|
2004-02-26 03:51:04 +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_IPQ) {
|
|
|
|
q->ipq_label = mac_ipq_label_alloc(flag);
|
|
|
|
if (q->ipq_label == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
} else
|
|
|
|
q->ipq_label = NULL;
|
2004-02-26 03:51:04 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mac_inpcb_label_free(struct label *label)
|
|
|
|
{
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
|
2004-02-26 03:51:04 +00:00
|
|
|
mac_labelzone_free(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_inpcb_destroy(struct inpcb *inp)
|
2004-02-26 03:51:04 +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 (inp->inp_label != NULL) {
|
|
|
|
mac_inpcb_label_free(inp->inp_label);
|
|
|
|
inp->inp_label = NULL;
|
|
|
|
}
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mac_ipq_label_free(struct label *label)
|
|
|
|
{
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
|
2004-02-26 03:51:04 +00:00
|
|
|
mac_labelzone_free(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-13 22:14:15 +00:00
|
|
|
mac_ipq_destroy(struct ipq *q)
|
2004-02-26 03:51:04 +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 (q->ipq_label != NULL) {
|
|
|
|
mac_ipq_label_free(q->ipq_label);
|
|
|
|
q->ipq_label = NULL;
|
|
|
|
}
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_inpcb_create(struct socket *so, struct inpcb *inp)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp,
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
inp->inp_label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-13 22:14:15 +00:00
|
|
|
mac_ipq_reassemble(struct ipq *q, struct mbuf *m)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-04-22 19:55:56 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m,
|
|
|
|
label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_netinet_fragment(struct mbuf *m, struct mbuf *frag)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
2007-04-22 19:55:56 +00:00
|
|
|
struct label *mlabel, *fraglabel;
|
2004-02-26 03:51:04 +00:00
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-04-22 19:55:56 +00:00
|
|
|
mlabel = mac_mbuf_to_label(m);
|
|
|
|
fraglabel = mac_mbuf_to_label(frag);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag,
|
|
|
|
fraglabel);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-13 22:14:15 +00:00
|
|
|
mac_ipq_create(struct mbuf *m, struct ipq *q)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-04-22 19:55:56 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *mlabel;
|
|
|
|
|
2008-04-19 18:35:27 +00:00
|
|
|
INP_LOCK_ASSERT(inp);
|
2009-06-03 18:46:28 +00:00
|
|
|
|
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
mlabel = mac_mbuf_to_label(m);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m,
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
mlabel);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-06-13 22:14:15 +00:00
|
|
|
mac_ipq_match(struct mbuf *m, struct ipq *q)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
int result;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return (1);
|
|
|
|
|
2007-04-22 19:55:56 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
|
|
|
result = 1;
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2007-10-28 15:55:23 +00:00
|
|
|
void
|
|
|
|
mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct label *mlabel;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-10-28 15:55:23 +00:00
|
|
|
mlabel = mac_mbuf_to_label(m);
|
|
|
|
|
|
|
|
MAC_IFNET_LOCK(ifp);
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m,
|
|
|
|
mlabel);
|
2007-10-28 15:55:23 +00:00
|
|
|
MAC_IFNET_UNLOCK(ifp);
|
|
|
|
}
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
void
|
2007-10-28 17:12:48 +00:00
|
|
|
mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend)
|
|
|
|
{
|
|
|
|
struct label *mrecvlabel, *msendlabel;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-10-28 17:12:48 +00:00
|
|
|
mrecvlabel = mac_mbuf_to_label(mrecv);
|
|
|
|
msendlabel = mac_mbuf_to_label(msend);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel,
|
|
|
|
msend, msendlabel);
|
2007-10-28 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mac_netinet_icmp_replyinplace(struct mbuf *m)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
2007-04-22 19:55:56 +00:00
|
|
|
|
2007-10-28 15:55:23 +00:00
|
|
|
void
|
|
|
|
mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct label *mlabel;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-10-28 15:55:23 +00:00
|
|
|
mlabel = mac_mbuf_to_label(m);
|
|
|
|
|
|
|
|
MAC_IFNET_LOCK(ifp);
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m,
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
mlabel);
|
2007-10-28 15:55:23 +00:00
|
|
|
MAC_IFNET_UNLOCK(ifp);
|
|
|
|
}
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
void
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_netinet_tcp_reply(struct mbuf *m)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-13 22:14:15 +00:00
|
|
|
mac_ipq_update(struct mbuf *m, struct ipq *q)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-04-22 19:55:56 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
|
|
|
|
"struct mbuf *");
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
int
|
2007-10-24 19:04:04 +00:00
|
|
|
mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
|
2004-02-26 03:51:04 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
M_ASSERTPKTHDR(m);
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return (0);
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
|
2004-02-26 03:51:04 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
|
|
|
|
"struct inpcb *");
|
|
|
|
|
2008-10-17 12:54:28 +00:00
|
|
|
int
|
|
|
|
mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
INP_LOCK_ASSERT(inp);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
|
|
|
|
inp->inp_label);
|
2009-03-08 00:50:37 +00:00
|
|
|
MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
|
2008-10-17 12:54:28 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-02-26 03:51:04 +00:00
|
|
|
void
|
|
|
|
mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
|
|
|
|
{
|
|
|
|
|
2008-04-17 21:38:18 +00:00
|
|
|
INP_WLOCK_ASSERT(inp);
|
2006-12-28 21:56:39 +00:00
|
|
|
SOCK_LOCK_ASSERT(so);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
inp->inp_label);
|
2004-02-26 03:51:04 +00:00
|
|
|
}
|
2006-09-12 04:25:13 +00:00
|
|
|
|
2007-10-28 17:12:48 +00:00
|
|
|
void
|
|
|
|
mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
|
|
|
|
{
|
|
|
|
struct label *mrecvlabel, *msendlabel;
|
|
|
|
|
|
|
|
M_ASSERTPKTHDR(mrecv);
|
|
|
|
M_ASSERTPKTHDR(msend);
|
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-10-28 17:12:48 +00:00
|
|
|
mrecvlabel = mac_mbuf_to_label(mrecv);
|
|
|
|
msendlabel = mac_mbuf_to_label(msend);
|
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
|
|
|
|
msend, msendlabel);
|
2007-10-28 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
2006-09-12 04:25:13 +00:00
|
|
|
void
|
2007-10-26 13:18:38 +00:00
|
|
|
mac_netinet_firewall_send(struct mbuf *m)
|
2006-09-12 04:25:13 +00:00
|
|
|
{
|
|
|
|
struct label *label;
|
|
|
|
|
|
|
|
M_ASSERTPKTHDR(m);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2006-09-12 04:25:13 +00:00
|
|
|
label = mac_mbuf_to_label(m);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
|
2006-09-12 04:25:13 +00:00
|
|
|
}
|
2006-12-13 06:00:57 +00:00
|
|
|
|
|
|
|
/*
|
2006-12-20 23:11:01 +00:00
|
|
|
* These functions really should be referencing the syncache structure
|
|
|
|
* instead of the label. However, due to some of the complexities associated
|
|
|
|
* with exposing this syncache structure we operate directly on it's label
|
|
|
|
* pointer. This should be OK since we aren't making any access control
|
|
|
|
* decisions within this code directly, we are merely allocating and copying
|
|
|
|
* label storage so we can properly initialize mbuf labels for any packets
|
|
|
|
* the syncache code might create.
|
2006-12-13 06:00:57 +00:00
|
|
|
*/
|
|
|
|
void
|
2007-10-25 14:37:37 +00:00
|
|
|
mac_syncache_destroy(struct label **label)
|
2006-12-13 06:00:57 +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 (*label != NULL) {
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
|
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
|
|
|
mac_labelzone_free(*label);
|
|
|
|
*label = NULL;
|
|
|
|
}
|
2006-12-13 06:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-10-25 14:37:37 +00:00
|
|
|
mac_syncache_init(struct label **label)
|
2006-12-13 06:00:57 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
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_SYNCACHE) {
|
|
|
|
*label = mac_labelzone_alloc(M_NOWAIT);
|
|
|
|
if (*label == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
/*
|
|
|
|
* Since we are holding the inpcb locks the policy can not
|
|
|
|
* allocate policy specific label storage using M_WAITOK. So
|
|
|
|
* we need to do a MAC_CHECK instead of the typical
|
|
|
|
* MAC_PERFORM so we can propagate allocation failures back
|
|
|
|
* to the syncache code.
|
|
|
|
*/
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
|
|
|
|
M_NOWAIT);
|
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 (error) {
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
|
|
|
|
*label);
|
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
|
|
|
mac_labelzone_free(*label);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
} else
|
|
|
|
*label = NULL;
|
|
|
|
return (0);
|
2006-12-13 06:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-25 14:37:37 +00:00
|
|
|
mac_syncache_create(struct label *label, struct inpcb *inp)
|
2006-12-13 06:00:57 +00:00
|
|
|
{
|
|
|
|
|
2008-04-17 21:38:18 +00:00
|
|
|
INP_WLOCK_ASSERT(inp);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
|
2006-12-13 06:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-25 14:37:37 +00:00
|
|
|
mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
|
2006-12-13 06:00:57 +00:00
|
|
|
{
|
2007-04-22 19:55:56 +00:00
|
|
|
struct label *mlabel;
|
2006-12-13 06:00:57 +00:00
|
|
|
|
|
|
|
M_ASSERTPKTHDR(m);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
|
2009-06-03 18:46:28 +00:00
|
|
|
if (mac_policy_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-04-22 19:55:56 +00:00
|
|
|
mlabel = mac_mbuf_to_label(m);
|
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
2009-03-14 16:06:06 +00:00
|
|
|
|
2009-05-01 21:05:40 +00:00
|
|
|
MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,
|
|
|
|
mlabel);
|
2006-12-13 06:00:57 +00:00
|
|
|
}
|