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