xref: /freebsd/sys/security/mac/mac_sysv_shm.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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/shm.h>
51 
52 #include <sys/mac_policy.h>
53 
54 #include <security/mac/mac_internal.h>
55 
56 static int	mac_enforce_sysv_shm = 1;
57 SYSCTL_INT(_security_mac, OID_AUTO, enforce_sysv_shm, CTLFLAG_RW,
58     &mac_enforce_sysv_shm, 0,
59     "Enforce MAC policy on System V IPC shared memory");
60 TUNABLE_INT("security.mac.enforce_sysv", &mac_enforce_sysv_shm);
61 
62 #ifdef MAC_DEBUG
63 static unsigned int nmacipcshms;
64 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipc_shms, CTLFLAG_RD,
65     &nmacipcshms, 0, "number of sysv ipc shm identifiers inuse");
66 #endif
67 
68 static struct label *
69 mac_sysv_shm_label_alloc(void)
70 {
71 	struct label *label;
72 
73 	label = mac_labelzone_alloc(M_WAITOK);
74 	MAC_PERFORM(init_sysv_shm_label, label);
75 	MAC_DEBUG_COUNTER_INC(&nmacipcshms);
76 	return (label);
77 }
78 
79 void
80 mac_init_sysv_shm(struct shmid_kernel *shmsegptr)
81 {
82 
83 	shmsegptr->label = mac_sysv_shm_label_alloc();
84 }
85 
86 static void
87 mac_sysv_shm_label_free(struct label *label)
88 {
89 
90 	MAC_PERFORM(destroy_sysv_shm_label, label);
91 	mac_labelzone_free(label);
92 	MAC_DEBUG_COUNTER_DEC(&nmacipcshms);
93 }
94 
95 void
96 mac_destroy_sysv_shm(struct shmid_kernel *shmsegptr)
97 {
98 
99 	mac_sysv_shm_label_free(shmsegptr->label);
100 	shmsegptr->label = NULL;
101 }
102 
103 void
104 mac_create_sysv_shm(struct ucred *cred, struct shmid_kernel *shmsegptr)
105 {
106 
107 	MAC_PERFORM(create_sysv_shm, cred, shmsegptr, shmsegptr->label);
108 }
109 
110 void
111 mac_cleanup_sysv_shm(struct shmid_kernel *shmsegptr)
112 {
113 
114 	MAC_PERFORM(cleanup_sysv_shm, shmsegptr->label);
115 }
116 
117 int
118 mac_check_sysv_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
119     int shmflg)
120 {
121 	int error;
122 
123 	if (!mac_enforce_sysv_shm)
124 		return (0);
125 
126 	MAC_CHECK(check_sysv_shmat, cred, shmsegptr, shmsegptr->label,
127 	    shmflg);
128 
129 	return(error);
130 }
131 
132 int
133 mac_check_sysv_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
134     int cmd)
135 {
136 	int error;
137 
138 	if (!mac_enforce_sysv_shm)
139 		return (0);
140 
141 	MAC_CHECK(check_sysv_shmctl, cred, shmsegptr, shmsegptr->label,
142 	    cmd);
143 
144 	return(error);
145 }
146 
147 int
148 mac_check_sysv_shmdt(struct ucred *cred, struct shmid_kernel *shmsegptr)
149 {
150 	int error;
151 
152 	if (!mac_enforce_sysv_shm)
153 		return (0);
154 
155 	MAC_CHECK(check_sysv_shmdt, cred, shmsegptr, shmsegptr->label);
156 
157 	return(error);
158 }
159 
160 int
161 mac_check_sysv_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
162     int shmflg)
163 {
164 	int error;
165 
166 	if (!mac_enforce_sysv_shm)
167 		return (0);
168 
169 	MAC_CHECK(check_sysv_shmget, cred, shmsegptr, shmsegptr->label,
170 	    shmflg);
171 
172 	return(error);
173 }
174