Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.32.16.1
      1 /*	$NetBSD: sysv_ipc.c,v 1.32.16.1 2018/03/09 01:27:50 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.1 2018/03/09 01:27:50 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 
     65 #include <compat/common/sysv_ipc_mod.h>	/* for sysctl routine vector */
     66 
     67 /*
     68  * Values in support of System V compatible shared memory.	XXX
     69  * (originally located in sys/conf/param.c)
     70  */
     71 #ifdef SYSVSHM
     72 #if !defined(SHMMAX) && defined(SHMMAXPGS)
     73 #define	SHMMAX	SHMMAXPGS	/* shminit() performs a `*= PAGE_SIZE' */
     74 #elif !defined(SHMMAX)
     75 #define SHMMAX 0
     76 #endif
     77 #ifndef	SHMMIN
     78 #define	SHMMIN	1
     79 #endif
     80 #ifndef	SHMMNI
     81 #define	SHMMNI	128		/* <64k, see IPCID_TO_IX in ipc.h */
     82 #endif
     83 #ifndef	SHMSEG
     84 #define	SHMSEG	128
     85 #endif
     86 
     87 struct	shminfo shminfo = {
     88 	SHMMAX,
     89 	SHMMIN,
     90 	SHMMNI,
     91 	SHMSEG,
     92 	0
     93 };
     94 #endif
     95 
     96 /*
     97  * Values in support of System V compatible semaphores.
     98  */
     99 #ifdef SYSVSEM
    100 struct	seminfo seminfo = {
    101 	SEMMAP,		/* # of entries in semaphore map */
    102 	SEMMNI,		/* # of semaphore identifiers */
    103 	SEMMNS,		/* # of semaphores in system */
    104 	SEMMNU,		/* # of undo structures in system */
    105 	SEMMSL,		/* max # of semaphores per id */
    106 	SEMOPM,		/* max # of operations per semop call */
    107 	SEMUME,		/* max # of undo entries per process */
    108 	SEMUSZ,		/* size in bytes of undo structure */
    109 	SEMVMX,		/* semaphore maximum value */
    110 	SEMAEM		/* adjust on exit max value */
    111 };
    112 #endif
    113 
    114 /*
    115  * Values in support of System V compatible messages.
    116  */
    117 #ifdef SYSVMSG
    118 struct	msginfo msginfo = {
    119 	MSGMAX,		/* max chars in a message */
    120 	MSGMNI,		/* # of message queue identifiers */
    121 	MSGMNB,		/* max chars in a queue */
    122 	MSGTQL,		/* max messages in system */
    123 	MSGSSZ,		/* size of a message segment */
    124 			/* (must be small power of 2 greater than 4) */
    125 	MSGSEG		/* number of message segments */
    126 };
    127 #endif
    128 
    129 MODULE(MODULE_CLASS_EXEC, sysv_ipc, NULL);
    130 
    131 SYSCTL_SETUP_PROTO(sysctl_ipc_setup);
    132 
    133 static struct sysctllog *sysctl_sysvipc_clog = NULL;
    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 #ifdef _MODULE
    170 		/* Set up the common sysctl tree */
    171 		sysctl_ipc_setup(&sysctl_sysvipc_clog);
    172 #endif
    173 
    174 		/* Link the system calls */
    175 		error = syscall_establish(NULL, sysvipc_syscalls);
    176 		if (error)
    177 			sysvipcfini();
    178 
    179 		/*
    180 		 * Initialize each sub-component, including their
    181 		 * sysctl data
    182 		 */
    183 #ifdef SYSVSHM
    184 		shminit(&sysctl_sysvipc_clog);
    185 #endif
    186 #ifdef SYSVSEM
    187 		seminit(&sysctl_sysvipc_clog);
    188 #endif
    189 #ifdef SYSVMSG
    190 		msginit(&sysctl_sysvipc_clog);
    191 #endif
    192 		break;
    193 	case MODULE_CMD_FINI:
    194 		/*
    195 		 * Make sure no subcomponents are active.  Each one
    196 		 * tells us if it is busy, and if it was _not_ busy,
    197 		 * we assume it has already done its own clean-up.
    198 		 * So we might need to re-init any components that
    199 		 * are successfully fini'd if we find one that is
    200 		 * still busy.
    201 		 */
    202 #ifdef SYSVSHM
    203 		if (shmfini()) {
    204 			return EBUSY;
    205 		}
    206 #endif
    207 #ifdef SYSVSEM
    208 		if (semfini()) {
    209 #ifdef SYSVSHM
    210 			shminit(NULL);
    211 #endif
    212 			return EBUSY;
    213 		}
    214 #endif
    215 #ifdef SYSVMSG
    216 		if (msgfini()) {
    217 #ifdef SYSVSEM
    218 			seminit(NULL);
    219 #endif
    220 #ifdef SYSVSHM
    221 			shminit(NULL);
    222 #endif
    223 			return EBUSY;
    224 		}
    225 #endif
    226 
    227 		/* Unlink the system calls. */
    228 		error = syscall_disestablish(NULL, sysvipc_syscalls);
    229 		if (error)
    230 			return error;
    231 
    232 #ifdef _MODULE
    233 		/* Remove the sysctl sub-trees */
    234 		sysctl_teardown(&sysctl_sysvipc_clog);
    235 #endif
    236 
    237 		/* Remove the kauth listener */
    238 		sysvipcfini();
    239 		break;
    240 	default:
    241 		return ENOTTY;
    242 	}
    243 	return error;
    244 }
    245 
    246 static kauth_listener_t sysvipc_listener = NULL;
    247 
    248 static int
    249 sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    250     void *arg0, void *arg1, void *arg2, void *arg3)
    251 {
    252 	mode_t mask;
    253 	int ismember = 0;
    254 	struct ipc_perm *perm;
    255 	int mode;
    256 	enum kauth_system_req req;
    257 
    258 	req = (enum kauth_system_req)arg0;
    259 
    260 	if (!(action == KAUTH_SYSTEM_SYSVIPC &&
    261 	      req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
    262 		return KAUTH_RESULT_DEFER;
    263 
    264 	perm = arg1;
    265 	mode = (int)(uintptr_t)arg2;
    266 
    267 	if (mode == IPC_M) {
    268 		if (kauth_cred_geteuid(cred) == perm->uid ||
    269 		    kauth_cred_geteuid(cred) == perm->cuid)
    270 			return (KAUTH_RESULT_ALLOW);
    271 		return (KAUTH_RESULT_DEFER); /* EPERM */
    272 	}
    273 
    274 	mask = 0;
    275 
    276 	if (kauth_cred_geteuid(cred) == perm->uid ||
    277 	    kauth_cred_geteuid(cred) == perm->cuid) {
    278 		if (mode & IPC_R)
    279 			mask |= S_IRUSR;
    280 		if (mode & IPC_W)
    281 			mask |= S_IWUSR;
    282 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    283 	}
    284 
    285 	if (kauth_cred_getegid(cred) == perm->gid ||
    286 	    (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
    287 	    kauth_cred_getegid(cred) == perm->cgid ||
    288 	    (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
    289 		if (mode & IPC_R)
    290 			mask |= S_IRGRP;
    291 		if (mode & IPC_W)
    292 			mask |= S_IWGRP;
    293 		return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    294 	}
    295 
    296 	if (mode & IPC_R)
    297 		mask |= S_IROTH;
    298 	if (mode & IPC_W)
    299 		mask |= S_IWOTH;
    300 	return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
    301 }
    302 
    303 /*
    304  * Check for ipc permission
    305  */
    306 
    307 int
    308 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
    309 {
    310 	int error;
    311 
    312 	error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
    313 	    KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
    314 	if (error == 0)
    315 		return (0);
    316 
    317 	/* Adjust EPERM and EACCES errors until there's a better way to do this. */
    318 	if (mode != IPC_M)
    319 		error = EACCES;
    320 
    321 	return error;
    322 }
    323 
    324 void
    325 sysvipcfini(void)
    326 {
    327 
    328 	KASSERT(sysvipc_listener != NULL);
    329 	kauth_unlisten_scope(sysvipc_listener);
    330 	sysvipc_listener = NULL;
    331 }
    332 
    333 void
    334 sysvipcinit(void)
    335 {
    336 
    337 	KASSERT(sysvipc_listener == NULL);
    338 
    339 	sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    340 	    sysvipc_listener_cb, NULL);
    341 }
    342 
    343 /* Vector the old 50 sysctl stuff */
    344 
    345 static int stub_sysvipc50_sysctl(SYSCTLFN_PROTO);
    346 int (*vec_sysvipc50_sysctl)(SYSCTLFN_PROTO) = stub_sysvipc50_sysctl;
    347 
    348 static int
    349 stub_sysvipc50_sysctl(SYSCTLFN_ARGS)
    350 {
    351 	return EPASSTHROUGH;
    352 }
    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 = (*vec_sysvipc50_sysctl)(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 = min(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