Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.15.32.3
      1  1.15.32.3      yamt /*	$NetBSD: sysv_ipc.c,v 1.15.32.3 2007/02/26 09:11:18 yamt Exp $	*/
      2        1.7       cgd 
      3       1.13   mycroft /*-
      4  1.15.32.3      yamt  * 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.6   hpeyerl  * 3. All advertising materials mentioning features or use of this software
     19        1.6   hpeyerl  *    must display the following acknowledgement:
     20       1.14  christos  *	This product includes software developed by the NetBSD
     21       1.14  christos  *	Foundation, Inc. and its contributors.
     22       1.13   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.13   mycroft  *    contributors may be used to endorse or promote products derived
     24       1.13   mycroft  *    from this software without specific prior written permission.
     25        1.1       cgd  *
     26       1.13   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.13   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.13   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.13   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.13   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.13   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.13   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.13   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.13   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.13   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.13   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1       cgd  */
     38       1.15     lukem 
     39       1.15     lukem #include <sys/cdefs.h>
     40  1.15.32.3      yamt __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.15.32.3 2007/02/26 09:11:18 yamt Exp $");
     41  1.15.32.2      yamt 
     42  1.15.32.2      yamt #include "opt_sysv.h"
     43        1.1       cgd 
     44        1.2   mycroft #include <sys/param.h>
     45        1.2   mycroft #include <sys/kernel.h>
     46        1.2   mycroft #include <sys/proc.h>
     47        1.2   mycroft #include <sys/ipc.h>
     48  1.15.32.2      yamt #ifdef SYSVMSG
     49  1.15.32.2      yamt #include <sys/msg.h>
     50  1.15.32.2      yamt #endif
     51  1.15.32.2      yamt #ifdef SYSVSEM
     52  1.15.32.2      yamt #include <sys/sem.h>
     53  1.15.32.2      yamt #endif
     54  1.15.32.2      yamt #ifdef SYSVSHM
     55  1.15.32.2      yamt #include <sys/shm.h>
     56  1.15.32.2      yamt #endif
     57        1.4   hpeyerl #include <sys/systm.h>
     58  1.15.32.2      yamt #include <sys/malloc.h>
     59       1.10   mycroft #include <sys/mount.h>
     60       1.10   mycroft #include <sys/vnode.h>
     61       1.12   mycroft #include <sys/stat.h>
     62  1.15.32.2      yamt #include <sys/sysctl.h>
     63  1.15.32.1      yamt #include <sys/kauth.h>
     64        1.1       cgd 
     65        1.1       cgd /*
     66        1.4   hpeyerl  * Check for ipc permission
     67        1.1       cgd  */
     68        1.1       cgd 
     69        1.4   hpeyerl int
     70  1.15.32.1      yamt ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
     71        1.1       cgd {
     72       1.12   mycroft 	mode_t mask;
     73  1.15.32.1      yamt 	int ismember = 0;
     74       1.12   mycroft 
     75  1.15.32.3      yamt 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
     76       1.12   mycroft 		return (0);
     77        1.1       cgd 
     78        1.9   mycroft 	if (mode == IPC_M) {
     79  1.15.32.1      yamt 		if (kauth_cred_geteuid(cred) == perm->uid ||
     80  1.15.32.1      yamt 		    kauth_cred_geteuid(cred) == perm->cuid)
     81        1.9   mycroft 			return (0);
     82        1.9   mycroft 		return (EPERM);
     83        1.1       cgd 	}
     84        1.4   hpeyerl 
     85       1.12   mycroft 	mask = 0;
     86       1.12   mycroft 
     87  1.15.32.1      yamt 	if (kauth_cred_geteuid(cred) == perm->uid ||
     88  1.15.32.1      yamt 	    kauth_cred_geteuid(cred) == perm->cuid) {
     89       1.12   mycroft 		if (mode & IPC_R)
     90       1.12   mycroft 			mask |= S_IRUSR;
     91       1.12   mycroft 		if (mode & IPC_W)
     92       1.12   mycroft 			mask |= S_IWUSR;
     93       1.12   mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
     94       1.12   mycroft 	}
     95       1.12   mycroft 
     96  1.15.32.1      yamt 	if (kauth_cred_getegid(cred) == perm->gid ||
     97  1.15.32.1      yamt 	    (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
     98  1.15.32.1      yamt 	    kauth_cred_getegid(cred) == perm->cgid ||
     99  1.15.32.1      yamt 	    (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
    100       1.12   mycroft 		if (mode & IPC_R)
    101       1.12   mycroft 			mask |= S_IRGRP;
    102       1.12   mycroft 		if (mode & IPC_W)
    103       1.12   mycroft 			mask |= S_IWGRP;
    104       1.12   mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
    105       1.12   mycroft 	}
    106       1.12   mycroft 
    107       1.12   mycroft 	if (mode & IPC_R)
    108       1.12   mycroft 		mask |= S_IROTH;
    109       1.12   mycroft 	if (mode & IPC_W)
    110       1.12   mycroft 		mask |= S_IWOTH;
    111       1.12   mycroft 	return ((perm->mode & mask) == mask ? 0 : EACCES);
    112        1.1       cgd }
    113  1.15.32.2      yamt 
    114  1.15.32.2      yamt /*
    115  1.15.32.2      yamt  * sysctl helper routine for kern.ipc.sysvipc_info subtree.
    116  1.15.32.2      yamt  */
    117  1.15.32.2      yamt 
    118  1.15.32.2      yamt #define FILL_PERM(src, dst) do { \
    119  1.15.32.2      yamt 	(dst)._key = (src)._key; \
    120  1.15.32.2      yamt 	(dst).uid = (src).uid; \
    121  1.15.32.2      yamt 	(dst).gid = (src).gid; \
    122  1.15.32.2      yamt 	(dst).cuid = (src).cuid; \
    123  1.15.32.2      yamt 	(dst).cgid = (src).cgid; \
    124  1.15.32.2      yamt 	(dst).mode = (src).mode; \
    125  1.15.32.2      yamt 	(dst)._seq = (src)._seq; \
    126  1.15.32.2      yamt } while (/*CONSTCOND*/ 0);
    127  1.15.32.2      yamt 
    128  1.15.32.2      yamt #define FILL_MSG(src, dst) do { \
    129  1.15.32.2      yamt 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
    130  1.15.32.2      yamt 	(dst).msg_qnum = (src).msg_qnum; \
    131  1.15.32.2      yamt 	(dst).msg_qbytes = (src).msg_qbytes; \
    132  1.15.32.2      yamt 	(dst)._msg_cbytes = (src)._msg_cbytes; \
    133  1.15.32.2      yamt 	(dst).msg_lspid = (src).msg_lspid; \
    134  1.15.32.2      yamt 	(dst).msg_lrpid = (src).msg_lrpid; \
    135  1.15.32.2      yamt 	(dst).msg_stime = (src).msg_stime; \
    136  1.15.32.2      yamt 	(dst).msg_rtime = (src).msg_rtime; \
    137  1.15.32.2      yamt 	(dst).msg_ctime = (src).msg_ctime; \
    138  1.15.32.2      yamt } while (/*CONSTCOND*/ 0)
    139  1.15.32.2      yamt 
    140  1.15.32.2      yamt #define FILL_SEM(src, dst) do { \
    141  1.15.32.2      yamt 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
    142  1.15.32.2      yamt 	(dst).sem_nsems = (src).sem_nsems; \
    143  1.15.32.2      yamt 	(dst).sem_otime = (src).sem_otime; \
    144  1.15.32.2      yamt 	(dst).sem_ctime = (src).sem_ctime; \
    145  1.15.32.2      yamt } while (/*CONSTCOND*/ 0)
    146  1.15.32.2      yamt 
    147  1.15.32.2      yamt #define FILL_SHM(src, dst) do { \
    148  1.15.32.2      yamt 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
    149  1.15.32.2      yamt 	(dst).shm_segsz = (src).shm_segsz; \
    150  1.15.32.2      yamt 	(dst).shm_lpid = (src).shm_lpid; \
    151  1.15.32.2      yamt 	(dst).shm_cpid = (src).shm_cpid; \
    152  1.15.32.2      yamt 	(dst).shm_atime = (src).shm_atime; \
    153  1.15.32.2      yamt 	(dst).shm_dtime = (src).shm_dtime; \
    154  1.15.32.2      yamt 	(dst).shm_ctime = (src).shm_ctime; \
    155  1.15.32.2      yamt 	(dst).shm_nattch = (src).shm_nattch; \
    156  1.15.32.2      yamt } while (/*CONSTCOND*/ 0)
    157  1.15.32.2      yamt 
    158  1.15.32.2      yamt static int
    159  1.15.32.2      yamt sysctl_kern_sysvipc(SYSCTLFN_ARGS)
    160  1.15.32.2      yamt {
    161  1.15.32.2      yamt 	void *where = oldp;
    162  1.15.32.2      yamt 	size_t *sizep = oldlenp;
    163  1.15.32.2      yamt #ifdef SYSVMSG
    164  1.15.32.2      yamt 	struct msg_sysctl_info *msgsi = NULL;
    165  1.15.32.2      yamt #endif
    166  1.15.32.2      yamt #ifdef SYSVSEM
    167  1.15.32.2      yamt 	struct sem_sysctl_info *semsi = NULL;
    168  1.15.32.2      yamt #endif
    169  1.15.32.2      yamt #ifdef SYSVSHM
    170  1.15.32.2      yamt 	struct shm_sysctl_info *shmsi = NULL;
    171  1.15.32.2      yamt #endif
    172  1.15.32.2      yamt 	size_t infosize, dssize, tsize, buflen;
    173  1.15.32.2      yamt 	void *bf = NULL;
    174  1.15.32.2      yamt 	char *start;
    175  1.15.32.2      yamt 	int32_t nds;
    176  1.15.32.2      yamt 	int i, error, ret;
    177  1.15.32.2      yamt 
    178  1.15.32.2      yamt 	if (namelen != 1)
    179  1.15.32.2      yamt 		return EINVAL;
    180  1.15.32.2      yamt 
    181  1.15.32.2      yamt 	start = where;
    182  1.15.32.2      yamt 	buflen = *sizep;
    183  1.15.32.2      yamt 
    184  1.15.32.2      yamt 	switch (*name) {
    185  1.15.32.2      yamt 	case KERN_SYSVIPC_MSG_INFO:
    186  1.15.32.2      yamt #ifdef SYSVMSG
    187  1.15.32.2      yamt 		infosize = sizeof(msgsi->msginfo);
    188  1.15.32.2      yamt 		nds = msginfo.msgmni;
    189  1.15.32.2      yamt 		dssize = sizeof(msgsi->msgids[0]);
    190  1.15.32.2      yamt 		break;
    191  1.15.32.2      yamt #else
    192  1.15.32.2      yamt 		return EINVAL;
    193  1.15.32.2      yamt #endif
    194  1.15.32.2      yamt 	case KERN_SYSVIPC_SEM_INFO:
    195  1.15.32.2      yamt #ifdef SYSVSEM
    196  1.15.32.2      yamt 		infosize = sizeof(semsi->seminfo);
    197  1.15.32.2      yamt 		nds = seminfo.semmni;
    198  1.15.32.2      yamt 		dssize = sizeof(semsi->semids[0]);
    199  1.15.32.2      yamt 		break;
    200  1.15.32.2      yamt #else
    201  1.15.32.2      yamt 		return EINVAL;
    202  1.15.32.2      yamt #endif
    203  1.15.32.2      yamt 	case KERN_SYSVIPC_SHM_INFO:
    204  1.15.32.2      yamt #ifdef SYSVSHM
    205  1.15.32.2      yamt 		infosize = sizeof(shmsi->shminfo);
    206  1.15.32.2      yamt 		nds = shminfo.shmmni;
    207  1.15.32.2      yamt 		dssize = sizeof(shmsi->shmids[0]);
    208  1.15.32.2      yamt 		break;
    209  1.15.32.2      yamt #else
    210  1.15.32.2      yamt 		return EINVAL;
    211  1.15.32.2      yamt #endif
    212  1.15.32.2      yamt 	default:
    213  1.15.32.2      yamt 		return EINVAL;
    214  1.15.32.2      yamt 	}
    215  1.15.32.2      yamt 	/*
    216  1.15.32.2      yamt 	 * Round infosize to 64 bit boundary if requesting more than just
    217  1.15.32.2      yamt 	 * the info structure or getting the total data size.
    218  1.15.32.2      yamt 	 */
    219  1.15.32.2      yamt 	if (where == NULL || *sizep > infosize)
    220  1.15.32.2      yamt 		infosize = roundup(infosize, sizeof(quad_t));
    221  1.15.32.2      yamt 	tsize = infosize + nds * dssize;
    222  1.15.32.2      yamt 
    223  1.15.32.2      yamt 	/* Return just the total size required. */
    224  1.15.32.2      yamt 	if (where == NULL) {
    225  1.15.32.2      yamt 		*sizep = tsize;
    226  1.15.32.2      yamt 		return 0;
    227  1.15.32.2      yamt 	}
    228  1.15.32.2      yamt 
    229  1.15.32.2      yamt 	/* Not enough room for even the info struct. */
    230  1.15.32.2      yamt 	if (buflen < infosize) {
    231  1.15.32.2      yamt 		*sizep = 0;
    232  1.15.32.2      yamt 		return ENOMEM;
    233  1.15.32.2      yamt 	}
    234  1.15.32.2      yamt 	bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO);
    235  1.15.32.2      yamt 
    236  1.15.32.2      yamt 	switch (*name) {
    237  1.15.32.2      yamt #ifdef SYSVMSG
    238  1.15.32.2      yamt 	case KERN_SYSVIPC_MSG_INFO:
    239  1.15.32.2      yamt 		msgsi = (struct msg_sysctl_info *)bf;
    240  1.15.32.2      yamt 		msgsi->msginfo = msginfo;
    241  1.15.32.2      yamt 		break;
    242  1.15.32.2      yamt #endif
    243  1.15.32.2      yamt #ifdef SYSVSEM
    244  1.15.32.2      yamt 	case KERN_SYSVIPC_SEM_INFO:
    245  1.15.32.2      yamt 		semsi = (struct sem_sysctl_info *)bf;
    246  1.15.32.2      yamt 		semsi->seminfo = seminfo;
    247  1.15.32.2      yamt 		break;
    248  1.15.32.2      yamt #endif
    249  1.15.32.2      yamt #ifdef SYSVSHM
    250  1.15.32.2      yamt 	case KERN_SYSVIPC_SHM_INFO:
    251  1.15.32.2      yamt 		shmsi = (struct shm_sysctl_info *)bf;
    252  1.15.32.2      yamt 		shmsi->shminfo = shminfo;
    253  1.15.32.2      yamt 		break;
    254  1.15.32.2      yamt #endif
    255  1.15.32.2      yamt 	}
    256  1.15.32.2      yamt 	buflen -= infosize;
    257  1.15.32.2      yamt 
    258  1.15.32.2      yamt 	ret = 0;
    259  1.15.32.2      yamt 	if (buflen > 0) {
    260  1.15.32.2      yamt 		/* Fill in the IPC data structures.  */
    261  1.15.32.2      yamt 		for (i = 0; i < nds; i++) {
    262  1.15.32.2      yamt 			if (buflen < dssize) {
    263  1.15.32.2      yamt 				ret = ENOMEM;
    264  1.15.32.2      yamt 				break;
    265  1.15.32.2      yamt 			}
    266  1.15.32.2      yamt 			switch (*name) {
    267  1.15.32.2      yamt #ifdef SYSVMSG
    268  1.15.32.2      yamt 			case KERN_SYSVIPC_MSG_INFO:
    269  1.15.32.3      yamt 				mutex_enter(&msgmutex);
    270  1.15.32.3      yamt 				FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
    271  1.15.32.3      yamt 				mutex_exit(&msgmutex);
    272  1.15.32.2      yamt 				break;
    273  1.15.32.2      yamt #endif
    274  1.15.32.2      yamt #ifdef SYSVSEM
    275  1.15.32.2      yamt 			case KERN_SYSVIPC_SEM_INFO:
    276  1.15.32.2      yamt 				FILL_SEM(sema[i], semsi->semids[i]);
    277  1.15.32.2      yamt 				break;
    278  1.15.32.2      yamt #endif
    279  1.15.32.2      yamt #ifdef SYSVSHM
    280  1.15.32.2      yamt 			case KERN_SYSVIPC_SHM_INFO:
    281  1.15.32.2      yamt 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
    282  1.15.32.2      yamt 				break;
    283  1.15.32.2      yamt #endif
    284  1.15.32.2      yamt 			}
    285  1.15.32.2      yamt 			buflen -= dssize;
    286  1.15.32.2      yamt 		}
    287  1.15.32.2      yamt 	}
    288  1.15.32.2      yamt 	*sizep -= buflen;
    289  1.15.32.2      yamt 	error = copyout(bf, start, *sizep);
    290  1.15.32.2      yamt 	/* If copyout succeeded, use return code set earlier. */
    291  1.15.32.2      yamt 	if (error == 0)
    292  1.15.32.2      yamt 		error = ret;
    293  1.15.32.2      yamt 	if (bf)
    294  1.15.32.2      yamt 		free(bf, M_TEMP);
    295  1.15.32.2      yamt 	return error;
    296  1.15.32.2      yamt }
    297  1.15.32.2      yamt 
    298  1.15.32.2      yamt #undef FILL_PERM
    299  1.15.32.2      yamt #undef FILL_MSG
    300  1.15.32.2      yamt #undef FILL_SEM
    301  1.15.32.2      yamt #undef FILL_SHM
    302  1.15.32.2      yamt 
    303  1.15.32.2      yamt SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
    304  1.15.32.2      yamt {
    305  1.15.32.2      yamt 	sysctl_createv(clog, 0, NULL, NULL,
    306  1.15.32.2      yamt 		CTLFLAG_PERMANENT,
    307  1.15.32.2      yamt 		CTLTYPE_NODE, "kern", NULL,
    308  1.15.32.2      yamt 		NULL, 0, NULL, 0,
    309  1.15.32.2      yamt 		CTL_KERN, CTL_EOL);
    310  1.15.32.2      yamt 
    311  1.15.32.2      yamt 	sysctl_createv(clog, 0, NULL, NULL,
    312  1.15.32.2      yamt 		CTLFLAG_PERMANENT,
    313  1.15.32.2      yamt 		CTLTYPE_NODE, "ipc",
    314  1.15.32.2      yamt 		SYSCTL_DESCR("SysV IPC options"),
    315  1.15.32.2      yamt 		NULL, 0, NULL, 0,
    316  1.15.32.2      yamt 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    317  1.15.32.2      yamt 
    318  1.15.32.2      yamt 	sysctl_createv(clog, 0, NULL, NULL,
    319  1.15.32.2      yamt 		CTLFLAG_PERMANENT,
    320  1.15.32.2      yamt 		CTLTYPE_STRUCT, "sysvipc_info",
    321  1.15.32.2      yamt 		SYSCTL_DESCR("System V style IPC information"),
    322  1.15.32.2      yamt 		sysctl_kern_sysvipc, 0, NULL, 0,
    323  1.15.32.2      yamt 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
    324  1.15.32.2      yamt }
    325