xref: /freebsd/sys/kern/sysv_shm.c (revision 036d2e814bf0f5d88ffb4b24c159320894541757)
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_sysvipc.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/limits.h>
80 #include <sys/lock.h>
81 #include <sys/sysctl.h>
82 #include <sys/shm.h>
83 #include <sys/proc.h>
84 #include <sys/malloc.h>
85 #include <sys/mman.h>
86 #include <sys/module.h>
87 #include <sys/mutex.h>
88 #include <sys/racct.h>
89 #include <sys/resourcevar.h>
90 #include <sys/rwlock.h>
91 #include <sys/stat.h>
92 #include <sys/syscall.h>
93 #include <sys/syscallsubr.h>
94 #include <sys/sysent.h>
95 #include <sys/sysproto.h>
96 #include <sys/jail.h>
97 
98 #include <security/audit/audit.h>
99 #include <security/mac/mac_framework.h>
100 
101 #include <vm/vm.h>
102 #include <vm/vm_param.h>
103 #include <vm/pmap.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_page.h>
107 #include <vm/vm_pager.h>
108 
109 FEATURE(sysv_shm, "System V shared memory segments support");
110 
111 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
112 
113 static int shmget_allocate_segment(struct thread *td,
114     struct shmget_args *uap, int mode);
115 static int shmget_existing(struct thread *td, struct shmget_args *uap,
116     int mode, int segnum);
117 
118 #define	SHMSEG_FREE     	0x0200
119 #define	SHMSEG_REMOVED  	0x0400
120 #define	SHMSEG_ALLOCATED	0x0800
121 
122 static int shm_last_free, shm_nused, shmalloced;
123 vm_size_t shm_committed;
124 static struct shmid_kernel *shmsegs;
125 static unsigned shm_prison_slot;
126 
127 struct shmmap_state {
128 	vm_offset_t va;
129 	int shmid;
130 };
131 
132 static void shm_deallocate_segment(struct shmid_kernel *);
133 static int shm_find_segment_by_key(struct prison *, key_t);
134 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
135 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
136 static void shmrealloc(void);
137 static int shminit(void);
138 static int sysvshm_modload(struct module *, int, void *);
139 static int shmunload(void);
140 #ifndef SYSVSHM
141 static void shmexit_myhook(struct vmspace *vm);
142 static void shmfork_myhook(struct proc *p1, struct proc *p2);
143 #endif
144 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
145 static void shm_remove(struct shmid_kernel *, int);
146 static struct prison *shm_find_prison(struct ucred *);
147 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
148 static int shm_prison_check(void *, void *);
149 static int shm_prison_set(void *, void *);
150 static int shm_prison_get(void *, void *);
151 static int shm_prison_remove(void *, void *);
152 static void shm_prison_cleanup(struct prison *);
153 
154 /*
155  * Tuneable values.
156  */
157 #ifndef SHMMAXPGS
158 #define	SHMMAXPGS	131072	/* Note: sysv shared memory is swap backed. */
159 #endif
160 #ifndef SHMMAX
161 #define	SHMMAX	(SHMMAXPGS*PAGE_SIZE)
162 #endif
163 #ifndef SHMMIN
164 #define	SHMMIN	1
165 #endif
166 #ifndef SHMMNI
167 #define	SHMMNI	192
168 #endif
169 #ifndef SHMSEG
170 #define	SHMSEG	128
171 #endif
172 #ifndef SHMALL
173 #define	SHMALL	(SHMMAXPGS)
174 #endif
175 
176 struct	shminfo shminfo = {
177 	.shmmax = SHMMAX,
178 	.shmmin = SHMMIN,
179 	.shmmni = SHMMNI,
180 	.shmseg = SHMSEG,
181 	.shmall = SHMALL
182 };
183 
184 static int shm_use_phys;
185 static int shm_allow_removed = 1;
186 
187 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0,
188     "Maximum shared memory segment size");
189 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0,
190     "Minimum shared memory segment size");
191 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
192     "Number of shared memory identifiers");
193 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
194     "Number of segments per process");
195 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0,
196     "Maximum number of pages available for shared memory");
197 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN,
198     &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
199 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN,
200     &shm_allow_removed, 0,
201     "Enable/Disable attachment to attached segments marked for removal");
202 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
203     CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
204     "Array of struct shmid_kernel for each potential shared memory segment");
205 
206 static struct sx sysvshmsx;
207 #define	SYSVSHM_LOCK()		sx_xlock(&sysvshmsx)
208 #define	SYSVSHM_UNLOCK()	sx_xunlock(&sysvshmsx)
209 #define	SYSVSHM_ASSERT_LOCKED()	sx_assert(&sysvshmsx, SA_XLOCKED)
210 
211 static int
212 shm_find_segment_by_key(struct prison *pr, key_t key)
213 {
214 	int i;
215 
216 	for (i = 0; i < shmalloced; i++)
217 		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
218 		    shmsegs[i].cred != NULL &&
219 		    shmsegs[i].cred->cr_prison == pr &&
220 		    shmsegs[i].u.shm_perm.key == key)
221 			return (i);
222 	return (-1);
223 }
224 
225 /*
226  * Finds segment either by shmid if is_shmid is true, or by segnum if
227  * is_shmid is false.
228  */
229 static struct shmid_kernel *
230 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
231 {
232 	struct shmid_kernel *shmseg;
233 	int segnum;
234 
235 	segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
236 	if (segnum < 0 || segnum >= shmalloced)
237 		return (NULL);
238 	shmseg = &shmsegs[segnum];
239 	if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
240 	    (!shm_allow_removed &&
241 	    (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
242 	    (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
243 	    shm_prison_cansee(rpr, shmseg) != 0)
244 		return (NULL);
245 	return (shmseg);
246 }
247 
248 static void
249 shm_deallocate_segment(struct shmid_kernel *shmseg)
250 {
251 	vm_size_t size;
252 
253 	SYSVSHM_ASSERT_LOCKED();
254 
255 	vm_object_deallocate(shmseg->object);
256 	shmseg->object = NULL;
257 	size = round_page(shmseg->u.shm_segsz);
258 	shm_committed -= btoc(size);
259 	shm_nused--;
260 	shmseg->u.shm_perm.mode = SHMSEG_FREE;
261 #ifdef MAC
262 	mac_sysvshm_cleanup(shmseg);
263 #endif
264 	racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
265 	racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
266 	crfree(shmseg->cred);
267 	shmseg->cred = NULL;
268 }
269 
270 static int
271 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
272 {
273 	struct shmid_kernel *shmseg;
274 	int segnum, result;
275 	vm_size_t size;
276 
277 	SYSVSHM_ASSERT_LOCKED();
278 	segnum = IPCID_TO_IX(shmmap_s->shmid);
279 	KASSERT(segnum >= 0 && segnum < shmalloced,
280 	    ("segnum %d shmalloced %d", segnum, shmalloced));
281 
282 	shmseg = &shmsegs[segnum];
283 	size = round_page(shmseg->u.shm_segsz);
284 	result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
285 	if (result != KERN_SUCCESS)
286 		return (EINVAL);
287 	shmmap_s->shmid = -1;
288 	shmseg->u.shm_dtime = time_second;
289 	if (--shmseg->u.shm_nattch == 0 &&
290 	    (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
291 		shm_deallocate_segment(shmseg);
292 		shm_last_free = segnum;
293 	}
294 	return (0);
295 }
296 
297 static void
298 shm_remove(struct shmid_kernel *shmseg, int segnum)
299 {
300 
301 	shmseg->u.shm_perm.key = IPC_PRIVATE;
302 	shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
303 	if (shmseg->u.shm_nattch == 0) {
304 		shm_deallocate_segment(shmseg);
305 		shm_last_free = segnum;
306 	}
307 }
308 
309 static struct prison *
310 shm_find_prison(struct ucred *cred)
311 {
312 	struct prison *pr, *rpr;
313 
314 	pr = cred->cr_prison;
315 	prison_lock(pr);
316 	rpr = osd_jail_get(pr, shm_prison_slot);
317 	prison_unlock(pr);
318 	return rpr;
319 }
320 
321 static int
322 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
323 {
324 
325 	if (shmseg->cred == NULL ||
326 	    !(rpr == shmseg->cred->cr_prison ||
327 	      prison_ischild(rpr, shmseg->cred->cr_prison)))
328 		return (EINVAL);
329 	return (0);
330 }
331 
332 static int
333 kern_shmdt_locked(struct thread *td, const void *shmaddr)
334 {
335 	struct proc *p = td->td_proc;
336 	struct shmmap_state *shmmap_s;
337 #ifdef MAC
338 	int error;
339 #endif
340 	int i;
341 
342 	SYSVSHM_ASSERT_LOCKED();
343 	if (shm_find_prison(td->td_ucred) == NULL)
344 		return (ENOSYS);
345 	shmmap_s = p->p_vmspace->vm_shm;
346  	if (shmmap_s == NULL)
347 		return (EINVAL);
348 	AUDIT_ARG_SVIPC_ID(shmmap_s->shmid);
349 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
350 		if (shmmap_s->shmid != -1 &&
351 		    shmmap_s->va == (vm_offset_t)shmaddr) {
352 			break;
353 		}
354 	}
355 	if (i == shminfo.shmseg)
356 		return (EINVAL);
357 #ifdef MAC
358 	error = mac_sysvshm_check_shmdt(td->td_ucred,
359 	    &shmsegs[IPCID_TO_IX(shmmap_s->shmid)]);
360 	if (error != 0)
361 		return (error);
362 #endif
363 	return (shm_delete_mapping(p->p_vmspace, shmmap_s));
364 }
365 
366 #ifndef _SYS_SYSPROTO_H_
367 struct shmdt_args {
368 	const void *shmaddr;
369 };
370 #endif
371 int
372 sys_shmdt(struct thread *td, struct shmdt_args *uap)
373 {
374 	int error;
375 
376 	SYSVSHM_LOCK();
377 	error = kern_shmdt_locked(td, uap->shmaddr);
378 	SYSVSHM_UNLOCK();
379 	return (error);
380 }
381 
382 static int
383 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
384     int shmflg)
385 {
386 	struct prison *rpr;
387 	struct proc *p = td->td_proc;
388 	struct shmid_kernel *shmseg;
389 	struct shmmap_state *shmmap_s;
390 	vm_offset_t attach_va;
391 	vm_prot_t prot;
392 	vm_size_t size;
393 	int cow, error, find_space, i, rv;
394 
395 	AUDIT_ARG_SVIPC_ID(shmid);
396 	AUDIT_ARG_VALUE(shmflg);
397 
398 	SYSVSHM_ASSERT_LOCKED();
399 	rpr = shm_find_prison(td->td_ucred);
400 	if (rpr == NULL)
401 		return (ENOSYS);
402 	shmmap_s = p->p_vmspace->vm_shm;
403 	if (shmmap_s == NULL) {
404 		shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
405 		    M_SHM, M_WAITOK);
406 		for (i = 0; i < shminfo.shmseg; i++)
407 			shmmap_s[i].shmid = -1;
408 		KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
409 		p->p_vmspace->vm_shm = shmmap_s;
410 	}
411 	shmseg = shm_find_segment(rpr, shmid, true);
412 	if (shmseg == NULL)
413 		return (EINVAL);
414 	error = ipcperm(td, &shmseg->u.shm_perm,
415 	    (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
416 	if (error != 0)
417 		return (error);
418 #ifdef MAC
419 	error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
420 	if (error != 0)
421 		return (error);
422 #endif
423 	for (i = 0; i < shminfo.shmseg; i++) {
424 		if (shmmap_s->shmid == -1)
425 			break;
426 		shmmap_s++;
427 	}
428 	if (i >= shminfo.shmseg)
429 		return (EMFILE);
430 	size = round_page(shmseg->u.shm_segsz);
431 	prot = VM_PROT_READ;
432 	cow = MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL;
433 	if ((shmflg & SHM_RDONLY) == 0)
434 		prot |= VM_PROT_WRITE;
435 	if (shmaddr != NULL) {
436 		if ((shmflg & SHM_RND) != 0)
437 			attach_va = rounddown2((vm_offset_t)shmaddr, SHMLBA);
438 		else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
439 			attach_va = (vm_offset_t)shmaddr;
440 		else
441 			return (EINVAL);
442 		if ((shmflg & SHM_REMAP) != 0)
443 			cow |= MAP_REMAP;
444 		find_space = VMFS_NO_SPACE;
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 		find_space = VMFS_OPTIMAL_SPACE;
453 	}
454 
455 	vm_object_reference(shmseg->object);
456 	rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va,
457 	    size, 0, find_space, prot, prot, cow);
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 #ifdef SYSVSHM
816 void
817 shmfork(struct proc *p1, struct proc *p2)
818 #else
819 static void
820 shmfork_myhook(struct proc *p1, struct proc *p2)
821 #endif
822 {
823 	struct shmmap_state *shmmap_s;
824 	size_t size;
825 	int i;
826 
827 	SYSVSHM_LOCK();
828 	size = shminfo.shmseg * sizeof(struct shmmap_state);
829 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
830 	bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
831 	p2->p_vmspace->vm_shm = shmmap_s;
832 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
833 		if (shmmap_s->shmid != -1) {
834 			KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
835 			    IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
836 			    ("segnum %d shmalloced %d",
837 			    IPCID_TO_IX(shmmap_s->shmid), shmalloced));
838 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
839 		}
840 	}
841 	SYSVSHM_UNLOCK();
842 }
843 
844 #ifdef SYSVSHM
845 void
846 shmexit(struct vmspace *vm)
847 #else
848 static void
849 shmexit_myhook(struct vmspace *vm)
850 #endif
851 {
852 	struct shmmap_state *base, *shm;
853 	int i;
854 
855 	base = vm->vm_shm;
856 	if (base != NULL) {
857 		vm->vm_shm = NULL;
858 		SYSVSHM_LOCK();
859 		for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
860 			if (shm->shmid != -1)
861 				shm_delete_mapping(vm, shm);
862 		}
863 		SYSVSHM_UNLOCK();
864 		free(base, M_SHM);
865 	}
866 }
867 
868 static void
869 shmrealloc(void)
870 {
871 	struct shmid_kernel *newsegs;
872 	int i;
873 
874 	SYSVSHM_ASSERT_LOCKED();
875 
876 	if (shmalloced >= shminfo.shmmni)
877 		return;
878 
879 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
880 	    M_WAITOK | M_ZERO);
881 	for (i = 0; i < shmalloced; i++)
882 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
883 	for (; i < shminfo.shmmni; i++) {
884 		newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
885 		newsegs[i].u.shm_perm.seq = 0;
886 #ifdef MAC
887 		mac_sysvshm_init(&newsegs[i]);
888 #endif
889 	}
890 	free(shmsegs, M_SHM);
891 	shmsegs = newsegs;
892 	shmalloced = shminfo.shmmni;
893 }
894 
895 static struct syscall_helper_data shm_syscalls[] = {
896 	SYSCALL_INIT_HELPER(shmat),
897 	SYSCALL_INIT_HELPER(shmctl),
898 	SYSCALL_INIT_HELPER(shmdt),
899 	SYSCALL_INIT_HELPER(shmget),
900 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
901     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
902 	SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
903 #endif
904 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
905 	SYSCALL_INIT_HELPER(shmsys),
906 #endif
907 	SYSCALL_INIT_LAST
908 };
909 
910 #ifdef COMPAT_FREEBSD32
911 #include <compat/freebsd32/freebsd32.h>
912 #include <compat/freebsd32/freebsd32_ipc.h>
913 #include <compat/freebsd32/freebsd32_proto.h>
914 #include <compat/freebsd32/freebsd32_signal.h>
915 #include <compat/freebsd32/freebsd32_syscall.h>
916 #include <compat/freebsd32/freebsd32_util.h>
917 
918 static struct syscall_helper_data shm32_syscalls[] = {
919 	SYSCALL32_INIT_HELPER_COMPAT(shmat),
920 	SYSCALL32_INIT_HELPER_COMPAT(shmdt),
921 	SYSCALL32_INIT_HELPER_COMPAT(shmget),
922 	SYSCALL32_INIT_HELPER(freebsd32_shmsys),
923 	SYSCALL32_INIT_HELPER(freebsd32_shmctl),
924 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
925     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
926 	SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
927 #endif
928 	SYSCALL_INIT_LAST
929 };
930 #endif
931 
932 static int
933 shminit(void)
934 {
935 	struct prison *pr;
936 	void **rsv;
937 	int i, error;
938 	osd_method_t methods[PR_MAXMETHOD] = {
939 	    [PR_METHOD_CHECK] =		shm_prison_check,
940 	    [PR_METHOD_SET] =		shm_prison_set,
941 	    [PR_METHOD_GET] =		shm_prison_get,
942 	    [PR_METHOD_REMOVE] =	shm_prison_remove,
943 	};
944 
945 #ifndef BURN_BRIDGES
946 	if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
947 		printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
948 #endif
949 	if (shminfo.shmmax == SHMMAX) {
950 		/* Initialize shmmax dealing with possible overflow. */
951 		for (i = PAGE_SIZE; i != 0; i--) {
952 			shminfo.shmmax = shminfo.shmall * i;
953 			if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
954 				break;
955 		}
956 	}
957 	shmalloced = shminfo.shmmni;
958 	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
959 	    M_WAITOK|M_ZERO);
960 	for (i = 0; i < shmalloced; i++) {
961 		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
962 		shmsegs[i].u.shm_perm.seq = 0;
963 #ifdef MAC
964 		mac_sysvshm_init(&shmsegs[i]);
965 #endif
966 	}
967 	shm_last_free = 0;
968 	shm_nused = 0;
969 	shm_committed = 0;
970 	sx_init(&sysvshmsx, "sysvshmsx");
971 #ifndef SYSVSHM
972 	shmexit_hook = &shmexit_myhook;
973 	shmfork_hook = &shmfork_myhook;
974 #endif
975 
976 	/* Set current prisons according to their allow.sysvipc. */
977 	shm_prison_slot = osd_jail_register(NULL, methods);
978 	rsv = osd_reserve(shm_prison_slot);
979 	prison_lock(&prison0);
980 	(void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
981 	prison_unlock(&prison0);
982 	rsv = NULL;
983 	sx_slock(&allprison_lock);
984 	TAILQ_FOREACH(pr, &allprison, pr_list) {
985 		if (rsv == NULL)
986 			rsv = osd_reserve(shm_prison_slot);
987 		prison_lock(pr);
988 		if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
989 			(void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
990 			    &prison0);
991 			rsv = NULL;
992 		}
993 		prison_unlock(pr);
994 	}
995 	if (rsv != NULL)
996 		osd_free_reserved(rsv);
997 	sx_sunlock(&allprison_lock);
998 
999 	error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
1000 	if (error != 0)
1001 		return (error);
1002 #ifdef COMPAT_FREEBSD32
1003 	error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
1004 	if (error != 0)
1005 		return (error);
1006 #endif
1007 	return (0);
1008 }
1009 
1010 static int
1011 shmunload(void)
1012 {
1013 	int i;
1014 
1015 	if (shm_nused > 0)
1016 		return (EBUSY);
1017 
1018 #ifdef COMPAT_FREEBSD32
1019 	syscall32_helper_unregister(shm32_syscalls);
1020 #endif
1021 	syscall_helper_unregister(shm_syscalls);
1022 	if (shm_prison_slot != 0)
1023 		osd_jail_deregister(shm_prison_slot);
1024 
1025 	for (i = 0; i < shmalloced; i++) {
1026 #ifdef MAC
1027 		mac_sysvshm_destroy(&shmsegs[i]);
1028 #endif
1029 		/*
1030 		 * Objects might be still mapped into the processes
1031 		 * address spaces.  Actual free would happen on the
1032 		 * last mapping destruction.
1033 		 */
1034 		if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1035 			vm_object_deallocate(shmsegs[i].object);
1036 	}
1037 	free(shmsegs, M_SHM);
1038 #ifndef SYSVSHM
1039 	shmexit_hook = NULL;
1040 	shmfork_hook = NULL;
1041 #endif
1042 	sx_destroy(&sysvshmsx);
1043 	return (0);
1044 }
1045 
1046 static int
1047 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1048 {
1049 	struct shmid_kernel tshmseg;
1050 #ifdef COMPAT_FREEBSD32
1051 	struct shmid_kernel32 tshmseg32;
1052 #endif
1053 	struct prison *pr, *rpr;
1054 	void *outaddr;
1055 	size_t outsize;
1056 	int error, i;
1057 
1058 	SYSVSHM_LOCK();
1059 	pr = req->td->td_ucred->cr_prison;
1060 	rpr = shm_find_prison(req->td->td_ucred);
1061 	error = 0;
1062 	for (i = 0; i < shmalloced; i++) {
1063 		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1064 		    rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1065 			bzero(&tshmseg, sizeof(tshmseg));
1066 			tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1067 		} else {
1068 			tshmseg = shmsegs[i];
1069 			if (tshmseg.cred->cr_prison != pr)
1070 				tshmseg.u.shm_perm.key = IPC_PRIVATE;
1071 		}
1072 #ifdef COMPAT_FREEBSD32
1073 		if (SV_CURPROC_FLAG(SV_ILP32)) {
1074 			bzero(&tshmseg32, sizeof(tshmseg32));
1075 			freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1076 			    &tshmseg32.u.shm_perm);
1077 			CP(tshmseg, tshmseg32, u.shm_segsz);
1078 			CP(tshmseg, tshmseg32, u.shm_lpid);
1079 			CP(tshmseg, tshmseg32, u.shm_cpid);
1080 			CP(tshmseg, tshmseg32, u.shm_nattch);
1081 			CP(tshmseg, tshmseg32, u.shm_atime);
1082 			CP(tshmseg, tshmseg32, u.shm_dtime);
1083 			CP(tshmseg, tshmseg32, u.shm_ctime);
1084 			/* Don't copy object, label, or cred */
1085 			outaddr = &tshmseg32;
1086 			outsize = sizeof(tshmseg32);
1087 		} else
1088 #endif
1089 		{
1090 			tshmseg.object = NULL;
1091 			tshmseg.label = NULL;
1092 			tshmseg.cred = NULL;
1093 			outaddr = &tshmseg;
1094 			outsize = sizeof(tshmseg);
1095 		}
1096 		error = SYSCTL_OUT(req, outaddr, outsize);
1097 		if (error != 0)
1098 			break;
1099 	}
1100 	SYSVSHM_UNLOCK();
1101 	return (error);
1102 }
1103 
1104 static int
1105 shm_prison_check(void *obj, void *data)
1106 {
1107 	struct prison *pr = obj;
1108 	struct prison *prpr;
1109 	struct vfsoptlist *opts = data;
1110 	int error, jsys;
1111 
1112 	/*
1113 	 * sysvshm is a jailsys integer.
1114 	 * It must be "disable" if the parent jail is disabled.
1115 	 */
1116 	error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1117 	if (error != ENOENT) {
1118 		if (error != 0)
1119 			return (error);
1120 		switch (jsys) {
1121 		case JAIL_SYS_DISABLE:
1122 			break;
1123 		case JAIL_SYS_NEW:
1124 		case JAIL_SYS_INHERIT:
1125 			prison_lock(pr->pr_parent);
1126 			prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1127 			prison_unlock(pr->pr_parent);
1128 			if (prpr == NULL)
1129 				return (EPERM);
1130 			break;
1131 		default:
1132 			return (EINVAL);
1133 		}
1134 	}
1135 
1136 	return (0);
1137 }
1138 
1139 static int
1140 shm_prison_set(void *obj, void *data)
1141 {
1142 	struct prison *pr = obj;
1143 	struct prison *tpr, *orpr, *nrpr, *trpr;
1144 	struct vfsoptlist *opts = data;
1145 	void *rsv;
1146 	int jsys, descend;
1147 
1148 	/*
1149 	 * sysvshm controls which jail is the root of the associated segments
1150 	 * (this jail or same as the parent), or if the feature is available
1151 	 * at all.
1152 	 */
1153 	if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1154 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1155 		    ? JAIL_SYS_INHERIT
1156 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1157 		    ? JAIL_SYS_DISABLE
1158 		    : -1;
1159 	if (jsys == JAIL_SYS_DISABLE) {
1160 		prison_lock(pr);
1161 		orpr = osd_jail_get(pr, shm_prison_slot);
1162 		if (orpr != NULL)
1163 			osd_jail_del(pr, shm_prison_slot);
1164 		prison_unlock(pr);
1165 		if (orpr != NULL) {
1166 			if (orpr == pr)
1167 				shm_prison_cleanup(pr);
1168 			/* Disable all child jails as well. */
1169 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1170 				prison_lock(tpr);
1171 				trpr = osd_jail_get(tpr, shm_prison_slot);
1172 				if (trpr != NULL) {
1173 					osd_jail_del(tpr, shm_prison_slot);
1174 					prison_unlock(tpr);
1175 					if (trpr == tpr)
1176 						shm_prison_cleanup(tpr);
1177 				} else {
1178 					prison_unlock(tpr);
1179 					descend = 0;
1180 				}
1181 			}
1182 		}
1183 	} else if (jsys != -1) {
1184 		if (jsys == JAIL_SYS_NEW)
1185 			nrpr = pr;
1186 		else {
1187 			prison_lock(pr->pr_parent);
1188 			nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1189 			prison_unlock(pr->pr_parent);
1190 		}
1191 		rsv = osd_reserve(shm_prison_slot);
1192 		prison_lock(pr);
1193 		orpr = osd_jail_get(pr, shm_prison_slot);
1194 		if (orpr != nrpr)
1195 			(void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1196 			    nrpr);
1197 		else
1198 			osd_free_reserved(rsv);
1199 		prison_unlock(pr);
1200 		if (orpr != nrpr) {
1201 			if (orpr == pr)
1202 				shm_prison_cleanup(pr);
1203 			if (orpr != NULL) {
1204 				/* Change child jails matching the old root, */
1205 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1206 					prison_lock(tpr);
1207 					trpr = osd_jail_get(tpr,
1208 					    shm_prison_slot);
1209 					if (trpr == orpr) {
1210 						(void)osd_jail_set(tpr,
1211 						    shm_prison_slot, nrpr);
1212 						prison_unlock(tpr);
1213 						if (trpr == tpr)
1214 							shm_prison_cleanup(tpr);
1215 					} else {
1216 						prison_unlock(tpr);
1217 						descend = 0;
1218 					}
1219 				}
1220 			}
1221 		}
1222 	}
1223 
1224 	return (0);
1225 }
1226 
1227 static int
1228 shm_prison_get(void *obj, void *data)
1229 {
1230 	struct prison *pr = obj;
1231 	struct prison *rpr;
1232 	struct vfsoptlist *opts = data;
1233 	int error, jsys;
1234 
1235 	/* Set sysvshm based on the jail's root prison. */
1236 	prison_lock(pr);
1237 	rpr = osd_jail_get(pr, shm_prison_slot);
1238 	prison_unlock(pr);
1239 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1240 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1241 	error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1242 	if (error == ENOENT)
1243 		error = 0;
1244 	return (error);
1245 }
1246 
1247 static int
1248 shm_prison_remove(void *obj, void *data __unused)
1249 {
1250 	struct prison *pr = obj;
1251 	struct prison *rpr;
1252 
1253 	SYSVSHM_LOCK();
1254 	prison_lock(pr);
1255 	rpr = osd_jail_get(pr, shm_prison_slot);
1256 	prison_unlock(pr);
1257 	if (rpr == pr)
1258 		shm_prison_cleanup(pr);
1259 	SYSVSHM_UNLOCK();
1260 	return (0);
1261 }
1262 
1263 static void
1264 shm_prison_cleanup(struct prison *pr)
1265 {
1266 	struct shmid_kernel *shmseg;
1267 	int i;
1268 
1269 	/* Remove any segments that belong to this jail. */
1270 	for (i = 0; i < shmalloced; i++) {
1271 		shmseg = &shmsegs[i];
1272 		if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1273 		    shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1274 			shm_remove(shmseg, i);
1275 		}
1276 	}
1277 }
1278 
1279 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1280 
1281 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1282 struct oshmid_ds {
1283 	struct	ipc_perm_old shm_perm;	/* operation perms */
1284 	int	shm_segsz;		/* size of segment (bytes) */
1285 	u_short	shm_cpid;		/* pid, creator */
1286 	u_short	shm_lpid;		/* pid, last operation */
1287 	short	shm_nattch;		/* no. of current attaches */
1288 	time_t	shm_atime;		/* last attach time */
1289 	time_t	shm_dtime;		/* last detach time */
1290 	time_t	shm_ctime;		/* last change time */
1291 	void	*shm_handle;		/* internal handle for shm segment */
1292 };
1293 
1294 struct oshmctl_args {
1295 	int shmid;
1296 	int cmd;
1297 	struct oshmid_ds *ubuf;
1298 };
1299 
1300 static int
1301 oshmctl(struct thread *td, struct oshmctl_args *uap)
1302 {
1303 #ifdef COMPAT_43
1304 	int error = 0;
1305 	struct prison *rpr;
1306 	struct shmid_kernel *shmseg;
1307 	struct oshmid_ds outbuf;
1308 
1309 	rpr = shm_find_prison(td->td_ucred);
1310 	if (rpr == NULL)
1311 		return (ENOSYS);
1312 	if (uap->cmd != IPC_STAT) {
1313 		return (freebsd7_shmctl(td,
1314 		    (struct freebsd7_shmctl_args *)uap));
1315 	}
1316 	SYSVSHM_LOCK();
1317 	shmseg = shm_find_segment(rpr, uap->shmid, true);
1318 	if (shmseg == NULL) {
1319 		SYSVSHM_UNLOCK();
1320 		return (EINVAL);
1321 	}
1322 	error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1323 	if (error != 0) {
1324 		SYSVSHM_UNLOCK();
1325 		return (error);
1326 	}
1327 #ifdef MAC
1328 	error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1329 	if (error != 0) {
1330 		SYSVSHM_UNLOCK();
1331 		return (error);
1332 	}
1333 #endif
1334 	ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1335 	outbuf.shm_segsz = shmseg->u.shm_segsz;
1336 	outbuf.shm_cpid = shmseg->u.shm_cpid;
1337 	outbuf.shm_lpid = shmseg->u.shm_lpid;
1338 	outbuf.shm_nattch = shmseg->u.shm_nattch;
1339 	outbuf.shm_atime = shmseg->u.shm_atime;
1340 	outbuf.shm_dtime = shmseg->u.shm_dtime;
1341 	outbuf.shm_ctime = shmseg->u.shm_ctime;
1342 	outbuf.shm_handle = shmseg->object;
1343 	SYSVSHM_UNLOCK();
1344 	return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1345 #else
1346 	return (EINVAL);
1347 #endif
1348 }
1349 
1350 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1351 static sy_call_t *shmcalls[] = {
1352 	(sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1353 	(sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1354 	(sy_call_t *)freebsd7_shmctl
1355 };
1356 
1357 #ifndef _SYS_SYSPROTO_H_
1358 /* XXX actually varargs. */
1359 struct shmsys_args {
1360 	int	which;
1361 	int	a2;
1362 	int	a3;
1363 	int	a4;
1364 };
1365 #endif
1366 int
1367 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1368 {
1369 
1370 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1371 	if (uap->which < 0 || uap->which >= nitems(shmcalls))
1372 		return (EINVAL);
1373 	return ((*shmcalls[uap->which])(td, &uap->a2));
1374 }
1375 
1376 #endif	/* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1377 
1378 #ifdef COMPAT_FREEBSD32
1379 
1380 int
1381 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1382 {
1383 
1384 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1385     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1386 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1387 	switch (uap->which) {
1388 	case 0:	{	/* shmat */
1389 		struct shmat_args ap;
1390 
1391 		ap.shmid = uap->a2;
1392 		ap.shmaddr = PTRIN(uap->a3);
1393 		ap.shmflg = uap->a4;
1394 		return (sysent[SYS_shmat].sy_call(td, &ap));
1395 	}
1396 	case 2: {	/* shmdt */
1397 		struct shmdt_args ap;
1398 
1399 		ap.shmaddr = PTRIN(uap->a2);
1400 		return (sysent[SYS_shmdt].sy_call(td, &ap));
1401 	}
1402 	case 3: {	/* shmget */
1403 		struct shmget_args ap;
1404 
1405 		ap.key = uap->a2;
1406 		ap.size = uap->a3;
1407 		ap.shmflg = uap->a4;
1408 		return (sysent[SYS_shmget].sy_call(td, &ap));
1409 	}
1410 	case 4: {	/* shmctl */
1411 		struct freebsd7_freebsd32_shmctl_args ap;
1412 
1413 		ap.shmid = uap->a2;
1414 		ap.cmd = uap->a3;
1415 		ap.buf = PTRIN(uap->a4);
1416 		return (freebsd7_freebsd32_shmctl(td, &ap));
1417 	}
1418 	case 1:		/* oshmctl */
1419 	default:
1420 		return (EINVAL);
1421 	}
1422 #else
1423 	return (nosys(td, NULL));
1424 #endif
1425 }
1426 
1427 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1428     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1429 int
1430 freebsd7_freebsd32_shmctl(struct thread *td,
1431     struct freebsd7_freebsd32_shmctl_args *uap)
1432 {
1433 	int error;
1434 	union {
1435 		struct shmid_ds shmid_ds;
1436 		struct shm_info shm_info;
1437 		struct shminfo shminfo;
1438 	} u;
1439 	union {
1440 		struct shmid_ds32_old shmid_ds32;
1441 		struct shm_info32 shm_info32;
1442 		struct shminfo32 shminfo32;
1443 	} u32;
1444 	size_t sz;
1445 
1446 	if (uap->cmd == IPC_SET) {
1447 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1448 		    sizeof(u32.shmid_ds32))))
1449 			goto done;
1450 		freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1451 		    &u.shmid_ds.shm_perm);
1452 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1453 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1454 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1455 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1456 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1457 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1458 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1459 	}
1460 
1461 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1462 	if (error)
1463 		goto done;
1464 
1465 	/* Cases in which we need to copyout */
1466 	switch (uap->cmd) {
1467 	case IPC_INFO:
1468 		CP(u.shminfo, u32.shminfo32, shmmax);
1469 		CP(u.shminfo, u32.shminfo32, shmmin);
1470 		CP(u.shminfo, u32.shminfo32, shmmni);
1471 		CP(u.shminfo, u32.shminfo32, shmseg);
1472 		CP(u.shminfo, u32.shminfo32, shmall);
1473 		error = copyout(&u32.shminfo32, uap->buf,
1474 		    sizeof(u32.shminfo32));
1475 		break;
1476 	case SHM_INFO:
1477 		CP(u.shm_info, u32.shm_info32, used_ids);
1478 		CP(u.shm_info, u32.shm_info32, shm_rss);
1479 		CP(u.shm_info, u32.shm_info32, shm_tot);
1480 		CP(u.shm_info, u32.shm_info32, shm_swp);
1481 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1482 		CP(u.shm_info, u32.shm_info32, swap_successes);
1483 		error = copyout(&u32.shm_info32, uap->buf,
1484 		    sizeof(u32.shm_info32));
1485 		break;
1486 	case SHM_STAT:
1487 	case IPC_STAT:
1488 		memset(&u32.shmid_ds32, 0, sizeof(u32.shmid_ds32));
1489 		freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1490 		    &u32.shmid_ds32.shm_perm);
1491 		if (u.shmid_ds.shm_segsz > INT32_MAX)
1492 			u32.shmid_ds32.shm_segsz = INT32_MAX;
1493 		else
1494 			CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1495 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1496 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1497 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1498 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1499 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1500 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1501 		u32.shmid_ds32.shm_internal = 0;
1502 		error = copyout(&u32.shmid_ds32, uap->buf,
1503 		    sizeof(u32.shmid_ds32));
1504 		break;
1505 	}
1506 
1507 done:
1508 	if (error) {
1509 		/* Invalidate the return value */
1510 		td->td_retval[0] = -1;
1511 	}
1512 	return (error);
1513 }
1514 #endif
1515 
1516 int
1517 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1518 {
1519 	int error;
1520 	union {
1521 		struct shmid_ds shmid_ds;
1522 		struct shm_info shm_info;
1523 		struct shminfo shminfo;
1524 	} u;
1525 	union {
1526 		struct shmid_ds32 shmid_ds32;
1527 		struct shm_info32 shm_info32;
1528 		struct shminfo32 shminfo32;
1529 	} u32;
1530 	size_t sz;
1531 
1532 	if (uap->cmd == IPC_SET) {
1533 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1534 		    sizeof(u32.shmid_ds32))))
1535 			goto done;
1536 		freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1537 		    &u.shmid_ds.shm_perm);
1538 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1539 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1540 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1541 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1542 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1543 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1544 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1545 	}
1546 
1547 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1548 	if (error)
1549 		goto done;
1550 
1551 	/* Cases in which we need to copyout */
1552 	switch (uap->cmd) {
1553 	case IPC_INFO:
1554 		CP(u.shminfo, u32.shminfo32, shmmax);
1555 		CP(u.shminfo, u32.shminfo32, shmmin);
1556 		CP(u.shminfo, u32.shminfo32, shmmni);
1557 		CP(u.shminfo, u32.shminfo32, shmseg);
1558 		CP(u.shminfo, u32.shminfo32, shmall);
1559 		error = copyout(&u32.shminfo32, uap->buf,
1560 		    sizeof(u32.shminfo32));
1561 		break;
1562 	case SHM_INFO:
1563 		CP(u.shm_info, u32.shm_info32, used_ids);
1564 		CP(u.shm_info, u32.shm_info32, shm_rss);
1565 		CP(u.shm_info, u32.shm_info32, shm_tot);
1566 		CP(u.shm_info, u32.shm_info32, shm_swp);
1567 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1568 		CP(u.shm_info, u32.shm_info32, swap_successes);
1569 		error = copyout(&u32.shm_info32, uap->buf,
1570 		    sizeof(u32.shm_info32));
1571 		break;
1572 	case SHM_STAT:
1573 	case IPC_STAT:
1574 		freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1575 		    &u32.shmid_ds32.shm_perm);
1576 		if (u.shmid_ds.shm_segsz > INT32_MAX)
1577 			u32.shmid_ds32.shm_segsz = INT32_MAX;
1578 		else
1579 			CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1580 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1581 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1582 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1583 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1584 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1585 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1586 		error = copyout(&u32.shmid_ds32, uap->buf,
1587 		    sizeof(u32.shmid_ds32));
1588 		break;
1589 	}
1590 
1591 done:
1592 	if (error) {
1593 		/* Invalidate the return value */
1594 		td->td_retval[0] = -1;
1595 	}
1596 	return (error);
1597 }
1598 #endif
1599 
1600 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1601     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1602 
1603 #ifndef CP
1604 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1605 #endif
1606 
1607 #ifndef _SYS_SYSPROTO_H_
1608 struct freebsd7_shmctl_args {
1609 	int shmid;
1610 	int cmd;
1611 	struct shmid_ds_old *buf;
1612 };
1613 #endif
1614 int
1615 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1616 {
1617 	int error;
1618 	struct shmid_ds_old old;
1619 	struct shmid_ds buf;
1620 	size_t bufsz;
1621 
1622 	/*
1623 	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1624 	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
1625 	 * return an error back to the user since we do not to support this.
1626 	 */
1627 	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1628 	    uap->cmd == SHM_STAT)
1629 		return (EINVAL);
1630 
1631 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1632 	if (uap->cmd == IPC_SET) {
1633 		if ((error = copyin(uap->buf, &old, sizeof(old))))
1634 			goto done;
1635 		ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1636 		CP(old, buf, shm_segsz);
1637 		CP(old, buf, shm_lpid);
1638 		CP(old, buf, shm_cpid);
1639 		CP(old, buf, shm_nattch);
1640 		CP(old, buf, shm_atime);
1641 		CP(old, buf, shm_dtime);
1642 		CP(old, buf, shm_ctime);
1643 	}
1644 
1645 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1646 	if (error)
1647 		goto done;
1648 
1649 	/* Cases in which we need to copyout */
1650 	switch (uap->cmd) {
1651 	case IPC_STAT:
1652 		memset(&old, 0, sizeof(old));
1653 		ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1654 		if (buf.shm_segsz > INT_MAX)
1655 			old.shm_segsz = INT_MAX;
1656 		else
1657 			CP(buf, old, shm_segsz);
1658 		CP(buf, old, shm_lpid);
1659 		CP(buf, old, shm_cpid);
1660 		if (buf.shm_nattch > SHRT_MAX)
1661 			old.shm_nattch = SHRT_MAX;
1662 		else
1663 			CP(buf, old, shm_nattch);
1664 		CP(buf, old, shm_atime);
1665 		CP(buf, old, shm_dtime);
1666 		CP(buf, old, shm_ctime);
1667 		old.shm_internal = NULL;
1668 		error = copyout(&old, uap->buf, sizeof(old));
1669 		break;
1670 	}
1671 
1672 done:
1673 	if (error) {
1674 		/* Invalidate the return value */
1675 		td->td_retval[0] = -1;
1676 	}
1677 	return (error);
1678 }
1679 
1680 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1681 	   COMPAT_FREEBSD7 */
1682 
1683 static int
1684 sysvshm_modload(struct module *module, int cmd, void *arg)
1685 {
1686 	int error = 0;
1687 
1688 	switch (cmd) {
1689 	case MOD_LOAD:
1690 		error = shminit();
1691 		if (error != 0)
1692 			shmunload();
1693 		break;
1694 	case MOD_UNLOAD:
1695 		error = shmunload();
1696 		break;
1697 	case MOD_SHUTDOWN:
1698 		break;
1699 	default:
1700 		error = EINVAL;
1701 		break;
1702 	}
1703 	return (error);
1704 }
1705 
1706 static moduledata_t sysvshm_mod = {
1707 	"sysvshm",
1708 	&sysvshm_modload,
1709 	NULL
1710 };
1711 
1712 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1713 MODULE_VERSION(sysvshm, 1);
1714