Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.32.16.10
      1 /*	$NetBSD: sysv_ipc.c,v 1.32.16.10 2019/01/18 08:50:57 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.32.16.10 2019/01/18 08:50:57 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 MODULE_CALL_HOOK_DECL(sysvipc50_sysctl_hook, int, (SYSCTLFN_PROTO));
    353 MODULE_CALL_HOOK(sysvipc50_sysctl_hook, int, (SYSCTLFN_ARGS),
    354     (SYSCTLFN_CALL(rnode)), stub_sysvipc50_sysctl(SYSCTLFN_CALL(rnode)));
    355 
    356 static int
    357 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
    358 {
    359 	void *where = oldp;
    360 	size_t sz, *sizep = oldlenp;
    361 #ifdef SYSVMSG
    362 	struct msg_sysctl_info *msgsi = NULL;
    363 #endif
    364 #ifdef SYSVSEM
    365 	struct sem_sysctl_info *semsi = NULL;
    366 #endif
    367 #ifdef SYSVSHM
    368 	struct shm_sysctl_info *shmsi = NULL;
    369 #endif
    370 	size_t infosize, dssize, tsize, buflen;
    371 	void *bf = NULL;
    372 	char *start;
    373 	int32_t nds;
    374 	int i, error, ret;
    375 
    376 /*
    377  * If present, call the compat sysctl() code.  If it handles the request
    378  * completely (either success or error), return.  Otherwise fallthrough
    379  * to the non-compat sysctl code.
    380  */
    381 
    382 	error = sysvipc50_sysctl_hook_call(SYSCTLFN_CALL(rnode));
    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