1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
5 * Copyright (c) 2006 nCircle Network Security, Inc.
6 * All rights reserved.
7 *
8 * This software was developed by Robert N. M. Watson for the TrustedBSD
9 * Project under contract to nCircle Network Security, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
24 *
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.
35 *
36 * $NetBSD: sysv_ipc.c,v 1.9 1995/06/02 19:04:22 mycroft Exp $
37 */
38
39#include <sys/cdefs.h>
40#include "opt_sysvipc.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/sem.h>
45#include <sys/shm.h>
46#include <sys/ipc.h>
47#include <sys/priv.h>
48#include <sys/proc.h>
49#include <sys/ucred.h>
50
51#ifndef SYSVSHM
52void (*shmfork_hook)(struct proc *, struct proc *) = NULL;
53void (*shmexit_hook)(struct vmspace *) = NULL;
54
55/* called from kern_fork.c */
56void
57shmfork(struct proc *p1, struct proc *p2)
58{
59	if (shmfork_hook != NULL)
60		shmfork_hook(p1, p2);
61}
62
63/* called from kern_exit.c */
64void
65shmexit(struct vmspace *vm)
66{
67	if (shmexit_hook != NULL)
68		shmexit_hook(vm);
69}
70#endif
71
72/*
73 * Check for IPC permission.
74 *
75 * Note: The MAC Framework does not require any modifications to the
76 * ipcperm() function, as access control checks are performed throughout the
77 * implementation of each primitive.  Those entry point calls complement the
78 * ipcperm() discertionary checks.  Unlike file system discretionary access
79 * control, the original create of an object is given the same rights as the
80 * current owner.
81 */
82int
83ipcperm(struct thread *td, struct ipc_perm *perm, int acc_mode)
84{
85	struct ucred *cred = td->td_ucred;
86	int error, obj_mode, dac_granted, priv_granted;
87
88	dac_granted = 0;
89	if (cred->cr_uid == perm->cuid || cred->cr_uid == perm->uid) {
90		obj_mode = perm->mode;
91		dac_granted |= IPC_M;
92	} else if (groupmember(perm->gid, cred) ||
93	    groupmember(perm->cgid, cred)) {
94		obj_mode = perm->mode;
95		obj_mode <<= 3;
96	} else {
97		obj_mode = perm->mode;
98		obj_mode <<= 6;
99	}
100
101	/*
102	 * While the System V IPC permission model allows IPC_M to be
103	 * granted, as part of the mode, our implementation requires
104	 * privilege to adminster the object if not the owner or creator.
105	 */
106#if 0
107	if (obj_mode & IPC_M)
108		dac_granted |= IPC_M;
109#endif
110	if (obj_mode & IPC_R)
111		dac_granted |= IPC_R;
112	if (obj_mode & IPC_W)
113		dac_granted |= IPC_W;
114
115	/*
116	 * Simple case: all required rights are granted by DAC.
117	 */
118	if ((dac_granted & acc_mode) == acc_mode)
119		return (0);
120
121	/*
122	 * Privilege is required to satisfy the request.
123	 */
124	priv_granted = 0;
125	if ((acc_mode & IPC_M) && !(dac_granted & IPC_M)) {
126		error = priv_check(td, PRIV_IPC_ADMIN);
127		if (error == 0)
128			priv_granted |= IPC_M;
129	}
130
131	if ((acc_mode & IPC_R) && !(dac_granted & IPC_R)) {
132		error = priv_check(td, PRIV_IPC_READ);
133		if (error == 0)
134			priv_granted |= IPC_R;
135	}
136
137	if ((acc_mode & IPC_W) && !(dac_granted & IPC_W)) {
138		error = priv_check(td, PRIV_IPC_WRITE);
139		if (error == 0)
140			priv_granted |= IPC_W;
141	}
142
143	if (((dac_granted | priv_granted) & acc_mode) == acc_mode)
144		return (0);
145	else
146		return (EACCES);
147}
148
149#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
150    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
151void
152ipcperm_old2new(struct ipc_perm_old *old, struct ipc_perm *new)
153{
154
155	new->cuid = old->cuid;
156	new->cgid = old->cgid;
157	new->uid = old->uid;
158	new->gid = old->gid;
159	new->mode = old->mode;
160	new->seq = old->seq;
161	new->key = old->key;
162}
163
164void
165ipcperm_new2old(struct ipc_perm *new, struct ipc_perm_old *old)
166{
167
168	/* XXX: How to handle ID's > USHORT_MAX? */
169	old->cuid = new->cuid;
170	old->cgid = new->cgid;
171	old->uid = new->uid;
172	old->gid = new->gid;
173	old->mode = new->mode;
174	old->seq = new->seq;
175	old->key = new->key;
176}
177#endif
178
179#ifdef COMPAT_FREEBSD32
180#include <sys/mount.h>
181#include <sys/socket.h>
182#include <compat/freebsd32/freebsd32.h>
183#include <compat/freebsd32/freebsd32_ipc.h>
184#include <compat/freebsd32/freebsd32_proto.h>
185#include <compat/freebsd32/freebsd32_signal.h>
186#include <compat/freebsd32/freebsd32_syscall.h>
187#include <compat/freebsd32/freebsd32_util.h>
188
189#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
190    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
191void
192freebsd32_ipcperm_old_in(struct ipc_perm_old32 *ip32, struct ipc_perm *ip)
193{
194
195	CP(*ip32, *ip, cuid);
196	CP(*ip32, *ip, cgid);
197	CP(*ip32, *ip, uid);
198	CP(*ip32, *ip, gid);
199	CP(*ip32, *ip, mode);
200	CP(*ip32, *ip, seq);
201	CP(*ip32, *ip, key);
202}
203
204void
205freebsd32_ipcperm_old_out(struct ipc_perm *ip, struct ipc_perm_old32 *ip32)
206{
207
208	CP(*ip, *ip32, cuid);
209	CP(*ip, *ip32, cgid);
210	CP(*ip, *ip32, uid);
211	CP(*ip, *ip32, gid);
212	CP(*ip, *ip32, mode);
213	CP(*ip, *ip32, seq);
214	CP(*ip, *ip32, key);
215}
216#endif
217
218void
219freebsd32_ipcperm_in(struct ipc_perm32 *ip32, struct ipc_perm *ip)
220{
221
222	CP(*ip32, *ip, cuid);
223	CP(*ip32, *ip, cgid);
224	CP(*ip32, *ip, uid);
225	CP(*ip32, *ip, gid);
226	CP(*ip32, *ip, mode);
227	CP(*ip32, *ip, seq);
228	CP(*ip32, *ip, key);
229}
230
231void
232freebsd32_ipcperm_out(struct ipc_perm *ip, struct ipc_perm32 *ip32)
233{
234
235	CP(*ip, *ip32, cuid);
236	CP(*ip, *ip32, cgid);
237	CP(*ip, *ip32, uid);
238	CP(*ip, *ip32, gid);
239	CP(*ip, *ip32, mode);
240	CP(*ip, *ip32, seq);
241	CP(*ip, *ip32, key);
242}
243#endif
244