1 /*- 2 * Implementation of SVID semaphores 3 * 4 * Author: Daniel Boulet 5 * 6 * This software is provided ``AS IS'' without any warranties of any kind. 7 */ 8 /*- 9 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 10 * 11 * Copyright (c) 2003-2005 McAfee, Inc. 12 * Copyright (c) 2016-2017 Robert N. M. Watson 13 * All rights reserved. 14 * 15 * This software was developed for the FreeBSD Project in part by McAfee 16 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR 17 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research 18 * program. 19 * 20 * Portions of this software were developed by BAE Systems, the University of 21 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL 22 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent 23 * Computing (TC) research program. 24 * 25 * Redistribution and use in source and binary forms, with or without 26 * modification, are permitted provided that the following conditions 27 * are met: 28 * 1. Redistributions of source code must retain the above copyright 29 * notice, this list of conditions and the following disclaimer. 30 * 2. Redistributions in binary form must reproduce the above copyright 31 * notice, this list of conditions and the following disclaimer in the 32 * documentation and/or other materials provided with the distribution. 33 * 34 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 */ 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include "opt_sysvipc.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/sysproto.h> 55 #include <sys/abi_compat.h> 56 #include <sys/eventhandler.h> 57 #include <sys/kernel.h> 58 #include <sys/proc.h> 59 #include <sys/lock.h> 60 #include <sys/module.h> 61 #include <sys/mutex.h> 62 #include <sys/racct.h> 63 #include <sys/sem.h> 64 #include <sys/sx.h> 65 #include <sys/syscall.h> 66 #include <sys/syscallsubr.h> 67 #include <sys/sysent.h> 68 #include <sys/sysctl.h> 69 #include <sys/uio.h> 70 #include <sys/malloc.h> 71 #include <sys/jail.h> 72 73 #include <security/audit/audit.h> 74 #include <security/mac/mac_framework.h> 75 76 FEATURE(sysv_sem, "System V semaphores support"); 77 78 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores"); 79 80 #ifdef SEM_DEBUG 81 #define DPRINTF(a) printf a 82 #else 83 #define DPRINTF(a) 84 #endif 85 86 static int seminit(void); 87 static int sysvsem_modload(struct module *, int, void *); 88 static int semunload(void); 89 static void semexit_myhook(void *arg, struct proc *p); 90 static int sysctl_sema(SYSCTL_HANDLER_ARGS); 91 static int semvalid(int semid, struct prison *rpr, 92 struct semid_kernel *semakptr); 93 static void sem_remove(int semidx, struct ucred *cred); 94 static struct prison *sem_find_prison(struct ucred *); 95 static int sem_prison_cansee(struct prison *, struct semid_kernel *); 96 static int sem_prison_check(void *, void *); 97 static int sem_prison_set(void *, void *); 98 static int sem_prison_get(void *, void *); 99 static int sem_prison_remove(void *, void *); 100 static void sem_prison_cleanup(struct prison *); 101 102 #ifndef _SYS_SYSPROTO_H_ 103 struct __semctl_args; 104 int __semctl(struct thread *td, struct __semctl_args *uap); 105 struct semget_args; 106 int semget(struct thread *td, struct semget_args *uap); 107 struct semop_args; 108 int semop(struct thread *td, struct semop_args *uap); 109 #endif 110 111 static struct sem_undo *semu_alloc(struct thread *td); 112 static int semundo_adjust(struct thread *td, struct sem_undo **supptr, 113 int semid, int semseq, int semnum, int adjval); 114 static void semundo_clear(int semid, int semnum); 115 116 static struct mtx sem_mtx; /* semaphore global lock */ 117 static struct mtx sem_undo_mtx; 118 static int semtot = 0; 119 static struct semid_kernel *sema; /* semaphore id pool */ 120 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/ 121 static struct sem *sem; /* semaphore pool */ 122 LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */ 123 LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */ 124 static int *semu; /* undo structure pool */ 125 static eventhandler_tag semexit_tag; 126 static unsigned sem_prison_slot; /* prison OSD slot */ 127 128 #define SEMUNDO_MTX sem_undo_mtx 129 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX); 130 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX); 131 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how)); 132 133 struct sem { 134 u_short semval; /* semaphore value */ 135 pid_t sempid; /* pid of last operation */ 136 u_short semncnt; /* # awaiting semval > cval */ 137 u_short semzcnt; /* # awaiting semval = 0 */ 138 }; 139 140 /* 141 * Undo structure (one per process) 142 */ 143 struct sem_undo { 144 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */ 145 struct proc *un_proc; /* owner of this structure */ 146 short un_cnt; /* # of active entries */ 147 struct undo { 148 short un_adjval; /* adjust on exit values */ 149 short un_num; /* semaphore # */ 150 int un_id; /* semid */ 151 unsigned short un_seq; 152 } un_ent[1]; /* undo entries */ 153 }; 154 155 /* 156 * Configuration parameters 157 */ 158 #ifndef SEMMNI 159 #define SEMMNI 50 /* # of semaphore identifiers */ 160 #endif 161 #ifndef SEMMNS 162 #define SEMMNS 340 /* # of semaphores in system */ 163 #endif 164 #ifndef SEMUME 165 #define SEMUME 50 /* max # of undo entries per process */ 166 #endif 167 #ifndef SEMMNU 168 #define SEMMNU 150 /* # of undo structures in system */ 169 #endif 170 171 /* shouldn't need tuning */ 172 #ifndef SEMMSL 173 #define SEMMSL SEMMNS /* max # of semaphores per id */ 174 #endif 175 #ifndef SEMOPM 176 #define SEMOPM 100 /* max # of operations per semop call */ 177 #endif 178 179 #define SEMVMX 32767 /* semaphore maximum value */ 180 #define SEMAEM 16384 /* adjust on exit max value */ 181 182 /* 183 * Due to the way semaphore memory is allocated, we have to ensure that 184 * SEMUSZ is properly aligned. 185 */ 186 187 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long)) 188 189 /* actual size of an undo structure */ 190 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME])) 191 192 /* 193 * Macro to find a particular sem_undo vector 194 */ 195 #define SEMU(ix) \ 196 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz)) 197 198 /* 199 * semaphore info struct 200 */ 201 struct seminfo seminfo = { 202 .semmni = SEMMNI, /* # of semaphore identifiers */ 203 .semmns = SEMMNS, /* # of semaphores in system */ 204 .semmnu = SEMMNU, /* # of undo structures in system */ 205 .semmsl = SEMMSL, /* max # of semaphores per id */ 206 .semopm = SEMOPM, /* max # of operations per semop call */ 207 .semume = SEMUME, /* max # of undo entries per process */ 208 .semusz = SEMUSZ, /* size in bytes of undo structure */ 209 .semvmx = SEMVMX, /* semaphore maximum value */ 210 .semaem = SEMAEM, /* adjust on exit max value */ 211 }; 212 213 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0, 214 "Number of semaphore identifiers"); 215 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0, 216 "Maximum number of semaphores in the system"); 217 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0, 218 "Maximum number of undo structures in the system"); 219 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RWTUN, &seminfo.semmsl, 0, 220 "Max semaphores per id"); 221 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0, 222 "Max operations per semop call"); 223 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0, 224 "Max undo entries per process"); 225 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0, 226 "Size in bytes of undo structure"); 227 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RWTUN, &seminfo.semvmx, 0, 228 "Semaphore maximum value"); 229 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RWTUN, &seminfo.semaem, 0, 230 "Adjust on exit max value"); 231 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, 232 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 233 NULL, 0, sysctl_sema, "", 234 "Array of struct semid_kernel for each potential semaphore"); 235 236 static struct syscall_helper_data sem_syscalls[] = { 237 SYSCALL_INIT_HELPER(__semctl), 238 SYSCALL_INIT_HELPER(semget), 239 SYSCALL_INIT_HELPER(semop), 240 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 241 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 242 SYSCALL_INIT_HELPER(semsys), 243 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl), 244 #endif 245 SYSCALL_INIT_LAST 246 }; 247 248 #ifdef COMPAT_FREEBSD32 249 #include <compat/freebsd32/freebsd32.h> 250 #include <compat/freebsd32/freebsd32_ipc.h> 251 #include <compat/freebsd32/freebsd32_proto.h> 252 #include <compat/freebsd32/freebsd32_signal.h> 253 #include <compat/freebsd32/freebsd32_syscall.h> 254 #include <compat/freebsd32/freebsd32_util.h> 255 256 static struct syscall_helper_data sem32_syscalls[] = { 257 SYSCALL32_INIT_HELPER(freebsd32_semctl), 258 SYSCALL32_INIT_HELPER_COMPAT(semget), 259 SYSCALL32_INIT_HELPER_COMPAT(semop), 260 SYSCALL32_INIT_HELPER(freebsd32_semsys), 261 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 262 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 263 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl), 264 #endif 265 SYSCALL_INIT_LAST 266 }; 267 #endif 268 269 static int 270 seminit(void) 271 { 272 struct prison *pr; 273 void **rsv; 274 int i, error; 275 osd_method_t methods[PR_MAXMETHOD] = { 276 [PR_METHOD_CHECK] = sem_prison_check, 277 [PR_METHOD_SET] = sem_prison_set, 278 [PR_METHOD_GET] = sem_prison_get, 279 [PR_METHOD_REMOVE] = sem_prison_remove, 280 }; 281 282 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK); 283 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM, 284 M_WAITOK | M_ZERO); 285 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM, 286 M_WAITOK | M_ZERO); 287 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK); 288 289 for (i = 0; i < seminfo.semmni; i++) { 290 sema[i].u.__sem_base = 0; 291 sema[i].u.sem_perm.mode = 0; 292 sema[i].u.sem_perm.seq = 0; 293 #ifdef MAC 294 mac_sysvsem_init(&sema[i]); 295 #endif 296 } 297 for (i = 0; i < seminfo.semmni; i++) 298 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF); 299 LIST_INIT(&semu_free_list); 300 for (i = 0; i < seminfo.semmnu; i++) { 301 struct sem_undo *suptr = SEMU(i); 302 suptr->un_proc = NULL; 303 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 304 } 305 LIST_INIT(&semu_list); 306 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF); 307 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF); 308 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL, 309 EVENTHANDLER_PRI_ANY); 310 311 /* Set current prisons according to their allow.sysvipc. */ 312 sem_prison_slot = osd_jail_register(NULL, methods); 313 rsv = osd_reserve(sem_prison_slot); 314 prison_lock(&prison0); 315 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0); 316 prison_unlock(&prison0); 317 rsv = NULL; 318 sx_slock(&allprison_lock); 319 TAILQ_FOREACH(pr, &allprison, pr_list) { 320 if (rsv == NULL) 321 rsv = osd_reserve(sem_prison_slot); 322 prison_lock(pr); 323 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) { 324 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv, 325 &prison0); 326 rsv = NULL; 327 } 328 prison_unlock(pr); 329 } 330 if (rsv != NULL) 331 osd_free_reserved(rsv); 332 sx_sunlock(&allprison_lock); 333 334 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD); 335 if (error != 0) 336 return (error); 337 #ifdef COMPAT_FREEBSD32 338 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD); 339 if (error != 0) 340 return (error); 341 #endif 342 return (0); 343 } 344 345 static int 346 semunload(void) 347 { 348 int i; 349 350 /* XXXKIB */ 351 if (semtot != 0) 352 return (EBUSY); 353 354 #ifdef COMPAT_FREEBSD32 355 syscall32_helper_unregister(sem32_syscalls); 356 #endif 357 syscall_helper_unregister(sem_syscalls); 358 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag); 359 if (sem_prison_slot != 0) 360 osd_jail_deregister(sem_prison_slot); 361 #ifdef MAC 362 for (i = 0; i < seminfo.semmni; i++) 363 mac_sysvsem_destroy(&sema[i]); 364 #endif 365 free(sem, M_SEM); 366 free(sema, M_SEM); 367 free(semu, M_SEM); 368 for (i = 0; i < seminfo.semmni; i++) 369 mtx_destroy(&sema_mtx[i]); 370 free(sema_mtx, M_SEM); 371 mtx_destroy(&sem_mtx); 372 mtx_destroy(&sem_undo_mtx); 373 return (0); 374 } 375 376 static int 377 sysvsem_modload(struct module *module, int cmd, void *arg) 378 { 379 int error = 0; 380 381 switch (cmd) { 382 case MOD_LOAD: 383 error = seminit(); 384 break; 385 case MOD_UNLOAD: 386 error = semunload(); 387 break; 388 case MOD_SHUTDOWN: 389 break; 390 default: 391 error = EINVAL; 392 break; 393 } 394 return (error); 395 } 396 397 static moduledata_t sysvsem_mod = { 398 "sysvsem", 399 &sysvsem_modload, 400 NULL 401 }; 402 403 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST); 404 MODULE_VERSION(sysvsem, 1); 405 406 /* 407 * Allocate a new sem_undo structure for a process 408 * (returns ptr to structure or NULL if no more room) 409 */ 410 411 static struct sem_undo * 412 semu_alloc(struct thread *td) 413 { 414 struct sem_undo *suptr; 415 416 SEMUNDO_LOCKASSERT(MA_OWNED); 417 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL) 418 return (NULL); 419 LIST_REMOVE(suptr, un_next); 420 LIST_INSERT_HEAD(&semu_list, suptr, un_next); 421 suptr->un_cnt = 0; 422 suptr->un_proc = td->td_proc; 423 return (suptr); 424 } 425 426 static int 427 semu_try_free(struct sem_undo *suptr) 428 { 429 430 SEMUNDO_LOCKASSERT(MA_OWNED); 431 432 if (suptr->un_cnt != 0) 433 return (0); 434 LIST_REMOVE(suptr, un_next); 435 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 436 return (1); 437 } 438 439 /* 440 * Adjust a particular entry for a particular proc 441 */ 442 443 static int 444 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid, 445 int semseq, int semnum, int adjval) 446 { 447 struct proc *p = td->td_proc; 448 struct sem_undo *suptr; 449 struct undo *sunptr; 450 int i; 451 452 SEMUNDO_LOCKASSERT(MA_OWNED); 453 /* Look for and remember the sem_undo if the caller doesn't provide 454 it */ 455 456 suptr = *supptr; 457 if (suptr == NULL) { 458 LIST_FOREACH(suptr, &semu_list, un_next) { 459 if (suptr->un_proc == p) { 460 *supptr = suptr; 461 break; 462 } 463 } 464 if (suptr == NULL) { 465 if (adjval == 0) 466 return(0); 467 suptr = semu_alloc(td); 468 if (suptr == NULL) 469 return (ENOSPC); 470 *supptr = suptr; 471 } 472 } 473 474 /* 475 * Look for the requested entry and adjust it (delete if adjval becomes 476 * 0). 477 */ 478 sunptr = &suptr->un_ent[0]; 479 for (i = 0; i < suptr->un_cnt; i++, sunptr++) { 480 if (sunptr->un_id != semid || sunptr->un_num != semnum) 481 continue; 482 if (adjval != 0) { 483 adjval += sunptr->un_adjval; 484 if (adjval > seminfo.semaem || adjval < -seminfo.semaem) 485 return (ERANGE); 486 } 487 sunptr->un_adjval = adjval; 488 if (sunptr->un_adjval == 0) { 489 suptr->un_cnt--; 490 if (i < suptr->un_cnt) 491 suptr->un_ent[i] = 492 suptr->un_ent[suptr->un_cnt]; 493 if (suptr->un_cnt == 0) 494 semu_try_free(suptr); 495 } 496 return (0); 497 } 498 499 /* Didn't find the right entry - create it */ 500 if (adjval == 0) 501 return (0); 502 if (adjval > seminfo.semaem || adjval < -seminfo.semaem) 503 return (ERANGE); 504 if (suptr->un_cnt != seminfo.semume) { 505 sunptr = &suptr->un_ent[suptr->un_cnt]; 506 suptr->un_cnt++; 507 sunptr->un_adjval = adjval; 508 sunptr->un_id = semid; 509 sunptr->un_num = semnum; 510 sunptr->un_seq = semseq; 511 } else 512 return (EINVAL); 513 return (0); 514 } 515 516 static void 517 semundo_clear(int semid, int semnum) 518 { 519 struct sem_undo *suptr, *suptr1; 520 struct undo *sunptr; 521 int i; 522 523 SEMUNDO_LOCKASSERT(MA_OWNED); 524 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) { 525 sunptr = &suptr->un_ent[0]; 526 for (i = 0; i < suptr->un_cnt; i++, sunptr++) { 527 if (sunptr->un_id != semid) 528 continue; 529 if (semnum == -1 || sunptr->un_num == semnum) { 530 suptr->un_cnt--; 531 if (i < suptr->un_cnt) { 532 suptr->un_ent[i] = 533 suptr->un_ent[suptr->un_cnt]; 534 continue; 535 } 536 semu_try_free(suptr); 537 } 538 if (semnum != -1) 539 break; 540 } 541 } 542 } 543 544 static int 545 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr) 546 { 547 548 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 549 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) || 550 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0); 551 } 552 553 static void 554 sem_remove(int semidx, struct ucred *cred) 555 { 556 struct semid_kernel *semakptr; 557 int i; 558 559 KASSERT(semidx >= 0 && semidx < seminfo.semmni, 560 ("semidx out of bounds")); 561 mtx_assert(&sem_mtx, MA_OWNED); 562 semakptr = &sema[semidx]; 563 KASSERT(semakptr->u.__sem_base - sem + semakptr->u.sem_nsems <= semtot, 564 ("sem_remove: sema %d corrupted sem pointer %p %p %d %d", 565 semidx, semakptr->u.__sem_base, sem, semakptr->u.sem_nsems, 566 semtot)); 567 568 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0; 569 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0; 570 semakptr->u.sem_perm.mode = 0; 571 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems); 572 crfree(semakptr->cred); 573 semakptr->cred = NULL; 574 SEMUNDO_LOCK(); 575 semundo_clear(semidx, -1); 576 SEMUNDO_UNLOCK(); 577 #ifdef MAC 578 mac_sysvsem_cleanup(semakptr); 579 #endif 580 wakeup(semakptr); 581 for (i = 0; i < seminfo.semmni; i++) { 582 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 583 sema[i].u.__sem_base > semakptr->u.__sem_base) 584 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK); 585 } 586 for (i = semakptr->u.__sem_base - sem + semakptr->u.sem_nsems; 587 i < semtot; i++) 588 sem[i - semakptr->u.sem_nsems] = sem[i]; 589 for (i = 0; i < seminfo.semmni; i++) { 590 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 591 sema[i].u.__sem_base > semakptr->u.__sem_base) { 592 sema[i].u.__sem_base -= semakptr->u.sem_nsems; 593 mtx_unlock(&sema_mtx[i]); 594 } 595 } 596 semtot -= semakptr->u.sem_nsems; 597 } 598 599 static struct prison * 600 sem_find_prison(struct ucred *cred) 601 { 602 struct prison *pr, *rpr; 603 604 pr = cred->cr_prison; 605 prison_lock(pr); 606 rpr = osd_jail_get(pr, sem_prison_slot); 607 prison_unlock(pr); 608 return rpr; 609 } 610 611 static int 612 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr) 613 { 614 615 if (semakptr->cred == NULL || 616 !(rpr == semakptr->cred->cr_prison || 617 prison_ischild(rpr, semakptr->cred->cr_prison))) 618 return (EINVAL); 619 return (0); 620 } 621 622 /* 623 * Note that the user-mode half of this passes a union, not a pointer. 624 */ 625 #ifndef _SYS_SYSPROTO_H_ 626 struct __semctl_args { 627 int semid; 628 int semnum; 629 int cmd; 630 union semun *arg; 631 }; 632 #endif 633 int 634 sys___semctl(struct thread *td, struct __semctl_args *uap) 635 { 636 struct semid_ds dsbuf; 637 union semun arg, semun; 638 register_t rval; 639 int error; 640 641 switch (uap->cmd) { 642 case SEM_STAT: 643 case IPC_SET: 644 case IPC_STAT: 645 case GETALL: 646 case SETVAL: 647 case SETALL: 648 error = copyin(uap->arg, &arg, sizeof(arg)); 649 if (error) 650 return (error); 651 break; 652 } 653 654 switch (uap->cmd) { 655 case SEM_STAT: 656 case IPC_STAT: 657 semun.buf = &dsbuf; 658 break; 659 case IPC_SET: 660 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf)); 661 if (error) 662 return (error); 663 semun.buf = &dsbuf; 664 break; 665 case GETALL: 666 case SETALL: 667 semun.array = arg.array; 668 break; 669 case SETVAL: 670 semun.val = arg.val; 671 break; 672 } 673 674 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 675 &rval); 676 if (error) 677 return (error); 678 679 switch (uap->cmd) { 680 case SEM_STAT: 681 case IPC_STAT: 682 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf)); 683 break; 684 } 685 686 if (error == 0) 687 td->td_retval[0] = rval; 688 return (error); 689 } 690 691 int 692 kern_semctl(struct thread *td, int semid, int semnum, int cmd, 693 union semun *arg, register_t *rval) 694 { 695 u_short *array; 696 struct ucred *cred = td->td_ucred; 697 int i, error; 698 struct prison *rpr; 699 struct semid_ds *sbuf; 700 struct semid_kernel *semakptr; 701 struct mtx *sema_mtxp; 702 u_short usval, count; 703 int semidx; 704 705 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n", 706 semid, semnum, cmd, arg)); 707 708 AUDIT_ARG_SVIPC_CMD(cmd); 709 AUDIT_ARG_SVIPC_ID(semid); 710 711 rpr = sem_find_prison(td->td_ucred); 712 if (sem == NULL) 713 return (ENOSYS); 714 715 array = NULL; 716 717 switch(cmd) { 718 case SEM_STAT: 719 /* 720 * For this command we assume semid is an array index 721 * rather than an IPC id. 722 */ 723 if (semid < 0 || semid >= seminfo.semmni) 724 return (EINVAL); 725 semakptr = &sema[semid]; 726 sema_mtxp = &sema_mtx[semid]; 727 mtx_lock(sema_mtxp); 728 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) { 729 error = EINVAL; 730 goto done2; 731 } 732 if ((error = sem_prison_cansee(rpr, semakptr))) 733 goto done2; 734 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 735 goto done2; 736 #ifdef MAC 737 error = mac_sysvsem_check_semctl(cred, semakptr, cmd); 738 if (error != 0) 739 goto done2; 740 #endif 741 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds)); 742 if (cred->cr_prison != semakptr->cred->cr_prison) 743 arg->buf->sem_perm.key = IPC_PRIVATE; 744 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm); 745 mtx_unlock(sema_mtxp); 746 return (0); 747 } 748 749 semidx = IPCID_TO_IX(semid); 750 if (semidx < 0 || semidx >= seminfo.semmni) 751 return (EINVAL); 752 753 semakptr = &sema[semidx]; 754 sema_mtxp = &sema_mtx[semidx]; 755 if (cmd == IPC_RMID) 756 mtx_lock(&sem_mtx); 757 mtx_lock(sema_mtxp); 758 759 #ifdef MAC 760 error = mac_sysvsem_check_semctl(cred, semakptr, cmd); 761 if (error != 0) 762 goto done2; 763 #endif 764 765 error = 0; 766 *rval = 0; 767 768 switch (cmd) { 769 case IPC_RMID: 770 if ((error = semvalid(semid, rpr, semakptr)) != 0) 771 goto done2; 772 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M))) 773 goto done2; 774 sem_remove(semidx, cred); 775 break; 776 777 case IPC_SET: 778 AUDIT_ARG_SVIPC_PERM(&arg->buf->sem_perm); 779 if ((error = semvalid(semid, rpr, semakptr)) != 0) 780 goto done2; 781 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M))) 782 goto done2; 783 sbuf = arg->buf; 784 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid; 785 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid; 786 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode & 787 ~0777) | (sbuf->sem_perm.mode & 0777); 788 semakptr->u.sem_ctime = time_second; 789 break; 790 791 case IPC_STAT: 792 if ((error = semvalid(semid, rpr, semakptr)) != 0) 793 goto done2; 794 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 795 goto done2; 796 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds)); 797 if (cred->cr_prison != semakptr->cred->cr_prison) 798 arg->buf->sem_perm.key = IPC_PRIVATE; 799 800 /* 801 * Try to hide the fact that the structure layout is shared by 802 * both the kernel and userland. This pointer is not useful to 803 * userspace. 804 */ 805 arg->buf->__sem_base = NULL; 806 break; 807 808 case GETNCNT: 809 if ((error = semvalid(semid, rpr, semakptr)) != 0) 810 goto done2; 811 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 812 goto done2; 813 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 814 error = EINVAL; 815 goto done2; 816 } 817 *rval = semakptr->u.__sem_base[semnum].semncnt; 818 break; 819 820 case GETPID: 821 if ((error = semvalid(semid, rpr, semakptr)) != 0) 822 goto done2; 823 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 824 goto done2; 825 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 826 error = EINVAL; 827 goto done2; 828 } 829 *rval = semakptr->u.__sem_base[semnum].sempid; 830 break; 831 832 case GETVAL: 833 if ((error = semvalid(semid, rpr, semakptr)) != 0) 834 goto done2; 835 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 836 goto done2; 837 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 838 error = EINVAL; 839 goto done2; 840 } 841 *rval = semakptr->u.__sem_base[semnum].semval; 842 break; 843 844 case GETALL: 845 /* 846 * Unfortunately, callers of this function don't know 847 * in advance how many semaphores are in this set. 848 * While we could just allocate the maximum size array 849 * and pass the actual size back to the caller, that 850 * won't work for SETALL since we can't copyin() more 851 * data than the user specified as we may return a 852 * spurious EFAULT. 853 * 854 * Note that the number of semaphores in a set is 855 * fixed for the life of that set. The only way that 856 * the 'count' could change while are blocked in 857 * malloc() is if this semaphore set were destroyed 858 * and a new one created with the same index. 859 * However, semvalid() will catch that due to the 860 * sequence number unless exactly 0x8000 (or a 861 * multiple thereof) semaphore sets for the same index 862 * are created and destroyed while we are in malloc! 863 * 864 */ 865 count = semakptr->u.sem_nsems; 866 mtx_unlock(sema_mtxp); 867 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK); 868 mtx_lock(sema_mtxp); 869 if ((error = semvalid(semid, rpr, semakptr)) != 0) 870 goto done2; 871 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed")); 872 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 873 goto done2; 874 for (i = 0; i < semakptr->u.sem_nsems; i++) 875 array[i] = semakptr->u.__sem_base[i].semval; 876 mtx_unlock(sema_mtxp); 877 error = copyout(array, arg->array, count * sizeof(*array)); 878 mtx_lock(sema_mtxp); 879 break; 880 881 case GETZCNT: 882 if ((error = semvalid(semid, rpr, semakptr)) != 0) 883 goto done2; 884 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 885 goto done2; 886 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 887 error = EINVAL; 888 goto done2; 889 } 890 *rval = semakptr->u.__sem_base[semnum].semzcnt; 891 break; 892 893 case SETVAL: 894 if ((error = semvalid(semid, rpr, semakptr)) != 0) 895 goto done2; 896 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W))) 897 goto done2; 898 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 899 error = EINVAL; 900 goto done2; 901 } 902 if (arg->val < 0 || arg->val > seminfo.semvmx) { 903 error = ERANGE; 904 goto done2; 905 } 906 semakptr->u.__sem_base[semnum].semval = arg->val; 907 SEMUNDO_LOCK(); 908 semundo_clear(semidx, semnum); 909 SEMUNDO_UNLOCK(); 910 wakeup(semakptr); 911 break; 912 913 case SETALL: 914 /* 915 * See comment on GETALL for why 'count' shouldn't change 916 * and why we require a userland buffer. 917 */ 918 count = semakptr->u.sem_nsems; 919 mtx_unlock(sema_mtxp); 920 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK); 921 error = copyin(arg->array, array, count * sizeof(*array)); 922 mtx_lock(sema_mtxp); 923 if (error) 924 break; 925 if ((error = semvalid(semid, rpr, semakptr)) != 0) 926 goto done2; 927 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed")); 928 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W))) 929 goto done2; 930 for (i = 0; i < semakptr->u.sem_nsems; i++) { 931 usval = array[i]; 932 if (usval > seminfo.semvmx) { 933 error = ERANGE; 934 break; 935 } 936 semakptr->u.__sem_base[i].semval = usval; 937 } 938 SEMUNDO_LOCK(); 939 semundo_clear(semidx, -1); 940 SEMUNDO_UNLOCK(); 941 wakeup(semakptr); 942 break; 943 944 default: 945 error = EINVAL; 946 break; 947 } 948 949 done2: 950 mtx_unlock(sema_mtxp); 951 if (cmd == IPC_RMID) 952 mtx_unlock(&sem_mtx); 953 if (array != NULL) 954 free(array, M_TEMP); 955 return(error); 956 } 957 958 #ifndef _SYS_SYSPROTO_H_ 959 struct semget_args { 960 key_t key; 961 int nsems; 962 int semflg; 963 }; 964 #endif 965 int 966 sys_semget(struct thread *td, struct semget_args *uap) 967 { 968 int semid, error = 0; 969 int key = uap->key; 970 int nsems = uap->nsems; 971 int semflg = uap->semflg; 972 struct ucred *cred = td->td_ucred; 973 974 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg)); 975 976 AUDIT_ARG_VALUE(semflg); 977 978 if (sem_find_prison(cred) == NULL) 979 return (ENOSYS); 980 981 mtx_lock(&sem_mtx); 982 if (key != IPC_PRIVATE) { 983 for (semid = 0; semid < seminfo.semmni; semid++) { 984 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) && 985 sema[semid].cred != NULL && 986 sema[semid].cred->cr_prison == cred->cr_prison && 987 sema[semid].u.sem_perm.key == key) 988 break; 989 } 990 if (semid < seminfo.semmni) { 991 AUDIT_ARG_SVIPC_ID(semid); 992 DPRINTF(("found public key\n")); 993 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) { 994 DPRINTF(("not exclusive\n")); 995 error = EEXIST; 996 goto done2; 997 } 998 if ((error = ipcperm(td, &sema[semid].u.sem_perm, 999 semflg & 0700))) { 1000 goto done2; 1001 } 1002 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) { 1003 DPRINTF(("too small\n")); 1004 error = EINVAL; 1005 goto done2; 1006 } 1007 #ifdef MAC 1008 error = mac_sysvsem_check_semget(cred, &sema[semid]); 1009 if (error != 0) 1010 goto done2; 1011 #endif 1012 goto found; 1013 } 1014 } 1015 1016 DPRINTF(("need to allocate the semid_kernel\n")); 1017 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) { 1018 if (nsems <= 0 || nsems > seminfo.semmsl) { 1019 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems, 1020 seminfo.semmsl)); 1021 error = EINVAL; 1022 goto done2; 1023 } 1024 if (nsems > seminfo.semmns - semtot) { 1025 DPRINTF(( 1026 "not enough semaphores left (need %d, got %d)\n", 1027 nsems, seminfo.semmns - semtot)); 1028 error = ENOSPC; 1029 goto done2; 1030 } 1031 for (semid = 0; semid < seminfo.semmni; semid++) { 1032 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0) 1033 break; 1034 } 1035 if (semid == seminfo.semmni) { 1036 DPRINTF(("no more semid_kernel's available\n")); 1037 error = ENOSPC; 1038 goto done2; 1039 } 1040 #ifdef RACCT 1041 if (racct_enable) { 1042 PROC_LOCK(td->td_proc); 1043 error = racct_add(td->td_proc, RACCT_NSEM, nsems); 1044 PROC_UNLOCK(td->td_proc); 1045 if (error != 0) { 1046 error = ENOSPC; 1047 goto done2; 1048 } 1049 } 1050 #endif 1051 DPRINTF(("semid %d is available\n", semid)); 1052 mtx_lock(&sema_mtx[semid]); 1053 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0, 1054 ("Lost semaphore %d", semid)); 1055 sema[semid].u.sem_perm.key = key; 1056 sema[semid].u.sem_perm.cuid = cred->cr_uid; 1057 sema[semid].u.sem_perm.uid = cred->cr_uid; 1058 sema[semid].u.sem_perm.cgid = cred->cr_gid; 1059 sema[semid].u.sem_perm.gid = cred->cr_gid; 1060 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC; 1061 sema[semid].cred = crhold(cred); 1062 sema[semid].u.sem_perm.seq = 1063 (sema[semid].u.sem_perm.seq + 1) & 0x7fff; 1064 sema[semid].u.sem_nsems = nsems; 1065 sema[semid].u.sem_otime = 0; 1066 sema[semid].u.sem_ctime = time_second; 1067 sema[semid].u.__sem_base = &sem[semtot]; 1068 semtot += nsems; 1069 bzero(sema[semid].u.__sem_base, 1070 sizeof(sema[semid].u.__sem_base[0])*nsems); 1071 #ifdef MAC 1072 mac_sysvsem_create(cred, &sema[semid]); 1073 #endif 1074 mtx_unlock(&sema_mtx[semid]); 1075 DPRINTF(("sembase = %p, next = %p\n", 1076 sema[semid].u.__sem_base, &sem[semtot])); 1077 } else { 1078 DPRINTF(("didn't find it and wasn't asked to create it\n")); 1079 error = ENOENT; 1080 goto done2; 1081 } 1082 1083 found: 1084 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm); 1085 done2: 1086 mtx_unlock(&sem_mtx); 1087 return (error); 1088 } 1089 1090 #ifndef _SYS_SYSPROTO_H_ 1091 struct semop_args { 1092 int semid; 1093 struct sembuf *sops; 1094 size_t nsops; 1095 }; 1096 #endif 1097 int 1098 sys_semop(struct thread *td, struct semop_args *uap) 1099 { 1100 #define SMALL_SOPS 8 1101 struct sembuf small_sops[SMALL_SOPS]; 1102 int semid = uap->semid; 1103 size_t nsops = uap->nsops; 1104 struct prison *rpr; 1105 struct sembuf *sops; 1106 struct semid_kernel *semakptr; 1107 struct sembuf *sopptr = NULL; 1108 struct sem *semptr = NULL; 1109 struct sem_undo *suptr; 1110 struct mtx *sema_mtxp; 1111 size_t i, j, k; 1112 int error; 1113 int do_wakeup, do_undos; 1114 unsigned short seq; 1115 1116 #ifdef SEM_DEBUG 1117 sops = NULL; 1118 #endif 1119 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops)); 1120 1121 AUDIT_ARG_SVIPC_ID(semid); 1122 1123 rpr = sem_find_prison(td->td_ucred); 1124 if (sem == NULL) 1125 return (ENOSYS); 1126 1127 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */ 1128 1129 if (semid < 0 || semid >= seminfo.semmni) 1130 return (EINVAL); 1131 1132 /* Allocate memory for sem_ops */ 1133 if (nsops <= SMALL_SOPS) 1134 sops = small_sops; 1135 else if (nsops > seminfo.semopm) { 1136 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm, 1137 nsops)); 1138 return (E2BIG); 1139 } else { 1140 #ifdef RACCT 1141 if (racct_enable) { 1142 PROC_LOCK(td->td_proc); 1143 if (nsops > 1144 racct_get_available(td->td_proc, RACCT_NSEMOP)) { 1145 PROC_UNLOCK(td->td_proc); 1146 return (E2BIG); 1147 } 1148 PROC_UNLOCK(td->td_proc); 1149 } 1150 #endif 1151 1152 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK); 1153 } 1154 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) { 1155 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error, 1156 uap->sops, sops, nsops * sizeof(sops[0]))); 1157 if (sops != small_sops) 1158 free(sops, M_TEMP); 1159 return (error); 1160 } 1161 1162 semakptr = &sema[semid]; 1163 sema_mtxp = &sema_mtx[semid]; 1164 mtx_lock(sema_mtxp); 1165 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) { 1166 error = EINVAL; 1167 goto done2; 1168 } 1169 seq = semakptr->u.sem_perm.seq; 1170 if (seq != IPCID_TO_SEQ(uap->semid)) { 1171 error = EINVAL; 1172 goto done2; 1173 } 1174 if ((error = sem_prison_cansee(rpr, semakptr)) != 0) 1175 goto done2; 1176 /* 1177 * Initial pass through sops to see what permissions are needed. 1178 * Also perform any checks that don't need repeating on each 1179 * attempt to satisfy the request vector. 1180 */ 1181 j = 0; /* permission needed */ 1182 do_undos = 0; 1183 for (i = 0; i < nsops; i++) { 1184 sopptr = &sops[i]; 1185 if (sopptr->sem_num >= semakptr->u.sem_nsems) { 1186 error = EFBIG; 1187 goto done2; 1188 } 1189 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0) 1190 do_undos = 1; 1191 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A; 1192 } 1193 1194 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) { 1195 DPRINTF(("error = %d from ipaccess\n", error)); 1196 goto done2; 1197 } 1198 #ifdef MAC 1199 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j); 1200 if (error != 0) 1201 goto done2; 1202 #endif 1203 1204 /* 1205 * Loop trying to satisfy the vector of requests. 1206 * If we reach a point where we must wait, any requests already 1207 * performed are rolled back and we go to sleep until some other 1208 * process wakes us up. At this point, we start all over again. 1209 * 1210 * This ensures that from the perspective of other tasks, a set 1211 * of requests is atomic (never partially satisfied). 1212 */ 1213 for (;;) { 1214 do_wakeup = 0; 1215 error = 0; /* error return if necessary */ 1216 1217 for (i = 0; i < nsops; i++) { 1218 sopptr = &sops[i]; 1219 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1220 1221 DPRINTF(( 1222 "semop: semakptr=%p, __sem_base=%p, " 1223 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n", 1224 semakptr, semakptr->u.__sem_base, semptr, 1225 sopptr->sem_num, semptr->semval, sopptr->sem_op, 1226 (sopptr->sem_flg & IPC_NOWAIT) ? 1227 "nowait" : "wait")); 1228 1229 if (sopptr->sem_op < 0) { 1230 if (semptr->semval + sopptr->sem_op < 0) { 1231 DPRINTF(("semop: can't do it now\n")); 1232 break; 1233 } else { 1234 semptr->semval += sopptr->sem_op; 1235 if (semptr->semval == 0 && 1236 semptr->semzcnt > 0) 1237 do_wakeup = 1; 1238 } 1239 } else if (sopptr->sem_op == 0) { 1240 if (semptr->semval != 0) { 1241 DPRINTF(("semop: not zero now\n")); 1242 break; 1243 } 1244 } else if (semptr->semval + sopptr->sem_op > 1245 seminfo.semvmx) { 1246 error = ERANGE; 1247 break; 1248 } else { 1249 if (semptr->semncnt > 0) 1250 do_wakeup = 1; 1251 semptr->semval += sopptr->sem_op; 1252 } 1253 } 1254 1255 /* 1256 * Did we get through the entire vector? 1257 */ 1258 if (i >= nsops) 1259 goto done; 1260 1261 /* 1262 * No ... rollback anything that we've already done 1263 */ 1264 DPRINTF(("semop: rollback 0 through %d\n", i-1)); 1265 for (j = 0; j < i; j++) 1266 semakptr->u.__sem_base[sops[j].sem_num].semval -= 1267 sops[j].sem_op; 1268 1269 /* If we detected an error, return it */ 1270 if (error != 0) 1271 goto done2; 1272 1273 /* 1274 * If the request that we couldn't satisfy has the 1275 * NOWAIT flag set then return with EAGAIN. 1276 */ 1277 if (sopptr->sem_flg & IPC_NOWAIT) { 1278 error = EAGAIN; 1279 goto done2; 1280 } 1281 1282 if (sopptr->sem_op == 0) 1283 semptr->semzcnt++; 1284 else 1285 semptr->semncnt++; 1286 1287 DPRINTF(("semop: good night!\n")); 1288 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH, 1289 "semwait", 0); 1290 DPRINTF(("semop: good morning (error=%d)!\n", error)); 1291 /* return code is checked below, after sem[nz]cnt-- */ 1292 1293 /* 1294 * Make sure that the semaphore still exists 1295 */ 1296 seq = semakptr->u.sem_perm.seq; 1297 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 1298 seq != IPCID_TO_SEQ(uap->semid)) { 1299 error = EIDRM; 1300 goto done2; 1301 } 1302 1303 /* 1304 * Renew the semaphore's pointer after wakeup since 1305 * during msleep __sem_base may have been modified and semptr 1306 * is not valid any more 1307 */ 1308 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1309 1310 /* 1311 * The semaphore is still alive. Readjust the count of 1312 * waiting processes. 1313 */ 1314 if (sopptr->sem_op == 0) 1315 semptr->semzcnt--; 1316 else 1317 semptr->semncnt--; 1318 1319 /* 1320 * Is it really morning, or was our sleep interrupted? 1321 * (Delayed check of msleep() return code because we 1322 * need to decrement sem[nz]cnt either way.) 1323 */ 1324 if (error != 0) { 1325 error = EINTR; 1326 goto done2; 1327 } 1328 DPRINTF(("semop: good morning!\n")); 1329 } 1330 1331 done: 1332 /* 1333 * Process any SEM_UNDO requests. 1334 */ 1335 if (do_undos) { 1336 SEMUNDO_LOCK(); 1337 suptr = NULL; 1338 for (i = 0; i < nsops; i++) { 1339 /* 1340 * We only need to deal with SEM_UNDO's for non-zero 1341 * op's. 1342 */ 1343 int adjval; 1344 1345 if ((sops[i].sem_flg & SEM_UNDO) == 0) 1346 continue; 1347 adjval = sops[i].sem_op; 1348 if (adjval == 0) 1349 continue; 1350 error = semundo_adjust(td, &suptr, semid, seq, 1351 sops[i].sem_num, -adjval); 1352 if (error == 0) 1353 continue; 1354 1355 /* 1356 * Oh-Oh! We ran out of either sem_undo's or undo's. 1357 * Rollback the adjustments to this point and then 1358 * rollback the semaphore ups and down so we can return 1359 * with an error with all structures restored. We 1360 * rollback the undo's in the exact reverse order that 1361 * we applied them. This guarantees that we won't run 1362 * out of space as we roll things back out. 1363 */ 1364 for (j = 0; j < i; j++) { 1365 k = i - j - 1; 1366 if ((sops[k].sem_flg & SEM_UNDO) == 0) 1367 continue; 1368 adjval = sops[k].sem_op; 1369 if (adjval == 0) 1370 continue; 1371 if (semundo_adjust(td, &suptr, semid, seq, 1372 sops[k].sem_num, adjval) != 0) 1373 panic("semop - can't undo undos"); 1374 } 1375 1376 for (j = 0; j < nsops; j++) 1377 semakptr->u.__sem_base[sops[j].sem_num].semval -= 1378 sops[j].sem_op; 1379 1380 DPRINTF(("error = %d from semundo_adjust\n", error)); 1381 SEMUNDO_UNLOCK(); 1382 goto done2; 1383 } /* loop through the sops */ 1384 SEMUNDO_UNLOCK(); 1385 } /* if (do_undos) */ 1386 1387 /* We're definitely done - set the sempid's and time */ 1388 for (i = 0; i < nsops; i++) { 1389 sopptr = &sops[i]; 1390 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1391 semptr->sempid = td->td_proc->p_pid; 1392 } 1393 semakptr->u.sem_otime = time_second; 1394 1395 /* 1396 * Do a wakeup if any semaphore was up'd whilst something was 1397 * sleeping on it. 1398 */ 1399 if (do_wakeup) { 1400 DPRINTF(("semop: doing wakeup\n")); 1401 wakeup(semakptr); 1402 DPRINTF(("semop: back from wakeup\n")); 1403 } 1404 DPRINTF(("semop: done\n")); 1405 td->td_retval[0] = 0; 1406 done2: 1407 mtx_unlock(sema_mtxp); 1408 if (sops != small_sops) 1409 free(sops, M_TEMP); 1410 return (error); 1411 } 1412 1413 /* 1414 * Go through the undo structures for this process and apply the adjustments to 1415 * semaphores. 1416 */ 1417 static void 1418 semexit_myhook(void *arg, struct proc *p) 1419 { 1420 struct sem_undo *suptr; 1421 struct semid_kernel *semakptr; 1422 struct mtx *sema_mtxp; 1423 int semid, semnum, adjval, ix; 1424 unsigned short seq; 1425 1426 /* 1427 * Go through the chain of undo vectors looking for one 1428 * associated with this process. 1429 */ 1430 if (LIST_EMPTY(&semu_list)) 1431 return; 1432 SEMUNDO_LOCK(); 1433 LIST_FOREACH(suptr, &semu_list, un_next) { 1434 if (suptr->un_proc == p) 1435 break; 1436 } 1437 if (suptr == NULL) { 1438 SEMUNDO_UNLOCK(); 1439 return; 1440 } 1441 LIST_REMOVE(suptr, un_next); 1442 1443 DPRINTF(("proc @%p has undo structure with %d entries\n", p, 1444 suptr->un_cnt)); 1445 1446 /* 1447 * If there are any active undo elements then process them. 1448 */ 1449 if (suptr->un_cnt > 0) { 1450 SEMUNDO_UNLOCK(); 1451 for (ix = 0; ix < suptr->un_cnt; ix++) { 1452 semid = suptr->un_ent[ix].un_id; 1453 semnum = suptr->un_ent[ix].un_num; 1454 adjval = suptr->un_ent[ix].un_adjval; 1455 seq = suptr->un_ent[ix].un_seq; 1456 semakptr = &sema[semid]; 1457 sema_mtxp = &sema_mtx[semid]; 1458 1459 mtx_lock(sema_mtxp); 1460 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 1461 (semakptr->u.sem_perm.seq != seq)) { 1462 mtx_unlock(sema_mtxp); 1463 continue; 1464 } 1465 if (semnum >= semakptr->u.sem_nsems) 1466 panic("semexit - semnum out of range"); 1467 1468 DPRINTF(( 1469 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n", 1470 suptr->un_proc, suptr->un_ent[ix].un_id, 1471 suptr->un_ent[ix].un_num, 1472 suptr->un_ent[ix].un_adjval, 1473 semakptr->u.__sem_base[semnum].semval)); 1474 1475 if (adjval < 0 && semakptr->u.__sem_base[semnum].semval < 1476 -adjval) 1477 semakptr->u.__sem_base[semnum].semval = 0; 1478 else 1479 semakptr->u.__sem_base[semnum].semval += adjval; 1480 1481 wakeup(semakptr); 1482 DPRINTF(("semexit: back from wakeup\n")); 1483 mtx_unlock(sema_mtxp); 1484 } 1485 SEMUNDO_LOCK(); 1486 } 1487 1488 /* 1489 * Deallocate the undo vector. 1490 */ 1491 DPRINTF(("removing vector\n")); 1492 suptr->un_proc = NULL; 1493 suptr->un_cnt = 0; 1494 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 1495 SEMUNDO_UNLOCK(); 1496 } 1497 1498 static int 1499 sysctl_sema(SYSCTL_HANDLER_ARGS) 1500 { 1501 struct prison *pr, *rpr; 1502 struct semid_kernel tsemak; 1503 #ifdef COMPAT_FREEBSD32 1504 struct semid_kernel32 tsemak32; 1505 #endif 1506 void *outaddr; 1507 size_t outsize; 1508 int error, i; 1509 1510 pr = req->td->td_ucred->cr_prison; 1511 rpr = sem_find_prison(req->td->td_ucred); 1512 error = 0; 1513 for (i = 0; i < seminfo.semmni; i++) { 1514 mtx_lock(&sema_mtx[i]); 1515 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 || 1516 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0) 1517 bzero(&tsemak, sizeof(tsemak)); 1518 else { 1519 tsemak = sema[i]; 1520 if (tsemak.cred->cr_prison != pr) 1521 tsemak.u.sem_perm.key = IPC_PRIVATE; 1522 } 1523 mtx_unlock(&sema_mtx[i]); 1524 #ifdef COMPAT_FREEBSD32 1525 if (SV_CURPROC_FLAG(SV_ILP32)) { 1526 bzero(&tsemak32, sizeof(tsemak32)); 1527 freebsd32_ipcperm_out(&tsemak.u.sem_perm, 1528 &tsemak32.u.sem_perm); 1529 /* Don't copy u.__sem_base */ 1530 CP(tsemak, tsemak32, u.sem_nsems); 1531 CP(tsemak, tsemak32, u.sem_otime); 1532 CP(tsemak, tsemak32, u.sem_ctime); 1533 /* Don't copy label or cred */ 1534 outaddr = &tsemak32; 1535 outsize = sizeof(tsemak32); 1536 } else 1537 #endif 1538 { 1539 tsemak.u.__sem_base = NULL; 1540 tsemak.label = NULL; 1541 tsemak.cred = NULL; 1542 outaddr = &tsemak; 1543 outsize = sizeof(tsemak); 1544 } 1545 error = SYSCTL_OUT(req, outaddr, outsize); 1546 if (error != 0) 1547 break; 1548 } 1549 return (error); 1550 } 1551 1552 static int 1553 sem_prison_check(void *obj, void *data) 1554 { 1555 struct prison *pr = obj; 1556 struct prison *prpr; 1557 struct vfsoptlist *opts = data; 1558 int error, jsys; 1559 1560 /* 1561 * sysvsem is a jailsys integer. 1562 * It must be "disable" if the parent jail is disabled. 1563 */ 1564 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)); 1565 if (error != ENOENT) { 1566 if (error != 0) 1567 return (error); 1568 switch (jsys) { 1569 case JAIL_SYS_DISABLE: 1570 break; 1571 case JAIL_SYS_NEW: 1572 case JAIL_SYS_INHERIT: 1573 prison_lock(pr->pr_parent); 1574 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot); 1575 prison_unlock(pr->pr_parent); 1576 if (prpr == NULL) 1577 return (EPERM); 1578 break; 1579 default: 1580 return (EINVAL); 1581 } 1582 } 1583 1584 return (0); 1585 } 1586 1587 static int 1588 sem_prison_set(void *obj, void *data) 1589 { 1590 struct prison *pr = obj; 1591 struct prison *tpr, *orpr, *nrpr, *trpr; 1592 struct vfsoptlist *opts = data; 1593 void *rsv; 1594 int jsys, descend; 1595 1596 /* 1597 * sysvsem controls which jail is the root of the associated sems (this 1598 * jail or same as the parent), or if the feature is available at all. 1599 */ 1600 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT) 1601 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0) 1602 ? JAIL_SYS_INHERIT 1603 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0) 1604 ? JAIL_SYS_DISABLE 1605 : -1; 1606 if (jsys == JAIL_SYS_DISABLE) { 1607 prison_lock(pr); 1608 orpr = osd_jail_get(pr, sem_prison_slot); 1609 if (orpr != NULL) 1610 osd_jail_del(pr, sem_prison_slot); 1611 prison_unlock(pr); 1612 if (orpr != NULL) { 1613 if (orpr == pr) 1614 sem_prison_cleanup(pr); 1615 /* Disable all child jails as well. */ 1616 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1617 prison_lock(tpr); 1618 trpr = osd_jail_get(tpr, sem_prison_slot); 1619 if (trpr != NULL) { 1620 osd_jail_del(tpr, sem_prison_slot); 1621 prison_unlock(tpr); 1622 if (trpr == tpr) 1623 sem_prison_cleanup(tpr); 1624 } else { 1625 prison_unlock(tpr); 1626 descend = 0; 1627 } 1628 } 1629 } 1630 } else if (jsys != -1) { 1631 if (jsys == JAIL_SYS_NEW) 1632 nrpr = pr; 1633 else { 1634 prison_lock(pr->pr_parent); 1635 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot); 1636 prison_unlock(pr->pr_parent); 1637 } 1638 rsv = osd_reserve(sem_prison_slot); 1639 prison_lock(pr); 1640 orpr = osd_jail_get(pr, sem_prison_slot); 1641 if (orpr != nrpr) 1642 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv, 1643 nrpr); 1644 else 1645 osd_free_reserved(rsv); 1646 prison_unlock(pr); 1647 if (orpr != nrpr) { 1648 if (orpr == pr) 1649 sem_prison_cleanup(pr); 1650 if (orpr != NULL) { 1651 /* Change child jails matching the old root, */ 1652 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1653 prison_lock(tpr); 1654 trpr = osd_jail_get(tpr, 1655 sem_prison_slot); 1656 if (trpr == orpr) { 1657 (void)osd_jail_set(tpr, 1658 sem_prison_slot, nrpr); 1659 prison_unlock(tpr); 1660 if (trpr == tpr) 1661 sem_prison_cleanup(tpr); 1662 } else { 1663 prison_unlock(tpr); 1664 descend = 0; 1665 } 1666 } 1667 } 1668 } 1669 } 1670 1671 return (0); 1672 } 1673 1674 static int 1675 sem_prison_get(void *obj, void *data) 1676 { 1677 struct prison *pr = obj; 1678 struct prison *rpr; 1679 struct vfsoptlist *opts = data; 1680 int error, jsys; 1681 1682 /* Set sysvsem based on the jail's root prison. */ 1683 prison_lock(pr); 1684 rpr = osd_jail_get(pr, sem_prison_slot); 1685 prison_unlock(pr); 1686 jsys = rpr == NULL ? JAIL_SYS_DISABLE 1687 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT; 1688 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys)); 1689 if (error == ENOENT) 1690 error = 0; 1691 return (error); 1692 } 1693 1694 static int 1695 sem_prison_remove(void *obj, void *data __unused) 1696 { 1697 struct prison *pr = obj; 1698 struct prison *rpr; 1699 1700 prison_lock(pr); 1701 rpr = osd_jail_get(pr, sem_prison_slot); 1702 prison_unlock(pr); 1703 if (rpr == pr) 1704 sem_prison_cleanup(pr); 1705 return (0); 1706 } 1707 1708 static void 1709 sem_prison_cleanup(struct prison *pr) 1710 { 1711 int i; 1712 1713 /* Remove any sems that belong to this jail. */ 1714 mtx_lock(&sem_mtx); 1715 for (i = 0; i < seminfo.semmni; i++) { 1716 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 1717 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) { 1718 mtx_lock(&sema_mtx[i]); 1719 sem_remove(i, NULL); 1720 mtx_unlock(&sema_mtx[i]); 1721 } 1722 } 1723 mtx_unlock(&sem_mtx); 1724 } 1725 1726 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores"); 1727 1728 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1729 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1730 1731 /* XXX casting to (sy_call_t *) is bogus, as usual. */ 1732 static sy_call_t *semcalls[] = { 1733 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget, 1734 (sy_call_t *)sys_semop 1735 }; 1736 1737 /* 1738 * Entry point for all SEM calls. 1739 */ 1740 int 1741 sys_semsys(td, uap) 1742 struct thread *td; 1743 /* XXX actually varargs. */ 1744 struct semsys_args /* { 1745 int which; 1746 int a2; 1747 int a3; 1748 int a4; 1749 int a5; 1750 } */ *uap; 1751 { 1752 int error; 1753 1754 AUDIT_ARG_SVIPC_WHICH(uap->which); 1755 if (uap->which < 0 || uap->which >= nitems(semcalls)) 1756 return (EINVAL); 1757 error = (*semcalls[uap->which])(td, &uap->a2); 1758 return (error); 1759 } 1760 1761 #ifndef _SYS_SYSPROTO_H_ 1762 struct freebsd7___semctl_args { 1763 int semid; 1764 int semnum; 1765 int cmd; 1766 union semun_old *arg; 1767 }; 1768 #endif 1769 int 1770 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap) 1771 { 1772 struct semid_ds_old dsold; 1773 struct semid_ds dsbuf; 1774 union semun_old arg; 1775 union semun semun; 1776 register_t rval; 1777 int error; 1778 1779 switch (uap->cmd) { 1780 case SEM_STAT: 1781 case IPC_SET: 1782 case IPC_STAT: 1783 case GETALL: 1784 case SETVAL: 1785 case SETALL: 1786 error = copyin(uap->arg, &arg, sizeof(arg)); 1787 if (error) 1788 return (error); 1789 break; 1790 } 1791 1792 switch (uap->cmd) { 1793 case SEM_STAT: 1794 case IPC_STAT: 1795 semun.buf = &dsbuf; 1796 break; 1797 case IPC_SET: 1798 error = copyin(arg.buf, &dsold, sizeof(dsold)); 1799 if (error) 1800 return (error); 1801 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm); 1802 CP(dsold, dsbuf, __sem_base); 1803 CP(dsold, dsbuf, sem_nsems); 1804 CP(dsold, dsbuf, sem_otime); 1805 CP(dsold, dsbuf, sem_ctime); 1806 semun.buf = &dsbuf; 1807 break; 1808 case GETALL: 1809 case SETALL: 1810 semun.array = arg.array; 1811 break; 1812 case SETVAL: 1813 semun.val = arg.val; 1814 break; 1815 } 1816 1817 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1818 &rval); 1819 if (error) 1820 return (error); 1821 1822 switch (uap->cmd) { 1823 case SEM_STAT: 1824 case IPC_STAT: 1825 bzero(&dsold, sizeof(dsold)); 1826 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm); 1827 CP(dsbuf, dsold, __sem_base); 1828 CP(dsbuf, dsold, sem_nsems); 1829 CP(dsbuf, dsold, sem_otime); 1830 CP(dsbuf, dsold, sem_ctime); 1831 error = copyout(&dsold, arg.buf, sizeof(dsold)); 1832 break; 1833 } 1834 1835 if (error == 0) 1836 td->td_retval[0] = rval; 1837 return (error); 1838 } 1839 1840 #endif /* COMPAT_FREEBSD{4,5,6,7} */ 1841 1842 #ifdef COMPAT_FREEBSD32 1843 1844 int 1845 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap) 1846 { 1847 1848 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1849 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1850 AUDIT_ARG_SVIPC_WHICH(uap->which); 1851 switch (uap->which) { 1852 case 0: 1853 return (freebsd7_freebsd32_semctl(td, 1854 (struct freebsd7_freebsd32_semctl_args *)&uap->a2)); 1855 default: 1856 return (sys_semsys(td, (struct semsys_args *)uap)); 1857 } 1858 #else 1859 return (nosys(td, NULL)); 1860 #endif 1861 } 1862 1863 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1864 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1865 int 1866 freebsd7_freebsd32_semctl(struct thread *td, 1867 struct freebsd7_freebsd32_semctl_args *uap) 1868 { 1869 struct semid_ds32_old dsbuf32; 1870 struct semid_ds dsbuf; 1871 union semun semun; 1872 union semun32 arg; 1873 register_t rval; 1874 int error; 1875 1876 switch (uap->cmd) { 1877 case SEM_STAT: 1878 case IPC_SET: 1879 case IPC_STAT: 1880 case GETALL: 1881 case SETVAL: 1882 case SETALL: 1883 error = copyin(uap->arg, &arg, sizeof(arg)); 1884 if (error) 1885 return (error); 1886 break; 1887 } 1888 1889 switch (uap->cmd) { 1890 case SEM_STAT: 1891 case IPC_STAT: 1892 semun.buf = &dsbuf; 1893 break; 1894 case IPC_SET: 1895 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32)); 1896 if (error) 1897 return (error); 1898 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm); 1899 PTRIN_CP(dsbuf32, dsbuf, __sem_base); 1900 CP(dsbuf32, dsbuf, sem_nsems); 1901 CP(dsbuf32, dsbuf, sem_otime); 1902 CP(dsbuf32, dsbuf, sem_ctime); 1903 semun.buf = &dsbuf; 1904 break; 1905 case GETALL: 1906 case SETALL: 1907 semun.array = PTRIN(arg.array); 1908 break; 1909 case SETVAL: 1910 semun.val = arg.val; 1911 break; 1912 } 1913 1914 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1915 &rval); 1916 if (error) 1917 return (error); 1918 1919 switch (uap->cmd) { 1920 case SEM_STAT: 1921 case IPC_STAT: 1922 bzero(&dsbuf32, sizeof(dsbuf32)); 1923 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm); 1924 PTROUT_CP(dsbuf, dsbuf32, __sem_base); 1925 CP(dsbuf, dsbuf32, sem_nsems); 1926 CP(dsbuf, dsbuf32, sem_otime); 1927 CP(dsbuf, dsbuf32, sem_ctime); 1928 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32)); 1929 break; 1930 } 1931 1932 if (error == 0) 1933 td->td_retval[0] = rval; 1934 return (error); 1935 } 1936 #endif 1937 1938 int 1939 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap) 1940 { 1941 struct semid_ds32 dsbuf32; 1942 struct semid_ds dsbuf; 1943 union semun semun; 1944 union semun32 arg; 1945 register_t rval; 1946 int error; 1947 1948 switch (uap->cmd) { 1949 case SEM_STAT: 1950 case IPC_SET: 1951 case IPC_STAT: 1952 case GETALL: 1953 case SETVAL: 1954 case SETALL: 1955 error = copyin(uap->arg, &arg, sizeof(arg)); 1956 if (error) 1957 return (error); 1958 break; 1959 } 1960 1961 switch (uap->cmd) { 1962 case SEM_STAT: 1963 case IPC_STAT: 1964 semun.buf = &dsbuf; 1965 break; 1966 case IPC_SET: 1967 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32)); 1968 if (error) 1969 return (error); 1970 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm); 1971 PTRIN_CP(dsbuf32, dsbuf, __sem_base); 1972 CP(dsbuf32, dsbuf, sem_nsems); 1973 CP(dsbuf32, dsbuf, sem_otime); 1974 CP(dsbuf32, dsbuf, sem_ctime); 1975 semun.buf = &dsbuf; 1976 break; 1977 case GETALL: 1978 case SETALL: 1979 semun.array = PTRIN(arg.array); 1980 break; 1981 case SETVAL: 1982 semun.val = arg.val; 1983 break; 1984 } 1985 1986 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1987 &rval); 1988 if (error) 1989 return (error); 1990 1991 switch (uap->cmd) { 1992 case SEM_STAT: 1993 case IPC_STAT: 1994 bzero(&dsbuf32, sizeof(dsbuf32)); 1995 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm); 1996 PTROUT_CP(dsbuf, dsbuf32, __sem_base); 1997 CP(dsbuf, dsbuf32, sem_nsems); 1998 CP(dsbuf, dsbuf32, sem_otime); 1999 CP(dsbuf, dsbuf32, sem_ctime); 2000 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32)); 2001 break; 2002 } 2003 2004 if (error == 0) 2005 td->td_retval[0] = rval; 2006 return (error); 2007 } 2008 2009 #endif /* COMPAT_FREEBSD32 */ 2010