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