Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.18
      1  1.18  christos /*	$NetBSD: sysv_ipc.c,v 1.18 2006/11/25 21:40:05 christos Exp $	*/
      2   1.7       cgd 
      3  1.13   mycroft /*-
      4  1.13   mycroft  * Copyright (c) 1998 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.18  christos __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.18 2006/11/25 21:40:05 christos Exp $");
     41  1.18  christos 
     42  1.18  christos #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.18  christos #ifdef SYSVMSG
     49  1.18  christos #include <sys/msg.h>
     50  1.18  christos #endif
     51  1.18  christos #ifdef SYSVSEM
     52  1.18  christos #include <sys/sem.h>
     53  1.18  christos #endif
     54  1.18  christos #ifdef SYSVSHM
     55  1.18  christos #include <sys/shm.h>
     56  1.18  christos #endif
     57   1.4   hpeyerl #include <sys/systm.h>
     58  1.18  christos #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.18  christos #include <sys/sysctl.h>
     63  1.17      elad #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.17      elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
     71   1.1       cgd {
     72  1.12   mycroft 	mode_t mask;
     73  1.17      elad 	int ismember = 0;
     74  1.12   mycroft 
     75  1.17      elad 	if (kauth_cred_geteuid(cred) == 0)
     76  1.12   mycroft 		return (0);
     77   1.1       cgd 
     78   1.9   mycroft 	if (mode == IPC_M) {
     79  1.17      elad 		if (kauth_cred_geteuid(cred) == perm->uid ||
     80  1.17      elad 		    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.17      elad 	if (kauth_cred_geteuid(cred) == perm->uid ||
     88  1.17      elad 	    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.17      elad 	if (kauth_cred_getegid(cred) == perm->gid ||
     97  1.17      elad 	    (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
     98  1.17      elad 	    kauth_cred_getegid(cred) == perm->cgid ||
     99  1.17      elad 	    (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.18  christos 
    114  1.18  christos /*
    115  1.18  christos  * sysctl helper routine for kern.ipc.sysvipc_info subtree.
    116  1.18  christos  */
    117  1.18  christos 
    118  1.18  christos #define FILL_PERM(src, dst) do { \
    119  1.18  christos 	(dst)._key = (src)._key; \
    120  1.18  christos 	(dst).uid = (src).uid; \
    121  1.18  christos 	(dst).gid = (src).gid; \
    122  1.18  christos 	(dst).cuid = (src).cuid; \
    123  1.18  christos 	(dst).cgid = (src).cgid; \
    124  1.18  christos 	(dst).mode = (src).mode; \
    125  1.18  christos 	(dst)._seq = (src)._seq; \
    126  1.18  christos } while (/*CONSTCOND*/ 0);
    127  1.18  christos 
    128  1.18  christos #define FILL_MSG(src, dst) do { \
    129  1.18  christos 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
    130  1.18  christos 	(dst).msg_qnum = (src).msg_qnum; \
    131  1.18  christos 	(dst).msg_qbytes = (src).msg_qbytes; \
    132  1.18  christos 	(dst)._msg_cbytes = (src)._msg_cbytes; \
    133  1.18  christos 	(dst).msg_lspid = (src).msg_lspid; \
    134  1.18  christos 	(dst).msg_lrpid = (src).msg_lrpid; \
    135  1.18  christos 	(dst).msg_stime = (src).msg_stime; \
    136  1.18  christos 	(dst).msg_rtime = (src).msg_rtime; \
    137  1.18  christos 	(dst).msg_ctime = (src).msg_ctime; \
    138  1.18  christos } while (/*CONSTCOND*/ 0)
    139  1.18  christos 
    140  1.18  christos #define FILL_SEM(src, dst) do { \
    141  1.18  christos 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
    142  1.18  christos 	(dst).sem_nsems = (src).sem_nsems; \
    143  1.18  christos 	(dst).sem_otime = (src).sem_otime; \
    144  1.18  christos 	(dst).sem_ctime = (src).sem_ctime; \
    145  1.18  christos } while (/*CONSTCOND*/ 0)
    146  1.18  christos 
    147  1.18  christos #define FILL_SHM(src, dst) do { \
    148  1.18  christos 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
    149  1.18  christos 	(dst).shm_segsz = (src).shm_segsz; \
    150  1.18  christos 	(dst).shm_lpid = (src).shm_lpid; \
    151  1.18  christos 	(dst).shm_cpid = (src).shm_cpid; \
    152  1.18  christos 	(dst).shm_atime = (src).shm_atime; \
    153  1.18  christos 	(dst).shm_dtime = (src).shm_dtime; \
    154  1.18  christos 	(dst).shm_ctime = (src).shm_ctime; \
    155  1.18  christos 	(dst).shm_nattch = (src).shm_nattch; \
    156  1.18  christos } while (/*CONSTCOND*/ 0)
    157  1.18  christos 
    158  1.18  christos static int
    159  1.18  christos sysctl_kern_sysvipc(SYSCTLFN_ARGS)
    160  1.18  christos {
    161  1.18  christos 	void *where = oldp;
    162  1.18  christos 	size_t *sizep = oldlenp;
    163  1.18  christos #ifdef SYSVMSG
    164  1.18  christos 	struct msg_sysctl_info *msgsi = NULL;
    165  1.18  christos #endif
    166  1.18  christos #ifdef SYSVSEM
    167  1.18  christos 	struct sem_sysctl_info *semsi = NULL;
    168  1.18  christos #endif
    169  1.18  christos #ifdef SYSVSHM
    170  1.18  christos 	struct shm_sysctl_info *shmsi = NULL;
    171  1.18  christos #endif
    172  1.18  christos 	size_t infosize, dssize, tsize, buflen;
    173  1.18  christos 	void *bf = NULL;
    174  1.18  christos 	char *start;
    175  1.18  christos 	int32_t nds;
    176  1.18  christos 	int i, error, ret;
    177  1.18  christos 
    178  1.18  christos 	if (namelen != 1)
    179  1.18  christos 		return EINVAL;
    180  1.18  christos 
    181  1.18  christos 	start = where;
    182  1.18  christos 	buflen = *sizep;
    183  1.18  christos 
    184  1.18  christos 	switch (*name) {
    185  1.18  christos 	case KERN_SYSVIPC_MSG_INFO:
    186  1.18  christos #ifdef SYSVMSG
    187  1.18  christos 		infosize = sizeof(msgsi->msginfo);
    188  1.18  christos 		nds = msginfo.msgmni;
    189  1.18  christos 		dssize = sizeof(msgsi->msgids[0]);
    190  1.18  christos 		break;
    191  1.18  christos #else
    192  1.18  christos 		return EINVAL;
    193  1.18  christos #endif
    194  1.18  christos 	case KERN_SYSVIPC_SEM_INFO:
    195  1.18  christos #ifdef SYSVSEM
    196  1.18  christos 		infosize = sizeof(semsi->seminfo);
    197  1.18  christos 		nds = seminfo.semmni;
    198  1.18  christos 		dssize = sizeof(semsi->semids[0]);
    199  1.18  christos 		break;
    200  1.18  christos #else
    201  1.18  christos 		return EINVAL;
    202  1.18  christos #endif
    203  1.18  christos 	case KERN_SYSVIPC_SHM_INFO:
    204  1.18  christos #ifdef SYSVSHM
    205  1.18  christos 		infosize = sizeof(shmsi->shminfo);
    206  1.18  christos 		nds = shminfo.shmmni;
    207  1.18  christos 		dssize = sizeof(shmsi->shmids[0]);
    208  1.18  christos 		break;
    209  1.18  christos #else
    210  1.18  christos 		return EINVAL;
    211  1.18  christos #endif
    212  1.18  christos 	default:
    213  1.18  christos 		return EINVAL;
    214  1.18  christos 	}
    215  1.18  christos 	/*
    216  1.18  christos 	 * Round infosize to 64 bit boundary if requesting more than just
    217  1.18  christos 	 * the info structure or getting the total data size.
    218  1.18  christos 	 */
    219  1.18  christos 	if (where == NULL || *sizep > infosize)
    220  1.18  christos 		infosize = roundup(infosize, sizeof(quad_t));
    221  1.18  christos 	tsize = infosize + nds * dssize;
    222  1.18  christos 
    223  1.18  christos 	/* Return just the total size required. */
    224  1.18  christos 	if (where == NULL) {
    225  1.18  christos 		*sizep = tsize;
    226  1.18  christos 		return 0;
    227  1.18  christos 	}
    228  1.18  christos 
    229  1.18  christos 	/* Not enough room for even the info struct. */
    230  1.18  christos 	if (buflen < infosize) {
    231  1.18  christos 		*sizep = 0;
    232  1.18  christos 		return ENOMEM;
    233  1.18  christos 	}
    234  1.18  christos 	bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO);
    235  1.18  christos 
    236  1.18  christos 	switch (*name) {
    237  1.18  christos #ifdef SYSVMSG
    238  1.18  christos 	case KERN_SYSVIPC_MSG_INFO:
    239  1.18  christos 		msgsi = (struct msg_sysctl_info *)bf;
    240  1.18  christos 		msgsi->msginfo = msginfo;
    241  1.18  christos 		break;
    242  1.18  christos #endif
    243  1.18  christos #ifdef SYSVSEM
    244  1.18  christos 	case KERN_SYSVIPC_SEM_INFO:
    245  1.18  christos 		semsi = (struct sem_sysctl_info *)bf;
    246  1.18  christos 		semsi->seminfo = seminfo;
    247  1.18  christos 		break;
    248  1.18  christos #endif
    249  1.18  christos #ifdef SYSVSHM
    250  1.18  christos 	case KERN_SYSVIPC_SHM_INFO:
    251  1.18  christos 		shmsi = (struct shm_sysctl_info *)bf;
    252  1.18  christos 		shmsi->shminfo = shminfo;
    253  1.18  christos 		break;
    254  1.18  christos #endif
    255  1.18  christos 	}
    256  1.18  christos 	buflen -= infosize;
    257  1.18  christos 
    258  1.18  christos 	ret = 0;
    259  1.18  christos 	if (buflen > 0) {
    260  1.18  christos 		/* Fill in the IPC data structures.  */
    261  1.18  christos 		for (i = 0; i < nds; i++) {
    262  1.18  christos 			if (buflen < dssize) {
    263  1.18  christos 				ret = ENOMEM;
    264  1.18  christos 				break;
    265  1.18  christos 			}
    266  1.18  christos 			switch (*name) {
    267  1.18  christos #ifdef SYSVMSG
    268  1.18  christos 			case KERN_SYSVIPC_MSG_INFO:
    269  1.18  christos 				FILL_MSG(msqids[i], msgsi->msgids[i]);
    270  1.18  christos 				break;
    271  1.18  christos #endif
    272  1.18  christos #ifdef SYSVSEM
    273  1.18  christos 			case KERN_SYSVIPC_SEM_INFO:
    274  1.18  christos 				FILL_SEM(sema[i], semsi->semids[i]);
    275  1.18  christos 				break;
    276  1.18  christos #endif
    277  1.18  christos #ifdef SYSVSHM
    278  1.18  christos 			case KERN_SYSVIPC_SHM_INFO:
    279  1.18  christos 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
    280  1.18  christos 				break;
    281  1.18  christos #endif
    282  1.18  christos 			}
    283  1.18  christos 			buflen -= dssize;
    284  1.18  christos 		}
    285  1.18  christos 	}
    286  1.18  christos 	*sizep -= buflen;
    287  1.18  christos 	error = copyout(bf, start, *sizep);
    288  1.18  christos 	/* If copyout succeeded, use return code set earlier. */
    289  1.18  christos 	if (error == 0)
    290  1.18  christos 		error = ret;
    291  1.18  christos 	if (bf)
    292  1.18  christos 		free(bf, M_TEMP);
    293  1.18  christos 	return error;
    294  1.18  christos }
    295  1.18  christos 
    296  1.18  christos #undef FILL_PERM
    297  1.18  christos #undef FILL_MSG
    298  1.18  christos #undef FILL_SEM
    299  1.18  christos #undef FILL_SHM
    300  1.18  christos 
    301  1.18  christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
    302  1.18  christos {
    303  1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    304  1.18  christos 		CTLFLAG_PERMANENT,
    305  1.18  christos 		CTLTYPE_NODE, "kern", NULL,
    306  1.18  christos 		NULL, 0, NULL, 0,
    307  1.18  christos 		CTL_KERN, CTL_EOL);
    308  1.18  christos 
    309  1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    310  1.18  christos 		CTLFLAG_PERMANENT,
    311  1.18  christos 		CTLTYPE_NODE, "ipc",
    312  1.18  christos 		SYSCTL_DESCR("SysV IPC options"),
    313  1.18  christos 		NULL, 0, NULL, 0,
    314  1.18  christos 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    315  1.18  christos 
    316  1.18  christos 	sysctl_createv(clog, 0, NULL, NULL,
    317  1.18  christos 		CTLFLAG_PERMANENT,
    318  1.18  christos 		CTLTYPE_STRUCT, "sysvipc_info",
    319  1.18  christos 		SYSCTL_DESCR("System V style IPC information"),
    320  1.18  christos 		sysctl_kern_sysvipc, 0, NULL, 0,
    321  1.18  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
    322  1.18  christos }
    323