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