2 * Copyright (c) 2003-2006 SPARTA, Inc.
3 * Copyright (c) 2009 Robert N. M. Watson
6 * This software was developed for the FreeBSD Project in part by Network
7 * Associates Laboratories, the Security Research Division of Network
8 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9 * as part of the DARPA CHATS research program.
11 * This software was enhanced by SPARTA ISSO under SPAWAR contract
12 * N66001-04-C-6019 ("SEFOS").
14 * This software was developed at the University of Cambridge Computer
15 * Laboratory with support from a grant from Google, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_kdtrace.h"
44 #include "opt_posix.h"
46 #include <sys/param.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
52 #include <sys/systm.h>
53 #include <sys/sysctl.h>
55 #include <security/mac/mac_framework.h>
56 #include <security/mac/mac_internal.h>
57 #include <security/mac/mac_policy.h>
60 mac_posixsem_label_alloc(void)
64 label = mac_labelzone_alloc(M_WAITOK);
65 MAC_POLICY_PERFORM(posixsem_init_label, label);
70 mac_posixsem_init(struct ksem *ks)
73 if (mac_labeled & MPC_OBJECT_POSIXSEM)
74 ks->ks_label = mac_posixsem_label_alloc();
80 mac_posixsem_label_free(struct label *label)
83 MAC_POLICY_PERFORM_NOSLEEP(posixsem_destroy_label, label);
84 mac_labelzone_free(label);
88 mac_posixsem_destroy(struct ksem *ks)
91 if (ks->ks_label != NULL) {
92 mac_posixsem_label_free(ks->ks_label);
98 mac_posixsem_create(struct ucred *cred, struct ksem *ks)
101 MAC_POLICY_PERFORM_NOSLEEP(posixsem_create, cred, ks, ks->ks_label);
104 MAC_CHECK_PROBE_DEFINE2(posixsem_check_open, "struct ucred *",
108 mac_posixsem_check_open(struct ucred *cred, struct ksem *ks)
112 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_open, cred, ks,
114 MAC_CHECK_PROBE2(posixsem_check_open, error, cred, ks);
119 MAC_CHECK_PROBE_DEFINE3(posixsem_check_getvalue, "struct ucred *",
120 "struct ucred *", "struct ksem *");
123 mac_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
128 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_getvalue, active_cred,
129 file_cred, ks, ks->ks_label);
130 MAC_CHECK_PROBE3(posixsem_check_getvalue, error, active_cred,
136 MAC_CHECK_PROBE_DEFINE3(posixsem_check_post, "struct ucred *",
137 "struct ucred *", "struct ksem *");
140 mac_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
145 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_post, active_cred, file_cred,
147 MAC_CHECK_PROBE3(posixsem_check_post, error, active_cred, file_cred,
153 MAC_CHECK_PROBE_DEFINE3(posixsem_check_stat, "struct ucred *",
154 "struct ucred *", "struct ksem *");
157 mac_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
162 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_stat, active_cred, file_cred,
164 MAC_CHECK_PROBE3(posixsem_check_stat, error, active_cred, file_cred,
170 MAC_CHECK_PROBE_DEFINE2(posixsem_check_unlink, "struct ucred *",
174 mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks)
178 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_unlink, cred, ks,
180 MAC_CHECK_PROBE2(posixsem_check_unlink, error, cred, ks);
185 MAC_CHECK_PROBE_DEFINE3(posixsem_check_wait, "struct ucred *",
186 "struct ucred *", "struct ksem *");
189 mac_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
194 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_wait, active_cred, file_cred,
196 MAC_CHECK_PROBE3(posixsem_check_wait, error, active_cred, file_cred,
202 MAC_CHECK_PROBE_DEFINE3(posixsem_check_setmode, "struct ucred *",
203 "struct ksem *", "mode_t");
206 mac_posixsem_check_setmode(struct ucred *cred, struct ksem *ks, mode_t mode)
210 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_setmode, cred, ks,
212 MAC_CHECK_PROBE3(posixsem_check_setmode, error, cred, ks, mode);
217 MAC_CHECK_PROBE_DEFINE4(posixsem_check_setowner, "struct ucred *",
218 "struct ks *", "uid_t", "gid_t");
221 mac_posixsem_check_setowner(struct ucred *cred, struct ksem *ks, uid_t uid,
226 MAC_POLICY_CHECK_NOSLEEP(posixsem_check_setowner, cred, ks,
227 ks->ks_label, uid, gid);
228 MAC_CHECK_PROBE4(posixsem_check_setowner, error, cred, ks,