FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_shm.c
1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Adam Glass and Charles
17 * Hannum.
18 * 4. The names of the authors may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $NetBSD: sysv_shm.c,v 1.39 1997/10/07 10:02:03 drochner Exp $
33 */
34 /*-
35 * Copyright (c) 2003-2005 McAfee, Inc.
36 * Copyright (c) 2016-2017 Robert N. M. Watson
37 * All rights reserved.
38 *
39 * This software was developed for the FreeBSD Project in part by McAfee
40 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
41 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
42 * program.
43 *
44 * Portions of this software were developed by BAE Systems, the University of
45 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
46 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
47 * Computing (TC) research program.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
73
74 #include "opt_sysvipc.h"
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/abi_compat.h>
79 #include <sys/kernel.h>
80 #include <sys/limits.h>
81 #include <sys/lock.h>
82 #include <sys/sysctl.h>
83 #include <sys/shm.h>
84 #include <sys/proc.h>
85 #include <sys/malloc.h>
86 #include <sys/mman.h>
87 #include <sys/module.h>
88 #include <sys/mutex.h>
89 #include <sys/racct.h>
90 #include <sys/resourcevar.h>
91 #include <sys/rwlock.h>
92 #include <sys/stat.h>
93 #include <sys/syscall.h>
94 #include <sys/syscallsubr.h>
95 #include <sys/sysent.h>
96 #include <sys/sysproto.h>
97 #include <sys/jail.h>
98
99 #include <security/audit/audit.h>
100 #include <security/mac/mac_framework.h>
101
102 #include <vm/vm.h>
103 #include <vm/vm_param.h>
104 #include <vm/pmap.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_map.h>
107 #include <vm/vm_page.h>
108 #include <vm/vm_pager.h>
109
110 FEATURE(sysv_shm, "System V shared memory segments support");
111
112 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
113
114 #define SHMSEG_FREE 0x0200
115 #define SHMSEG_REMOVED 0x0400
116 #define SHMSEG_ALLOCATED 0x0800
117
118 static int shm_last_free, shm_nused, shmalloced;
119 vm_size_t shm_committed;
120 static struct shmid_kernel *shmsegs;
121 static unsigned shm_prison_slot;
122
123 struct shmmap_state {
124 vm_offset_t va;
125 int shmid;
126 };
127
128 static void shm_deallocate_segment(struct shmid_kernel *);
129 static int shm_find_segment_by_key(struct prison *, key_t);
130 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
131 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
132 static int shmget_allocate_segment(struct thread *td, key_t key, size_t size,
133 int mode);
134 static int shmget_existing(struct thread *td, size_t size, int shmflg,
135 int mode, int segnum);
136 static void shmrealloc(void);
137 static int shminit(void);
138 static int sysvshm_modload(struct module *, int, void *);
139 static int shmunload(void);
140 static void shmexit_myhook(struct vmspace *vm);
141 static void shmfork_myhook(struct proc *p1, struct proc *p2);
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 131072 /* 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
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 *
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
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
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
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 *
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
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
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
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
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
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
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
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
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
611 #ifndef _SYS_SYSPROTO_H_
612 struct shmctl_args {
613 int shmid;
614 int cmd;
615 struct shmid_ds *buf;
616 };
617 #endif
618 int
619 sys_shmctl(struct thread *td, struct shmctl_args *uap)
620 {
621 int error;
622 struct shmid_ds buf;
623 size_t bufsz;
624
625 /*
626 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
627 * Linux binaries. If we see the call come through the FreeBSD ABI,
628 * return an error back to the user since we do not to support this.
629 */
630 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
631 uap->cmd == SHM_STAT)
632 return (EINVAL);
633
634 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
635 if (uap->cmd == IPC_SET) {
636 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
637 goto done;
638 }
639
640 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
641 if (error)
642 goto done;
643
644 /* Cases in which we need to copyout */
645 switch (uap->cmd) {
646 case IPC_STAT:
647 error = copyout(&buf, uap->buf, bufsz);
648 break;
649 }
650
651 done:
652 if (error) {
653 /* Invalidate the return value */
654 td->td_retval[0] = -1;
655 }
656 return (error);
657 }
658
659
660 static int
661 shmget_existing(struct thread *td, size_t size, int shmflg, int mode,
662 int segnum)
663 {
664 struct shmid_kernel *shmseg;
665 #ifdef MAC
666 int error;
667 #endif
668
669 SYSVSHM_ASSERT_LOCKED();
670 KASSERT(segnum >= 0 && segnum < shmalloced,
671 ("segnum %d shmalloced %d", segnum, shmalloced));
672 shmseg = &shmsegs[segnum];
673 if ((shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
674 return (EEXIST);
675 #ifdef MAC
676 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, shmflg);
677 if (error != 0)
678 return (error);
679 #endif
680 if (size != 0 && size > shmseg->u.shm_segsz)
681 return (EINVAL);
682 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
683 return (0);
684 }
685
686 static int
687 shmget_allocate_segment(struct thread *td, key_t key, size_t size, int mode)
688 {
689 struct ucred *cred = td->td_ucred;
690 struct shmid_kernel *shmseg;
691 vm_object_t shm_object;
692 int i, segnum;
693
694 SYSVSHM_ASSERT_LOCKED();
695
696 if (size < shminfo.shmmin || size > shminfo.shmmax)
697 return (EINVAL);
698 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
699 return (ENOSPC);
700 size = round_page(size);
701 if (shm_committed + btoc(size) > shminfo.shmall)
702 return (ENOMEM);
703 if (shm_last_free < 0) {
704 shmrealloc(); /* Maybe expand the shmsegs[] array. */
705 for (i = 0; i < shmalloced; i++)
706 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
707 break;
708 if (i == shmalloced)
709 return (ENOSPC);
710 segnum = i;
711 } else {
712 segnum = shm_last_free;
713 shm_last_free = -1;
714 }
715 KASSERT(segnum >= 0 && segnum < shmalloced,
716 ("segnum %d shmalloced %d", segnum, shmalloced));
717 shmseg = &shmsegs[segnum];
718 #ifdef RACCT
719 if (racct_enable) {
720 PROC_LOCK(td->td_proc);
721 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
722 PROC_UNLOCK(td->td_proc);
723 return (ENOSPC);
724 }
725 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
726 racct_sub(td->td_proc, RACCT_NSHM, 1);
727 PROC_UNLOCK(td->td_proc);
728 return (ENOMEM);
729 }
730 PROC_UNLOCK(td->td_proc);
731 }
732 #endif
733
734 /*
735 * We make sure that we have allocated a pager before we need
736 * to.
737 */
738 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
739 0, size, VM_PROT_DEFAULT, 0, cred);
740 if (shm_object == NULL) {
741 #ifdef RACCT
742 if (racct_enable) {
743 PROC_LOCK(td->td_proc);
744 racct_sub(td->td_proc, RACCT_NSHM, 1);
745 racct_sub(td->td_proc, RACCT_SHMSIZE, size);
746 PROC_UNLOCK(td->td_proc);
747 }
748 #endif
749 return (ENOMEM);
750 }
751 shm_object->pg_color = 0;
752 VM_OBJECT_WLOCK(shm_object);
753 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
754 vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
755 VM_OBJECT_WUNLOCK(shm_object);
756
757 shmseg->object = shm_object;
758 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
759 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
760 shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
761 shmseg->u.shm_perm.key = key;
762 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
763 shmseg->cred = crhold(cred);
764 shmseg->u.shm_segsz = size;
765 shmseg->u.shm_cpid = td->td_proc->p_pid;
766 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
767 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
768 #ifdef MAC
769 mac_sysvshm_create(cred, shmseg);
770 #endif
771 shmseg->u.shm_ctime = time_second;
772 shm_committed += btoc(size);
773 shm_nused++;
774 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
775
776 return (0);
777 }
778
779 #ifndef _SYS_SYSPROTO_H_
780 struct shmget_args {
781 key_t key;
782 size_t size;
783 int shmflg;
784 };
785 #endif
786 int
787 sys_shmget(struct thread *td, struct shmget_args *uap)
788 {
789 int segnum, mode;
790 int error;
791
792 if (shm_find_prison(td->td_ucred) == NULL)
793 return (ENOSYS);
794 mode = uap->shmflg & ACCESSPERMS;
795 SYSVSHM_LOCK();
796 if (uap->key == IPC_PRIVATE) {
797 error = shmget_allocate_segment(td, uap->key, uap->size, mode);
798 } else {
799 segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
800 uap->key);
801 if (segnum >= 0)
802 error = shmget_existing(td, uap->size, uap->shmflg,
803 mode, segnum);
804 else if ((uap->shmflg & IPC_CREAT) == 0)
805 error = ENOENT;
806 else
807 error = shmget_allocate_segment(td, uap->key,
808 uap->size, mode);
809 }
810 SYSVSHM_UNLOCK();
811 return (error);
812 }
813
814 static void
815 shmfork_myhook(struct proc *p1, struct proc *p2)
816 {
817 struct shmmap_state *shmmap_s;
818 size_t size;
819 int i;
820
821 SYSVSHM_LOCK();
822 size = shminfo.shmseg * sizeof(struct shmmap_state);
823 shmmap_s = malloc(size, M_SHM, M_WAITOK);
824 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
825 p2->p_vmspace->vm_shm = shmmap_s;
826 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
827 if (shmmap_s->shmid != -1) {
828 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
829 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
830 ("segnum %d shmalloced %d",
831 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
832 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
833 }
834 }
835 SYSVSHM_UNLOCK();
836 }
837
838 static void
839 shmexit_myhook(struct vmspace *vm)
840 {
841 struct shmmap_state *base, *shm;
842 int i;
843
844 base = vm->vm_shm;
845 if (base != NULL) {
846 vm->vm_shm = NULL;
847 SYSVSHM_LOCK();
848 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
849 if (shm->shmid != -1)
850 shm_delete_mapping(vm, shm);
851 }
852 SYSVSHM_UNLOCK();
853 free(base, M_SHM);
854 }
855 }
856
857 static void
858 shmrealloc(void)
859 {
860 struct shmid_kernel *newsegs;
861 int i;
862
863 SYSVSHM_ASSERT_LOCKED();
864
865 if (shmalloced >= shminfo.shmmni)
866 return;
867
868 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
869 M_WAITOK | M_ZERO);
870 for (i = 0; i < shmalloced; i++)
871 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
872 for (; i < shminfo.shmmni; i++) {
873 newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
874 newsegs[i].u.shm_perm.seq = 0;
875 #ifdef MAC
876 mac_sysvshm_init(&newsegs[i]);
877 #endif
878 }
879 free(shmsegs, M_SHM);
880 shmsegs = newsegs;
881 shmalloced = shminfo.shmmni;
882 }
883
884 static struct syscall_helper_data shm_syscalls[] = {
885 SYSCALL_INIT_HELPER(shmat),
886 SYSCALL_INIT_HELPER(shmctl),
887 SYSCALL_INIT_HELPER(shmdt),
888 SYSCALL_INIT_HELPER(shmget),
889 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
890 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
891 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
892 #endif
893 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
894 SYSCALL_INIT_HELPER(shmsys),
895 #endif
896 SYSCALL_INIT_LAST
897 };
898
899 #ifdef COMPAT_FREEBSD32
900 #include <compat/freebsd32/freebsd32.h>
901 #include <compat/freebsd32/freebsd32_ipc.h>
902 #include <compat/freebsd32/freebsd32_proto.h>
903 #include <compat/freebsd32/freebsd32_signal.h>
904 #include <compat/freebsd32/freebsd32_syscall.h>
905 #include <compat/freebsd32/freebsd32_util.h>
906
907 static struct syscall_helper_data shm32_syscalls[] = {
908 SYSCALL32_INIT_HELPER_COMPAT(shmat),
909 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
910 SYSCALL32_INIT_HELPER_COMPAT(shmget),
911 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
912 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
913 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
914 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
915 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
916 #endif
917 SYSCALL_INIT_LAST
918 };
919 #endif
920
921 static int
922 shminit(void)
923 {
924 struct prison *pr;
925 void **rsv;
926 int i, error;
927 osd_method_t methods[PR_MAXMETHOD] = {
928 [PR_METHOD_CHECK] = shm_prison_check,
929 [PR_METHOD_SET] = shm_prison_set,
930 [PR_METHOD_GET] = shm_prison_get,
931 [PR_METHOD_REMOVE] = shm_prison_remove,
932 };
933
934 #ifndef BURN_BRIDGES
935 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
936 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
937 #endif
938 if (shminfo.shmmax == SHMMAX) {
939 /* Initialize shmmax dealing with possible overflow. */
940 for (i = PAGE_SIZE; i != 0; i--) {
941 shminfo.shmmax = shminfo.shmall * i;
942 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
943 break;
944 }
945 }
946 shmalloced = shminfo.shmmni;
947 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
948 M_WAITOK|M_ZERO);
949 for (i = 0; i < shmalloced; i++) {
950 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
951 shmsegs[i].u.shm_perm.seq = 0;
952 #ifdef MAC
953 mac_sysvshm_init(&shmsegs[i]);
954 #endif
955 }
956 shm_last_free = 0;
957 shm_nused = 0;
958 shm_committed = 0;
959 sx_init(&sysvshmsx, "sysvshmsx");
960 shmexit_hook = &shmexit_myhook;
961 shmfork_hook = &shmfork_myhook;
962
963 /* Set current prisons according to their allow.sysvipc. */
964 shm_prison_slot = osd_jail_register(NULL, methods);
965 rsv = osd_reserve(shm_prison_slot);
966 prison_lock(&prison0);
967 (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
968 prison_unlock(&prison0);
969 rsv = NULL;
970 sx_slock(&allprison_lock);
971 TAILQ_FOREACH(pr, &allprison, pr_list) {
972 if (rsv == NULL)
973 rsv = osd_reserve(shm_prison_slot);
974 prison_lock(pr);
975 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
976 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
977 &prison0);
978 rsv = NULL;
979 }
980 prison_unlock(pr);
981 }
982 if (rsv != NULL)
983 osd_free_reserved(rsv);
984 sx_sunlock(&allprison_lock);
985
986 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
987 if (error != 0)
988 return (error);
989 #ifdef COMPAT_FREEBSD32
990 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
991 if (error != 0)
992 return (error);
993 #endif
994 return (0);
995 }
996
997 static int
998 shmunload(void)
999 {
1000 int i;
1001
1002 if (shm_nused > 0)
1003 return (EBUSY);
1004
1005 #ifdef COMPAT_FREEBSD32
1006 syscall32_helper_unregister(shm32_syscalls);
1007 #endif
1008 syscall_helper_unregister(shm_syscalls);
1009 if (shm_prison_slot != 0)
1010 osd_jail_deregister(shm_prison_slot);
1011
1012 for (i = 0; i < shmalloced; i++) {
1013 #ifdef MAC
1014 mac_sysvshm_destroy(&shmsegs[i]);
1015 #endif
1016 /*
1017 * Objects might be still mapped into the processes
1018 * address spaces. Actual free would happen on the
1019 * last mapping destruction.
1020 */
1021 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1022 vm_object_deallocate(shmsegs[i].object);
1023 }
1024 free(shmsegs, M_SHM);
1025 shmexit_hook = NULL;
1026 shmfork_hook = NULL;
1027 sx_destroy(&sysvshmsx);
1028 return (0);
1029 }
1030
1031 static int
1032 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1033 {
1034 struct shmid_kernel tshmseg;
1035 #ifdef COMPAT_FREEBSD32
1036 struct shmid_kernel32 tshmseg32;
1037 #endif
1038 struct prison *pr, *rpr;
1039 void *outaddr;
1040 size_t outsize;
1041 int error, i;
1042
1043 SYSVSHM_LOCK();
1044 pr = req->td->td_ucred->cr_prison;
1045 rpr = shm_find_prison(req->td->td_ucred);
1046 error = 0;
1047 for (i = 0; i < shmalloced; i++) {
1048 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1049 rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1050 bzero(&tshmseg, sizeof(tshmseg));
1051 tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1052 } else {
1053 tshmseg = shmsegs[i];
1054 if (tshmseg.cred->cr_prison != pr)
1055 tshmseg.u.shm_perm.key = IPC_PRIVATE;
1056 }
1057 #ifdef COMPAT_FREEBSD32
1058 if (SV_CURPROC_FLAG(SV_ILP32)) {
1059 bzero(&tshmseg32, sizeof(tshmseg32));
1060 freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1061 &tshmseg32.u.shm_perm);
1062 CP(tshmseg, tshmseg32, u.shm_segsz);
1063 CP(tshmseg, tshmseg32, u.shm_lpid);
1064 CP(tshmseg, tshmseg32, u.shm_cpid);
1065 CP(tshmseg, tshmseg32, u.shm_nattch);
1066 CP(tshmseg, tshmseg32, u.shm_atime);
1067 CP(tshmseg, tshmseg32, u.shm_dtime);
1068 CP(tshmseg, tshmseg32, u.shm_ctime);
1069 /* Don't copy object, label, or cred */
1070 outaddr = &tshmseg32;
1071 outsize = sizeof(tshmseg32);
1072 } else
1073 #endif
1074 {
1075 tshmseg.object = NULL;
1076 tshmseg.label = NULL;
1077 tshmseg.cred = NULL;
1078 outaddr = &tshmseg;
1079 outsize = sizeof(tshmseg);
1080 }
1081 error = SYSCTL_OUT(req, outaddr, outsize);
1082 if (error != 0)
1083 break;
1084 }
1085 SYSVSHM_UNLOCK();
1086 return (error);
1087 }
1088
1089 static int
1090 shm_prison_check(void *obj, void *data)
1091 {
1092 struct prison *pr = obj;
1093 struct prison *prpr;
1094 struct vfsoptlist *opts = data;
1095 int error, jsys;
1096
1097 /*
1098 * sysvshm is a jailsys integer.
1099 * It must be "disable" if the parent jail is disabled.
1100 */
1101 error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1102 if (error != ENOENT) {
1103 if (error != 0)
1104 return (error);
1105 switch (jsys) {
1106 case JAIL_SYS_DISABLE:
1107 break;
1108 case JAIL_SYS_NEW:
1109 case JAIL_SYS_INHERIT:
1110 prison_lock(pr->pr_parent);
1111 prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1112 prison_unlock(pr->pr_parent);
1113 if (prpr == NULL)
1114 return (EPERM);
1115 break;
1116 default:
1117 return (EINVAL);
1118 }
1119 }
1120
1121 return (0);
1122 }
1123
1124 static int
1125 shm_prison_set(void *obj, void *data)
1126 {
1127 struct prison *pr = obj;
1128 struct prison *tpr, *orpr, *nrpr, *trpr;
1129 struct vfsoptlist *opts = data;
1130 void *rsv;
1131 int jsys, descend;
1132
1133 /*
1134 * sysvshm controls which jail is the root of the associated segments
1135 * (this jail or same as the parent), or if the feature is available
1136 * at all.
1137 */
1138 if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1139 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1140 ? JAIL_SYS_INHERIT
1141 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1142 ? JAIL_SYS_DISABLE
1143 : -1;
1144 if (jsys == JAIL_SYS_DISABLE) {
1145 prison_lock(pr);
1146 orpr = osd_jail_get(pr, shm_prison_slot);
1147 if (orpr != NULL)
1148 osd_jail_del(pr, shm_prison_slot);
1149 prison_unlock(pr);
1150 if (orpr != NULL) {
1151 if (orpr == pr)
1152 shm_prison_cleanup(pr);
1153 /* Disable all child jails as well. */
1154 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1155 prison_lock(tpr);
1156 trpr = osd_jail_get(tpr, shm_prison_slot);
1157 if (trpr != NULL) {
1158 osd_jail_del(tpr, shm_prison_slot);
1159 prison_unlock(tpr);
1160 if (trpr == tpr)
1161 shm_prison_cleanup(tpr);
1162 } else {
1163 prison_unlock(tpr);
1164 descend = 0;
1165 }
1166 }
1167 }
1168 } else if (jsys != -1) {
1169 if (jsys == JAIL_SYS_NEW)
1170 nrpr = pr;
1171 else {
1172 prison_lock(pr->pr_parent);
1173 nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1174 prison_unlock(pr->pr_parent);
1175 }
1176 rsv = osd_reserve(shm_prison_slot);
1177 prison_lock(pr);
1178 orpr = osd_jail_get(pr, shm_prison_slot);
1179 if (orpr != nrpr)
1180 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1181 nrpr);
1182 else
1183 osd_free_reserved(rsv);
1184 prison_unlock(pr);
1185 if (orpr != nrpr) {
1186 if (orpr == pr)
1187 shm_prison_cleanup(pr);
1188 if (orpr != NULL) {
1189 /* Change child jails matching the old root, */
1190 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1191 prison_lock(tpr);
1192 trpr = osd_jail_get(tpr,
1193 shm_prison_slot);
1194 if (trpr == orpr) {
1195 (void)osd_jail_set(tpr,
1196 shm_prison_slot, nrpr);
1197 prison_unlock(tpr);
1198 if (trpr == tpr)
1199 shm_prison_cleanup(tpr);
1200 } else {
1201 prison_unlock(tpr);
1202 descend = 0;
1203 }
1204 }
1205 }
1206 }
1207 }
1208
1209 return (0);
1210 }
1211
1212 static int
1213 shm_prison_get(void *obj, void *data)
1214 {
1215 struct prison *pr = obj;
1216 struct prison *rpr;
1217 struct vfsoptlist *opts = data;
1218 int error, jsys;
1219
1220 /* Set sysvshm based on the jail's root prison. */
1221 prison_lock(pr);
1222 rpr = osd_jail_get(pr, shm_prison_slot);
1223 prison_unlock(pr);
1224 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1225 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1226 error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1227 if (error == ENOENT)
1228 error = 0;
1229 return (error);
1230 }
1231
1232 static int
1233 shm_prison_remove(void *obj, void *data __unused)
1234 {
1235 struct prison *pr = obj;
1236 struct prison *rpr;
1237
1238 SYSVSHM_LOCK();
1239 prison_lock(pr);
1240 rpr = osd_jail_get(pr, shm_prison_slot);
1241 prison_unlock(pr);
1242 if (rpr == pr)
1243 shm_prison_cleanup(pr);
1244 SYSVSHM_UNLOCK();
1245 return (0);
1246 }
1247
1248 static void
1249 shm_prison_cleanup(struct prison *pr)
1250 {
1251 struct shmid_kernel *shmseg;
1252 int i;
1253
1254 /* Remove any segments that belong to this jail. */
1255 for (i = 0; i < shmalloced; i++) {
1256 shmseg = &shmsegs[i];
1257 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1258 shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1259 shm_remove(shmseg, i);
1260 }
1261 }
1262 }
1263
1264 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1265
1266 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1267 struct oshmid_ds {
1268 struct ipc_perm_old shm_perm; /* operation perms */
1269 int shm_segsz; /* size of segment (bytes) */
1270 u_short shm_cpid; /* pid, creator */
1271 u_short shm_lpid; /* pid, last operation */
1272 short shm_nattch; /* no. of current attaches */
1273 time_t shm_atime; /* last attach time */
1274 time_t shm_dtime; /* last detach time */
1275 time_t shm_ctime; /* last change time */
1276 void *shm_handle; /* internal handle for shm segment */
1277 };
1278
1279 struct oshmctl_args {
1280 int shmid;
1281 int cmd;
1282 struct oshmid_ds *ubuf;
1283 };
1284
1285 static int
1286 oshmctl(struct thread *td, struct oshmctl_args *uap)
1287 {
1288 #ifdef COMPAT_43
1289 int error = 0;
1290 struct prison *rpr;
1291 struct shmid_kernel *shmseg;
1292 struct oshmid_ds outbuf;
1293
1294 rpr = shm_find_prison(td->td_ucred);
1295 if (rpr == NULL)
1296 return (ENOSYS);
1297 if (uap->cmd != IPC_STAT) {
1298 return (freebsd7_shmctl(td,
1299 (struct freebsd7_shmctl_args *)uap));
1300 }
1301 SYSVSHM_LOCK();
1302 shmseg = shm_find_segment(rpr, uap->shmid, true);
1303 if (shmseg == NULL) {
1304 SYSVSHM_UNLOCK();
1305 return (EINVAL);
1306 }
1307 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1308 if (error != 0) {
1309 SYSVSHM_UNLOCK();
1310 return (error);
1311 }
1312 #ifdef MAC
1313 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1314 if (error != 0) {
1315 SYSVSHM_UNLOCK();
1316 return (error);
1317 }
1318 #endif
1319 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1320 outbuf.shm_segsz = shmseg->u.shm_segsz;
1321 outbuf.shm_cpid = shmseg->u.shm_cpid;
1322 outbuf.shm_lpid = shmseg->u.shm_lpid;
1323 outbuf.shm_nattch = shmseg->u.shm_nattch;
1324 outbuf.shm_atime = shmseg->u.shm_atime;
1325 outbuf.shm_dtime = shmseg->u.shm_dtime;
1326 outbuf.shm_ctime = shmseg->u.shm_ctime;
1327 outbuf.shm_handle = shmseg->object;
1328 SYSVSHM_UNLOCK();
1329 return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1330 #else
1331 return (EINVAL);
1332 #endif
1333 }
1334
1335 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1336 static sy_call_t *shmcalls[] = {
1337 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1338 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1339 (sy_call_t *)freebsd7_shmctl
1340 };
1341
1342 #ifndef _SYS_SYSPROTO_H_
1343 /* XXX actually varargs. */
1344 struct shmsys_args {
1345 int which;
1346 int a2;
1347 int a3;
1348 int a4;
1349 };
1350 #endif
1351 int
1352 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1353 {
1354
1355 AUDIT_ARG_SVIPC_WHICH(uap->which);
1356 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1357 return (EINVAL);
1358 return ((*shmcalls[uap->which])(td, &uap->a2));
1359 }
1360
1361 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1362
1363 #ifdef COMPAT_FREEBSD32
1364
1365 int
1366 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1367 {
1368
1369 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1370 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1371 AUDIT_ARG_SVIPC_WHICH(uap->which);
1372 switch (uap->which) {
1373 case 0: { /* shmat */
1374 struct shmat_args ap;
1375
1376 ap.shmid = uap->a2;
1377 ap.shmaddr = PTRIN(uap->a3);
1378 ap.shmflg = uap->a4;
1379 return (sysent[SYS_shmat].sy_call(td, &ap));
1380 }
1381 case 2: { /* shmdt */
1382 struct shmdt_args ap;
1383
1384 ap.shmaddr = PTRIN(uap->a2);
1385 return (sysent[SYS_shmdt].sy_call(td, &ap));
1386 }
1387 case 3: { /* shmget */
1388 struct shmget_args ap;
1389
1390 ap.key = uap->a2;
1391 ap.size = uap->a3;
1392 ap.shmflg = uap->a4;
1393 return (sysent[SYS_shmget].sy_call(td, &ap));
1394 }
1395 case 4: { /* shmctl */
1396 struct freebsd7_freebsd32_shmctl_args ap;
1397
1398 ap.shmid = uap->a2;
1399 ap.cmd = uap->a3;
1400 ap.buf = PTRIN(uap->a4);
1401 return (freebsd7_freebsd32_shmctl(td, &ap));
1402 }
1403 case 1: /* oshmctl */
1404 default:
1405 return (EINVAL);
1406 }
1407 #else
1408 return (nosys(td, NULL));
1409 #endif
1410 }
1411
1412 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1413 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1414 int
1415 freebsd7_freebsd32_shmctl(struct thread *td,
1416 struct freebsd7_freebsd32_shmctl_args *uap)
1417 {
1418 int error;
1419 union {
1420 struct shmid_ds shmid_ds;
1421 struct shm_info shm_info;
1422 struct shminfo shminfo;
1423 } u;
1424 union {
1425 struct shmid_ds32_old shmid_ds32;
1426 struct shm_info32 shm_info32;
1427 struct shminfo32 shminfo32;
1428 } u32;
1429 size_t sz;
1430
1431 if (uap->cmd == IPC_SET) {
1432 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1433 sizeof(u32.shmid_ds32))))
1434 goto done;
1435 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1436 &u.shmid_ds.shm_perm);
1437 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1438 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1439 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1440 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1441 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1442 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1443 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1444 }
1445
1446 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1447 if (error)
1448 goto done;
1449
1450 /* Cases in which we need to copyout */
1451 switch (uap->cmd) {
1452 case IPC_INFO:
1453 CP(u.shminfo, u32.shminfo32, shmmax);
1454 CP(u.shminfo, u32.shminfo32, shmmin);
1455 CP(u.shminfo, u32.shminfo32, shmmni);
1456 CP(u.shminfo, u32.shminfo32, shmseg);
1457 CP(u.shminfo, u32.shminfo32, shmall);
1458 error = copyout(&u32.shminfo32, uap->buf,
1459 sizeof(u32.shminfo32));
1460 break;
1461 case SHM_INFO:
1462 CP(u.shm_info, u32.shm_info32, used_ids);
1463 CP(u.shm_info, u32.shm_info32, shm_rss);
1464 CP(u.shm_info, u32.shm_info32, shm_tot);
1465 CP(u.shm_info, u32.shm_info32, shm_swp);
1466 CP(u.shm_info, u32.shm_info32, swap_attempts);
1467 CP(u.shm_info, u32.shm_info32, swap_successes);
1468 error = copyout(&u32.shm_info32, uap->buf,
1469 sizeof(u32.shm_info32));
1470 break;
1471 case SHM_STAT:
1472 case IPC_STAT:
1473 memset(&u32.shmid_ds32, 0, sizeof(u32.shmid_ds32));
1474 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1475 &u32.shmid_ds32.shm_perm);
1476 if (u.shmid_ds.shm_segsz > INT32_MAX)
1477 u32.shmid_ds32.shm_segsz = INT32_MAX;
1478 else
1479 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1480 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1481 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1482 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1483 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1484 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1485 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1486 u32.shmid_ds32.shm_internal = 0;
1487 error = copyout(&u32.shmid_ds32, uap->buf,
1488 sizeof(u32.shmid_ds32));
1489 break;
1490 }
1491
1492 done:
1493 if (error) {
1494 /* Invalidate the return value */
1495 td->td_retval[0] = -1;
1496 }
1497 return (error);
1498 }
1499 #endif
1500
1501 int
1502 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1503 {
1504 int error;
1505 union {
1506 struct shmid_ds shmid_ds;
1507 struct shm_info shm_info;
1508 struct shminfo shminfo;
1509 } u;
1510 union {
1511 struct shmid_ds32 shmid_ds32;
1512 struct shm_info32 shm_info32;
1513 struct shminfo32 shminfo32;
1514 } u32;
1515 size_t sz;
1516
1517 if (uap->cmd == IPC_SET) {
1518 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1519 sizeof(u32.shmid_ds32))))
1520 goto done;
1521 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1522 &u.shmid_ds.shm_perm);
1523 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1524 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1525 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1526 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1527 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1528 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1529 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1530 }
1531
1532 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1533 if (error)
1534 goto done;
1535
1536 /* Cases in which we need to copyout */
1537 switch (uap->cmd) {
1538 case IPC_INFO:
1539 CP(u.shminfo, u32.shminfo32, shmmax);
1540 CP(u.shminfo, u32.shminfo32, shmmin);
1541 CP(u.shminfo, u32.shminfo32, shmmni);
1542 CP(u.shminfo, u32.shminfo32, shmseg);
1543 CP(u.shminfo, u32.shminfo32, shmall);
1544 error = copyout(&u32.shminfo32, uap->buf,
1545 sizeof(u32.shminfo32));
1546 break;
1547 case SHM_INFO:
1548 CP(u.shm_info, u32.shm_info32, used_ids);
1549 CP(u.shm_info, u32.shm_info32, shm_rss);
1550 CP(u.shm_info, u32.shm_info32, shm_tot);
1551 CP(u.shm_info, u32.shm_info32, shm_swp);
1552 CP(u.shm_info, u32.shm_info32, swap_attempts);
1553 CP(u.shm_info, u32.shm_info32, swap_successes);
1554 error = copyout(&u32.shm_info32, uap->buf,
1555 sizeof(u32.shm_info32));
1556 break;
1557 case SHM_STAT:
1558 case IPC_STAT:
1559 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1560 &u32.shmid_ds32.shm_perm);
1561 if (u.shmid_ds.shm_segsz > INT32_MAX)
1562 u32.shmid_ds32.shm_segsz = INT32_MAX;
1563 else
1564 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1565 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1566 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1567 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1568 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1569 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1570 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1571 error = copyout(&u32.shmid_ds32, uap->buf,
1572 sizeof(u32.shmid_ds32));
1573 break;
1574 }
1575
1576 done:
1577 if (error) {
1578 /* Invalidate the return value */
1579 td->td_retval[0] = -1;
1580 }
1581 return (error);
1582 }
1583 #endif
1584
1585 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1586 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1587
1588 #ifndef _SYS_SYSPROTO_H_
1589 struct freebsd7_shmctl_args {
1590 int shmid;
1591 int cmd;
1592 struct shmid_ds_old *buf;
1593 };
1594 #endif
1595 int
1596 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1597 {
1598 int error;
1599 struct shmid_ds_old old;
1600 struct shmid_ds buf;
1601 size_t bufsz;
1602
1603 /*
1604 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1605 * Linux binaries. If we see the call come through the FreeBSD ABI,
1606 * return an error back to the user since we do not to support this.
1607 */
1608 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1609 uap->cmd == SHM_STAT)
1610 return (EINVAL);
1611
1612 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1613 if (uap->cmd == IPC_SET) {
1614 if ((error = copyin(uap->buf, &old, sizeof(old))))
1615 goto done;
1616 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1617 CP(old, buf, shm_segsz);
1618 CP(old, buf, shm_lpid);
1619 CP(old, buf, shm_cpid);
1620 CP(old, buf, shm_nattch);
1621 CP(old, buf, shm_atime);
1622 CP(old, buf, shm_dtime);
1623 CP(old, buf, shm_ctime);
1624 }
1625
1626 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1627 if (error)
1628 goto done;
1629
1630 /* Cases in which we need to copyout */
1631 switch (uap->cmd) {
1632 case IPC_STAT:
1633 memset(&old, 0, sizeof(old));
1634 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1635 if (buf.shm_segsz > INT_MAX)
1636 old.shm_segsz = INT_MAX;
1637 else
1638 CP(buf, old, shm_segsz);
1639 CP(buf, old, shm_lpid);
1640 CP(buf, old, shm_cpid);
1641 if (buf.shm_nattch > SHRT_MAX)
1642 old.shm_nattch = SHRT_MAX;
1643 else
1644 CP(buf, old, shm_nattch);
1645 CP(buf, old, shm_atime);
1646 CP(buf, old, shm_dtime);
1647 CP(buf, old, shm_ctime);
1648 old.shm_internal = NULL;
1649 error = copyout(&old, uap->buf, sizeof(old));
1650 break;
1651 }
1652
1653 done:
1654 if (error) {
1655 /* Invalidate the return value */
1656 td->td_retval[0] = -1;
1657 }
1658 return (error);
1659 }
1660
1661 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1662 COMPAT_FREEBSD7 */
1663
1664 static int
1665 sysvshm_modload(struct module *module, int cmd, void *arg)
1666 {
1667 int error = 0;
1668
1669 switch (cmd) {
1670 case MOD_LOAD:
1671 error = shminit();
1672 if (error != 0)
1673 shmunload();
1674 break;
1675 case MOD_UNLOAD:
1676 error = shmunload();
1677 break;
1678 case MOD_SHUTDOWN:
1679 break;
1680 default:
1681 error = EINVAL;
1682 break;
1683 }
1684 return (error);
1685 }
1686
1687 static moduledata_t sysvshm_mod = {
1688 "sysvshm",
1689 &sysvshm_modload,
1690 NULL
1691 };
1692
1693 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1694 MODULE_VERSION(sysvshm, 1);
Cache object: 03f8c9ab18bcd74eaeea55e3bc458890
|