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