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