Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.16.10.2
      1  1.16.10.2      elad /*	$NetBSD: sysv_ipc.c,v 1.16.10.2 2006/03/11 04:55:28 elad 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.16.10.2      elad __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.16.10.2 2006/03/11 04:55:28 elad Exp $");
     41        1.1       cgd 
     42        1.2   mycroft #include <sys/param.h>
     43        1.2   mycroft #include <sys/kernel.h>
     44        1.2   mycroft #include <sys/proc.h>
     45        1.2   mycroft #include <sys/ipc.h>
     46        1.4   hpeyerl #include <sys/systm.h>
     47       1.10   mycroft #include <sys/mount.h>
     48       1.10   mycroft #include <sys/vnode.h>
     49       1.12   mycroft #include <sys/stat.h>
     50        1.1       cgd 
     51        1.1       cgd /*
     52        1.4   hpeyerl  * Check for ipc permission
     53        1.1       cgd  */
     54        1.1       cgd 
     55        1.4   hpeyerl int
     56  1.16.10.1      elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
     57        1.1       cgd {
     58       1.12   mycroft 	mode_t mask;
     59  1.16.10.2      elad 	int error, ismember;
     60       1.12   mycroft 
     61  1.16.10.1      elad 	if (kauth_cred_geteuid(cred) == 0)
     62       1.12   mycroft 		return (0);
     63        1.1       cgd 
     64        1.9   mycroft 	if (mode == IPC_M) {
     65  1.16.10.1      elad 		if (kauth_cred_geteuid(cred) == perm->uid ||
     66  1.16.10.1      elad 		    kauth_cred_geteuid(cred) == perm->cuid)
     67        1.9   mycroft 			return (0);
     68        1.9   mycroft 		return (EPERM);
     69        1.1       cgd 	}
     70        1.4   hpeyerl 
     71       1.12   mycroft 	mask = 0;
     72       1.12   mycroft 
     73  1.16.10.1      elad 	if (kauth_cred_geteuid(cred) == perm->uid ||
     74  1.16.10.1      elad 	    kauth_cred_geteuid(cred) == perm->cuid) {
     75       1.12   mycroft 		if (mode & IPC_R)
     76       1.12   mycroft 			mask |= S_IRUSR;
     77       1.12   mycroft 		if (mode & IPC_W)
     78       1.12   mycroft 			mask |= S_IWUSR;
     79       1.12   mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
     80       1.12   mycroft 	}
     81       1.12   mycroft 
     82  1.16.10.2      elad 	error = kauth_cred_ismember_gid(cred, perm->gid, &ismember);
     83  1.16.10.2      elad 	if (error)
     84  1.16.10.2      elad 		return (error);
     85  1.16.10.2      elad 	if (!ismember) {
     86  1.16.10.2      elad 		error = kauth_cred_ismember_gid(cred, perm->cgid, &ismember);
     87  1.16.10.2      elad 		if (error)
     88  1.16.10.2      elad 			return (error);
     89  1.16.10.2      elad 	}
     90  1.16.10.2      elad 
     91  1.16.10.2      elad 	if (kauth_cred_getegid(cred) == perm->gid ||
     92  1.16.10.2      elad 	    kauth_cred_getegid(cred) == perm->cgid || ismember) {
     93       1.12   mycroft 		if (mode & IPC_R)
     94       1.12   mycroft 			mask |= S_IRGRP;
     95       1.12   mycroft 		if (mode & IPC_W)
     96       1.12   mycroft 			mask |= S_IWGRP;
     97       1.12   mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
     98       1.12   mycroft 	}
     99       1.12   mycroft 
    100       1.12   mycroft 	if (mode & IPC_R)
    101       1.12   mycroft 		mask |= S_IROTH;
    102       1.12   mycroft 	if (mode & IPC_W)
    103       1.12   mycroft 		mask |= S_IWOTH;
    104       1.12   mycroft 	return ((perm->mode & mask) == mask ? 0 : EACCES);
    105        1.1       cgd }
    106