xref: /freebsd/sys/security/mac/mac_posix_shm.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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_kdtrace.h"
43 #include "opt_mac.h"
44 
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/mman.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_posixshm_label_alloc(void)
60 {
61 	struct label *label;
62 
63 	label = mac_labelzone_alloc(M_WAITOK);
64 	MAC_POLICY_PERFORM(posixshm_init_label, label);
65 	return (label);
66 }
67 
68 void
69 mac_posixshm_init(struct shmfd *shmfd)
70 {
71 
72 	if (mac_labeled & MPC_OBJECT_POSIXSHM)
73 		shmfd->shm_label = mac_posixshm_label_alloc();
74 	else
75 		shmfd->shm_label = NULL;
76 }
77 
78 static void
79 mac_posixshm_label_free(struct label *label)
80 {
81 
82 	MAC_POLICY_PERFORM_NOSLEEP(posixshm_destroy_label, label);
83 	mac_labelzone_free(label);
84 }
85 
86 void
87 mac_posixshm_destroy(struct shmfd *shmfd)
88 {
89 
90 	if (shmfd->shm_label != NULL) {
91 		mac_posixshm_label_free(shmfd->shm_label);
92 		shmfd->shm_label = NULL;
93 	}
94 }
95 
96 void
97 mac_posixshm_create(struct ucred *cred, struct shmfd *shmfd)
98 {
99 
100 	MAC_POLICY_PERFORM_NOSLEEP(posixshm_create, cred, shmfd,
101 	    shmfd->shm_label);
102 }
103 
104 MAC_CHECK_PROBE_DEFINE4(posixshm_check_mmap, "struct ucred *",
105     "struct shmfd *", "int", "int");
106 
107 int
108 mac_posixshm_check_mmap(struct ucred *cred, struct shmfd *shmfd, int prot,
109     int flags)
110 {
111 	int error;
112 
113 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_mmap, cred, shmfd,
114 	    shmfd->shm_label, prot, flags);
115 	MAC_CHECK_PROBE4(posixshm_check_mmap, error, cred, shmfd, prot,
116 	    flags);
117 
118 	return (error);
119 }
120 
121 MAC_CHECK_PROBE_DEFINE2(posixshm_check_open, "struct ucred *",
122     "struct shmfd *");
123 
124 int
125 mac_posixshm_check_open(struct ucred *cred, struct shmfd *shmfd)
126 {
127 	int error;
128 
129 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_open, cred, shmfd,
130 	    shmfd->shm_label);
131 	MAC_CHECK_PROBE2(posixshm_check_open, error, cred, shmfd);
132 
133 	return (error);
134 }
135 
136 MAC_CHECK_PROBE_DEFINE3(posixshm_check_stat, "struct ucred *",
137     "struct ucred *", "struct shmfd *");
138 
139 int
140 mac_posixshm_check_stat(struct ucred *active_cred, struct ucred *file_cred,
141     struct shmfd *shmfd)
142 {
143 	int error;
144 
145 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_stat, active_cred, file_cred,
146 	    shmfd, shmfd->shm_label);
147 	MAC_CHECK_PROBE3(posixshm_check_stat, error, active_cred, file_cred,
148 	    shmfd);
149 
150 	return (error);
151 }
152 
153 MAC_CHECK_PROBE_DEFINE3(posixshm_check_truncate, "struct ucred *",
154     "struct ucred *", "struct shmfd *");
155 
156 int
157 mac_posixshm_check_truncate(struct ucred *active_cred, struct ucred *file_cred,
158     struct shmfd *shmfd)
159 {
160 	int error;
161 
162 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_truncate, active_cred,
163 	    file_cred, shmfd, shmfd->shm_label);
164 	MAC_CHECK_PROBE3(posixshm_check_truncate, error, active_cred,
165 	    file_cred, shmfd);
166 
167 	return (error);
168 }
169 
170 MAC_CHECK_PROBE_DEFINE2(posixshm_check_unlink, "struct ucred *",
171     "struct shmfd *");
172 
173 int
174 mac_posixshm_check_unlink(struct ucred *cred, struct shmfd *shmfd)
175 {
176 	int error;
177 
178 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_unlink, cred, shmfd,
179 	    shmfd->shm_label);
180 	MAC_CHECK_PROBE2(posixshm_check_unlink, error, cred, shmfd);
181 
182 	return (error);
183 }
184