xref: /freebsd/sys/security/mac/mac_sysv_msg.c (revision 57c4583f70ab9d25b3aed17f20ec7843f9673539)
1 /*-
2  * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project in part by Network
6  * Associates Laboratories, the Security Research Division of Network
7  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
8  * as part of the DARPA CHATS research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/mac.h>
43 #include <sys/sbuf.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/file.h>
48 #include <sys/namei.h>
49 #include <sys/sysctl.h>
50 #include <sys/msg.h>
51 
52 #include <sys/mac_policy.h>
53 
54 #include <security/mac/mac_framework.h>
55 #include <security/mac/mac_internal.h>
56 
57 static int	mac_enforce_sysv_msg = 1;
58 SYSCTL_INT(_security_mac, OID_AUTO, enforce_sysv_msg, CTLFLAG_RW,
59     &mac_enforce_sysv_msg, 0,
60     "Enforce MAC policy on System V IPC Message Queues");
61 TUNABLE_INT("security.mac.enforce_sysv_msg", &mac_enforce_sysv_msg);
62 
63 static struct label *
64 mac_sysv_msgmsg_label_alloc(void)
65 {
66 	struct label *label;
67 
68 	label = mac_labelzone_alloc(M_WAITOK);
69 	MAC_PERFORM(init_sysv_msgmsg_label, label);
70 	return (label);
71 }
72 
73 void
74 mac_init_sysv_msgmsg(struct msg *msgptr)
75 {
76 
77 	msgptr->label = mac_sysv_msgmsg_label_alloc();
78 }
79 
80 static struct label *
81 mac_sysv_msgqueue_label_alloc(void)
82 {
83 	struct label *label;
84 
85 	label = mac_labelzone_alloc(M_WAITOK);
86 	MAC_PERFORM(init_sysv_msgqueue_label, label);
87 	return (label);
88 }
89 
90 void
91 mac_init_sysv_msgqueue(struct msqid_kernel *msqkptr)
92 {
93 
94 	msqkptr->label = mac_sysv_msgqueue_label_alloc();
95 }
96 
97 static void
98 mac_sysv_msgmsg_label_free(struct label *label)
99 {
100 
101 	MAC_PERFORM(destroy_sysv_msgmsg_label, label);
102 	mac_labelzone_free(label);
103 }
104 
105 void
106 mac_destroy_sysv_msgmsg(struct msg *msgptr)
107 {
108 
109 	mac_sysv_msgmsg_label_free(msgptr->label);
110 	msgptr->label = NULL;
111 }
112 
113 static void
114 mac_sysv_msgqueue_label_free(struct label *label)
115 {
116 
117 	MAC_PERFORM(destroy_sysv_msgqueue_label, label);
118 	mac_labelzone_free(label);
119 }
120 
121 void
122 mac_destroy_sysv_msgqueue(struct msqid_kernel *msqkptr)
123 {
124 
125 	mac_sysv_msgqueue_label_free(msqkptr->label);
126 	msqkptr->label = NULL;
127 }
128 
129 void
130 mac_create_sysv_msgmsg(struct ucred *cred, struct msqid_kernel *msqkptr,
131     struct msg *msgptr)
132 {
133 
134 	MAC_PERFORM(create_sysv_msgmsg, cred, msqkptr, msqkptr->label,
135 		msgptr, msgptr->label);
136 }
137 
138 void
139 mac_create_sysv_msgqueue(struct ucred *cred, struct msqid_kernel *msqkptr)
140 {
141 
142 	MAC_PERFORM(create_sysv_msgqueue, cred, msqkptr, msqkptr->label);
143 }
144 
145 void
146 mac_cleanup_sysv_msgmsg(struct msg *msgptr)
147 {
148 
149 	MAC_PERFORM(cleanup_sysv_msgmsg, msgptr->label);
150 }
151 
152 void
153 mac_cleanup_sysv_msgqueue(struct msqid_kernel *msqkptr)
154 {
155 
156 	MAC_PERFORM(cleanup_sysv_msgqueue, msqkptr->label);
157 }
158 
159 int
160 mac_check_sysv_msgmsq(struct ucred *cred, struct msg *msgptr,
161 	struct msqid_kernel *msqkptr)
162 {
163 	int error;
164 
165 	if (!mac_enforce_sysv_msg)
166 		return (0);
167 
168 	MAC_CHECK(check_sysv_msgmsq, cred,  msgptr, msgptr->label, msqkptr,
169 	    msqkptr->label);
170 
171 	return(error);
172 }
173 
174 int
175 mac_check_sysv_msgrcv(struct ucred *cred, struct msg *msgptr)
176 {
177 	int error;
178 
179 	if (!mac_enforce_sysv_msg)
180 		return (0);
181 
182 	MAC_CHECK(check_sysv_msgrcv, cred, msgptr, msgptr->label);
183 
184 	return(error);
185 }
186 
187 int
188 mac_check_sysv_msgrmid(struct ucred *cred, struct msg *msgptr)
189 {
190 	int error;
191 
192 	if (!mac_enforce_sysv_msg)
193 		return (0);
194 
195 	MAC_CHECK(check_sysv_msgrmid, cred,  msgptr, msgptr->label);
196 
197 	return(error);
198 }
199 
200 int
201 mac_check_sysv_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
202 {
203 	int error;
204 
205 	if (!mac_enforce_sysv_msg)
206 		return (0);
207 
208 	MAC_CHECK(check_sysv_msqget, cred, msqkptr, msqkptr->label);
209 
210 	return(error);
211 }
212 
213 int
214 mac_check_sysv_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
215 {
216 	int error;
217 
218 	if (!mac_enforce_sysv_msg)
219 		return (0);
220 
221 	MAC_CHECK(check_sysv_msqsnd, cred, msqkptr, msqkptr->label);
222 
223 	return(error);
224 }
225 
226 int
227 mac_check_sysv_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
228 {
229 	int error;
230 
231 	if (!mac_enforce_sysv_msg)
232 		return (0);
233 
234 	MAC_CHECK(check_sysv_msqrcv, cred, msqkptr, msqkptr->label);
235 
236 	return(error);
237 }
238 
239 int
240 mac_check_sysv_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
241     int cmd)
242 {
243 	int error;
244 
245 	if (!mac_enforce_sysv_msg)
246 		return (0);
247 
248 	MAC_CHECK(check_sysv_msqctl, cred, msqkptr, msqkptr->label, cmd);
249 
250 	return(error);
251 }
252