Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.22.2.1
      1  1.22.2.1       jym /*	$NetBSD: sysv_ipc.c,v 1.22.2.1 2009/05/13 17:21:57 jym Exp $	*/
      2       1.7       cgd 
      3      1.13   mycroft /*-
      4      1.20        ad  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
      5      1.13   mycroft  * All rights reserved.
      6      1.13   mycroft  *
      7      1.13   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8      1.13   mycroft  * by Charles M. Hannum.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.6   hpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.6   hpeyerl  *    notice, this list of conditions and the following disclaimer in the
     17       1.6   hpeyerl  *    documentation and/or other materials provided with the distribution.
     18       1.1       cgd  *
     19      1.13   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.13   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.13   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.13   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.13   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.13   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.13   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.13   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.13   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.13   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.13   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       cgd  */
     31      1.15     lukem 
     32      1.15     lukem #include <sys/cdefs.h>
     33  1.22.2.1       jym __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.22.2.1 2009/05/13 17:21:57 jym Exp $");
     34      1.18  christos 
     35      1.18  christos #include "opt_sysv.h"
     36      1.22  christos #include "opt_compat_netbsd.h"
     37       1.2   mycroft #include <sys/param.h>
     38       1.2   mycroft #include <sys/kernel.h>
     39       1.2   mycroft #include <sys/proc.h>
     40       1.2   mycroft #include <sys/ipc.h>
     41      1.18  christos #ifdef SYSVMSG
     42      1.18  christos #include <sys/msg.h>
     43      1.18  christos #endif
     44      1.18  christos #ifdef SYSVSEM
     45      1.18  christos #include <sys/sem.h>
     46      1.18  christos #endif
     47      1.18  christos #ifdef SYSVSHM
     48      1.18  christos #include <sys/shm.h>
     49      1.18  christos #endif
     50       1.4   hpeyerl #include <sys/systm.h>
     51  1.22.2.1       jym #include <sys/kmem.h>
     52      1.10   mycroft #include <sys/mount.h>
     53      1.10   mycroft #include <sys/vnode.h>
     54      1.12   mycroft #include <sys/stat.h>
     55      1.18  christos #include <sys/sysctl.h>
     56      1.17      elad #include <sys/kauth.h>
     57       1.1       cgd 
     58      1.22  christos #ifdef COMPAT_50
     59      1.22  christos #include <compat/sys/ipc.h>
     60      1.22  christos #endif
     61      1.22  christos 
     62       1.1       cgd /*
     63       1.4   hpeyerl  * Check for ipc permission
     64       1.1       cgd  */
     65       1.1       cgd 
     66       1.4   hpeyerl int
     67      1.17      elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
     68       1.1       cgd {
     69      1.12   mycroft 	mode_t mask;
     70      1.17      elad 	int ismember = 0;
     71      1.12   mycroft 
     72      1.19      elad 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
     73      1.12   mycroft 		return (0);
     74       1.1       cgd 
     75       1.9   mycroft 	if (mode == IPC_M) {
     76      1.17      elad 		if (kauth_cred_geteuid(cred) == perm->uid ||
     77      1.17      elad 		    kauth_cred_geteuid(cred) == perm->cuid)
     78       1.9   mycroft 			return (0);
     79       1.9   mycroft 		return (EPERM);
     80       1.1       cgd 	}
     81       1.4   hpeyerl 
     82      1.12   mycroft 	mask = 0;
     83      1.12   mycroft 
     84      1.17      elad 	if (kauth_cred_geteuid(cred) == perm->uid ||
     85      1.17      elad 	    kauth_cred_geteuid(cred) == perm->cuid) {
     86      1.12   mycroft 		if (mode & IPC_R)
     87      1.12   mycroft 			mask |= S_IRUSR;
     88      1.12   mycroft 		if (mode & IPC_W)
     89      1.12   mycroft 			mask |= S_IWUSR;
     90      1.12   mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
     91      1.12   mycroft 	}
     92      1.12   mycroft 
     93      1.17      elad 	if (kauth_cred_getegid(cred) == perm->gid ||
     94      1.17      elad 	    (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
     95      1.17      elad 	    kauth_cred_getegid(cred) == perm->cgid ||
     96      1.17      elad 	    (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
     97      1.12   mycroft 		if (mode & IPC_R)
     98      1.12   mycroft 			mask |= S_IRGRP;
     99      1.12   mycroft 		if (mode & IPC_W)
    100      1.12   mycroft 			mask |= S_IWGRP;
    101      1.12   mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
    102      1.12   mycroft 	}
    103      1.12   mycroft 
    104      1.12   mycroft 	if (mode & IPC_R)
    105      1.12   mycroft 		mask |= S_IROTH;
    106      1.12   mycroft 	if (mode & IPC_W)
    107      1.12   mycroft 		mask |= S_IWOTH;
    108      1.12   mycroft 	return ((perm->mode & mask) == mask ? 0 : EACCES);
    109       1.1       cgd }
    110      1.18  christos 
    111      1.18  christos static int
    112      1.18  christos sysctl_kern_sysvipc(SYSCTLFN_ARGS)
    113      1.18  christos {
    114      1.18  christos 	void *where = oldp;
    115  1.22.2.1       jym 	size_t sz, *sizep = oldlenp;
    116      1.18  christos #ifdef SYSVMSG
    117      1.18  christos 	struct msg_sysctl_info *msgsi = NULL;
    118      1.18  christos #endif
    119      1.18  christos #ifdef SYSVSEM
    120      1.18  christos 	struct sem_sysctl_info *semsi = NULL;
    121      1.18  christos #endif
    122      1.18  christos #ifdef SYSVSHM
    123      1.18  christos 	struct shm_sysctl_info *shmsi = NULL;
    124      1.18  christos #endif
    125      1.18  christos 	size_t infosize, dssize, tsize, buflen;
    126      1.18  christos 	void *bf = NULL;
    127      1.18  christos 	char *start;
    128      1.18  christos 	int32_t nds;
    129      1.18  christos 	int i, error, ret;
    130      1.18  christos 
    131      1.22  christos #ifdef COMPAT_50
    132      1.22  christos 	switch ((error = sysctl_kern_sysvipc50(SYSCTLFN_CALL(rnode)))) {
    133      1.22  christos 	case 0:
    134      1.22  christos 		return 0;
    135      1.22  christos 	case EPASSTHROUGH:
    136      1.22  christos 		break;
    137      1.22  christos 	default:
    138      1.22  christos 		return error;
    139      1.22  christos 	}
    140      1.22  christos #endif
    141      1.18  christos 	if (namelen != 1)
    142      1.18  christos 		return EINVAL;
    143      1.18  christos 
    144      1.18  christos 	start = where;
    145      1.18  christos 	buflen = *sizep;
    146      1.18  christos 
    147      1.18  christos 	switch (*name) {
    148      1.18  christos 	case KERN_SYSVIPC_MSG_INFO:
    149      1.18  christos #ifdef SYSVMSG
    150      1.18  christos 		infosize = sizeof(msgsi->msginfo);
    151      1.18  christos 		nds = msginfo.msgmni;
    152      1.18  christos 		dssize = sizeof(msgsi->msgids[0]);
    153      1.18  christos 		break;
    154      1.18  christos #else
    155      1.18  christos 		return EINVAL;
    156      1.18  christos #endif
    157      1.18  christos 	case KERN_SYSVIPC_SEM_INFO:
    158      1.18  christos #ifdef SYSVSEM
    159      1.18  christos 		infosize = sizeof(semsi->seminfo);
    160      1.18  christos 		nds = seminfo.semmni;
    161      1.18  christos 		dssize = sizeof(semsi->semids[0]);
    162      1.18  christos 		break;
    163      1.18  christos #else
    164      1.18  christos 		return EINVAL;
    165      1.18  christos #endif
    166      1.18  christos 	case KERN_SYSVIPC_SHM_INFO:
    167      1.18  christos #ifdef SYSVSHM
    168      1.18  christos 		infosize = sizeof(shmsi->shminfo);
    169      1.18  christos 		nds = shminfo.shmmni;
    170      1.18  christos 		dssize = sizeof(shmsi->shmids[0]);
    171      1.18  christos 		break;
    172      1.18  christos #else
    173      1.18  christos 		return EINVAL;
    174      1.18  christos #endif
    175      1.18  christos 	default:
    176      1.18  christos 		return EINVAL;
    177      1.18  christos 	}
    178      1.18  christos 	/*
    179      1.18  christos 	 * Round infosize to 64 bit boundary if requesting more than just
    180      1.18  christos 	 * the info structure or getting the total data size.
    181      1.18  christos 	 */
    182      1.18  christos 	if (where == NULL || *sizep > infosize)
    183      1.18  christos 		infosize = roundup(infosize, sizeof(quad_t));
    184      1.18  christos 	tsize = infosize + nds * dssize;
    185      1.18  christos 
    186      1.18  christos 	/* Return just the total size required. */
    187      1.18  christos 	if (where == NULL) {
    188      1.18  christos 		*sizep = tsize;
    189      1.18  christos 		return 0;
    190      1.18  christos 	}
    191      1.18  christos 
    192      1.18  christos 	/* Not enough room for even the info struct. */
    193      1.18  christos 	if (buflen < infosize) {
    194      1.18  christos 		*sizep = 0;
    195      1.18  christos 		return ENOMEM;
    196      1.18  christos 	}
    197  1.22.2.1       jym 	sz = min(tsize, buflen);
    198  1.22.2.1       jym 	bf = kmem_zalloc(sz, KM_SLEEP);
    199      1.18  christos 
    200      1.18  christos 	switch (*name) {
    201      1.18  christos #ifdef SYSVMSG
    202      1.18  christos 	case KERN_SYSVIPC_MSG_INFO:
    203      1.18  christos 		msgsi = (struct msg_sysctl_info *)bf;
    204      1.18  christos 		msgsi->msginfo = msginfo;
    205      1.18  christos 		break;
    206      1.18  christos #endif
    207      1.18  christos #ifdef SYSVSEM
    208      1.18  christos 	case KERN_SYSVIPC_SEM_INFO:
    209      1.18  christos 		semsi = (struct sem_sysctl_info *)bf;
    210      1.18  christos 		semsi->seminfo = seminfo;
    211      1.18  christos 		break;
    212      1.18  christos #endif
    213      1.18  christos #ifdef SYSVSHM
    214      1.18  christos 	case KERN_SYSVIPC_SHM_INFO:
    215      1.18  christos 		shmsi = (struct shm_sysctl_info *)bf;
    216      1.18  christos 		shmsi->shminfo = shminfo;
    217      1.18  christos 		break;
    218      1.18  christos #endif
    219      1.18  christos 	}
    220      1.18  christos 	buflen -= infosize;
    221      1.18  christos 
    222      1.18  christos 	ret = 0;
    223      1.18  christos 	if (buflen > 0) {
    224      1.18  christos 		/* Fill in the IPC data structures.  */
    225      1.18  christos 		for (i = 0; i < nds; i++) {
    226      1.18  christos 			if (buflen < dssize) {
    227      1.18  christos 				ret = ENOMEM;
    228      1.18  christos 				break;
    229      1.18  christos 			}
    230      1.18  christos 			switch (*name) {
    231      1.18  christos #ifdef SYSVMSG
    232      1.18  christos 			case KERN_SYSVIPC_MSG_INFO:
    233      1.20        ad 				mutex_enter(&msgmutex);
    234      1.22  christos 				SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
    235      1.20        ad 				mutex_exit(&msgmutex);
    236      1.18  christos 				break;
    237      1.18  christos #endif
    238      1.18  christos #ifdef SYSVSEM
    239      1.18  christos 			case KERN_SYSVIPC_SEM_INFO:
    240      1.22  christos 				SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
    241      1.18  christos 				break;
    242      1.18  christos #endif
    243      1.18  christos #ifdef SYSVSHM
    244      1.18  christos 			case KERN_SYSVIPC_SHM_INFO:
    245      1.22  christos 				SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
    246      1.18  christos 				break;
    247      1.18  christos #endif
    248      1.18  christos 			}
    249      1.18  christos 			buflen -= dssize;
    250      1.18  christos 		}
    251      1.18  christos 	}
    252      1.18  christos 	*sizep -= buflen;
    253      1.18  christos 	error = copyout(bf, start, *sizep);
    254      1.18  christos 	/* If copyout succeeded, use return code set earlier. */
    255      1.18  christos 	if (error == 0)
    256      1.18  christos 		error = ret;
    257      1.18  christos 	if (bf)
    258  1.22.2.1       jym 		kmem_free(bf, sz);
    259      1.18  christos 	return error;
    260      1.18  christos }
    261      1.18  christos 
    262      1.18  christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
    263      1.18  christos {
    264      1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    265      1.18  christos 		CTLFLAG_PERMANENT,
    266      1.18  christos 		CTLTYPE_NODE, "kern", NULL,
    267      1.18  christos 		NULL, 0, NULL, 0,
    268      1.18  christos 		CTL_KERN, CTL_EOL);
    269      1.18  christos 
    270      1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    271      1.18  christos 		CTLFLAG_PERMANENT,
    272      1.18  christos 		CTLTYPE_NODE, "ipc",
    273      1.18  christos 		SYSCTL_DESCR("SysV IPC options"),
    274      1.18  christos 		NULL, 0, NULL, 0,
    275      1.18  christos 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    276      1.18  christos 
    277      1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    278      1.18  christos 		CTLFLAG_PERMANENT,
    279      1.18  christos 		CTLTYPE_STRUCT, "sysvipc_info",
    280      1.18  christos 		SYSCTL_DESCR("System V style IPC information"),
    281      1.18  christos 		sysctl_kern_sysvipc, 0, NULL, 0,
    282      1.18  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
    283      1.18  christos }
    284