2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
5 * Copyright (c) 2006 nCircle Network Security, Inc.
8 * This software was developed by Robert N. M. Watson for the TrustedBSD
9 * Project under contract to nCircle Network Security, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Herb Peyerl.
22 * 4. The name of Herb Peyerl may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * $NetBSD: sysv_ipc.c,v 1.9 1995/06/02 19:04:22 mycroft Exp $
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_compat.h"
43 #include "opt_sysvipc.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
52 #include <sys/ucred.h>
54 void (*shmfork_hook)(struct proc *, struct proc *) = NULL;
55 void (*shmexit_hook)(struct vmspace *) = NULL;
57 /* called from kern_fork.c */
63 if (shmfork_hook != NULL)
68 /* called from kern_exit.c */
70 shmexit(struct vmspace *vm)
73 if (shmexit_hook != NULL)
79 * Check for IPC permission.
81 * Note: The MAC Framework does not require any modifications to the
82 * ipcperm() function, as access control checks are performed throughout the
83 * implementation of each primitive. Those entry point calls complement the
84 * ipcperm() discertionary checks. Unlike file system discretionary access
85 * control, the original create of an object is given the same rights as the
89 ipcperm(struct thread *td, struct ipc_perm *perm, int acc_mode)
91 struct ucred *cred = td->td_ucred;
92 int error, obj_mode, dac_granted, priv_granted;
95 if (cred->cr_uid == perm->cuid || cred->cr_uid == perm->uid) {
96 obj_mode = perm->mode;
98 } else if (groupmember(perm->gid, cred) ||
99 groupmember(perm->cgid, cred)) {
100 obj_mode = perm->mode;
103 obj_mode = perm->mode;
108 * While the System V IPC permission model allows IPC_M to be
109 * granted, as part of the mode, our implementation requires
110 * privilege to adminster the object if not the owner or creator.
113 if (obj_mode & IPC_M)
114 dac_granted |= IPC_M;
116 if (obj_mode & IPC_R)
117 dac_granted |= IPC_R;
118 if (obj_mode & IPC_W)
119 dac_granted |= IPC_W;
122 * Simple case: all required rights are granted by DAC.
124 if ((dac_granted & acc_mode) == acc_mode)
128 * Privilege is required to satisfy the request.
131 if ((acc_mode & IPC_M) && !(dac_granted & IPC_M)) {
132 error = priv_check(td, PRIV_IPC_ADMIN);
134 priv_granted |= IPC_M;
137 if ((acc_mode & IPC_R) && !(dac_granted & IPC_R)) {
138 error = priv_check(td, PRIV_IPC_READ);
140 priv_granted |= IPC_R;
143 if ((acc_mode & IPC_W) && !(dac_granted & IPC_W)) {
144 error = priv_check(td, PRIV_IPC_WRITE);
146 priv_granted |= IPC_W;
149 if (((dac_granted | priv_granted) & acc_mode) == acc_mode)
155 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
156 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
158 ipcperm_old2new(struct ipc_perm_old *old, struct ipc_perm *new)
161 new->cuid = old->cuid;
162 new->cgid = old->cgid;
165 new->mode = old->mode;
171 ipcperm_new2old(struct ipc_perm *new, struct ipc_perm_old *old)
174 /* XXX: How to handle ID's > USHORT_MAX? */
175 old->cuid = new->cuid;
176 old->cgid = new->cgid;
179 old->mode = new->mode;
185 #ifdef COMPAT_FREEBSD32
186 #include <sys/mount.h>
187 #include <sys/socket.h>
188 #include <compat/freebsd32/freebsd32.h>
189 #include <compat/freebsd32/freebsd32_ipc.h>
190 #include <compat/freebsd32/freebsd32_proto.h>
191 #include <compat/freebsd32/freebsd32_signal.h>
192 #include <compat/freebsd32/freebsd32_syscall.h>
193 #include <compat/freebsd32/freebsd32_util.h>
195 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
196 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
198 freebsd32_ipcperm_old_in(struct ipc_perm32_old *ip32, struct ipc_perm *ip)
201 CP(*ip32, *ip, cuid);
202 CP(*ip32, *ip, cgid);
205 CP(*ip32, *ip, mode);
211 freebsd32_ipcperm_old_out(struct ipc_perm *ip, struct ipc_perm32_old *ip32)
214 CP(*ip, *ip32, cuid);
215 CP(*ip, *ip32, cgid);
218 CP(*ip, *ip32, mode);
225 freebsd32_ipcperm_in(struct ipc_perm32 *ip32, struct ipc_perm *ip)
228 CP(*ip32, *ip, cuid);
229 CP(*ip32, *ip, cgid);
232 CP(*ip32, *ip, mode);
238 freebsd32_ipcperm_out(struct ipc_perm *ip, struct ipc_perm32 *ip32)
241 CP(*ip, *ip32, cuid);
242 CP(*ip, *ip32, cgid);
245 CP(*ip, *ip32, mode);