Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.32.18.3
      1 /*	$NetBSD: sysv_ipc.c,v 1.32.18.3 2020/04/13 08:05:04 martin 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.18.3 2020/04/13 08:05:04 martin 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 #endif	/* SYSVSEM */
    149 
    150 #if defined(SYSVMSG)
    151 	{ SYS___msgctl50, 0, (sy_call_t *)sys___msgctl50 },
    152 	{ SYS_msgget, 0, (sy_call_t *)sys_msgget },
    153 	{ SYS_msgsnd, 0, (sy_call_t *)sys_msgsnd },
    154 	{ SYS_msgrcv, 0, (sy_call_t *)sys_msgrcv },
    155 #endif	/* SYSVMSG */
    156 	{ 0, 0, NULL }
    157 };
    158 
    159 static int
    160 sysv_ipc_modcmd(modcmd_t cmd, void *arg)
    161 {
    162 	int error = 0;
    163 
    164 	switch (cmd) {
    165 	case MODULE_CMD_INIT:
    166 		/* Set up the kauth listener */
    167 		sysvipcinit();
    168 
    169 		/* Link the system calls */
    170 		error = syscall_establish(NULL, sysvipc_syscalls);
    171 		if (error) {
    172 			sysvipcfini();
    173 			return error;
    174 		}
    175 
    176 		/*
    177 		 * Initialize each sub-component, including their
    178 		 * sysctl data
    179 		 */
    180 #ifdef SYSVSHM
    181 		error = shminit();
    182 		if (error != 0)
    183 			return error;
    184 #endif
    185 #ifdef SYSVSEM
    186 		error = seminit();
    187 		if (error != 0) {
    188 #ifdef SYSVSHM
    189 			shmfini();
    190 #endif
    191 			return error;
    192 		}
    193 #endif
    194 #ifdef SYSVMSG
    195 		error = msginit();
    196 		if (error != 0) {
    197 #ifdef SYSVSEM
    198 			semfini();
    199 #endif
    200 #ifdef SYSVSHM
    201 			shmfini();
    202 #endif
    203 			return error;
    204 		}
    205 #endif
    206 		break;
    207 	case MODULE_CMD_FINI:
    208 		/*
    209 		 * Make sure no subcomponents are active.  Each one
    210 		 * tells us if it is busy, and if it was _not_ busy,
    211 		 * we assume it has already done its own clean-up.
    212 		 * So we might need to re-init any components that
    213 		 * are successfully fini'd if we find one that is
    214 		 * still busy.
    215 		 */
    216 #ifdef SYSVSHM
    217 		if (shmfini()) {
    218 			return EBUSY;
    219 		}
    220 #endif
    221 #ifdef SYSVSEM
    222 		if (semfini()) {
    223 #ifdef SYSVSHM
    224 			shminit();
    225 #endif
    226 			return EBUSY;
    227 		}
    228 #endif
    229 #ifdef SYSVMSG
    230 		if (msgfini()) {
    231 #ifdef SYSVSEM
    232 			seminit();
    233 #endif
    234 #ifdef SYSVSHM
    235 			shminit();
    236 #endif
    237 			return EBUSY;
    238 		}
    239 #endif
    240 		/* Unlink the system calls. */
    241 		error = syscall_disestablish(NULL, sysvipc_syscalls);
    242 		if (error)
    243 			return error;
    244 
    245 		/* Remove the kauth listener */
    246 		sysvipcfini();
    247 		break;
    248 	default:
    249 		return ENOTTY;
    250 	}
    251 	return error;
    252 }
    253 
    254 static kauth_listener_t sysvipc_listener = NULL;
    255 
    256 static int
    257 sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    258     void *arg0, void *arg1, void *arg2, void *arg3)
    259 {
    260 	mode_t mask;
    261 	int ismember = 0;
    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_getegid(cred) == perm->gid ||
    294 	    (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
    295 	    kauth_cred_getegid(cred) == perm->cgid ||
    296 	    (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
    297 		if (mode & IPC_R)
    298 			mask |= S_IRGRP;
    299 		if (mode & IPC_W)
    300 			mask |= S_IWGRP;
    301 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    302 	}
    303 
    304 	if (mode & IPC_R)
    305 		mask |= S_IROTH;
    306 	if (mode & IPC_W)
    307 		mask |= S_IWOTH;
    308 	return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    309 }
    310 
    311 /*
    312  * Check for ipc permission
    313  */
    314 
    315 int
    316 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
    317 {
    318 	int error;
    319 
    320 	error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
    321 	    KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
    322 	if (error == 0)
    323 		return (0);
    324 
    325 	/* Adjust EPERM and EACCES errors until there's a better way to do this. */
    326 	if (mode != IPC_M)
    327 		error = EACCES;
    328 
    329 	return error;
    330 }
    331 
    332 void
    333 sysvipcfini(void)
    334 {
    335 
    336 	KASSERT(sysvipc_listener != NULL);
    337 	kauth_unlisten_scope(sysvipc_listener);
    338 	sysvipc_listener = NULL;
    339 }
    340 
    341 void
    342 sysvipcinit(void)
    343 {
    344 
    345 	KASSERT(sysvipc_listener == NULL);
    346 
    347 	sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    348 	    sysvipc_listener_cb, NULL);
    349 }
    350 
    351 static int
    352 stub_sysvipc50_sysctl(SYSCTLFN_ARGS)
    353 {
    354 	return EPASSTHROUGH;
    355 }
    356 
    357 static int
    358 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
    359 {
    360 	void *where = oldp;
    361 	size_t sz, *sizep = oldlenp;
    362 #ifdef SYSVMSG
    363 	struct msg_sysctl_info *msgsi = NULL;
    364 #endif
    365 #ifdef SYSVSEM
    366 	struct sem_sysctl_info *semsi = NULL;
    367 #endif
    368 #ifdef SYSVSHM
    369 	struct shm_sysctl_info *shmsi = NULL;
    370 #endif
    371 	size_t infosize, dssize, tsize, buflen;
    372 	void *bf = NULL;
    373 	char *start;
    374 	int32_t nds;
    375 	int i, error, ret;
    376 
    377 /*
    378  * If present, call the compat sysctl() code.  If it handles the request
    379  * completely (either success or error), return.  Otherwise fallthrough
    380  * to the non-compat sysctl code.
    381  */
    382 
    383 	MODULE_HOOK_CALL(sysvipc_sysctl_50_hook, (SYSCTLFN_CALL(rnode)),
    384 	    stub_sysvipc50_sysctl(SYSCTLFN_CALL(rnode)), error);
    385 	if (error != EPASSTHROUGH)
    386 		return error;
    387 
    388 	if (namelen != 1)
    389 		return EINVAL;
    390 
    391 	start = where;
    392 	buflen = *sizep;
    393 
    394 	switch (*name) {
    395 	case KERN_SYSVIPC_MSG_INFO:
    396 #ifdef SYSVMSG
    397 		infosize = sizeof(msgsi->msginfo);
    398 		nds = msginfo.msgmni;
    399 		dssize = sizeof(msgsi->msgids[0]);
    400 		break;
    401 #else
    402 		return EINVAL;
    403 #endif
    404 	case KERN_SYSVIPC_SEM_INFO:
    405 #ifdef SYSVSEM
    406 		infosize = sizeof(semsi->seminfo);
    407 		nds = seminfo.semmni;
    408 		dssize = sizeof(semsi->semids[0]);
    409 		break;
    410 #else
    411 		return EINVAL;
    412 #endif
    413 	case KERN_SYSVIPC_SHM_INFO:
    414 #ifdef SYSVSHM
    415 		infosize = sizeof(shmsi->shminfo);
    416 		nds = shminfo.shmmni;
    417 		dssize = sizeof(shmsi->shmids[0]);
    418 		break;
    419 #else
    420 		return EINVAL;
    421 #endif
    422 	default:
    423 		return EINVAL;
    424 	}
    425 	/*
    426 	 * Round infosize to 64 bit boundary if requesting more than just
    427 	 * the info structure or getting the total data size.
    428 	 */
    429 	if (where == NULL || *sizep > infosize)
    430 		infosize = roundup(infosize, sizeof(quad_t));
    431 	tsize = infosize + nds * dssize;
    432 
    433 	/* Return just the total size required. */
    434 	if (where == NULL) {
    435 		*sizep = tsize;
    436 		return 0;
    437 	}
    438 
    439 	/* Not enough room for even the info struct. */
    440 	if (buflen < infosize) {
    441 		*sizep = 0;
    442 		return ENOMEM;
    443 	}
    444 	sz = uimin(tsize, buflen);
    445 	bf = kmem_zalloc(sz, KM_SLEEP);
    446 
    447 	switch (*name) {
    448 #ifdef SYSVMSG
    449 	case KERN_SYSVIPC_MSG_INFO:
    450 		msgsi = (struct msg_sysctl_info *)bf;
    451 		msgsi->msginfo = msginfo;
    452 		break;
    453 #endif
    454 #ifdef SYSVSEM
    455 	case KERN_SYSVIPC_SEM_INFO:
    456 		semsi = (struct sem_sysctl_info *)bf;
    457 		semsi->seminfo = seminfo;
    458 		break;
    459 #endif
    460 #ifdef SYSVSHM
    461 	case KERN_SYSVIPC_SHM_INFO:
    462 		shmsi = (struct shm_sysctl_info *)bf;
    463 		shmsi->shminfo = shminfo;
    464 		break;
    465 #endif
    466 	}
    467 	buflen -= infosize;
    468 
    469 	ret = 0;
    470 	if (buflen > 0) {
    471 		/* Fill in the IPC data structures.  */
    472 		for (i = 0; i < nds; i++) {
    473 			if (buflen < dssize) {
    474 				ret = ENOMEM;
    475 				break;
    476 			}
    477 			switch (*name) {
    478 #ifdef SYSVMSG
    479 			case KERN_SYSVIPC_MSG_INFO:
    480 				mutex_enter(&msgmutex);
    481 				SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
    482 				mutex_exit(&msgmutex);
    483 				break;
    484 #endif
    485 #ifdef SYSVSEM
    486 			case KERN_SYSVIPC_SEM_INFO:
    487 				SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
    488 				break;
    489 #endif
    490 #ifdef SYSVSHM
    491 			case KERN_SYSVIPC_SHM_INFO:
    492 				SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
    493 				break;
    494 #endif
    495 			}
    496 			buflen -= dssize;
    497 		}
    498 	}
    499 	*sizep -= buflen;
    500 	error = copyout(bf, start, *sizep);
    501 	/* If copyout succeeded, use return code set earlier. */
    502 	if (error == 0)
    503 		error = ret;
    504 	if (bf)
    505 		kmem_free(bf, sz);
    506 	return error;
    507 }
    508 
    509 SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
    510 {
    511 
    512 	sysctl_createv(clog, 0, NULL, NULL,
    513 		CTLFLAG_PERMANENT,
    514 		CTLTYPE_NODE, "ipc",
    515 		SYSCTL_DESCR("SysV IPC options"),
    516 		NULL, 0, NULL, 0,
    517 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    518 
    519 	sysctl_createv(clog, 0, NULL, NULL,
    520 		CTLFLAG_PERMANENT,
    521 		CTLTYPE_STRUCT, "sysvipc_info",
    522 		SYSCTL_DESCR("System V style IPC information"),
    523 		sysctl_kern_sysvipc, 0, NULL, 0,
    524 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
    525 }
    526