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