1137817Srwatson/*-
2137817Srwatson * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3172930Srwatson * Copyright (c) 2006 SPARTA, Inc.
4182063Srwatson * Copyright (c) 2008 Apple Inc.
5189503Srwatson * Copyright (c) 2009 Robert N. M. Watson
6137817Srwatson * All rights reserved.
7137817Srwatson *
8137817Srwatson * This software was developed for the FreeBSD Project in part by Network
9137817Srwatson * Associates Laboratories, the Security Research Division of Network
10137817Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11137817Srwatson * as part of the DARPA CHATS research program.
12137817Srwatson *
13172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract
14172930Srwatson * N66001-04-C-6019 ("SEFOS").
15172930Srwatson *
16189503Srwatson * This software was developed at the University of Cambridge Computer
17189503Srwatson * Laboratory with support from a grant from Google, Inc.
18189503Srwatson *
19137817Srwatson * Redistribution and use in source and binary forms, with or without
20137817Srwatson * modification, are permitted provided that the following conditions
21137817Srwatson * are met:
22137817Srwatson * 1. Redistributions of source code must retain the above copyright
23137817Srwatson *    notice, this list of conditions and the following disclaimer.
24137817Srwatson * 2. Redistributions in binary form must reproduce the above copyright
25137817Srwatson *    notice, this list of conditions and the following disclaimer in the
26137817Srwatson *    documentation and/or other materials provided with the distribution.
27137817Srwatson *
28137817Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29137817Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30137817Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31137817Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32137817Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33137817Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34137817Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35137817Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36137817Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37137817Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38137817Srwatson * SUCH DAMAGE.
39137817Srwatson */
40137817Srwatson
41137817Srwatson#include <sys/cdefs.h>
42137817Srwatson__FBSDID("$FreeBSD$");
43137817Srwatson
44189503Srwatson#include "opt_kdtrace.h"
45137817Srwatson#include "opt_mac.h"
46137817Srwatson
47137817Srwatson#include <sys/param.h>
48137817Srwatson#include <sys/kernel.h>
49137817Srwatson#include <sys/lock.h>
50137817Srwatson#include <sys/malloc.h>
51137817Srwatson#include <sys/mutex.h>
52137817Srwatson#include <sys/sbuf.h>
53189503Srwatson#include <sys/sdt.h>
54137817Srwatson#include <sys/systm.h>
55137817Srwatson#include <sys/vnode.h>
56137817Srwatson#include <sys/mount.h>
57137817Srwatson#include <sys/file.h>
58137817Srwatson#include <sys/namei.h>
59137817Srwatson#include <sys/sysctl.h>
60137817Srwatson#include <sys/msg.h>
61137817Srwatson
62163606Srwatson#include <security/mac/mac_framework.h>
63137817Srwatson#include <security/mac/mac_internal.h>
64165469Srwatson#include <security/mac/mac_policy.h>
65137817Srwatson
66137817Srwatsonstatic struct label *
67137817Srwatsonmac_sysv_msgmsg_label_alloc(void)
68137817Srwatson{
69137817Srwatson	struct label *label;
70137817Srwatson
71137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
72191731Srwatson	MAC_POLICY_PERFORM(sysvmsg_init_label, label);
73137817Srwatson	return (label);
74137817Srwatson}
75137817Srwatson
76137817Srwatsonvoid
77172930Srwatsonmac_sysvmsg_init(struct msg *msgptr)
78137817Srwatson{
79137817Srwatson
80182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSG)
81182063Srwatson		msgptr->label = mac_sysv_msgmsg_label_alloc();
82182063Srwatson	else
83182063Srwatson		msgptr->label = NULL;
84137817Srwatson}
85137817Srwatson
86137817Srwatsonstatic struct label *
87137817Srwatsonmac_sysv_msgqueue_label_alloc(void)
88137817Srwatson{
89137817Srwatson	struct label *label;
90137817Srwatson
91137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
92191731Srwatson	MAC_POLICY_PERFORM(sysvmsq_init_label, label);
93137817Srwatson	return (label);
94137817Srwatson}
95137817Srwatson
96137817Srwatsonvoid
97172930Srwatsonmac_sysvmsq_init(struct msqid_kernel *msqkptr)
98137817Srwatson{
99137817Srwatson
100182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSQ)
101182063Srwatson		msqkptr->label = mac_sysv_msgqueue_label_alloc();
102182063Srwatson	else
103182063Srwatson		msqkptr->label = NULL;
104137817Srwatson}
105137817Srwatson
106137817Srwatsonstatic void
107137817Srwatsonmac_sysv_msgmsg_label_free(struct label *label)
108137817Srwatson{
109137817Srwatson
110191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_destroy_label, label);
111137817Srwatson	mac_labelzone_free(label);
112137817Srwatson}
113137817Srwatson
114137817Srwatsonvoid
115172930Srwatsonmac_sysvmsg_destroy(struct msg *msgptr)
116137817Srwatson{
117137817Srwatson
118182063Srwatson	if (msgptr->label != NULL) {
119182063Srwatson		mac_sysv_msgmsg_label_free(msgptr->label);
120182063Srwatson		msgptr->label = NULL;
121182063Srwatson	}
122137817Srwatson}
123137817Srwatson
124137817Srwatsonstatic void
125137817Srwatsonmac_sysv_msgqueue_label_free(struct label *label)
126137817Srwatson{
127137817Srwatson
128191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_destroy_label, label);
129137817Srwatson	mac_labelzone_free(label);
130137817Srwatson}
131137817Srwatson
132137817Srwatsonvoid
133172930Srwatsonmac_sysvmsq_destroy(struct msqid_kernel *msqkptr)
134137817Srwatson{
135137817Srwatson
136182063Srwatson	if (msqkptr->label != NULL) {
137182063Srwatson		mac_sysv_msgqueue_label_free(msqkptr->label);
138182063Srwatson		msqkptr->label = NULL;
139182063Srwatson	}
140137817Srwatson}
141137817Srwatson
142137817Srwatsonvoid
143172930Srwatsonmac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
144137817Srwatson    struct msg *msgptr)
145137817Srwatson{
146165427Srwatson
147191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_create, cred, msqkptr,
148191731Srwatson	    msqkptr->label, msgptr, msgptr->label);
149137817Srwatson}
150137817Srwatson
151137817Srwatsonvoid
152172930Srwatsonmac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr)
153137817Srwatson{
154165427Srwatson
155191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_create, cred, msqkptr,
156191731Srwatson	    msqkptr->label);
157137817Srwatson}
158137817Srwatson
159137817Srwatsonvoid
160172930Srwatsonmac_sysvmsg_cleanup(struct msg *msgptr)
161137817Srwatson{
162137817Srwatson
163191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_cleanup, msgptr->label);
164137817Srwatson}
165137817Srwatson
166137817Srwatsonvoid
167172930Srwatsonmac_sysvmsq_cleanup(struct msqid_kernel *msqkptr)
168137817Srwatson{
169165427Srwatson
170191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_cleanup, msqkptr->label);
171137817Srwatson}
172137817Srwatson
173189503SrwatsonMAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msgmsq, "struct ucred *",
174189503Srwatson    "struct msg *", "struct msqid_kernel *");
175189503Srwatson
176137817Srwatsonint
177172930Srwatsonmac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr,
178137817Srwatson	struct msqid_kernel *msqkptr)
179137817Srwatson{
180137817Srwatson	int error;
181137817Srwatson
182191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgmsq, cred, msgptr,
183191731Srwatson	    msgptr->label, msqkptr, msqkptr->label);
184189503Srwatson	MAC_CHECK_PROBE3(sysvmsq_check_msgmsq, error, cred, msgptr, msqkptr);
185137817Srwatson
186165434Srwatson	return (error);
187137817Srwatson}
188137817Srwatson
189189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrcv, "struct ucred *",
190189503Srwatson    "struct msg *");
191189503Srwatson
192137817Srwatsonint
193172930Srwatsonmac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr)
194137817Srwatson{
195137817Srwatson	int error;
196137817Srwatson
197191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrcv, cred, msgptr,
198191731Srwatson	    msgptr->label);
199189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr);
200137817Srwatson
201165434Srwatson	return (error);
202137817Srwatson}
203137817Srwatson
204189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *",
205189503Srwatson    "struct msg *");
206189503Srwatson
207137817Srwatsonint
208172930Srwatsonmac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr)
209137817Srwatson{
210137817Srwatson	int error;
211137817Srwatson
212191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrmid, cred, msgptr,
213189797Srwatson	    msgptr->label);
214189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr);
215137817Srwatson
216165434Srwatson	return (error);
217137817Srwatson}
218137817Srwatson
219189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *",
220189503Srwatson    "struct msqid_kernel *");
221189503Srwatson
222137817Srwatsonint
223172930Srwatsonmac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
224137817Srwatson{
225137817Srwatson	int error;
226137817Srwatson
227191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqget, cred, msqkptr,
228189797Srwatson	    msqkptr->label);
229189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr);
230137817Srwatson
231165434Srwatson	return (error);
232137817Srwatson}
233137817Srwatson
234189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *",
235189503Srwatson    "struct msqid_kernel *");
236189503Srwatson
237137817Srwatsonint
238172930Srwatsonmac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
239137817Srwatson{
240137817Srwatson	int error;
241137817Srwatson
242191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqsnd, cred, msqkptr,
243189797Srwatson	    msqkptr->label);
244189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr);
245137817Srwatson
246165434Srwatson	return (error);
247137817Srwatson}
248137817Srwatson
249189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *",
250189503Srwatson    "struct msqid_kernel *");
251189503Srwatson
252137817Srwatsonint
253172930Srwatsonmac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
254137817Srwatson{
255137817Srwatson	int error;
256137817Srwatson
257191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqrcv, cred, msqkptr,
258189797Srwatson	    msqkptr->label);
259189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr);
260137817Srwatson
261165434Srwatson	return (error);
262137817Srwatson}
263137817Srwatson
264189503SrwatsonMAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *",
265189503Srwatson    "struct msqid_kernel *", "int");
266189503Srwatson
267137817Srwatsonint
268172930Srwatsonmac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
269137817Srwatson    int cmd)
270137817Srwatson{
271137817Srwatson	int error;
272137817Srwatson
273191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqctl, cred, msqkptr,
274189797Srwatson	    msqkptr->label, cmd);
275189503Srwatson	MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd);
276137817Srwatson
277165434Srwatson	return (error);
278137817Srwatson}
279