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