xref: /freebsd/sys/security/mac/mac_posix_sem.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (c) 2003-2006 SPARTA, Inc.
3  * Copyright (c) 2009 Robert N. M. Watson
4  * All rights reserved.
5  *
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.
10  *
11  * This software was enhanced by SPARTA ISSO under SPAWAR contract
12  * N66001-04-C-6019 ("SEFOS").
13  *
14  * This software was developed at the University of Cambridge Computer
15  * Laboratory with support from a grant from Google, Inc.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_mac.h"
43 #include "opt_posix.h"
44 
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/ksem.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/sdt.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 
54 #include <security/mac/mac_framework.h>
55 #include <security/mac/mac_internal.h>
56 #include <security/mac/mac_policy.h>
57 
58 static struct label *
59 mac_posixsem_label_alloc(void)
60 {
61 	struct label *label;
62 
63 	label = mac_labelzone_alloc(M_WAITOK);
64 	MAC_POLICY_PERFORM(posixsem_init_label, label);
65 	return (label);
66 }
67 
68 void
69 mac_posixsem_init(struct ksem *ks)
70 {
71 
72 	if (mac_labeled & MPC_OBJECT_POSIXSEM)
73 		ks->ks_label = mac_posixsem_label_alloc();
74 	else
75 		ks->ks_label = NULL;
76 }
77 
78 static void
79 mac_posixsem_label_free(struct label *label)
80 {
81 
82 	MAC_POLICY_PERFORM_NOSLEEP(posixsem_destroy_label, label);
83 	mac_labelzone_free(label);
84 }
85 
86 void
87 mac_posixsem_destroy(struct ksem *ks)
88 {
89 
90 	if (ks->ks_label != NULL) {
91 		mac_posixsem_label_free(ks->ks_label);
92 		ks->ks_label = NULL;
93 	}
94 }
95 
96 void
97 mac_posixsem_create(struct ucred *cred, struct ksem *ks)
98 {
99 
100 	MAC_POLICY_PERFORM_NOSLEEP(posixsem_create, cred, ks, ks->ks_label);
101 }
102 
103 MAC_CHECK_PROBE_DEFINE2(posixsem_check_open, "struct ucred *",
104     "struct ksem *");
105 
106 int
107 mac_posixsem_check_open(struct ucred *cred, struct ksem *ks)
108 {
109 	int error;
110 
111 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_open, cred, ks,
112 	    ks->ks_label);
113 	MAC_CHECK_PROBE2(posixsem_check_open, error, cred, ks);
114 
115 	return (error);
116 }
117 
118 MAC_CHECK_PROBE_DEFINE3(posixsem_check_getvalue, "struct ucred *",
119     "struct ucred *", "struct ksem *");
120 
121 int
122 mac_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
123     struct ksem *ks)
124 {
125 	int error;
126 
127 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_getvalue, active_cred,
128 	    file_cred, ks, ks->ks_label);
129 	MAC_CHECK_PROBE3(posixsem_check_getvalue, error, active_cred,
130 	    file_cred, ks);
131 
132 	return (error);
133 }
134 
135 MAC_CHECK_PROBE_DEFINE3(posixsem_check_post, "struct ucred *",
136     "struct ucred *", "struct ksem *");
137 
138 int
139 mac_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
140     struct ksem *ks)
141 {
142 	int error;
143 
144 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_post, active_cred, file_cred,
145 	    ks, ks->ks_label);
146 	MAC_CHECK_PROBE3(posixsem_check_post, error, active_cred, file_cred,
147 	    ks);
148 
149 	return (error);
150 }
151 
152 MAC_CHECK_PROBE_DEFINE3(posixsem_check_stat, "struct ucred *",
153     "struct ucred *", "struct ksem *");
154 
155 int
156 mac_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
157     struct ksem *ks)
158 {
159 	int error;
160 
161 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_stat, active_cred, file_cred,
162 	    ks, ks->ks_label);
163 	MAC_CHECK_PROBE3(posixsem_check_stat, error, active_cred, file_cred,
164 	    ks);
165 
166 	return (error);
167 }
168 
169 MAC_CHECK_PROBE_DEFINE2(posixsem_check_unlink, "struct ucred *",
170     "struct ksem *");
171 
172 int
173 mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks)
174 {
175 	int error;
176 
177 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_unlink, cred, ks,
178 	    ks->ks_label);
179 	MAC_CHECK_PROBE2(posixsem_check_unlink, error, cred, ks);
180 
181 	return (error);
182 }
183 
184 MAC_CHECK_PROBE_DEFINE3(posixsem_check_wait, "struct ucred *",
185     "struct ucred *", "struct ksem *");
186 
187 int
188 mac_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
189     struct ksem *ks)
190 {
191 	int error;
192 
193 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_wait, active_cred, file_cred,
194 	    ks, ks->ks_label);
195 	MAC_CHECK_PROBE3(posixsem_check_wait, error, active_cred, file_cred,
196 	    ks);
197 
198 	return (error);
199 }
200 
201 MAC_CHECK_PROBE_DEFINE3(posixsem_check_setmode, "struct ucred *",
202     "struct ksem *", "mode_t");
203 
204 int
205 mac_posixsem_check_setmode(struct ucred *cred, struct ksem *ks, mode_t mode)
206 {
207 	int error;
208 
209 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_setmode, cred, ks,
210 	    ks->ks_label, mode);
211 	MAC_CHECK_PROBE3(posixsem_check_setmode, error, cred, ks, mode);
212 
213 	return (error);
214 }
215 
216 MAC_CHECK_PROBE_DEFINE4(posixsem_check_setowner, "struct ucred *",
217     "struct ks *", "uid_t", "gid_t");
218 
219 int
220 mac_posixsem_check_setowner(struct ucred *cred, struct ksem *ks, uid_t uid,
221     gid_t gid)
222 {
223 	int error;
224 
225 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_setowner, cred, ks,
226 	    ks->ks_label, uid, gid);
227 	MAC_CHECK_PROBE4(posixsem_check_setowner, error, cred, ks,
228 	    uid, gid);
229 
230 	return (error);
231 }
232