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