xref: /freebsd/sys/kern/sysv_shm.c (revision 77a1348b3c1cfe8547be49a121b56299a1e18b69)
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 #ifndef _SYS_SYSPROTO_H_
613 struct shmctl_args {
614 	int shmid;
615 	int cmd;
616 	struct shmid_ds *buf;
617 };
618 #endif
619 int
620 sys_shmctl(struct thread *td, struct shmctl_args *uap)
621 {
622 	int error;
623 	struct shmid_ds buf;
624 	size_t bufsz;
625 
626 	/*
627 	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
628 	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
629 	 * return an error back to the user since we do not to support this.
630 	 */
631 	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
632 	    uap->cmd == SHM_STAT)
633 		return (EINVAL);
634 
635 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
636 	if (uap->cmd == IPC_SET) {
637 		if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
638 			goto done;
639 	}
640 
641 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
642 	if (error)
643 		goto done;
644 
645 	/* Cases in which we need to copyout */
646 	switch (uap->cmd) {
647 	case IPC_STAT:
648 		error = copyout(&buf, uap->buf, bufsz);
649 		break;
650 	}
651 
652 done:
653 	if (error) {
654 		/* Invalidate the return value */
655 		td->td_retval[0] = -1;
656 	}
657 	return (error);
658 }
659 
660 static int
661 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
662     int segnum)
663 {
664 	struct shmid_kernel *shmseg;
665 #ifdef MAC
666 	int error;
667 #endif
668 
669 	SYSVSHM_ASSERT_LOCKED();
670 	KASSERT(segnum >= 0 && segnum < shmalloced,
671 	    ("segnum %d shmalloced %d", segnum, shmalloced));
672 	shmseg = &shmsegs[segnum];
673 	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
674 		return (EEXIST);
675 #ifdef MAC
676 	error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
677 	if (error != 0)
678 		return (error);
679 #endif
680 	if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
681 		return (EINVAL);
682 	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
683 	return (0);
684 }
685 
686 static int
687 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
688 {
689 	struct ucred *cred = td->td_ucred;
690 	struct shmid_kernel *shmseg;
691 	vm_object_t shm_object;
692 	int i, segnum;
693 	size_t size;
694 
695 	SYSVSHM_ASSERT_LOCKED();
696 
697 	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
698 		return (EINVAL);
699 	if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
700 		return (ENOSPC);
701 	size = round_page(uap->size);
702 	if (shm_committed + btoc(size) > shminfo.shmall)
703 		return (ENOMEM);
704 	if (shm_last_free < 0) {
705 		shmrealloc();	/* Maybe expand the shmsegs[] array. */
706 		for (i = 0; i < shmalloced; i++)
707 			if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
708 				break;
709 		if (i == shmalloced)
710 			return (ENOSPC);
711 		segnum = i;
712 	} else  {
713 		segnum = shm_last_free;
714 		shm_last_free = -1;
715 	}
716 	KASSERT(segnum >= 0 && segnum < shmalloced,
717 	    ("segnum %d shmalloced %d", segnum, shmalloced));
718 	shmseg = &shmsegs[segnum];
719 #ifdef RACCT
720 	if (racct_enable) {
721 		PROC_LOCK(td->td_proc);
722 		if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
723 			PROC_UNLOCK(td->td_proc);
724 			return (ENOSPC);
725 		}
726 		if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
727 			racct_sub(td->td_proc, RACCT_NSHM, 1);
728 			PROC_UNLOCK(td->td_proc);
729 			return (ENOMEM);
730 		}
731 		PROC_UNLOCK(td->td_proc);
732 	}
733 #endif
734 
735 	/*
736 	 * We make sure that we have allocated a pager before we need
737 	 * to.
738 	 */
739 	shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
740 	    0, size, VM_PROT_DEFAULT, 0, cred);
741 	if (shm_object == NULL) {
742 #ifdef RACCT
743 		if (racct_enable) {
744 			PROC_LOCK(td->td_proc);
745 			racct_sub(td->td_proc, RACCT_NSHM, 1);
746 			racct_sub(td->td_proc, RACCT_SHMSIZE, size);
747 			PROC_UNLOCK(td->td_proc);
748 		}
749 #endif
750 		return (ENOMEM);
751 	}
752 
753 	shmseg->object = shm_object;
754 	shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
755 	shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
756 	shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
757 	shmseg->u.shm_perm.key = uap->key;
758 	shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
759 	shmseg->cred = crhold(cred);
760 	shmseg->u.shm_segsz = uap->size;
761 	shmseg->u.shm_cpid = td->td_proc->p_pid;
762 	shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
763 	shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
764 #ifdef MAC
765 	mac_sysvshm_create(cred, shmseg);
766 #endif
767 	shmseg->u.shm_ctime = time_second;
768 	shm_committed += btoc(size);
769 	shm_nused++;
770 	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
771 
772 	return (0);
773 }
774 
775 #ifndef _SYS_SYSPROTO_H_
776 struct shmget_args {
777 	key_t key;
778 	size_t size;
779 	int shmflg;
780 };
781 #endif
782 int
783 sys_shmget(struct thread *td, struct shmget_args *uap)
784 {
785 	int segnum, mode;
786 	int error;
787 
788 	if (shm_find_prison(td->td_ucred) == NULL)
789 		return (ENOSYS);
790 	mode = uap->shmflg & ACCESSPERMS;
791 	SYSVSHM_LOCK();
792 	if (uap->key == IPC_PRIVATE) {
793 		error = shmget_allocate_segment(td, uap, mode);
794 	} else {
795 		segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
796 		    uap->key);
797 		if (segnum >= 0)
798 			error = shmget_existing(td, uap, mode, segnum);
799 		else if ((uap->shmflg & IPC_CREAT) == 0)
800 			error = ENOENT;
801 		else
802 			error = shmget_allocate_segment(td, uap, mode);
803 	}
804 	SYSVSHM_UNLOCK();
805 	return (error);
806 }
807 
808 #ifdef SYSVSHM
809 void
810 shmfork(struct proc *p1, struct proc *p2)
811 #else
812 static void
813 shmfork_myhook(struct proc *p1, struct proc *p2)
814 #endif
815 {
816 	struct shmmap_state *shmmap_s;
817 	size_t size;
818 	int i;
819 
820 	SYSVSHM_LOCK();
821 	size = shminfo.shmseg * sizeof(struct shmmap_state);
822 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
823 	bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
824 	p2->p_vmspace->vm_shm = shmmap_s;
825 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
826 		if (shmmap_s->shmid != -1) {
827 			KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
828 			    IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
829 			    ("segnum %d shmalloced %d",
830 			    IPCID_TO_IX(shmmap_s->shmid), shmalloced));
831 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
832 		}
833 	}
834 	SYSVSHM_UNLOCK();
835 }
836 
837 #ifdef SYSVSHM
838 void
839 shmexit(struct vmspace *vm)
840 #else
841 static void
842 shmexit_myhook(struct vmspace *vm)
843 #endif
844 {
845 	struct shmmap_state *base, *shm;
846 	int i;
847 
848 	base = vm->vm_shm;
849 	if (base != NULL) {
850 		vm->vm_shm = NULL;
851 		SYSVSHM_LOCK();
852 		for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
853 			if (shm->shmid != -1)
854 				shm_delete_mapping(vm, shm);
855 		}
856 		SYSVSHM_UNLOCK();
857 		free(base, M_SHM);
858 	}
859 }
860 
861 static void
862 shmrealloc(void)
863 {
864 	struct shmid_kernel *newsegs;
865 	int i;
866 
867 	SYSVSHM_ASSERT_LOCKED();
868 
869 	if (shmalloced >= shminfo.shmmni)
870 		return;
871 
872 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
873 	    M_WAITOK | M_ZERO);
874 	for (i = 0; i < shmalloced; i++)
875 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
876 	for (; i < shminfo.shmmni; i++) {
877 		newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
878 		newsegs[i].u.shm_perm.seq = 0;
879 #ifdef MAC
880 		mac_sysvshm_init(&newsegs[i]);
881 #endif
882 	}
883 	free(shmsegs, M_SHM);
884 	shmsegs = newsegs;
885 	shmalloced = shminfo.shmmni;
886 }
887 
888 static struct syscall_helper_data shm_syscalls[] = {
889 	SYSCALL_INIT_HELPER(shmat),
890 	SYSCALL_INIT_HELPER(shmctl),
891 	SYSCALL_INIT_HELPER(shmdt),
892 	SYSCALL_INIT_HELPER(shmget),
893 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
894     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
895 	SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
896 #endif
897 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
898 	SYSCALL_INIT_HELPER(shmsys),
899 #endif
900 	SYSCALL_INIT_LAST
901 };
902 
903 #ifdef COMPAT_FREEBSD32
904 #include <compat/freebsd32/freebsd32.h>
905 #include <compat/freebsd32/freebsd32_ipc.h>
906 #include <compat/freebsd32/freebsd32_proto.h>
907 #include <compat/freebsd32/freebsd32_signal.h>
908 #include <compat/freebsd32/freebsd32_syscall.h>
909 #include <compat/freebsd32/freebsd32_util.h>
910 
911 static struct syscall_helper_data shm32_syscalls[] = {
912 	SYSCALL32_INIT_HELPER_COMPAT(shmat),
913 	SYSCALL32_INIT_HELPER_COMPAT(shmdt),
914 	SYSCALL32_INIT_HELPER_COMPAT(shmget),
915 	SYSCALL32_INIT_HELPER(freebsd32_shmsys),
916 	SYSCALL32_INIT_HELPER(freebsd32_shmctl),
917 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
918     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
919 	SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
920 #endif
921 	SYSCALL_INIT_LAST
922 };
923 #endif
924 
925 static int
926 shminit(void)
927 {
928 	struct prison *pr;
929 	void **rsv;
930 	int i, error;
931 	osd_method_t methods[PR_MAXMETHOD] = {
932 	    [PR_METHOD_CHECK] =		shm_prison_check,
933 	    [PR_METHOD_SET] =		shm_prison_set,
934 	    [PR_METHOD_GET] =		shm_prison_get,
935 	    [PR_METHOD_REMOVE] =	shm_prison_remove,
936 	};
937 
938 #ifndef BURN_BRIDGES
939 	if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
940 		printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
941 #endif
942 	if (shminfo.shmmax == SHMMAX) {
943 		/* Initialize shmmax dealing with possible overflow. */
944 		for (i = PAGE_SIZE; i != 0; i--) {
945 			shminfo.shmmax = shminfo.shmall * i;
946 			if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
947 				break;
948 		}
949 	}
950 	shmalloced = shminfo.shmmni;
951 	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
952 	    M_WAITOK|M_ZERO);
953 	for (i = 0; i < shmalloced; i++) {
954 		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
955 		shmsegs[i].u.shm_perm.seq = 0;
956 #ifdef MAC
957 		mac_sysvshm_init(&shmsegs[i]);
958 #endif
959 	}
960 	shm_last_free = 0;
961 	shm_nused = 0;
962 	shm_committed = 0;
963 	sx_init(&sysvshmsx, "sysvshmsx");
964 #ifndef SYSVSHM
965 	shmexit_hook = &shmexit_myhook;
966 	shmfork_hook = &shmfork_myhook;
967 #endif
968 
969 	/* Set current prisons according to their allow.sysvipc. */
970 	shm_prison_slot = osd_jail_register(NULL, methods);
971 	rsv = osd_reserve(shm_prison_slot);
972 	prison_lock(&prison0);
973 	(void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
974 	prison_unlock(&prison0);
975 	rsv = NULL;
976 	sx_slock(&allprison_lock);
977 	TAILQ_FOREACH(pr, &allprison, pr_list) {
978 		if (rsv == NULL)
979 			rsv = osd_reserve(shm_prison_slot);
980 		prison_lock(pr);
981 		if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
982 			(void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
983 			    &prison0);
984 			rsv = NULL;
985 		}
986 		prison_unlock(pr);
987 	}
988 	if (rsv != NULL)
989 		osd_free_reserved(rsv);
990 	sx_sunlock(&allprison_lock);
991 
992 	error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
993 	if (error != 0)
994 		return (error);
995 #ifdef COMPAT_FREEBSD32
996 	error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
997 	if (error != 0)
998 		return (error);
999 #endif
1000 	return (0);
1001 }
1002 
1003 static int
1004 shmunload(void)
1005 {
1006 	int i;
1007 
1008 	if (shm_nused > 0)
1009 		return (EBUSY);
1010 
1011 #ifdef COMPAT_FREEBSD32
1012 	syscall32_helper_unregister(shm32_syscalls);
1013 #endif
1014 	syscall_helper_unregister(shm_syscalls);
1015 	if (shm_prison_slot != 0)
1016 		osd_jail_deregister(shm_prison_slot);
1017 
1018 	for (i = 0; i < shmalloced; i++) {
1019 #ifdef MAC
1020 		mac_sysvshm_destroy(&shmsegs[i]);
1021 #endif
1022 		/*
1023 		 * Objects might be still mapped into the processes
1024 		 * address spaces.  Actual free would happen on the
1025 		 * last mapping destruction.
1026 		 */
1027 		if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1028 			vm_object_deallocate(shmsegs[i].object);
1029 	}
1030 	free(shmsegs, M_SHM);
1031 #ifndef SYSVSHM
1032 	shmexit_hook = NULL;
1033 	shmfork_hook = NULL;
1034 #endif
1035 	sx_destroy(&sysvshmsx);
1036 	return (0);
1037 }
1038 
1039 static int
1040 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1041 {
1042 	struct shmid_kernel tshmseg;
1043 #ifdef COMPAT_FREEBSD32
1044 	struct shmid_kernel32 tshmseg32;
1045 #endif
1046 	struct prison *pr, *rpr;
1047 	void *outaddr;
1048 	size_t outsize;
1049 	int error, i;
1050 
1051 	SYSVSHM_LOCK();
1052 	pr = req->td->td_ucred->cr_prison;
1053 	rpr = shm_find_prison(req->td->td_ucred);
1054 	error = 0;
1055 	for (i = 0; i < shmalloced; i++) {
1056 		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1057 		    rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1058 			bzero(&tshmseg, sizeof(tshmseg));
1059 			tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1060 		} else {
1061 			tshmseg = shmsegs[i];
1062 			if (tshmseg.cred->cr_prison != pr)
1063 				tshmseg.u.shm_perm.key = IPC_PRIVATE;
1064 		}
1065 #ifdef COMPAT_FREEBSD32
1066 		if (SV_CURPROC_FLAG(SV_ILP32)) {
1067 			bzero(&tshmseg32, sizeof(tshmseg32));
1068 			freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1069 			    &tshmseg32.u.shm_perm);
1070 			CP(tshmseg, tshmseg32, u.shm_segsz);
1071 			CP(tshmseg, tshmseg32, u.shm_lpid);
1072 			CP(tshmseg, tshmseg32, u.shm_cpid);
1073 			CP(tshmseg, tshmseg32, u.shm_nattch);
1074 			CP(tshmseg, tshmseg32, u.shm_atime);
1075 			CP(tshmseg, tshmseg32, u.shm_dtime);
1076 			CP(tshmseg, tshmseg32, u.shm_ctime);
1077 			/* Don't copy object, label, or cred */
1078 			outaddr = &tshmseg32;
1079 			outsize = sizeof(tshmseg32);
1080 		} else
1081 #endif
1082 		{
1083 			tshmseg.object = NULL;
1084 			tshmseg.label = NULL;
1085 			tshmseg.cred = NULL;
1086 			outaddr = &tshmseg;
1087 			outsize = sizeof(tshmseg);
1088 		}
1089 		error = SYSCTL_OUT(req, outaddr, outsize);
1090 		if (error != 0)
1091 			break;
1092 	}
1093 	SYSVSHM_UNLOCK();
1094 	return (error);
1095 }
1096 
1097 static int
1098 shm_prison_check(void *obj, void *data)
1099 {
1100 	struct prison *pr = obj;
1101 	struct prison *prpr;
1102 	struct vfsoptlist *opts = data;
1103 	int error, jsys;
1104 
1105 	/*
1106 	 * sysvshm is a jailsys integer.
1107 	 * It must be "disable" if the parent jail is disabled.
1108 	 */
1109 	error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1110 	if (error != ENOENT) {
1111 		if (error != 0)
1112 			return (error);
1113 		switch (jsys) {
1114 		case JAIL_SYS_DISABLE:
1115 			break;
1116 		case JAIL_SYS_NEW:
1117 		case JAIL_SYS_INHERIT:
1118 			prison_lock(pr->pr_parent);
1119 			prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1120 			prison_unlock(pr->pr_parent);
1121 			if (prpr == NULL)
1122 				return (EPERM);
1123 			break;
1124 		default:
1125 			return (EINVAL);
1126 		}
1127 	}
1128 
1129 	return (0);
1130 }
1131 
1132 static int
1133 shm_prison_set(void *obj, void *data)
1134 {
1135 	struct prison *pr = obj;
1136 	struct prison *tpr, *orpr, *nrpr, *trpr;
1137 	struct vfsoptlist *opts = data;
1138 	void *rsv;
1139 	int jsys, descend;
1140 
1141 	/*
1142 	 * sysvshm controls which jail is the root of the associated segments
1143 	 * (this jail or same as the parent), or if the feature is available
1144 	 * at all.
1145 	 */
1146 	if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1147 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1148 		    ? JAIL_SYS_INHERIT
1149 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1150 		    ? JAIL_SYS_DISABLE
1151 		    : -1;
1152 	if (jsys == JAIL_SYS_DISABLE) {
1153 		prison_lock(pr);
1154 		orpr = osd_jail_get(pr, shm_prison_slot);
1155 		if (orpr != NULL)
1156 			osd_jail_del(pr, shm_prison_slot);
1157 		prison_unlock(pr);
1158 		if (orpr != NULL) {
1159 			if (orpr == pr)
1160 				shm_prison_cleanup(pr);
1161 			/* Disable all child jails as well. */
1162 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1163 				prison_lock(tpr);
1164 				trpr = osd_jail_get(tpr, shm_prison_slot);
1165 				if (trpr != NULL) {
1166 					osd_jail_del(tpr, shm_prison_slot);
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 	} else if (jsys != -1) {
1177 		if (jsys == JAIL_SYS_NEW)
1178 			nrpr = pr;
1179 		else {
1180 			prison_lock(pr->pr_parent);
1181 			nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1182 			prison_unlock(pr->pr_parent);
1183 		}
1184 		rsv = osd_reserve(shm_prison_slot);
1185 		prison_lock(pr);
1186 		orpr = osd_jail_get(pr, shm_prison_slot);
1187 		if (orpr != nrpr)
1188 			(void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1189 			    nrpr);
1190 		else
1191 			osd_free_reserved(rsv);
1192 		prison_unlock(pr);
1193 		if (orpr != nrpr) {
1194 			if (orpr == pr)
1195 				shm_prison_cleanup(pr);
1196 			if (orpr != NULL) {
1197 				/* Change child jails matching the old root, */
1198 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1199 					prison_lock(tpr);
1200 					trpr = osd_jail_get(tpr,
1201 					    shm_prison_slot);
1202 					if (trpr == orpr) {
1203 						(void)osd_jail_set(tpr,
1204 						    shm_prison_slot, nrpr);
1205 						prison_unlock(tpr);
1206 						if (trpr == tpr)
1207 							shm_prison_cleanup(tpr);
1208 					} else {
1209 						prison_unlock(tpr);
1210 						descend = 0;
1211 					}
1212 				}
1213 			}
1214 		}
1215 	}
1216 
1217 	return (0);
1218 }
1219 
1220 static int
1221 shm_prison_get(void *obj, void *data)
1222 {
1223 	struct prison *pr = obj;
1224 	struct prison *rpr;
1225 	struct vfsoptlist *opts = data;
1226 	int error, jsys;
1227 
1228 	/* Set sysvshm based on the jail's root prison. */
1229 	prison_lock(pr);
1230 	rpr = osd_jail_get(pr, shm_prison_slot);
1231 	prison_unlock(pr);
1232 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1233 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1234 	error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1235 	if (error == ENOENT)
1236 		error = 0;
1237 	return (error);
1238 }
1239 
1240 static int
1241 shm_prison_remove(void *obj, void *data __unused)
1242 {
1243 	struct prison *pr = obj;
1244 	struct prison *rpr;
1245 
1246 	SYSVSHM_LOCK();
1247 	prison_lock(pr);
1248 	rpr = osd_jail_get(pr, shm_prison_slot);
1249 	prison_unlock(pr);
1250 	if (rpr == pr)
1251 		shm_prison_cleanup(pr);
1252 	SYSVSHM_UNLOCK();
1253 	return (0);
1254 }
1255 
1256 static void
1257 shm_prison_cleanup(struct prison *pr)
1258 {
1259 	struct shmid_kernel *shmseg;
1260 	int i;
1261 
1262 	/* Remove any segments that belong to this jail. */
1263 	for (i = 0; i < shmalloced; i++) {
1264 		shmseg = &shmsegs[i];
1265 		if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1266 		    shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1267 			shm_remove(shmseg, i);
1268 		}
1269 	}
1270 }
1271 
1272 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1273 
1274 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1275 struct oshmid_ds {
1276 	struct	ipc_perm_old shm_perm;	/* operation perms */
1277 	int	shm_segsz;		/* size of segment (bytes) */
1278 	u_short	shm_cpid;		/* pid, creator */
1279 	u_short	shm_lpid;		/* pid, last operation */
1280 	short	shm_nattch;		/* no. of current attaches */
1281 	time_t	shm_atime;		/* last attach time */
1282 	time_t	shm_dtime;		/* last detach time */
1283 	time_t	shm_ctime;		/* last change time */
1284 	void	*shm_handle;		/* internal handle for shm segment */
1285 };
1286 
1287 struct oshmctl_args {
1288 	int shmid;
1289 	int cmd;
1290 	struct oshmid_ds *ubuf;
1291 };
1292 
1293 static int
1294 oshmctl(struct thread *td, struct oshmctl_args *uap)
1295 {
1296 #ifdef COMPAT_43
1297 	int error = 0;
1298 	struct prison *rpr;
1299 	struct shmid_kernel *shmseg;
1300 	struct oshmid_ds outbuf;
1301 
1302 	rpr = shm_find_prison(td->td_ucred);
1303 	if (rpr == NULL)
1304 		return (ENOSYS);
1305 	if (uap->cmd != IPC_STAT) {
1306 		return (freebsd7_shmctl(td,
1307 		    (struct freebsd7_shmctl_args *)uap));
1308 	}
1309 	SYSVSHM_LOCK();
1310 	shmseg = shm_find_segment(rpr, uap->shmid, true);
1311 	if (shmseg == NULL) {
1312 		SYSVSHM_UNLOCK();
1313 		return (EINVAL);
1314 	}
1315 	error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1316 	if (error != 0) {
1317 		SYSVSHM_UNLOCK();
1318 		return (error);
1319 	}
1320 #ifdef MAC
1321 	error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1322 	if (error != 0) {
1323 		SYSVSHM_UNLOCK();
1324 		return (error);
1325 	}
1326 #endif
1327 	ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1328 	outbuf.shm_segsz = shmseg->u.shm_segsz;
1329 	outbuf.shm_cpid = shmseg->u.shm_cpid;
1330 	outbuf.shm_lpid = shmseg->u.shm_lpid;
1331 	outbuf.shm_nattch = shmseg->u.shm_nattch;
1332 	outbuf.shm_atime = shmseg->u.shm_atime;
1333 	outbuf.shm_dtime = shmseg->u.shm_dtime;
1334 	outbuf.shm_ctime = shmseg->u.shm_ctime;
1335 	outbuf.shm_handle = shmseg->object;
1336 	SYSVSHM_UNLOCK();
1337 	return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1338 #else
1339 	return (EINVAL);
1340 #endif
1341 }
1342 
1343 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1344 static sy_call_t *shmcalls[] = {
1345 	(sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1346 	(sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1347 	(sy_call_t *)freebsd7_shmctl
1348 };
1349 
1350 #ifndef _SYS_SYSPROTO_H_
1351 /* XXX actually varargs. */
1352 struct shmsys_args {
1353 	int	which;
1354 	int	a2;
1355 	int	a3;
1356 	int	a4;
1357 };
1358 #endif
1359 int
1360 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1361 {
1362 
1363 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1364 	if (uap->which < 0 || uap->which >= nitems(shmcalls))
1365 		return (EINVAL);
1366 	return ((*shmcalls[uap->which])(td, &uap->a2));
1367 }
1368 
1369 #endif	/* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1370 
1371 #ifdef COMPAT_FREEBSD32
1372 
1373 int
1374 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1375 {
1376 
1377 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1378     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1379 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1380 	switch (uap->which) {
1381 	case 0:	{	/* shmat */
1382 		struct shmat_args ap;
1383 
1384 		ap.shmid = uap->a2;
1385 		ap.shmaddr = PTRIN(uap->a3);
1386 		ap.shmflg = uap->a4;
1387 		return (sysent[SYS_shmat].sy_call(td, &ap));
1388 	}
1389 	case 2: {	/* shmdt */
1390 		struct shmdt_args ap;
1391 
1392 		ap.shmaddr = PTRIN(uap->a2);
1393 		return (sysent[SYS_shmdt].sy_call(td, &ap));
1394 	}
1395 	case 3: {	/* shmget */
1396 		struct shmget_args ap;
1397 
1398 		ap.key = uap->a2;
1399 		ap.size = uap->a3;
1400 		ap.shmflg = uap->a4;
1401 		return (sysent[SYS_shmget].sy_call(td, &ap));
1402 	}
1403 	case 4: {	/* shmctl */
1404 		struct freebsd7_freebsd32_shmctl_args ap;
1405 
1406 		ap.shmid = uap->a2;
1407 		ap.cmd = uap->a3;
1408 		ap.buf = PTRIN(uap->a4);
1409 		return (freebsd7_freebsd32_shmctl(td, &ap));
1410 	}
1411 	case 1:		/* oshmctl */
1412 	default:
1413 		return (EINVAL);
1414 	}
1415 #else
1416 	return (nosys(td, NULL));
1417 #endif
1418 }
1419 
1420 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1421     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1422 int
1423 freebsd7_freebsd32_shmctl(struct thread *td,
1424     struct freebsd7_freebsd32_shmctl_args *uap)
1425 {
1426 	int error;
1427 	union {
1428 		struct shmid_ds shmid_ds;
1429 		struct shm_info shm_info;
1430 		struct shminfo shminfo;
1431 	} u;
1432 	union {
1433 		struct shmid_ds32_old shmid_ds32;
1434 		struct shm_info32 shm_info32;
1435 		struct shminfo32 shminfo32;
1436 	} u32;
1437 	size_t sz;
1438 
1439 	if (uap->cmd == IPC_SET) {
1440 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1441 		    sizeof(u32.shmid_ds32))))
1442 			goto done;
1443 		freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1444 		    &u.shmid_ds.shm_perm);
1445 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1446 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1447 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1448 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1449 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1450 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1451 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1452 	}
1453 
1454 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1455 	if (error)
1456 		goto done;
1457 
1458 	/* Cases in which we need to copyout */
1459 	switch (uap->cmd) {
1460 	case IPC_INFO:
1461 		CP(u.shminfo, u32.shminfo32, shmmax);
1462 		CP(u.shminfo, u32.shminfo32, shmmin);
1463 		CP(u.shminfo, u32.shminfo32, shmmni);
1464 		CP(u.shminfo, u32.shminfo32, shmseg);
1465 		CP(u.shminfo, u32.shminfo32, shmall);
1466 		error = copyout(&u32.shminfo32, uap->buf,
1467 		    sizeof(u32.shminfo32));
1468 		break;
1469 	case SHM_INFO:
1470 		CP(u.shm_info, u32.shm_info32, used_ids);
1471 		CP(u.shm_info, u32.shm_info32, shm_rss);
1472 		CP(u.shm_info, u32.shm_info32, shm_tot);
1473 		CP(u.shm_info, u32.shm_info32, shm_swp);
1474 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1475 		CP(u.shm_info, u32.shm_info32, swap_successes);
1476 		error = copyout(&u32.shm_info32, uap->buf,
1477 		    sizeof(u32.shm_info32));
1478 		break;
1479 	case SHM_STAT:
1480 	case IPC_STAT:
1481 		memset(&u32.shmid_ds32, 0, sizeof(u32.shmid_ds32));
1482 		freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1483 		    &u32.shmid_ds32.shm_perm);
1484 		if (u.shmid_ds.shm_segsz > INT32_MAX)
1485 			u32.shmid_ds32.shm_segsz = INT32_MAX;
1486 		else
1487 			CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1488 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1489 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1490 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1491 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1492 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1493 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1494 		u32.shmid_ds32.shm_internal = 0;
1495 		error = copyout(&u32.shmid_ds32, uap->buf,
1496 		    sizeof(u32.shmid_ds32));
1497 		break;
1498 	}
1499 
1500 done:
1501 	if (error) {
1502 		/* Invalidate the return value */
1503 		td->td_retval[0] = -1;
1504 	}
1505 	return (error);
1506 }
1507 #endif
1508 
1509 int
1510 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1511 {
1512 	int error;
1513 	union {
1514 		struct shmid_ds shmid_ds;
1515 		struct shm_info shm_info;
1516 		struct shminfo shminfo;
1517 	} u;
1518 	union {
1519 		struct shmid_ds32 shmid_ds32;
1520 		struct shm_info32 shm_info32;
1521 		struct shminfo32 shminfo32;
1522 	} u32;
1523 	size_t sz;
1524 
1525 	if (uap->cmd == IPC_SET) {
1526 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1527 		    sizeof(u32.shmid_ds32))))
1528 			goto done;
1529 		freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1530 		    &u.shmid_ds.shm_perm);
1531 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1532 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1533 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1534 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1535 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1536 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1537 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1538 	}
1539 
1540 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1541 	if (error)
1542 		goto done;
1543 
1544 	/* Cases in which we need to copyout */
1545 	switch (uap->cmd) {
1546 	case IPC_INFO:
1547 		CP(u.shminfo, u32.shminfo32, shmmax);
1548 		CP(u.shminfo, u32.shminfo32, shmmin);
1549 		CP(u.shminfo, u32.shminfo32, shmmni);
1550 		CP(u.shminfo, u32.shminfo32, shmseg);
1551 		CP(u.shminfo, u32.shminfo32, shmall);
1552 		error = copyout(&u32.shminfo32, uap->buf,
1553 		    sizeof(u32.shminfo32));
1554 		break;
1555 	case SHM_INFO:
1556 		CP(u.shm_info, u32.shm_info32, used_ids);
1557 		CP(u.shm_info, u32.shm_info32, shm_rss);
1558 		CP(u.shm_info, u32.shm_info32, shm_tot);
1559 		CP(u.shm_info, u32.shm_info32, shm_swp);
1560 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1561 		CP(u.shm_info, u32.shm_info32, swap_successes);
1562 		error = copyout(&u32.shm_info32, uap->buf,
1563 		    sizeof(u32.shm_info32));
1564 		break;
1565 	case SHM_STAT:
1566 	case IPC_STAT:
1567 		freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1568 		    &u32.shmid_ds32.shm_perm);
1569 		if (u.shmid_ds.shm_segsz > INT32_MAX)
1570 			u32.shmid_ds32.shm_segsz = INT32_MAX;
1571 		else
1572 			CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1573 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1574 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1575 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1576 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1577 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1578 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1579 		error = copyout(&u32.shmid_ds32, uap->buf,
1580 		    sizeof(u32.shmid_ds32));
1581 		break;
1582 	}
1583 
1584 done:
1585 	if (error) {
1586 		/* Invalidate the return value */
1587 		td->td_retval[0] = -1;
1588 	}
1589 	return (error);
1590 }
1591 #endif
1592 
1593 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1594     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1595 
1596 #ifndef CP
1597 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1598 #endif
1599 
1600 #ifndef _SYS_SYSPROTO_H_
1601 struct freebsd7_shmctl_args {
1602 	int shmid;
1603 	int cmd;
1604 	struct shmid_ds_old *buf;
1605 };
1606 #endif
1607 int
1608 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1609 {
1610 	int error;
1611 	struct shmid_ds_old old;
1612 	struct shmid_ds buf;
1613 	size_t bufsz;
1614 
1615 	/*
1616 	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1617 	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
1618 	 * return an error back to the user since we do not to support this.
1619 	 */
1620 	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1621 	    uap->cmd == SHM_STAT)
1622 		return (EINVAL);
1623 
1624 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1625 	if (uap->cmd == IPC_SET) {
1626 		if ((error = copyin(uap->buf, &old, sizeof(old))))
1627 			goto done;
1628 		ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1629 		CP(old, buf, shm_segsz);
1630 		CP(old, buf, shm_lpid);
1631 		CP(old, buf, shm_cpid);
1632 		CP(old, buf, shm_nattch);
1633 		CP(old, buf, shm_atime);
1634 		CP(old, buf, shm_dtime);
1635 		CP(old, buf, shm_ctime);
1636 	}
1637 
1638 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1639 	if (error)
1640 		goto done;
1641 
1642 	/* Cases in which we need to copyout */
1643 	switch (uap->cmd) {
1644 	case IPC_STAT:
1645 		memset(&old, 0, sizeof(old));
1646 		ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1647 		if (buf.shm_segsz > INT_MAX)
1648 			old.shm_segsz = INT_MAX;
1649 		else
1650 			CP(buf, old, shm_segsz);
1651 		CP(buf, old, shm_lpid);
1652 		CP(buf, old, shm_cpid);
1653 		if (buf.shm_nattch > SHRT_MAX)
1654 			old.shm_nattch = SHRT_MAX;
1655 		else
1656 			CP(buf, old, shm_nattch);
1657 		CP(buf, old, shm_atime);
1658 		CP(buf, old, shm_dtime);
1659 		CP(buf, old, shm_ctime);
1660 		old.shm_internal = NULL;
1661 		error = copyout(&old, uap->buf, sizeof(old));
1662 		break;
1663 	}
1664 
1665 done:
1666 	if (error) {
1667 		/* Invalidate the return value */
1668 		td->td_retval[0] = -1;
1669 	}
1670 	return (error);
1671 }
1672 
1673 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1674 	   COMPAT_FREEBSD7 */
1675 
1676 static int
1677 sysvshm_modload(struct module *module, int cmd, void *arg)
1678 {
1679 	int error = 0;
1680 
1681 	switch (cmd) {
1682 	case MOD_LOAD:
1683 		error = shminit();
1684 		if (error != 0)
1685 			shmunload();
1686 		break;
1687 	case MOD_UNLOAD:
1688 		error = shmunload();
1689 		break;
1690 	case MOD_SHUTDOWN:
1691 		break;
1692 	default:
1693 		error = EINVAL;
1694 		break;
1695 	}
1696 	return (error);
1697 }
1698 
1699 static moduledata_t sysvshm_mod = {
1700 	"sysvshm",
1701 	&sysvshm_modload,
1702 	NULL
1703 };
1704 
1705 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1706 MODULE_VERSION(sysvshm, 1);
1707