xref: /freebsd/sys/security/mac/mac_posix_sem.c (revision 1c4ee7dfb8affed302171232b0f612e6bcba3c10)
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 #include "opt_mac.h"
41 #include "opt_posix.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/ksem.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/sdt.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 
52 #include <security/mac/mac_framework.h>
53 #include <security/mac/mac_internal.h>
54 #include <security/mac/mac_policy.h>
55 
56 static struct label *
57 mac_posixsem_label_alloc(void)
58 {
59 	struct label *label;
60 
61 	label = mac_labelzone_alloc(M_WAITOK);
62 	MAC_POLICY_PERFORM(posixsem_init_label, label);
63 	return (label);
64 }
65 
66 void
67 mac_posixsem_init(struct ksem *ks)
68 {
69 
70 	if (mac_labeled & MPC_OBJECT_POSIXSEM)
71 		ks->ks_label = mac_posixsem_label_alloc();
72 	else
73 		ks->ks_label = NULL;
74 }
75 
76 static void
77 mac_posixsem_label_free(struct label *label)
78 {
79 
80 	MAC_POLICY_PERFORM_NOSLEEP(posixsem_destroy_label, label);
81 	mac_labelzone_free(label);
82 }
83 
84 void
85 mac_posixsem_destroy(struct ksem *ks)
86 {
87 
88 	if (ks->ks_label != NULL) {
89 		mac_posixsem_label_free(ks->ks_label);
90 		ks->ks_label = NULL;
91 	}
92 }
93 
94 void
95 mac_posixsem_create(struct ucred *cred, struct ksem *ks)
96 {
97 
98 	MAC_POLICY_PERFORM_NOSLEEP(posixsem_create, cred, ks, ks->ks_label);
99 }
100 
101 MAC_CHECK_PROBE_DEFINE2(posixsem_check_open, "struct ucred *",
102     "struct ksem *");
103 
104 int
105 mac_posixsem_check_open(struct ucred *cred, struct ksem *ks)
106 {
107 	int error;
108 
109 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_open, cred, ks,
110 	    ks->ks_label);
111 	MAC_CHECK_PROBE2(posixsem_check_open, error, cred, ks);
112 
113 	return (error);
114 }
115 
116 MAC_CHECK_PROBE_DEFINE3(posixsem_check_getvalue, "struct ucred *",
117     "struct ucred *", "struct ksem *");
118 
119 int
120 mac_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
121     struct ksem *ks)
122 {
123 	int error;
124 
125 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_getvalue, active_cred,
126 	    file_cred, ks, ks->ks_label);
127 	MAC_CHECK_PROBE3(posixsem_check_getvalue, error, active_cred,
128 	    file_cred, ks);
129 
130 	return (error);
131 }
132 
133 MAC_CHECK_PROBE_DEFINE3(posixsem_check_post, "struct ucred *",
134     "struct ucred *", "struct ksem *");
135 
136 int
137 mac_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
138     struct ksem *ks)
139 {
140 	int error;
141 
142 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_post, active_cred, file_cred,
143 	    ks, ks->ks_label);
144 	MAC_CHECK_PROBE3(posixsem_check_post, error, active_cred, file_cred,
145 	    ks);
146 
147 	return (error);
148 }
149 
150 MAC_CHECK_PROBE_DEFINE3(posixsem_check_stat, "struct ucred *",
151     "struct ucred *", "struct ksem *");
152 
153 int
154 mac_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
155     struct ksem *ks)
156 {
157 	int error;
158 
159 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_stat, active_cred, file_cred,
160 	    ks, ks->ks_label);
161 	MAC_CHECK_PROBE3(posixsem_check_stat, error, active_cred, file_cred,
162 	    ks);
163 
164 	return (error);
165 }
166 
167 MAC_CHECK_PROBE_DEFINE2(posixsem_check_unlink, "struct ucred *",
168     "struct ksem *");
169 
170 int
171 mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks)
172 {
173 	int error;
174 
175 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_unlink, cred, ks,
176 	    ks->ks_label);
177 	MAC_CHECK_PROBE2(posixsem_check_unlink, error, cred, ks);
178 
179 	return (error);
180 }
181 
182 MAC_CHECK_PROBE_DEFINE3(posixsem_check_wait, "struct ucred *",
183     "struct ucred *", "struct ksem *");
184 
185 int
186 mac_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
187     struct ksem *ks)
188 {
189 	int error;
190 
191 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_wait, active_cred, file_cred,
192 	    ks, ks->ks_label);
193 	MAC_CHECK_PROBE3(posixsem_check_wait, error, active_cred, file_cred,
194 	    ks);
195 
196 	return (error);
197 }
198 
199 MAC_CHECK_PROBE_DEFINE3(posixsem_check_setmode, "struct ucred *",
200     "struct ksem *", "mode_t");
201 
202 int
203 mac_posixsem_check_setmode(struct ucred *cred, struct ksem *ks, mode_t mode)
204 {
205 	int error;
206 
207 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_setmode, cred, ks,
208 	    ks->ks_label, mode);
209 	MAC_CHECK_PROBE3(posixsem_check_setmode, error, cred, ks, mode);
210 
211 	return (error);
212 }
213 
214 MAC_CHECK_PROBE_DEFINE4(posixsem_check_setowner, "struct ucred *",
215     "struct ks *", "uid_t", "gid_t");
216 
217 int
218 mac_posixsem_check_setowner(struct ucred *cred, struct ksem *ks, uid_t uid,
219     gid_t gid)
220 {
221 	int error;
222 
223 	MAC_POLICY_CHECK_NOSLEEP(posixsem_check_setowner, cred, ks,
224 	    ks->ks_label, uid, gid);
225 	MAC_CHECK_PROBE4(posixsem_check_setowner, error, cred, ks,
226 	    uid, gid);
227 
228 	return (error);
229 }
230