sysv_ipc.c revision 1.32.16.4 1 /* $NetBSD: sysv_ipc.c,v 1.32.16.4 2018/04/17 21:59:27 pgoyette 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.32.16.4 2018/04/17 21:59:27 pgoyette Exp $");
34
35 #ifdef _KERNEL_OPT
36 #include "opt_sysv.h"
37 #include "opt_compat_netbsd.h"
38 #endif
39
40 #include <sys/syscall.h>
41 #include <sys/syscallargs.h>
42 #include <sys/syscallvar.h>
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/ipc.h>
47 #ifdef SYSVMSG
48 #include <sys/msg.h>
49 #endif
50 #ifdef SYSVSEM
51 #include <sys/sem.h>
52 #endif
53 #ifdef SYSVSHM
54 #include <sys/shm.h>
55 #endif
56 #include <sys/systm.h>
57 #include <sys/kmem.h>
58 #include <sys/module.h>
59 #include <sys/mount.h>
60 #include <sys/vnode.h>
61 #include <sys/stat.h>
62 #include <sys/sysctl.h>
63 #include <sys/kauth.h>
64
65 #include <compat/common/compat_sysv_mod.h> /* for sysctl routine vector */
66
67 int (*vec_sysvipc50_sysctl)(SYSCTLFN_PROTO);
68
69 /*
70 * Values in support of System V compatible shared memory. XXX
71 * (originally located in sys/conf/param.c)
72 */
73 #ifdef SYSVSHM
74 #if !defined(SHMMAX) && defined(SHMMAXPGS)
75 #define SHMMAX SHMMAXPGS /* shminit() performs a `*= PAGE_SIZE' */
76 #elif !defined(SHMMAX)
77 #define SHMMAX 0
78 #endif
79 #ifndef SHMMIN
80 #define SHMMIN 1
81 #endif
82 #ifndef SHMMNI
83 #define SHMMNI 128 /* <64k, see IPCID_TO_IX in ipc.h */
84 #endif
85 #ifndef SHMSEG
86 #define SHMSEG 128
87 #endif
88
89 struct shminfo shminfo = {
90 SHMMAX,
91 SHMMIN,
92 SHMMNI,
93 SHMSEG,
94 0
95 };
96 #endif
97
98 /*
99 * Values in support of System V compatible semaphores.
100 */
101 #ifdef SYSVSEM
102 struct seminfo seminfo = {
103 SEMMAP, /* # of entries in semaphore map */
104 SEMMNI, /* # of semaphore identifiers */
105 SEMMNS, /* # of semaphores in system */
106 SEMMNU, /* # of undo structures in system */
107 SEMMSL, /* max # of semaphores per id */
108 SEMOPM, /* max # of operations per semop call */
109 SEMUME, /* max # of undo entries per process */
110 SEMUSZ, /* size in bytes of undo structure */
111 SEMVMX, /* semaphore maximum value */
112 SEMAEM /* adjust on exit max value */
113 };
114 #endif
115
116 /*
117 * Values in support of System V compatible messages.
118 */
119 #ifdef SYSVMSG
120 struct msginfo msginfo = {
121 MSGMAX, /* max chars in a message */
122 MSGMNI, /* # of message queue identifiers */
123 MSGMNB, /* max chars in a queue */
124 MSGTQL, /* max messages in system */
125 MSGSSZ, /* size of a message segment */
126 /* (must be small power of 2 greater than 4) */
127 MSGSEG /* number of message segments */
128 };
129 #endif
130
131 MODULE(MODULE_CLASS_EXEC, sysv_ipc, NULL);
132
133 SYSCTL_SETUP_PROTO(sysctl_ipc_setup);
134
135 static struct sysctllog *sysctl_sysvipc_clog = NULL;
136
137 static const struct syscall_package sysvipc_syscalls[] = {
138 #if defined(SYSVSHM)
139 { SYS___shmctl50, 0, (sy_call_t *)sys___shmctl50 },
140 { SYS_shmat, 0, (sy_call_t *)sys_shmat },
141 { SYS_shmdt, 0, (sy_call_t *)sys_shmdt },
142 { SYS_shmget, 0, (sy_call_t *)sys_shmget },
143 #endif /* SYSVSHM */
144
145 #if defined(SYSVSEM)
146 { SYS_____semctl50, 0, (sy_call_t *)sys_____semctl50 },
147 { SYS_semget, 0, (sy_call_t *)sys_semget },
148 { SYS_semop, 0, (sy_call_t *)sys_semop },
149 { SYS_semconfig, 0, (sy_call_t *)sys_semconfig },
150 #endif /* SYSVSEM */
151
152 #if defined(SYSVMSG)
153 { SYS___msgctl50, 0, (sy_call_t *)sys___msgctl50 },
154 { SYS_msgget, 0, (sy_call_t *)sys_msgget },
155 { SYS_msgsnd, 0, (sy_call_t *)sys_msgsnd },
156 { SYS_msgrcv, 0, (sy_call_t *)sys_msgrcv },
157 #endif /* SYSVMSG */
158 { 0, 0, NULL }
159 };
160
161 static int
162 sysv_ipc_modcmd(modcmd_t cmd, void *arg)
163 {
164 int error = 0;
165
166 switch (cmd) {
167 case MODULE_CMD_INIT:
168 /* Set up the kauth listener */
169 sysvipcinit();
170
171 #ifdef _MODULE
172 /* Set up the common sysctl tree */
173 sysctl_ipc_setup(&sysctl_sysvipc_clog);
174 #endif
175
176 /* Link the system calls */
177 error = syscall_establish(NULL, sysvipc_syscalls);
178 if (error)
179 sysvipcfini();
180
181 /*
182 * Initialize each sub-component, including their
183 * sysctl data
184 */
185 #ifdef SYSVSHM
186 shminit(&sysctl_sysvipc_clog);
187 #endif
188 #ifdef SYSVSEM
189 seminit(&sysctl_sysvipc_clog);
190 #endif
191 #ifdef SYSVMSG
192 msginit(&sysctl_sysvipc_clog);
193 #endif
194 break;
195 case MODULE_CMD_FINI:
196 /*
197 * Make sure no subcomponents are active. Each one
198 * tells us if it is busy, and if it was _not_ busy,
199 * we assume it has already done its own clean-up.
200 * So we might need to re-init any components that
201 * are successfully fini'd if we find one that is
202 * still busy.
203 */
204 #ifdef SYSVSHM
205 if (shmfini()) {
206 return EBUSY;
207 }
208 #endif
209 #ifdef SYSVSEM
210 if (semfini()) {
211 #ifdef SYSVSHM
212 shminit(NULL);
213 #endif
214 return EBUSY;
215 }
216 #endif
217 #ifdef SYSVMSG
218 if (msgfini()) {
219 #ifdef SYSVSEM
220 seminit(NULL);
221 #endif
222 #ifdef SYSVSHM
223 shminit(NULL);
224 #endif
225 return EBUSY;
226 }
227 #endif
228
229 /* Unlink the system calls. */
230 error = syscall_disestablish(NULL, sysvipc_syscalls);
231 if (error)
232 return error;
233
234 #ifdef _MODULE
235 /* Remove the sysctl sub-trees */
236 sysctl_teardown(&sysctl_sysvipc_clog);
237 #endif
238
239 /* Remove the kauth listener */
240 sysvipcfini();
241 break;
242 default:
243 return ENOTTY;
244 }
245 return error;
246 }
247
248 static kauth_listener_t sysvipc_listener = NULL;
249
250 static int
251 sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
252 void *arg0, void *arg1, void *arg2, void *arg3)
253 {
254 mode_t mask;
255 int ismember = 0;
256 struct ipc_perm *perm;
257 int mode;
258 enum kauth_system_req req;
259
260 req = (enum kauth_system_req)arg0;
261
262 if (!(action == KAUTH_SYSTEM_SYSVIPC &&
263 req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
264 return KAUTH_RESULT_DEFER;
265
266 perm = arg1;
267 mode = (int)(uintptr_t)arg2;
268
269 if (mode == IPC_M) {
270 if (kauth_cred_geteuid(cred) == perm->uid ||
271 kauth_cred_geteuid(cred) == perm->cuid)
272 return (KAUTH_RESULT_ALLOW);
273 return (KAUTH_RESULT_DEFER); /* EPERM */
274 }
275
276 mask = 0;
277
278 if (kauth_cred_geteuid(cred) == perm->uid ||
279 kauth_cred_geteuid(cred) == perm->cuid) {
280 if (mode & IPC_R)
281 mask |= S_IRUSR;
282 if (mode & IPC_W)
283 mask |= S_IWUSR;
284 return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
285 }
286
287 if (kauth_cred_getegid(cred) == perm->gid ||
288 (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
289 kauth_cred_getegid(cred) == perm->cgid ||
290 (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
291 if (mode & IPC_R)
292 mask |= S_IRGRP;
293 if (mode & IPC_W)
294 mask |= S_IWGRP;
295 return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
296 }
297
298 if (mode & IPC_R)
299 mask |= S_IROTH;
300 if (mode & IPC_W)
301 mask |= S_IWOTH;
302 return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
303 }
304
305 /*
306 * Check for ipc permission
307 */
308
309 int
310 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
311 {
312 int error;
313
314 error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
315 KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
316 if (error == 0)
317 return (0);
318
319 /* Adjust EPERM and EACCES errors until there's a better way to do this. */
320 if (mode != IPC_M)
321 error = EACCES;
322
323 return error;
324 }
325
326 void
327 sysvipcfini(void)
328 {
329
330 KASSERT(sysvipc_listener != NULL);
331 kauth_unlisten_scope(sysvipc_listener);
332 sysvipc_listener = NULL;
333 }
334
335 void
336 sysvipcinit(void)
337 {
338
339 KASSERT(sysvipc_listener == NULL);
340
341 sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
342 sysvipc_listener_cb, NULL);
343 }
344
345 /* Vector the old 50 sysctl stuff */
346
347 static int stub_sysvipc50_sysctl(SYSCTLFN_PROTO);
348 int (*vec_sysvipc50_sysctl)(SYSCTLFN_PROTO) = stub_sysvipc50_sysctl;
349
350 static int
351 stub_sysvipc50_sysctl(SYSCTLFN_ARGS)
352 {
353 return EPASSTHROUGH;
354 }
355
356 static int
357 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
358 {
359 void *where = oldp;
360 size_t sz, *sizep = oldlenp;
361 #ifdef SYSVMSG
362 struct msg_sysctl_info *msgsi = NULL;
363 #endif
364 #ifdef SYSVSEM
365 struct sem_sysctl_info *semsi = NULL;
366 #endif
367 #ifdef SYSVSHM
368 struct shm_sysctl_info *shmsi = NULL;
369 #endif
370 size_t infosize, dssize, tsize, buflen;
371 void *bf = NULL;
372 char *start;
373 int32_t nds;
374 int i, error, ret;
375
376 /*
377 * If present, call the compat sysctl() code. If it handles the request
378 * completely (either success or error), return. Otherwise fallthrough
379 * to the non-compat sysctl code.
380 */
381
382 error = (*vec_sysvipc50_sysctl)(SYSCTLFN_CALL(rnode));
383 if (error != EPASSTHROUGH)
384 return error;
385
386 if (namelen != 1)
387 return EINVAL;
388
389 start = where;
390 buflen = *sizep;
391
392 switch (*name) {
393 case KERN_SYSVIPC_MSG_INFO:
394 #ifdef SYSVMSG
395 infosize = sizeof(msgsi->msginfo);
396 nds = msginfo.msgmni;
397 dssize = sizeof(msgsi->msgids[0]);
398 break;
399 #else
400 return EINVAL;
401 #endif
402 case KERN_SYSVIPC_SEM_INFO:
403 #ifdef SYSVSEM
404 infosize = sizeof(semsi->seminfo);
405 nds = seminfo.semmni;
406 dssize = sizeof(semsi->semids[0]);
407 break;
408 #else
409 return EINVAL;
410 #endif
411 case KERN_SYSVIPC_SHM_INFO:
412 #ifdef SYSVSHM
413 infosize = sizeof(shmsi->shminfo);
414 nds = shminfo.shmmni;
415 dssize = sizeof(shmsi->shmids[0]);
416 break;
417 #else
418 return EINVAL;
419 #endif
420 default:
421 return EINVAL;
422 }
423 /*
424 * Round infosize to 64 bit boundary if requesting more than just
425 * the info structure or getting the total data size.
426 */
427 if (where == NULL || *sizep > infosize)
428 infosize = roundup(infosize, sizeof(quad_t));
429 tsize = infosize + nds * dssize;
430
431 /* Return just the total size required. */
432 if (where == NULL) {
433 *sizep = tsize;
434 return 0;
435 }
436
437 /* Not enough room for even the info struct. */
438 if (buflen < infosize) {
439 *sizep = 0;
440 return ENOMEM;
441 }
442 sz = min(tsize, buflen);
443 bf = kmem_zalloc(sz, KM_SLEEP);
444
445 switch (*name) {
446 #ifdef SYSVMSG
447 case KERN_SYSVIPC_MSG_INFO:
448 msgsi = (struct msg_sysctl_info *)bf;
449 msgsi->msginfo = msginfo;
450 break;
451 #endif
452 #ifdef SYSVSEM
453 case KERN_SYSVIPC_SEM_INFO:
454 semsi = (struct sem_sysctl_info *)bf;
455 semsi->seminfo = seminfo;
456 break;
457 #endif
458 #ifdef SYSVSHM
459 case KERN_SYSVIPC_SHM_INFO:
460 shmsi = (struct shm_sysctl_info *)bf;
461 shmsi->shminfo = shminfo;
462 break;
463 #endif
464 }
465 buflen -= infosize;
466
467 ret = 0;
468 if (buflen > 0) {
469 /* Fill in the IPC data structures. */
470 for (i = 0; i < nds; i++) {
471 if (buflen < dssize) {
472 ret = ENOMEM;
473 break;
474 }
475 switch (*name) {
476 #ifdef SYSVMSG
477 case KERN_SYSVIPC_MSG_INFO:
478 mutex_enter(&msgmutex);
479 SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
480 mutex_exit(&msgmutex);
481 break;
482 #endif
483 #ifdef SYSVSEM
484 case KERN_SYSVIPC_SEM_INFO:
485 SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
486 break;
487 #endif
488 #ifdef SYSVSHM
489 case KERN_SYSVIPC_SHM_INFO:
490 SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
491 break;
492 #endif
493 }
494 buflen -= dssize;
495 }
496 }
497 *sizep -= buflen;
498 error = copyout(bf, start, *sizep);
499 /* If copyout succeeded, use return code set earlier. */
500 if (error == 0)
501 error = ret;
502 if (bf)
503 kmem_free(bf, sz);
504 return error;
505 }
506
507 SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
508 {
509
510 sysctl_createv(clog, 0, NULL, NULL,
511 CTLFLAG_PERMANENT,
512 CTLTYPE_NODE, "ipc",
513 SYSCTL_DESCR("SysV IPC options"),
514 NULL, 0, NULL, 0,
515 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
516
517 sysctl_createv(clog, 0, NULL, NULL,
518 CTLFLAG_PERMANENT,
519 CTLTYPE_STRUCT, "sysvipc_info",
520 SYSCTL_DESCR("System V style IPC information"),
521 sysctl_kern_sysvipc, 0, NULL, 0,
522 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
523 }
524