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