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