sysv_ipc.c revision 1.16.6.1 1 1.16.6.1 kardel /* $NetBSD: sysv_ipc.c,v 1.16.6.1 2006/06/01 22:38:09 kardel 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.6.1 kardel __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.16.6.1 2006/06/01 22:38:09 kardel 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.16.6.1 kardel #include <sys/kauth.h>
51 1.1 cgd
52 1.1 cgd /*
53 1.4 hpeyerl * Check for ipc permission
54 1.1 cgd */
55 1.1 cgd
56 1.4 hpeyerl int
57 1.16.6.1 kardel ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
58 1.1 cgd {
59 1.12 mycroft mode_t mask;
60 1.16.6.1 kardel int ismember = 0;
61 1.12 mycroft
62 1.16.6.1 kardel if (kauth_cred_geteuid(cred) == 0)
63 1.12 mycroft return (0);
64 1.1 cgd
65 1.9 mycroft if (mode == IPC_M) {
66 1.16.6.1 kardel if (kauth_cred_geteuid(cred) == perm->uid ||
67 1.16.6.1 kardel kauth_cred_geteuid(cred) == perm->cuid)
68 1.9 mycroft return (0);
69 1.9 mycroft return (EPERM);
70 1.1 cgd }
71 1.4 hpeyerl
72 1.12 mycroft mask = 0;
73 1.12 mycroft
74 1.16.6.1 kardel if (kauth_cred_geteuid(cred) == perm->uid ||
75 1.16.6.1 kardel kauth_cred_geteuid(cred) == perm->cuid) {
76 1.12 mycroft if (mode & IPC_R)
77 1.12 mycroft mask |= S_IRUSR;
78 1.12 mycroft if (mode & IPC_W)
79 1.12 mycroft mask |= S_IWUSR;
80 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
81 1.12 mycroft }
82 1.12 mycroft
83 1.16.6.1 kardel if (kauth_cred_getegid(cred) == perm->gid ||
84 1.16.6.1 kardel (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
85 1.16.6.1 kardel kauth_cred_getegid(cred) == perm->cgid ||
86 1.16.6.1 kardel (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
87 1.12 mycroft if (mode & IPC_R)
88 1.12 mycroft mask |= S_IRGRP;
89 1.12 mycroft if (mode & IPC_W)
90 1.12 mycroft mask |= S_IWGRP;
91 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
92 1.12 mycroft }
93 1.12 mycroft
94 1.12 mycroft if (mode & IPC_R)
95 1.12 mycroft mask |= S_IROTH;
96 1.12 mycroft if (mode & IPC_W)
97 1.12 mycroft mask |= S_IWOTH;
98 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
99 1.1 cgd }
100