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