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