sysv_ipc.c revision 1.24.2.1 1 /* $NetBSD: sysv_ipc.c,v 1.24.2.1 2014/08/20 00:04:29 tls Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.24.2.1 2014/08/20 00:04:29 tls Exp $");
34
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/ipc.h>
41 #ifdef SYSVMSG
42 #include <sys/msg.h>
43 #endif
44 #ifdef SYSVSEM
45 #include <sys/sem.h>
46 #endif
47 #ifdef SYSVSHM
48 #include <sys/shm.h>
49 #endif
50 #include <sys/systm.h>
51 #include <sys/kmem.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/stat.h>
55 #include <sys/sysctl.h>
56 #include <sys/kauth.h>
57
58 #ifdef COMPAT_50
59 #include <compat/sys/ipc.h>
60 #endif
61
62 static kauth_listener_t sysvipc_listener = NULL;
63
64 static int
65 sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
66 void *arg0, void *arg1, void *arg2, void *arg3)
67 {
68 mode_t mask;
69 int ismember = 0;
70 struct ipc_perm *perm;
71 int mode;
72 enum kauth_system_req req;
73
74 req = (enum kauth_system_req)arg0;
75
76 if (!(action == KAUTH_SYSTEM_SYSVIPC &&
77 req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
78 return KAUTH_RESULT_DEFER;
79
80 perm = arg1;
81 mode = (int)(uintptr_t)arg2;
82
83 if (mode == IPC_M) {
84 if (kauth_cred_geteuid(cred) == perm->uid ||
85 kauth_cred_geteuid(cred) == perm->cuid)
86 return (KAUTH_RESULT_ALLOW);
87 return (KAUTH_RESULT_DEFER); /* EPERM */
88 }
89
90 mask = 0;
91
92 if (kauth_cred_geteuid(cred) == perm->uid ||
93 kauth_cred_geteuid(cred) == perm->cuid) {
94 if (mode & IPC_R)
95 mask |= S_IRUSR;
96 if (mode & IPC_W)
97 mask |= S_IWUSR;
98 return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
99 }
100
101 if (kauth_cred_getegid(cred) == perm->gid ||
102 (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
103 kauth_cred_getegid(cred) == perm->cgid ||
104 (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
105 if (mode & IPC_R)
106 mask |= S_IRGRP;
107 if (mode & IPC_W)
108 mask |= S_IWGRP;
109 return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
110 }
111
112 if (mode & IPC_R)
113 mask |= S_IROTH;
114 if (mode & IPC_W)
115 mask |= S_IWOTH;
116 return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
117 }
118
119 /*
120 * Check for ipc permission
121 */
122
123 int
124 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
125 {
126 int error;
127
128 error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
129 KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
130 if (error == 0)
131 return (0);
132
133 /* Adjust EPERM and EACCES errors until there's a better way to do this. */
134 if (mode != IPC_M)
135 error = EACCES;
136
137 return error;
138 }
139
140 void
141 sysvipcinit(void)
142 {
143
144 if (sysvipc_listener != NULL)
145 return;
146
147 sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
148 sysvipc_listener_cb, NULL);
149 }
150
151 static int
152 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
153 {
154 void *where = oldp;
155 size_t sz, *sizep = oldlenp;
156 #ifdef SYSVMSG
157 struct msg_sysctl_info *msgsi = NULL;
158 #endif
159 #ifdef SYSVSEM
160 struct sem_sysctl_info *semsi = NULL;
161 #endif
162 #ifdef SYSVSHM
163 struct shm_sysctl_info *shmsi = NULL;
164 #endif
165 size_t infosize, dssize, tsize, buflen;
166 void *bf = NULL;
167 char *start;
168 int32_t nds;
169 int i, error, ret;
170
171 #ifdef COMPAT_50
172 switch ((error = sysctl_kern_sysvipc50(SYSCTLFN_CALL(rnode)))) {
173 case 0:
174 return 0;
175 case EPASSTHROUGH:
176 break;
177 default:
178 return error;
179 }
180 #endif
181 if (namelen != 1)
182 return EINVAL;
183
184 start = where;
185 buflen = *sizep;
186
187 switch (*name) {
188 case KERN_SYSVIPC_MSG_INFO:
189 #ifdef SYSVMSG
190 infosize = sizeof(msgsi->msginfo);
191 nds = msginfo.msgmni;
192 dssize = sizeof(msgsi->msgids[0]);
193 break;
194 #else
195 return EINVAL;
196 #endif
197 case KERN_SYSVIPC_SEM_INFO:
198 #ifdef SYSVSEM
199 infosize = sizeof(semsi->seminfo);
200 nds = seminfo.semmni;
201 dssize = sizeof(semsi->semids[0]);
202 break;
203 #else
204 return EINVAL;
205 #endif
206 case KERN_SYSVIPC_SHM_INFO:
207 #ifdef SYSVSHM
208 infosize = sizeof(shmsi->shminfo);
209 nds = shminfo.shmmni;
210 dssize = sizeof(shmsi->shmids[0]);
211 break;
212 #else
213 return EINVAL;
214 #endif
215 default:
216 return EINVAL;
217 }
218 /*
219 * Round infosize to 64 bit boundary if requesting more than just
220 * the info structure or getting the total data size.
221 */
222 if (where == NULL || *sizep > infosize)
223 infosize = roundup(infosize, sizeof(quad_t));
224 tsize = infosize + nds * dssize;
225
226 /* Return just the total size required. */
227 if (where == NULL) {
228 *sizep = tsize;
229 return 0;
230 }
231
232 /* Not enough room for even the info struct. */
233 if (buflen < infosize) {
234 *sizep = 0;
235 return ENOMEM;
236 }
237 sz = min(tsize, buflen);
238 bf = kmem_zalloc(sz, KM_SLEEP);
239
240 switch (*name) {
241 #ifdef SYSVMSG
242 case KERN_SYSVIPC_MSG_INFO:
243 msgsi = (struct msg_sysctl_info *)bf;
244 msgsi->msginfo = msginfo;
245 break;
246 #endif
247 #ifdef SYSVSEM
248 case KERN_SYSVIPC_SEM_INFO:
249 semsi = (struct sem_sysctl_info *)bf;
250 semsi->seminfo = seminfo;
251 break;
252 #endif
253 #ifdef SYSVSHM
254 case KERN_SYSVIPC_SHM_INFO:
255 shmsi = (struct shm_sysctl_info *)bf;
256 shmsi->shminfo = shminfo;
257 break;
258 #endif
259 }
260 buflen -= infosize;
261
262 ret = 0;
263 if (buflen > 0) {
264 /* Fill in the IPC data structures. */
265 for (i = 0; i < nds; i++) {
266 if (buflen < dssize) {
267 ret = ENOMEM;
268 break;
269 }
270 switch (*name) {
271 #ifdef SYSVMSG
272 case KERN_SYSVIPC_MSG_INFO:
273 mutex_enter(&msgmutex);
274 SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
275 mutex_exit(&msgmutex);
276 break;
277 #endif
278 #ifdef SYSVSEM
279 case KERN_SYSVIPC_SEM_INFO:
280 SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
281 break;
282 #endif
283 #ifdef SYSVSHM
284 case KERN_SYSVIPC_SHM_INFO:
285 SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
286 break;
287 #endif
288 }
289 buflen -= dssize;
290 }
291 }
292 *sizep -= buflen;
293 error = copyout(bf, start, *sizep);
294 /* If copyout succeeded, use return code set earlier. */
295 if (error == 0)
296 error = ret;
297 if (bf)
298 kmem_free(bf, sz);
299 return error;
300 }
301
302 SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
303 {
304
305 sysctl_createv(clog, 0, NULL, NULL,
306 CTLFLAG_PERMANENT,
307 CTLTYPE_NODE, "ipc",
308 SYSCTL_DESCR("SysV IPC options"),
309 NULL, 0, NULL, 0,
310 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
311
312 sysctl_createv(clog, 0, NULL, NULL,
313 CTLFLAG_PERMANENT,
314 CTLTYPE_STRUCT, "sysvipc_info",
315 SYSCTL_DESCR("System V style IPC information"),
316 sysctl_kern_sysvipc, 0, NULL, 0,
317 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
318 }
319