xref: /netbsd-src/sys/kern/sysv_ipc.c (revision f269b04f028fa7b57bc00c9b7be1fbf7b7d39f2b)
1 /*	$NetBSD: sysv_ipc.c,v 1.43 2024/10/09 16:27:28 christos 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.43 2024/10/09 16:27:28 christos Exp $");
34 
35 #ifdef _KERNEL_OPT
36 #include "opt_sysv.h"
37 #include "opt_sysvparam.h"
38 #include "opt_compat_netbsd.h"
39 #endif
40 
41 #include <sys/syscall.h>
42 #include <sys/syscallargs.h>
43 #include <sys/syscallvar.h>
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/ipc.h>
48 #ifdef SYSVMSG
49 #include <sys/msg.h>
50 #endif
51 #ifdef SYSVSEM
52 #include <sys/sem.h>
53 #endif
54 #ifdef SYSVSHM
55 #include <sys/shm.h>
56 #endif
57 #include <sys/systm.h>
58 #include <sys/kmem.h>
59 #include <sys/module.h>
60 #include <sys/mount.h>
61 #include <sys/vnode.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64 #include <sys/kauth.h>
65 #include <sys/compat_stub.h>
66 
67 #include <compat/common/compat_sysv_mod.h>	/* for sysctl routine vector */
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 const struct syscall_package sysvipc_syscalls[] = {
136 #if defined(SYSVSHM)
137 	{ SYS___shmctl50, 0, (sy_call_t *)sys___shmctl50 },
138 	{ SYS_shmat, 0, (sy_call_t *)sys_shmat },
139 	{ SYS_shmdt, 0, (sy_call_t *)sys_shmdt },
140 	{ SYS_shmget, 0, (sy_call_t *)sys_shmget },
141 #endif	/* SYSVSHM */
142 
143 #if defined(SYSVSEM)
144 	{ SYS_____semctl50, 0, (sy_call_t *)sys_____semctl50 },
145 	{ SYS_semget, 0, (sy_call_t *)sys_semget },
146 	{ SYS_semop, 0, (sy_call_t *)sys_semop },
147 	{ SYS_semconfig, 0, (sy_call_t *)sys_semconfig },
148 	{ SYS_semtimedop, 0, (sy_call_t *)sys_semtimedop },
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 		error = shminit();
183 		if (error != 0)
184 			return error;
185 #endif
186 #ifdef SYSVSEM
187 		error = seminit();
188 		if (error != 0) {
189 #ifdef SYSVSHM
190 			shmfini();
191 #endif
192 			return error;
193 		}
194 #endif
195 #ifdef SYSVMSG
196 		error = msginit();
197 		if (error != 0) {
198 #ifdef SYSVSEM
199 			semfini();
200 #endif
201 #ifdef SYSVSHM
202 			shmfini();
203 #endif
204 			return error;
205 		}
206 #endif
207 		break;
208 	case MODULE_CMD_FINI:
209 		/*
210 		 * Make sure no subcomponents are active.  Each one
211 		 * tells us if it is busy, and if it was _not_ busy,
212 		 * we assume it has already done its own clean-up.
213 		 * So we might need to re-init any components that
214 		 * are successfully fini'd if we find one that is
215 		 * still busy.
216 		 */
217 #ifdef SYSVSHM
218 		if (shmfini()) {
219 			return EBUSY;
220 		}
221 #endif
222 #ifdef SYSVSEM
223 		if (semfini()) {
224 #ifdef SYSVSHM
225 			shminit();
226 #endif
227 			return EBUSY;
228 		}
229 #endif
230 #ifdef SYSVMSG
231 		if (msgfini()) {
232 #ifdef SYSVSEM
233 			seminit();
234 #endif
235 #ifdef SYSVSHM
236 			shminit();
237 #endif
238 			return EBUSY;
239 		}
240 #endif
241 		/* Unlink the system calls. */
242 		error = syscall_disestablish(NULL, sysvipc_syscalls);
243 		if (error)
244 			return error;
245 
246 		/* Remove the kauth listener */
247 		sysvipcfini();
248 		break;
249 	default:
250 		return ENOTTY;
251 	}
252 	return error;
253 }
254 
255 static kauth_listener_t sysvipc_listener = NULL;
256 
257 static int
258 sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
259     void *arg0, void *arg1, void *arg2, void *arg3)
260 {
261 	mode_t mask;
262 	struct ipc_perm *perm;
263 	int mode;
264 	enum kauth_system_req req;
265 
266 	req = (enum kauth_system_req)(uintptr_t)arg0;
267 
268 	if (!(action == KAUTH_SYSTEM_SYSVIPC &&
269 	      req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
270 		return KAUTH_RESULT_DEFER;
271 
272 	perm = arg1;
273 	mode = (int)(uintptr_t)arg2;
274 
275 	if (mode == IPC_M) {
276 		if (kauth_cred_geteuid(cred) == perm->uid ||
277 		    kauth_cred_geteuid(cred) == perm->cuid)
278 			return (KAUTH_RESULT_ALLOW);
279 		return (KAUTH_RESULT_DEFER); /* EPERM */
280 	}
281 
282 	mask = 0;
283 
284 	if (kauth_cred_geteuid(cred) == perm->uid ||
285 	    kauth_cred_geteuid(cred) == perm->cuid) {
286 		if (mode & IPC_R)
287 			mask |= S_IRUSR;
288 		if (mode & IPC_W)
289 			mask |= S_IWUSR;
290 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
291 	}
292 
293 	if (kauth_cred_groupmember(cred, perm->gid) == 0 ||
294 	    kauth_cred_groupmember(cred, perm->cgid) == 0) {
295 		if (mode & IPC_R)
296 			mask |= S_IRGRP;
297 		if (mode & IPC_W)
298 			mask |= S_IWGRP;
299 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
300 	}
301 
302 	if (mode & IPC_R)
303 		mask |= S_IROTH;
304 	if (mode & IPC_W)
305 		mask |= S_IWOTH;
306 	return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
307 }
308 
309 /*
310  * Check for ipc permission
311  */
312 
313 int
314 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
315 {
316 	int error;
317 
318 	error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
319 	    KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
320 	if (error == 0)
321 		return (0);
322 
323 	/* Adjust EPERM and EACCES errors until there's a better way to do this. */
324 	if (mode != IPC_M)
325 		error = EACCES;
326 
327 	return error;
328 }
329 
330 void
331 sysvipcfini(void)
332 {
333 
334 	KASSERT(sysvipc_listener != NULL);
335 	kauth_unlisten_scope(sysvipc_listener);
336 	sysvipc_listener = NULL;
337 }
338 
339 void
340 sysvipcinit(void)
341 {
342 
343 	KASSERT(sysvipc_listener == NULL);
344 
345 	sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
346 	    sysvipc_listener_cb, NULL);
347 }
348 
349 static int
350 stub_sysvipc50_sysctl(SYSCTLFN_ARGS)
351 {
352 	return EPASSTHROUGH;
353 }
354 
355 static int
356 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
357 {
358 	void *where = oldp;
359 	size_t sz, *sizep = oldlenp;
360 #ifdef SYSVMSG
361 	struct msg_sysctl_info *msgsi = NULL;
362 #endif
363 #ifdef SYSVSEM
364 	struct sem_sysctl_info *semsi = NULL;
365 #endif
366 #ifdef SYSVSHM
367 	struct shm_sysctl_info *shmsi = NULL;
368 #endif
369 	size_t infosize, dssize, tsize, buflen;
370 	void *bf = NULL;
371 	char *start;
372 	int32_t nds;
373 	int i, error, ret;
374 
375 /*
376  * If present, call the compat sysctl() code.  If it handles the request
377  * completely (either success or error), return.  Otherwise fallthrough
378  * to the non-compat sysctl code.
379  */
380 
381 	MODULE_HOOK_CALL(sysvipc_sysctl_50_hook, (SYSCTLFN_CALL(rnode)),
382 	    stub_sysvipc50_sysctl(SYSCTLFN_CALL(rnode)), error);
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 = uimin(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