xref: /freebsd/sys/kern/sysv_shm.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /*	$NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $	*/
2 /*-
3  * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 1994 Adam Glass and Charles Hannum.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Adam Glass and Charles
18  *	Hannum.
19  * 4. The names of the authors may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*-
34  * Copyright (c) 2003-2005 McAfee, Inc.
35  * Copyright (c) 2016-2017 Robert N. M. Watson
36  * All rights reserved.
37  *
38  * This software was developed for the FreeBSD Project in part by McAfee
39  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
40  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
41  * program.
42  *
43  * Portions of this software were developed by BAE Systems, the University of
44  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
45  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
46  * Computing (TC) research program.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
72 
73 #include "opt_compat.h"
74 #include "opt_sysvipc.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/limits.h>
80 #include <sys/lock.h>
81 #include <sys/sysctl.h>
82 #include <sys/shm.h>
83 #include <sys/proc.h>
84 #include <sys/malloc.h>
85 #include <sys/mman.h>
86 #include <sys/module.h>
87 #include <sys/mutex.h>
88 #include <sys/racct.h>
89 #include <sys/resourcevar.h>
90 #include <sys/rwlock.h>
91 #include <sys/stat.h>
92 #include <sys/syscall.h>
93 #include <sys/syscallsubr.h>
94 #include <sys/sysent.h>
95 #include <sys/sysproto.h>
96 #include <sys/jail.h>
97 
98 #include <security/audit/audit.h>
99 #include <security/mac/mac_framework.h>
100 
101 #include <vm/vm.h>
102 #include <vm/vm_param.h>
103 #include <vm/pmap.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_page.h>
107 #include <vm/vm_pager.h>
108 
109 FEATURE(sysv_shm, "System V shared memory segments support");
110 
111 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
112 
113 static int shmget_allocate_segment(struct thread *td,
114     struct shmget_args *uap, int mode);
115 static int shmget_existing(struct thread *td, struct shmget_args *uap,
116     int mode, int segnum);
117 
118 #define	SHMSEG_FREE     	0x0200
119 #define	SHMSEG_REMOVED  	0x0400
120 #define	SHMSEG_ALLOCATED	0x0800
121 
122 static int shm_last_free, shm_nused, shmalloced;
123 vm_size_t shm_committed;
124 static struct shmid_kernel *shmsegs;
125 static unsigned shm_prison_slot;
126 
127 struct shmmap_state {
128 	vm_offset_t va;
129 	int shmid;
130 };
131 
132 static void shm_deallocate_segment(struct shmid_kernel *);
133 static int shm_find_segment_by_key(struct prison *, key_t);
134 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
135 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
136 static void shmrealloc(void);
137 static int shminit(void);
138 static int sysvshm_modload(struct module *, int, void *);
139 static int shmunload(void);
140 static void shmexit_myhook(struct vmspace *vm);
141 static void shmfork_myhook(struct proc *p1, struct proc *p2);
142 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
143 static void shm_remove(struct shmid_kernel *, int);
144 static struct prison *shm_find_prison(struct ucred *);
145 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
146 static int shm_prison_check(void *, void *);
147 static int shm_prison_set(void *, void *);
148 static int shm_prison_get(void *, void *);
149 static int shm_prison_remove(void *, void *);
150 static void shm_prison_cleanup(struct prison *);
151 
152 /*
153  * Tuneable values.
154  */
155 #ifndef SHMMAXPGS
156 #define	SHMMAXPGS	131072	/* Note: sysv shared memory is swap backed. */
157 #endif
158 #ifndef SHMMAX
159 #define	SHMMAX	(SHMMAXPGS*PAGE_SIZE)
160 #endif
161 #ifndef SHMMIN
162 #define	SHMMIN	1
163 #endif
164 #ifndef SHMMNI
165 #define	SHMMNI	192
166 #endif
167 #ifndef SHMSEG
168 #define	SHMSEG	128
169 #endif
170 #ifndef SHMALL
171 #define	SHMALL	(SHMMAXPGS)
172 #endif
173 
174 struct	shminfo shminfo = {
175 	.shmmax = SHMMAX,
176 	.shmmin = SHMMIN,
177 	.shmmni = SHMMNI,
178 	.shmseg = SHMSEG,
179 	.shmall = SHMALL
180 };
181 
182 static int shm_use_phys;
183 static int shm_allow_removed = 1;
184 
185 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0,
186     "Maximum shared memory segment size");
187 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0,
188     "Minimum shared memory segment size");
189 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
190     "Number of shared memory identifiers");
191 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
192     "Number of segments per process");
193 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0,
194     "Maximum number of pages available for shared memory");
195 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN,
196     &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
197 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN,
198     &shm_allow_removed, 0,
199     "Enable/Disable attachment to attached segments marked for removal");
200 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
201     CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
202     "Current number of shared memory segments allocated");
203 
204 static struct sx sysvshmsx;
205 #define	SYSVSHM_LOCK()		sx_xlock(&sysvshmsx)
206 #define	SYSVSHM_UNLOCK()	sx_xunlock(&sysvshmsx)
207 #define	SYSVSHM_ASSERT_LOCKED()	sx_assert(&sysvshmsx, SA_XLOCKED)
208 
209 static int
210 shm_find_segment_by_key(struct prison *pr, key_t key)
211 {
212 	int i;
213 
214 	for (i = 0; i < shmalloced; i++)
215 		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
216 		    shmsegs[i].cred != NULL &&
217 		    shmsegs[i].cred->cr_prison == pr &&
218 		    shmsegs[i].u.shm_perm.key == key)
219 			return (i);
220 	return (-1);
221 }
222 
223 /*
224  * Finds segment either by shmid if is_shmid is true, or by segnum if
225  * is_shmid is false.
226  */
227 static struct shmid_kernel *
228 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
229 {
230 	struct shmid_kernel *shmseg;
231 	int segnum;
232 
233 	segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
234 	if (segnum < 0 || segnum >= shmalloced)
235 		return (NULL);
236 	shmseg = &shmsegs[segnum];
237 	if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
238 	    (!shm_allow_removed &&
239 	    (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
240 	    (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
241 	    shm_prison_cansee(rpr, shmseg) != 0)
242 		return (NULL);
243 	return (shmseg);
244 }
245 
246 static void
247 shm_deallocate_segment(struct shmid_kernel *shmseg)
248 {
249 	vm_size_t size;
250 
251 	SYSVSHM_ASSERT_LOCKED();
252 
253 	vm_object_deallocate(shmseg->object);
254 	shmseg->object = NULL;
255 	size = round_page(shmseg->u.shm_segsz);
256 	shm_committed -= btoc(size);
257 	shm_nused--;
258 	shmseg->u.shm_perm.mode = SHMSEG_FREE;
259 #ifdef MAC
260 	mac_sysvshm_cleanup(shmseg);
261 #endif
262 	racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
263 	racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
264 	crfree(shmseg->cred);
265 	shmseg->cred = NULL;
266 }
267 
268 static int
269 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
270 {
271 	struct shmid_kernel *shmseg;
272 	int segnum, result;
273 	vm_size_t size;
274 
275 	SYSVSHM_ASSERT_LOCKED();
276 	segnum = IPCID_TO_IX(shmmap_s->shmid);
277 	KASSERT(segnum >= 0 && segnum < shmalloced,
278 	    ("segnum %d shmalloced %d", segnum, shmalloced));
279 
280 	shmseg = &shmsegs[segnum];
281 	size = round_page(shmseg->u.shm_segsz);
282 	result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
283 	if (result != KERN_SUCCESS)
284 		return (EINVAL);
285 	shmmap_s->shmid = -1;
286 	shmseg->u.shm_dtime = time_second;
287 	if (--shmseg->u.shm_nattch == 0 &&
288 	    (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
289 		shm_deallocate_segment(shmseg);
290 		shm_last_free = segnum;
291 	}
292 	return (0);
293 }
294 
295 static void
296 shm_remove(struct shmid_kernel *shmseg, int segnum)
297 {
298 
299 	shmseg->u.shm_perm.key = IPC_PRIVATE;
300 	shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
301 	if (shmseg->u.shm_nattch == 0) {
302 		shm_deallocate_segment(shmseg);
303 		shm_last_free = segnum;
304 	}
305 }
306 
307 static struct prison *
308 shm_find_prison(struct ucred *cred)
309 {
310 	struct prison *pr, *rpr;
311 
312 	pr = cred->cr_prison;
313 	prison_lock(pr);
314 	rpr = osd_jail_get(pr, shm_prison_slot);
315 	prison_unlock(pr);
316 	return rpr;
317 }
318 
319 static int
320 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
321 {
322 
323 	if (shmseg->cred == NULL ||
324 	    !(rpr == shmseg->cred->cr_prison ||
325 	      prison_ischild(rpr, shmseg->cred->cr_prison)))
326 		return (EINVAL);
327 	return (0);
328 }
329 
330 static int
331 kern_shmdt_locked(struct thread *td, const void *shmaddr)
332 {
333 	struct proc *p = td->td_proc;
334 	struct shmmap_state *shmmap_s;
335 #if defined(AUDIT) || defined(MAC)
336 	struct shmid_kernel *shmsegptr;
337 #endif
338 #ifdef MAC
339 	int error;
340 #endif
341 	int i;
342 
343 	SYSVSHM_ASSERT_LOCKED();
344 	if (shm_find_prison(td->td_ucred) == NULL)
345 		return (ENOSYS);
346 	shmmap_s = p->p_vmspace->vm_shm;
347  	if (shmmap_s == NULL)
348 		return (EINVAL);
349 	AUDIT_ARG_SVIPC_ID(shmmap_s->shmid);
350 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
351 		if (shmmap_s->shmid != -1 &&
352 		    shmmap_s->va == (vm_offset_t)shmaddr) {
353 			break;
354 		}
355 	}
356 	if (i == shminfo.shmseg)
357 		return (EINVAL);
358 #if (defined(AUDIT) && defined(KDTRACE_HOOKS)) || defined(MAC)
359 	shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
360 #endif
361 #ifdef MAC
362 	error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
363 	if (error != 0)
364 		return (error);
365 #endif
366 	return (shm_delete_mapping(p->p_vmspace, shmmap_s));
367 }
368 
369 #ifndef _SYS_SYSPROTO_H_
370 struct shmdt_args {
371 	const void *shmaddr;
372 };
373 #endif
374 int
375 sys_shmdt(struct thread *td, struct shmdt_args *uap)
376 {
377 	int error;
378 
379 	SYSVSHM_LOCK();
380 	error = kern_shmdt_locked(td, uap->shmaddr);
381 	SYSVSHM_UNLOCK();
382 	return (error);
383 }
384 
385 static int
386 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
387     int shmflg)
388 {
389 	struct prison *rpr;
390 	struct proc *p = td->td_proc;
391 	struct shmid_kernel *shmseg;
392 	struct shmmap_state *shmmap_s;
393 	vm_offset_t attach_va;
394 	vm_prot_t prot;
395 	vm_size_t size;
396 	int error, i, rv;
397 
398 	AUDIT_ARG_SVIPC_ID(shmid);
399 	AUDIT_ARG_VALUE(shmflg);
400 
401 	SYSVSHM_ASSERT_LOCKED();
402 	rpr = shm_find_prison(td->td_ucred);
403 	if (rpr == NULL)
404 		return (ENOSYS);
405 	shmmap_s = p->p_vmspace->vm_shm;
406 	if (shmmap_s == NULL) {
407 		shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
408 		    M_SHM, M_WAITOK);
409 		for (i = 0; i < shminfo.shmseg; i++)
410 			shmmap_s[i].shmid = -1;
411 		KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
412 		p->p_vmspace->vm_shm = shmmap_s;
413 	}
414 	shmseg = shm_find_segment(rpr, shmid, true);
415 	if (shmseg == NULL)
416 		return (EINVAL);
417 	error = ipcperm(td, &shmseg->u.shm_perm,
418 	    (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
419 	if (error != 0)
420 		return (error);
421 #ifdef MAC
422 	error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
423 	if (error != 0)
424 		return (error);
425 #endif
426 	for (i = 0; i < shminfo.shmseg; i++) {
427 		if (shmmap_s->shmid == -1)
428 			break;
429 		shmmap_s++;
430 	}
431 	if (i >= shminfo.shmseg)
432 		return (EMFILE);
433 	size = round_page(shmseg->u.shm_segsz);
434 	prot = VM_PROT_READ;
435 	if ((shmflg & SHM_RDONLY) == 0)
436 		prot |= VM_PROT_WRITE;
437 	if (shmaddr != NULL) {
438 		if ((shmflg & SHM_RND) != 0)
439 			attach_va = rounddown2((vm_offset_t)shmaddr, SHMLBA);
440 		else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
441 			attach_va = (vm_offset_t)shmaddr;
442 		else
443 			return (EINVAL);
444 	} else {
445 		/*
446 		 * This is just a hint to vm_map_find() about where to
447 		 * put it.
448 		 */
449 		attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
450 		    lim_max(td, RLIMIT_DATA));
451 	}
452 
453 	vm_object_reference(shmseg->object);
454 	rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va,
455 	    size, 0, shmaddr != NULL ? VMFS_NO_SPACE : VMFS_OPTIMAL_SPACE,
456 	    prot, prot, MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL);
457 	if (rv != KERN_SUCCESS) {
458 		vm_object_deallocate(shmseg->object);
459 		return (ENOMEM);
460 	}
461 
462 	shmmap_s->va = attach_va;
463 	shmmap_s->shmid = shmid;
464 	shmseg->u.shm_lpid = p->p_pid;
465 	shmseg->u.shm_atime = time_second;
466 	shmseg->u.shm_nattch++;
467 	td->td_retval[0] = attach_va;
468 	return (error);
469 }
470 
471 int
472 kern_shmat(struct thread *td, int shmid, const void *shmaddr, int shmflg)
473 {
474 	int error;
475 
476 	SYSVSHM_LOCK();
477 	error = kern_shmat_locked(td, shmid, shmaddr, shmflg);
478 	SYSVSHM_UNLOCK();
479 	return (error);
480 }
481 
482 #ifndef _SYS_SYSPROTO_H_
483 struct shmat_args {
484 	int shmid;
485 	const void *shmaddr;
486 	int shmflg;
487 };
488 #endif
489 int
490 sys_shmat(struct thread *td, struct shmat_args *uap)
491 {
492 
493 	return (kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg));
494 }
495 
496 static int
497 kern_shmctl_locked(struct thread *td, int shmid, int cmd, void *buf,
498     size_t *bufsz)
499 {
500 	struct prison *rpr;
501 	struct shmid_kernel *shmseg;
502 	struct shmid_ds *shmidp;
503 	struct shm_info shm_info;
504 	int error;
505 
506 	SYSVSHM_ASSERT_LOCKED();
507 
508 	rpr = shm_find_prison(td->td_ucred);
509 	if (rpr == NULL)
510 		return (ENOSYS);
511 
512 	AUDIT_ARG_SVIPC_ID(shmid);
513 	AUDIT_ARG_SVIPC_CMD(cmd);
514 
515 	switch (cmd) {
516 	/*
517 	 * It is possible that kern_shmctl is being called from the Linux ABI
518 	 * layer, in which case, we will need to implement IPC_INFO.  It should
519 	 * be noted that other shmctl calls will be funneled through here for
520 	 * Linix binaries as well.
521 	 *
522 	 * NB: The Linux ABI layer will convert this data to structure(s) more
523 	 * consistent with the Linux ABI.
524 	 */
525 	case IPC_INFO:
526 		memcpy(buf, &shminfo, sizeof(shminfo));
527 		if (bufsz)
528 			*bufsz = sizeof(shminfo);
529 		td->td_retval[0] = shmalloced;
530 		return (0);
531 	case SHM_INFO: {
532 		shm_info.used_ids = shm_nused;
533 		shm_info.shm_rss = 0;	/*XXX where to get from ? */
534 		shm_info.shm_tot = 0;	/*XXX where to get from ? */
535 		shm_info.shm_swp = 0;	/*XXX where to get from ? */
536 		shm_info.swap_attempts = 0;	/*XXX where to get from ? */
537 		shm_info.swap_successes = 0;	/*XXX where to get from ? */
538 		memcpy(buf, &shm_info, sizeof(shm_info));
539 		if (bufsz != NULL)
540 			*bufsz = sizeof(shm_info);
541 		td->td_retval[0] = shmalloced;
542 		return (0);
543 	}
544 	}
545 	shmseg = shm_find_segment(rpr, shmid, cmd != SHM_STAT);
546 	if (shmseg == NULL)
547 		return (EINVAL);
548 #ifdef MAC
549 	error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
550 	if (error != 0)
551 		return (error);
552 #endif
553 	switch (cmd) {
554 	case SHM_STAT:
555 	case IPC_STAT:
556 		shmidp = (struct shmid_ds *)buf;
557 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
558 		if (error != 0)
559 			return (error);
560 		memcpy(shmidp, &shmseg->u, sizeof(struct shmid_ds));
561 		if (td->td_ucred->cr_prison != shmseg->cred->cr_prison)
562 			shmidp->shm_perm.key = IPC_PRIVATE;
563 		if (bufsz != NULL)
564 			*bufsz = sizeof(struct shmid_ds);
565 		if (cmd == SHM_STAT) {
566 			td->td_retval[0] = IXSEQ_TO_IPCID(shmid,
567 			    shmseg->u.shm_perm);
568 		}
569 		break;
570 	case IPC_SET:
571 		shmidp = (struct shmid_ds *)buf;
572 		AUDIT_ARG_SVIPC_PERM(&shmidp->shm_perm);
573 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
574 		if (error != 0)
575 			return (error);
576 		shmseg->u.shm_perm.uid = shmidp->shm_perm.uid;
577 		shmseg->u.shm_perm.gid = shmidp->shm_perm.gid;
578 		shmseg->u.shm_perm.mode =
579 		    (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
580 		    (shmidp->shm_perm.mode & ACCESSPERMS);
581 		shmseg->u.shm_ctime = time_second;
582 		break;
583 	case IPC_RMID:
584 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
585 		if (error != 0)
586 			return (error);
587 		shm_remove(shmseg, IPCID_TO_IX(shmid));
588 		break;
589 #if 0
590 	case SHM_LOCK:
591 	case SHM_UNLOCK:
592 #endif
593 	default:
594 		error = EINVAL;
595 		break;
596 	}
597 	return (error);
598 }
599 
600 int
601 kern_shmctl(struct thread *td, int shmid, int cmd, void *buf, size_t *bufsz)
602 {
603 	int error;
604 
605 	SYSVSHM_LOCK();
606 	error = kern_shmctl_locked(td, shmid, cmd, buf, bufsz);
607 	SYSVSHM_UNLOCK();
608 	return (error);
609 }
610 
611 
612 #ifndef _SYS_SYSPROTO_H_
613 struct shmctl_args {
614 	int shmid;
615 	int cmd;
616 	struct shmid_ds *buf;
617 };
618 #endif
619 int
620 sys_shmctl(struct thread *td, struct shmctl_args *uap)
621 {
622 	int error;
623 	struct shmid_ds buf;
624 	size_t bufsz;
625 
626 	/*
627 	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
628 	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
629 	 * return an error back to the user since we do not to support this.
630 	 */
631 	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
632 	    uap->cmd == SHM_STAT)
633 		return (EINVAL);
634 
635 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
636 	if (uap->cmd == IPC_SET) {
637 		if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
638 			goto done;
639 	}
640 
641 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
642 	if (error)
643 		goto done;
644 
645 	/* Cases in which we need to copyout */
646 	switch (uap->cmd) {
647 	case IPC_STAT:
648 		error = copyout(&buf, uap->buf, bufsz);
649 		break;
650 	}
651 
652 done:
653 	if (error) {
654 		/* Invalidate the return value */
655 		td->td_retval[0] = -1;
656 	}
657 	return (error);
658 }
659 
660 
661 static int
662 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
663     int segnum)
664 {
665 	struct shmid_kernel *shmseg;
666 #ifdef MAC
667 	int error;
668 #endif
669 
670 	SYSVSHM_ASSERT_LOCKED();
671 	KASSERT(segnum >= 0 && segnum < shmalloced,
672 	    ("segnum %d shmalloced %d", segnum, shmalloced));
673 	shmseg = &shmsegs[segnum];
674 	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
675 		return (EEXIST);
676 #ifdef MAC
677 	error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
678 	if (error != 0)
679 		return (error);
680 #endif
681 	if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
682 		return (EINVAL);
683 	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
684 	return (0);
685 }
686 
687 static int
688 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
689 {
690 	struct ucred *cred = td->td_ucred;
691 	struct shmid_kernel *shmseg;
692 	vm_object_t shm_object;
693 	int i, segnum;
694 	size_t size;
695 
696 	SYSVSHM_ASSERT_LOCKED();
697 
698 	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
699 		return (EINVAL);
700 	if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
701 		return (ENOSPC);
702 	size = round_page(uap->size);
703 	if (shm_committed + btoc(size) > shminfo.shmall)
704 		return (ENOMEM);
705 	if (shm_last_free < 0) {
706 		shmrealloc();	/* Maybe expand the shmsegs[] array. */
707 		for (i = 0; i < shmalloced; i++)
708 			if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
709 				break;
710 		if (i == shmalloced)
711 			return (ENOSPC);
712 		segnum = i;
713 	} else  {
714 		segnum = shm_last_free;
715 		shm_last_free = -1;
716 	}
717 	KASSERT(segnum >= 0 && segnum < shmalloced,
718 	    ("segnum %d shmalloced %d", segnum, shmalloced));
719 	shmseg = &shmsegs[segnum];
720 #ifdef RACCT
721 	if (racct_enable) {
722 		PROC_LOCK(td->td_proc);
723 		if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
724 			PROC_UNLOCK(td->td_proc);
725 			return (ENOSPC);
726 		}
727 		if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
728 			racct_sub(td->td_proc, RACCT_NSHM, 1);
729 			PROC_UNLOCK(td->td_proc);
730 			return (ENOMEM);
731 		}
732 		PROC_UNLOCK(td->td_proc);
733 	}
734 #endif
735 
736 	/*
737 	 * We make sure that we have allocated a pager before we need
738 	 * to.
739 	 */
740 	shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
741 	    0, size, VM_PROT_DEFAULT, 0, cred);
742 	if (shm_object == NULL) {
743 #ifdef RACCT
744 		if (racct_enable) {
745 			PROC_LOCK(td->td_proc);
746 			racct_sub(td->td_proc, RACCT_NSHM, 1);
747 			racct_sub(td->td_proc, RACCT_SHMSIZE, size);
748 			PROC_UNLOCK(td->td_proc);
749 		}
750 #endif
751 		return (ENOMEM);
752 	}
753 	shm_object->pg_color = 0;
754 	VM_OBJECT_WLOCK(shm_object);
755 	vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
756 	vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
757 	VM_OBJECT_WUNLOCK(shm_object);
758 
759 	shmseg->object = shm_object;
760 	shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
761 	shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
762 	shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
763 	shmseg->u.shm_perm.key = uap->key;
764 	shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
765 	shmseg->cred = crhold(cred);
766 	shmseg->u.shm_segsz = uap->size;
767 	shmseg->u.shm_cpid = td->td_proc->p_pid;
768 	shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
769 	shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
770 #ifdef MAC
771 	mac_sysvshm_create(cred, shmseg);
772 #endif
773 	shmseg->u.shm_ctime = time_second;
774 	shm_committed += btoc(size);
775 	shm_nused++;
776 	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
777 
778 	return (0);
779 }
780 
781 #ifndef _SYS_SYSPROTO_H_
782 struct shmget_args {
783 	key_t key;
784 	size_t size;
785 	int shmflg;
786 };
787 #endif
788 int
789 sys_shmget(struct thread *td, struct shmget_args *uap)
790 {
791 	int segnum, mode;
792 	int error;
793 
794 	if (shm_find_prison(td->td_ucred) == NULL)
795 		return (ENOSYS);
796 	mode = uap->shmflg & ACCESSPERMS;
797 	SYSVSHM_LOCK();
798 	if (uap->key == IPC_PRIVATE) {
799 		error = shmget_allocate_segment(td, uap, mode);
800 	} else {
801 		segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
802 		    uap->key);
803 		if (segnum >= 0)
804 			error = shmget_existing(td, uap, mode, segnum);
805 		else if ((uap->shmflg & IPC_CREAT) == 0)
806 			error = ENOENT;
807 		else
808 			error = shmget_allocate_segment(td, uap, mode);
809 	}
810 	SYSVSHM_UNLOCK();
811 	return (error);
812 }
813 
814 static void
815 shmfork_myhook(struct proc *p1, struct proc *p2)
816 {
817 	struct shmmap_state *shmmap_s;
818 	size_t size;
819 	int i;
820 
821 	SYSVSHM_LOCK();
822 	size = shminfo.shmseg * sizeof(struct shmmap_state);
823 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
824 	bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
825 	p2->p_vmspace->vm_shm = shmmap_s;
826 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
827 		if (shmmap_s->shmid != -1) {
828 			KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
829 			    IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
830 			    ("segnum %d shmalloced %d",
831 			    IPCID_TO_IX(shmmap_s->shmid), shmalloced));
832 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
833 		}
834 	}
835 	SYSVSHM_UNLOCK();
836 }
837 
838 static void
839 shmexit_myhook(struct vmspace *vm)
840 {
841 	struct shmmap_state *base, *shm;
842 	int i;
843 
844 	base = vm->vm_shm;
845 	if (base != NULL) {
846 		vm->vm_shm = NULL;
847 		SYSVSHM_LOCK();
848 		for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
849 			if (shm->shmid != -1)
850 				shm_delete_mapping(vm, shm);
851 		}
852 		SYSVSHM_UNLOCK();
853 		free(base, M_SHM);
854 	}
855 }
856 
857 static void
858 shmrealloc(void)
859 {
860 	struct shmid_kernel *newsegs;
861 	int i;
862 
863 	SYSVSHM_ASSERT_LOCKED();
864 
865 	if (shmalloced >= shminfo.shmmni)
866 		return;
867 
868 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
869 	for (i = 0; i < shmalloced; i++)
870 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
871 	for (; i < shminfo.shmmni; i++) {
872 		newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
873 		newsegs[i].u.shm_perm.seq = 0;
874 #ifdef MAC
875 		mac_sysvshm_init(&newsegs[i]);
876 #endif
877 	}
878 	free(shmsegs, M_SHM);
879 	shmsegs = newsegs;
880 	shmalloced = shminfo.shmmni;
881 }
882 
883 static struct syscall_helper_data shm_syscalls[] = {
884 	SYSCALL_INIT_HELPER(shmat),
885 	SYSCALL_INIT_HELPER(shmctl),
886 	SYSCALL_INIT_HELPER(shmdt),
887 	SYSCALL_INIT_HELPER(shmget),
888 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
889     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
890 	SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
891 #endif
892 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
893 	SYSCALL_INIT_HELPER(shmsys),
894 #endif
895 	SYSCALL_INIT_LAST
896 };
897 
898 #ifdef COMPAT_FREEBSD32
899 #include <compat/freebsd32/freebsd32.h>
900 #include <compat/freebsd32/freebsd32_ipc.h>
901 #include <compat/freebsd32/freebsd32_proto.h>
902 #include <compat/freebsd32/freebsd32_signal.h>
903 #include <compat/freebsd32/freebsd32_syscall.h>
904 #include <compat/freebsd32/freebsd32_util.h>
905 
906 static struct syscall_helper_data shm32_syscalls[] = {
907 	SYSCALL32_INIT_HELPER_COMPAT(shmat),
908 	SYSCALL32_INIT_HELPER_COMPAT(shmdt),
909 	SYSCALL32_INIT_HELPER_COMPAT(shmget),
910 	SYSCALL32_INIT_HELPER(freebsd32_shmsys),
911 	SYSCALL32_INIT_HELPER(freebsd32_shmctl),
912 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
913     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
914 	SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
915 #endif
916 	SYSCALL_INIT_LAST
917 };
918 #endif
919 
920 static int
921 shminit(void)
922 {
923 	struct prison *pr;
924 	void **rsv;
925 	int i, error;
926 	osd_method_t methods[PR_MAXMETHOD] = {
927 	    [PR_METHOD_CHECK] =		shm_prison_check,
928 	    [PR_METHOD_SET] =		shm_prison_set,
929 	    [PR_METHOD_GET] =		shm_prison_get,
930 	    [PR_METHOD_REMOVE] =	shm_prison_remove,
931 	};
932 
933 #ifndef BURN_BRIDGES
934 	if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
935 		printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
936 #endif
937 	if (shminfo.shmmax == SHMMAX) {
938 		/* Initialize shmmax dealing with possible overflow. */
939 		for (i = PAGE_SIZE; i != 0; i--) {
940 			shminfo.shmmax = shminfo.shmall * i;
941 			if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
942 				break;
943 		}
944 	}
945 	shmalloced = shminfo.shmmni;
946 	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
947 	for (i = 0; i < shmalloced; i++) {
948 		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
949 		shmsegs[i].u.shm_perm.seq = 0;
950 #ifdef MAC
951 		mac_sysvshm_init(&shmsegs[i]);
952 #endif
953 	}
954 	shm_last_free = 0;
955 	shm_nused = 0;
956 	shm_committed = 0;
957 	sx_init(&sysvshmsx, "sysvshmsx");
958 	shmexit_hook = &shmexit_myhook;
959 	shmfork_hook = &shmfork_myhook;
960 
961 	/* Set current prisons according to their allow.sysvipc. */
962 	shm_prison_slot = osd_jail_register(NULL, methods);
963 	rsv = osd_reserve(shm_prison_slot);
964 	prison_lock(&prison0);
965 	(void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
966 	prison_unlock(&prison0);
967 	rsv = NULL;
968 	sx_slock(&allprison_lock);
969 	TAILQ_FOREACH(pr, &allprison, pr_list) {
970 		if (rsv == NULL)
971 			rsv = osd_reserve(shm_prison_slot);
972 		prison_lock(pr);
973 		if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
974 			(void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
975 			    &prison0);
976 			rsv = NULL;
977 		}
978 		prison_unlock(pr);
979 	}
980 	if (rsv != NULL)
981 		osd_free_reserved(rsv);
982 	sx_sunlock(&allprison_lock);
983 
984 	error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
985 	if (error != 0)
986 		return (error);
987 #ifdef COMPAT_FREEBSD32
988 	error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
989 	if (error != 0)
990 		return (error);
991 #endif
992 	return (0);
993 }
994 
995 static int
996 shmunload(void)
997 {
998 	int i;
999 
1000 	if (shm_nused > 0)
1001 		return (EBUSY);
1002 
1003 #ifdef COMPAT_FREEBSD32
1004 	syscall32_helper_unregister(shm32_syscalls);
1005 #endif
1006 	syscall_helper_unregister(shm_syscalls);
1007 	if (shm_prison_slot != 0)
1008 		osd_jail_deregister(shm_prison_slot);
1009 
1010 	for (i = 0; i < shmalloced; i++) {
1011 #ifdef MAC
1012 		mac_sysvshm_destroy(&shmsegs[i]);
1013 #endif
1014 		/*
1015 		 * Objects might be still mapped into the processes
1016 		 * address spaces.  Actual free would happen on the
1017 		 * last mapping destruction.
1018 		 */
1019 		if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1020 			vm_object_deallocate(shmsegs[i].object);
1021 	}
1022 	free(shmsegs, M_SHM);
1023 	shmexit_hook = NULL;
1024 	shmfork_hook = NULL;
1025 	sx_destroy(&sysvshmsx);
1026 	return (0);
1027 }
1028 
1029 static int
1030 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1031 {
1032 	struct shmid_kernel tshmseg;
1033 	struct prison *pr, *rpr;
1034 	int error, i;
1035 
1036 	SYSVSHM_LOCK();
1037 	pr = req->td->td_ucred->cr_prison;
1038 	rpr = shm_find_prison(req->td->td_ucred);
1039 	error = 0;
1040 	for (i = 0; i < shmalloced; i++) {
1041 		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1042 		    rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1043 			bzero(&tshmseg, sizeof(tshmseg));
1044 			tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1045 		} else {
1046 			tshmseg = shmsegs[i];
1047 			if (tshmseg.cred->cr_prison != pr)
1048 				tshmseg.u.shm_perm.key = IPC_PRIVATE;
1049 		}
1050 		error = SYSCTL_OUT(req, &tshmseg, sizeof(tshmseg));
1051 		if (error != 0)
1052 			break;
1053 	}
1054 	SYSVSHM_UNLOCK();
1055 	return (error);
1056 }
1057 
1058 static int
1059 shm_prison_check(void *obj, void *data)
1060 {
1061 	struct prison *pr = obj;
1062 	struct prison *prpr;
1063 	struct vfsoptlist *opts = data;
1064 	int error, jsys;
1065 
1066 	/*
1067 	 * sysvshm is a jailsys integer.
1068 	 * It must be "disable" if the parent jail is disabled.
1069 	 */
1070 	error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1071 	if (error != ENOENT) {
1072 		if (error != 0)
1073 			return (error);
1074 		switch (jsys) {
1075 		case JAIL_SYS_DISABLE:
1076 			break;
1077 		case JAIL_SYS_NEW:
1078 		case JAIL_SYS_INHERIT:
1079 			prison_lock(pr->pr_parent);
1080 			prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1081 			prison_unlock(pr->pr_parent);
1082 			if (prpr == NULL)
1083 				return (EPERM);
1084 			break;
1085 		default:
1086 			return (EINVAL);
1087 		}
1088 	}
1089 
1090 	return (0);
1091 }
1092 
1093 static int
1094 shm_prison_set(void *obj, void *data)
1095 {
1096 	struct prison *pr = obj;
1097 	struct prison *tpr, *orpr, *nrpr, *trpr;
1098 	struct vfsoptlist *opts = data;
1099 	void *rsv;
1100 	int jsys, descend;
1101 
1102 	/*
1103 	 * sysvshm controls which jail is the root of the associated segments
1104 	 * (this jail or same as the parent), or if the feature is available
1105 	 * at all.
1106 	 */
1107 	if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1108 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1109 		    ? JAIL_SYS_INHERIT
1110 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1111 		    ? JAIL_SYS_DISABLE
1112 		    : -1;
1113 	if (jsys == JAIL_SYS_DISABLE) {
1114 		prison_lock(pr);
1115 		orpr = osd_jail_get(pr, shm_prison_slot);
1116 		if (orpr != NULL)
1117 			osd_jail_del(pr, shm_prison_slot);
1118 		prison_unlock(pr);
1119 		if (orpr != NULL) {
1120 			if (orpr == pr)
1121 				shm_prison_cleanup(pr);
1122 			/* Disable all child jails as well. */
1123 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1124 				prison_lock(tpr);
1125 				trpr = osd_jail_get(tpr, shm_prison_slot);
1126 				if (trpr != NULL) {
1127 					osd_jail_del(tpr, shm_prison_slot);
1128 					prison_unlock(tpr);
1129 					if (trpr == tpr)
1130 						shm_prison_cleanup(tpr);
1131 				} else {
1132 					prison_unlock(tpr);
1133 					descend = 0;
1134 				}
1135 			}
1136 		}
1137 	} else if (jsys != -1) {
1138 		if (jsys == JAIL_SYS_NEW)
1139 			nrpr = pr;
1140 		else {
1141 			prison_lock(pr->pr_parent);
1142 			nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1143 			prison_unlock(pr->pr_parent);
1144 		}
1145 		rsv = osd_reserve(shm_prison_slot);
1146 		prison_lock(pr);
1147 		orpr = osd_jail_get(pr, shm_prison_slot);
1148 		if (orpr != nrpr)
1149 			(void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1150 			    nrpr);
1151 		else
1152 			osd_free_reserved(rsv);
1153 		prison_unlock(pr);
1154 		if (orpr != nrpr) {
1155 			if (orpr == pr)
1156 				shm_prison_cleanup(pr);
1157 			if (orpr != NULL) {
1158 				/* Change child jails matching the old root, */
1159 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1160 					prison_lock(tpr);
1161 					trpr = osd_jail_get(tpr,
1162 					    shm_prison_slot);
1163 					if (trpr == orpr) {
1164 						(void)osd_jail_set(tpr,
1165 						    shm_prison_slot, nrpr);
1166 						prison_unlock(tpr);
1167 						if (trpr == tpr)
1168 							shm_prison_cleanup(tpr);
1169 					} else {
1170 						prison_unlock(tpr);
1171 						descend = 0;
1172 					}
1173 				}
1174 			}
1175 		}
1176 	}
1177 
1178 	return (0);
1179 }
1180 
1181 static int
1182 shm_prison_get(void *obj, void *data)
1183 {
1184 	struct prison *pr = obj;
1185 	struct prison *rpr;
1186 	struct vfsoptlist *opts = data;
1187 	int error, jsys;
1188 
1189 	/* Set sysvshm based on the jail's root prison. */
1190 	prison_lock(pr);
1191 	rpr = osd_jail_get(pr, shm_prison_slot);
1192 	prison_unlock(pr);
1193 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1194 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1195 	error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1196 	if (error == ENOENT)
1197 		error = 0;
1198 	return (error);
1199 }
1200 
1201 static int
1202 shm_prison_remove(void *obj, void *data __unused)
1203 {
1204 	struct prison *pr = obj;
1205 	struct prison *rpr;
1206 
1207 	SYSVSHM_LOCK();
1208 	prison_lock(pr);
1209 	rpr = osd_jail_get(pr, shm_prison_slot);
1210 	prison_unlock(pr);
1211 	if (rpr == pr)
1212 		shm_prison_cleanup(pr);
1213 	SYSVSHM_UNLOCK();
1214 	return (0);
1215 }
1216 
1217 static void
1218 shm_prison_cleanup(struct prison *pr)
1219 {
1220 	struct shmid_kernel *shmseg;
1221 	int i;
1222 
1223 	/* Remove any segments that belong to this jail. */
1224 	for (i = 0; i < shmalloced; i++) {
1225 		shmseg = &shmsegs[i];
1226 		if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1227 		    shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1228 			shm_remove(shmseg, i);
1229 		}
1230 	}
1231 }
1232 
1233 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1234 
1235 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1236 struct oshmid_ds {
1237 	struct	ipc_perm_old shm_perm;	/* operation perms */
1238 	int	shm_segsz;		/* size of segment (bytes) */
1239 	u_short	shm_cpid;		/* pid, creator */
1240 	u_short	shm_lpid;		/* pid, last operation */
1241 	short	shm_nattch;		/* no. of current attaches */
1242 	time_t	shm_atime;		/* last attach time */
1243 	time_t	shm_dtime;		/* last detach time */
1244 	time_t	shm_ctime;		/* last change time */
1245 	void	*shm_handle;		/* internal handle for shm segment */
1246 };
1247 
1248 struct oshmctl_args {
1249 	int shmid;
1250 	int cmd;
1251 	struct oshmid_ds *ubuf;
1252 };
1253 
1254 static int
1255 oshmctl(struct thread *td, struct oshmctl_args *uap)
1256 {
1257 #ifdef COMPAT_43
1258 	int error = 0;
1259 	struct prison *rpr;
1260 	struct shmid_kernel *shmseg;
1261 	struct oshmid_ds outbuf;
1262 
1263 	rpr = shm_find_prison(td->td_ucred);
1264 	if (rpr == NULL)
1265 		return (ENOSYS);
1266 	if (uap->cmd != IPC_STAT) {
1267 		return (freebsd7_shmctl(td,
1268 		    (struct freebsd7_shmctl_args *)uap));
1269 	}
1270 	SYSVSHM_LOCK();
1271 	shmseg = shm_find_segment(rpr, uap->shmid, true);
1272 	if (shmseg == NULL) {
1273 		SYSVSHM_UNLOCK();
1274 		return (EINVAL);
1275 	}
1276 	error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1277 	if (error != 0) {
1278 		SYSVSHM_UNLOCK();
1279 		return (error);
1280 	}
1281 #ifdef MAC
1282 	error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1283 	if (error != 0) {
1284 		SYSVSHM_UNLOCK();
1285 		return (error);
1286 	}
1287 #endif
1288 	ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1289 	outbuf.shm_segsz = shmseg->u.shm_segsz;
1290 	outbuf.shm_cpid = shmseg->u.shm_cpid;
1291 	outbuf.shm_lpid = shmseg->u.shm_lpid;
1292 	outbuf.shm_nattch = shmseg->u.shm_nattch;
1293 	outbuf.shm_atime = shmseg->u.shm_atime;
1294 	outbuf.shm_dtime = shmseg->u.shm_dtime;
1295 	outbuf.shm_ctime = shmseg->u.shm_ctime;
1296 	outbuf.shm_handle = shmseg->object;
1297 	SYSVSHM_UNLOCK();
1298 	return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1299 #else
1300 	return (EINVAL);
1301 #endif
1302 }
1303 
1304 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1305 static sy_call_t *shmcalls[] = {
1306 	(sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1307 	(sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1308 	(sy_call_t *)freebsd7_shmctl
1309 };
1310 
1311 #ifndef _SYS_SYSPROTO_H_
1312 /* XXX actually varargs. */
1313 struct shmsys_args {
1314 	int	which;
1315 	int	a2;
1316 	int	a3;
1317 	int	a4;
1318 };
1319 #endif
1320 int
1321 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1322 {
1323 
1324 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1325 	if (uap->which < 0 || uap->which >= nitems(shmcalls))
1326 		return (EINVAL);
1327 	return ((*shmcalls[uap->which])(td, &uap->a2));
1328 }
1329 
1330 #endif	/* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1331 
1332 #ifdef COMPAT_FREEBSD32
1333 
1334 int
1335 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1336 {
1337 
1338 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1339     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1340 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1341 	switch (uap->which) {
1342 	case 0:	{	/* shmat */
1343 		struct shmat_args ap;
1344 
1345 		ap.shmid = uap->a2;
1346 		ap.shmaddr = PTRIN(uap->a3);
1347 		ap.shmflg = uap->a4;
1348 		return (sysent[SYS_shmat].sy_call(td, &ap));
1349 	}
1350 	case 2: {	/* shmdt */
1351 		struct shmdt_args ap;
1352 
1353 		ap.shmaddr = PTRIN(uap->a2);
1354 		return (sysent[SYS_shmdt].sy_call(td, &ap));
1355 	}
1356 	case 3: {	/* shmget */
1357 		struct shmget_args ap;
1358 
1359 		ap.key = uap->a2;
1360 		ap.size = uap->a3;
1361 		ap.shmflg = uap->a4;
1362 		return (sysent[SYS_shmget].sy_call(td, &ap));
1363 	}
1364 	case 4: {	/* shmctl */
1365 		struct freebsd7_freebsd32_shmctl_args ap;
1366 
1367 		ap.shmid = uap->a2;
1368 		ap.cmd = uap->a3;
1369 		ap.buf = PTRIN(uap->a4);
1370 		return (freebsd7_freebsd32_shmctl(td, &ap));
1371 	}
1372 	case 1:		/* oshmctl */
1373 	default:
1374 		return (EINVAL);
1375 	}
1376 #else
1377 	return (nosys(td, NULL));
1378 #endif
1379 }
1380 
1381 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1382     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1383 int
1384 freebsd7_freebsd32_shmctl(struct thread *td,
1385     struct freebsd7_freebsd32_shmctl_args *uap)
1386 {
1387 	int error;
1388 	union {
1389 		struct shmid_ds shmid_ds;
1390 		struct shm_info shm_info;
1391 		struct shminfo shminfo;
1392 	} u;
1393 	union {
1394 		struct shmid_ds32_old shmid_ds32;
1395 		struct shm_info32 shm_info32;
1396 		struct shminfo32 shminfo32;
1397 	} u32;
1398 	size_t sz;
1399 
1400 	if (uap->cmd == IPC_SET) {
1401 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1402 		    sizeof(u32.shmid_ds32))))
1403 			goto done;
1404 		freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1405 		    &u.shmid_ds.shm_perm);
1406 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1407 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1408 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1409 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1410 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1411 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1412 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1413 	}
1414 
1415 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1416 	if (error)
1417 		goto done;
1418 
1419 	/* Cases in which we need to copyout */
1420 	switch (uap->cmd) {
1421 	case IPC_INFO:
1422 		CP(u.shminfo, u32.shminfo32, shmmax);
1423 		CP(u.shminfo, u32.shminfo32, shmmin);
1424 		CP(u.shminfo, u32.shminfo32, shmmni);
1425 		CP(u.shminfo, u32.shminfo32, shmseg);
1426 		CP(u.shminfo, u32.shminfo32, shmall);
1427 		error = copyout(&u32.shminfo32, uap->buf,
1428 		    sizeof(u32.shminfo32));
1429 		break;
1430 	case SHM_INFO:
1431 		CP(u.shm_info, u32.shm_info32, used_ids);
1432 		CP(u.shm_info, u32.shm_info32, shm_rss);
1433 		CP(u.shm_info, u32.shm_info32, shm_tot);
1434 		CP(u.shm_info, u32.shm_info32, shm_swp);
1435 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1436 		CP(u.shm_info, u32.shm_info32, swap_successes);
1437 		error = copyout(&u32.shm_info32, uap->buf,
1438 		    sizeof(u32.shm_info32));
1439 		break;
1440 	case SHM_STAT:
1441 	case IPC_STAT:
1442 		freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1443 		    &u32.shmid_ds32.shm_perm);
1444 		if (u.shmid_ds.shm_segsz > INT32_MAX)
1445 			u32.shmid_ds32.shm_segsz = INT32_MAX;
1446 		else
1447 			CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1448 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1449 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1450 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1451 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1452 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1453 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1454 		u32.shmid_ds32.shm_internal = 0;
1455 		error = copyout(&u32.shmid_ds32, uap->buf,
1456 		    sizeof(u32.shmid_ds32));
1457 		break;
1458 	}
1459 
1460 done:
1461 	if (error) {
1462 		/* Invalidate the return value */
1463 		td->td_retval[0] = -1;
1464 	}
1465 	return (error);
1466 }
1467 #endif
1468 
1469 int
1470 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1471 {
1472 	int error;
1473 	union {
1474 		struct shmid_ds shmid_ds;
1475 		struct shm_info shm_info;
1476 		struct shminfo shminfo;
1477 	} u;
1478 	union {
1479 		struct shmid_ds32 shmid_ds32;
1480 		struct shm_info32 shm_info32;
1481 		struct shminfo32 shminfo32;
1482 	} u32;
1483 	size_t sz;
1484 
1485 	if (uap->cmd == IPC_SET) {
1486 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1487 		    sizeof(u32.shmid_ds32))))
1488 			goto done;
1489 		freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1490 		    &u.shmid_ds.shm_perm);
1491 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1492 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1493 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1494 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1495 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1496 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1497 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1498 	}
1499 
1500 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1501 	if (error)
1502 		goto done;
1503 
1504 	/* Cases in which we need to copyout */
1505 	switch (uap->cmd) {
1506 	case IPC_INFO:
1507 		CP(u.shminfo, u32.shminfo32, shmmax);
1508 		CP(u.shminfo, u32.shminfo32, shmmin);
1509 		CP(u.shminfo, u32.shminfo32, shmmni);
1510 		CP(u.shminfo, u32.shminfo32, shmseg);
1511 		CP(u.shminfo, u32.shminfo32, shmall);
1512 		error = copyout(&u32.shminfo32, uap->buf,
1513 		    sizeof(u32.shminfo32));
1514 		break;
1515 	case SHM_INFO:
1516 		CP(u.shm_info, u32.shm_info32, used_ids);
1517 		CP(u.shm_info, u32.shm_info32, shm_rss);
1518 		CP(u.shm_info, u32.shm_info32, shm_tot);
1519 		CP(u.shm_info, u32.shm_info32, shm_swp);
1520 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1521 		CP(u.shm_info, u32.shm_info32, swap_successes);
1522 		error = copyout(&u32.shm_info32, uap->buf,
1523 		    sizeof(u32.shm_info32));
1524 		break;
1525 	case SHM_STAT:
1526 	case IPC_STAT:
1527 		freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1528 		    &u32.shmid_ds32.shm_perm);
1529 		if (u.shmid_ds.shm_segsz > INT32_MAX)
1530 			u32.shmid_ds32.shm_segsz = INT32_MAX;
1531 		else
1532 			CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1533 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1534 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1535 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1536 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1537 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1538 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1539 		error = copyout(&u32.shmid_ds32, uap->buf,
1540 		    sizeof(u32.shmid_ds32));
1541 		break;
1542 	}
1543 
1544 done:
1545 	if (error) {
1546 		/* Invalidate the return value */
1547 		td->td_retval[0] = -1;
1548 	}
1549 	return (error);
1550 }
1551 #endif
1552 
1553 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1554     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1555 
1556 #ifndef CP
1557 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1558 #endif
1559 
1560 #ifndef _SYS_SYSPROTO_H_
1561 struct freebsd7_shmctl_args {
1562 	int shmid;
1563 	int cmd;
1564 	struct shmid_ds_old *buf;
1565 };
1566 #endif
1567 int
1568 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1569 {
1570 	int error;
1571 	struct shmid_ds_old old;
1572 	struct shmid_ds buf;
1573 	size_t bufsz;
1574 
1575 	/*
1576 	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1577 	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
1578 	 * return an error back to the user since we do not to support this.
1579 	 */
1580 	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1581 	    uap->cmd == SHM_STAT)
1582 		return (EINVAL);
1583 
1584 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1585 	if (uap->cmd == IPC_SET) {
1586 		if ((error = copyin(uap->buf, &old, sizeof(old))))
1587 			goto done;
1588 		ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1589 		CP(old, buf, shm_segsz);
1590 		CP(old, buf, shm_lpid);
1591 		CP(old, buf, shm_cpid);
1592 		CP(old, buf, shm_nattch);
1593 		CP(old, buf, shm_atime);
1594 		CP(old, buf, shm_dtime);
1595 		CP(old, buf, shm_ctime);
1596 	}
1597 
1598 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1599 	if (error)
1600 		goto done;
1601 
1602 	/* Cases in which we need to copyout */
1603 	switch (uap->cmd) {
1604 	case IPC_STAT:
1605 		ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1606 		if (buf.shm_segsz > INT_MAX)
1607 			old.shm_segsz = INT_MAX;
1608 		else
1609 			CP(buf, old, shm_segsz);
1610 		CP(buf, old, shm_lpid);
1611 		CP(buf, old, shm_cpid);
1612 		if (buf.shm_nattch > SHRT_MAX)
1613 			old.shm_nattch = SHRT_MAX;
1614 		else
1615 			CP(buf, old, shm_nattch);
1616 		CP(buf, old, shm_atime);
1617 		CP(buf, old, shm_dtime);
1618 		CP(buf, old, shm_ctime);
1619 		old.shm_internal = NULL;
1620 		error = copyout(&old, uap->buf, sizeof(old));
1621 		break;
1622 	}
1623 
1624 done:
1625 	if (error) {
1626 		/* Invalidate the return value */
1627 		td->td_retval[0] = -1;
1628 	}
1629 	return (error);
1630 }
1631 
1632 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1633 	   COMPAT_FREEBSD7 */
1634 
1635 static int
1636 sysvshm_modload(struct module *module, int cmd, void *arg)
1637 {
1638 	int error = 0;
1639 
1640 	switch (cmd) {
1641 	case MOD_LOAD:
1642 		error = shminit();
1643 		if (error != 0)
1644 			shmunload();
1645 		break;
1646 	case MOD_UNLOAD:
1647 		error = shmunload();
1648 		break;
1649 	case MOD_SHUTDOWN:
1650 		break;
1651 	default:
1652 		error = EINVAL;
1653 		break;
1654 	}
1655 	return (error);
1656 }
1657 
1658 static moduledata_t sysvshm_mod = {
1659 	"sysvshm",
1660 	&sysvshm_modload,
1661 	NULL
1662 };
1663 
1664 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1665 MODULE_VERSION(sysvshm, 1);
1666