1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1994-1995 Søren Schmidt 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "opt_ktrace.h" 30 31 #include <sys/param.h> 32 #include <sys/ktr.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/proc.h> 36 #include <sys/signalvar.h> 37 #include <sys/sx.h> 38 #include <sys/syscallsubr.h> 39 #include <sys/sysproto.h> 40 #ifdef KTRACE 41 #include <sys/ktrace.h> 42 #endif 43 44 #include <security/audit/audit.h> 45 46 #ifdef COMPAT_LINUX32 47 #include <machine/../linux32/linux.h> 48 #include <machine/../linux32/linux32_proto.h> 49 #else 50 #include <machine/../linux/linux.h> 51 #include <machine/../linux/linux_proto.h> 52 #endif 53 #include <compat/linux/linux_mib.h> 54 #include <compat/linux/linux_signal.h> 55 #include <compat/linux/linux_time.h> 56 #include <compat/linux/linux_util.h> 57 #include <compat/linux/linux_emul.h> 58 #include <compat/linux/linux_misc.h> 59 60 static int linux_pksignal(struct thread *td, int pid, int sig, 61 ksiginfo_t *ksi); 62 static int linux_psignal(struct thread *td, int pid, int sig); 63 static int linux_tdksignal(struct thread *td, lwpid_t tid, 64 int tgid, int sig, ksiginfo_t *ksi); 65 static int linux_tdsignal(struct thread *td, lwpid_t tid, 66 int tgid, int sig); 67 static void sicode_to_lsicode(int sig, int si_code, int *lsi_code); 68 static int linux_common_rt_sigtimedwait(struct thread *, 69 l_sigset_t *, struct timespec *, l_siginfo_t *, 70 l_size_t); 71 72 static void 73 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa) 74 { 75 unsigned long flags; 76 77 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask); 78 bsa->sa_handler = PTRIN(lsa->lsa_handler); 79 bsa->sa_flags = 0; 80 81 flags = lsa->lsa_flags; 82 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) { 83 flags &= ~LINUX_SA_NOCLDSTOP; 84 bsa->sa_flags |= SA_NOCLDSTOP; 85 } 86 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) { 87 flags &= ~LINUX_SA_NOCLDWAIT; 88 bsa->sa_flags |= SA_NOCLDWAIT; 89 } 90 if (lsa->lsa_flags & LINUX_SA_SIGINFO) { 91 flags &= ~LINUX_SA_SIGINFO; 92 bsa->sa_flags |= SA_SIGINFO; 93 #ifdef notyet 94 /* 95 * XXX: We seem to be missing code to convert 96 * some of the fields in ucontext_t. 97 */ 98 linux_msg(curthread, 99 "partially unsupported sigaction flag SA_SIGINFO"); 100 #endif 101 } 102 if (lsa->lsa_flags & LINUX_SA_RESTORER) { 103 flags &= ~LINUX_SA_RESTORER; 104 /* 105 * We ignore the lsa_restorer and always use our own signal 106 * trampoline instead. It looks like SA_RESTORER is obsolete 107 * in Linux too - it doesn't seem to be used at all on arm64. 108 * In any case: see Linux sigreturn(2). 109 */ 110 } 111 if (lsa->lsa_flags & LINUX_SA_ONSTACK) { 112 flags &= ~LINUX_SA_ONSTACK; 113 bsa->sa_flags |= SA_ONSTACK; 114 } 115 if (lsa->lsa_flags & LINUX_SA_RESTART) { 116 flags &= ~LINUX_SA_RESTART; 117 bsa->sa_flags |= SA_RESTART; 118 } 119 if (lsa->lsa_flags & LINUX_SA_INTERRUPT) { 120 flags &= ~LINUX_SA_INTERRUPT; 121 /* Documented to be a "historical no-op". */ 122 } 123 if (lsa->lsa_flags & LINUX_SA_ONESHOT) { 124 flags &= ~LINUX_SA_ONESHOT; 125 bsa->sa_flags |= SA_RESETHAND; 126 } 127 if (lsa->lsa_flags & LINUX_SA_NOMASK) { 128 flags &= ~LINUX_SA_NOMASK; 129 bsa->sa_flags |= SA_NODEFER; 130 } 131 132 /* 133 * SA_UNSUPPORTED was introduced in Linux 5.11 to probe support for 134 * other flags such as SA_EXPOSE_TAGBITS, introduced at the same time. 135 * Ignore both. 136 */ 137 if (lsa->lsa_flags & (LINUX_SA_UNSUPPORTED | LINUX_SA_EXPOSE_TAGBITS)) 138 flags &= ~(LINUX_SA_UNSUPPORTED | LINUX_SA_EXPOSE_TAGBITS); 139 140 if (flags != 0) 141 linux_msg(curthread, "unsupported sigaction flag %#lx", flags); 142 } 143 144 static void 145 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa) 146 { 147 148 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask); 149 #ifdef COMPAT_LINUX32 150 lsa->lsa_handler = (uintptr_t)bsa->sa_handler; 151 #else 152 lsa->lsa_handler = bsa->sa_handler; 153 #endif 154 lsa->lsa_restorer = 0; /* unsupported */ 155 lsa->lsa_flags = 0; 156 if (bsa->sa_flags & SA_NOCLDSTOP) 157 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP; 158 if (bsa->sa_flags & SA_NOCLDWAIT) 159 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT; 160 if (bsa->sa_flags & SA_SIGINFO) 161 lsa->lsa_flags |= LINUX_SA_SIGINFO; 162 if (bsa->sa_flags & SA_ONSTACK) 163 lsa->lsa_flags |= LINUX_SA_ONSTACK; 164 if (bsa->sa_flags & SA_RESTART) 165 lsa->lsa_flags |= LINUX_SA_RESTART; 166 if (bsa->sa_flags & SA_RESETHAND) 167 lsa->lsa_flags |= LINUX_SA_ONESHOT; 168 if (bsa->sa_flags & SA_NODEFER) 169 lsa->lsa_flags |= LINUX_SA_NOMASK; 170 } 171 172 int 173 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa, 174 l_sigaction_t *linux_osa) 175 { 176 struct sigaction act, oact, *nsa, *osa; 177 int error, sig; 178 179 if (!LINUX_SIG_VALID(linux_sig)) 180 return (EINVAL); 181 sig = linux_to_bsd_signal(linux_sig); 182 183 osa = (linux_osa != NULL) ? &oact : NULL; 184 if (linux_nsa != NULL) { 185 nsa = &act; 186 linux_to_bsd_sigaction(linux_nsa, nsa); 187 #ifdef KTRACE 188 if (KTRPOINT(td, KTR_STRUCT)) 189 linux_ktrsigset(&linux_nsa->lsa_mask, 190 sizeof(linux_nsa->lsa_mask)); 191 #endif 192 if ((sig == SIGKILL || sig == SIGSTOP) && 193 nsa->sa_handler == SIG_DFL) 194 return (EINVAL); 195 } else 196 nsa = NULL; 197 198 error = kern_sigaction(td, sig, nsa, osa, 0); 199 if (error != 0) 200 return (error); 201 202 if (linux_osa != NULL) { 203 bsd_to_linux_sigaction(osa, linux_osa); 204 #ifdef KTRACE 205 if (KTRPOINT(td, KTR_STRUCT)) 206 linux_ktrsigset(&linux_osa->lsa_mask, 207 sizeof(linux_osa->lsa_mask)); 208 #endif 209 } 210 return (0); 211 } 212 213 int 214 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 215 { 216 stack_t ss, oss; 217 l_stack_t lss; 218 int error; 219 220 memset(&lss, 0, sizeof(lss)); 221 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss); 222 223 if (uap->uss != NULL) { 224 error = copyin(uap->uss, &lss, sizeof(lss)); 225 if (error != 0) 226 return (error); 227 228 ss.ss_sp = PTRIN(lss.ss_sp); 229 ss.ss_size = lss.ss_size; 230 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 231 } 232 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 233 (uap->uoss != NULL) ? &oss : NULL); 234 if (error == 0 && uap->uoss != NULL) { 235 lss.ss_sp = PTROUT(oss.ss_sp); 236 lss.ss_size = oss.ss_size; 237 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 238 error = copyout(&lss, uap->uoss, sizeof(lss)); 239 } 240 241 return (error); 242 } 243 244 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 245 int 246 linux_signal(struct thread *td, struct linux_signal_args *args) 247 { 248 l_sigaction_t nsa, osa; 249 int error; 250 251 nsa.lsa_handler = args->handler; 252 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK; 253 LINUX_SIGEMPTYSET(nsa.lsa_mask); 254 255 error = linux_do_sigaction(td, args->sig, &nsa, &osa); 256 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler; 257 258 return (error); 259 } 260 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 261 262 int 263 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args) 264 { 265 l_sigaction_t nsa, osa; 266 int error; 267 268 if (args->sigsetsize != sizeof(l_sigset_t)) 269 return (EINVAL); 270 271 if (args->act != NULL) { 272 error = copyin(args->act, &nsa, sizeof(nsa)); 273 if (error != 0) 274 return (error); 275 } 276 277 error = linux_do_sigaction(td, args->sig, 278 args->act ? &nsa : NULL, 279 args->oact ? &osa : NULL); 280 281 if (args->oact != NULL && error == 0) 282 error = copyout(&osa, args->oact, sizeof(osa)); 283 284 return (error); 285 } 286 287 static int 288 linux_do_sigprocmask(struct thread *td, int how, sigset_t *new, 289 l_sigset_t *old) 290 { 291 sigset_t omask; 292 int error; 293 294 td->td_retval[0] = 0; 295 296 switch (how) { 297 case LINUX_SIG_BLOCK: 298 how = SIG_BLOCK; 299 break; 300 case LINUX_SIG_UNBLOCK: 301 how = SIG_UNBLOCK; 302 break; 303 case LINUX_SIG_SETMASK: 304 how = SIG_SETMASK; 305 break; 306 default: 307 return (EINVAL); 308 } 309 error = kern_sigprocmask(td, how, new, &omask, 0); 310 if (error == 0 && old != NULL) 311 bsd_to_linux_sigset(&omask, old); 312 313 return (error); 314 } 315 316 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 317 int 318 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args) 319 { 320 l_osigset_t mask; 321 l_sigset_t lset, oset; 322 sigset_t set; 323 int error; 324 325 if (args->mask != NULL) { 326 error = copyin(args->mask, &mask, sizeof(mask)); 327 if (error != 0) 328 return (error); 329 LINUX_SIGEMPTYSET(lset); 330 lset.__mask = mask; 331 #ifdef KTRACE 332 if (KTRPOINT(td, KTR_STRUCT)) 333 linux_ktrsigset(&lset, sizeof(lset)); 334 #endif 335 linux_to_bsd_sigset(&lset, &set); 336 } 337 338 error = linux_do_sigprocmask(td, args->how, 339 args->mask ? &set : NULL, 340 args->omask ? &oset : NULL); 341 342 if (args->omask != NULL && error == 0) { 343 #ifdef KTRACE 344 if (KTRPOINT(td, KTR_STRUCT)) 345 linux_ktrsigset(&oset, sizeof(oset)); 346 #endif 347 mask = oset.__mask; 348 error = copyout(&mask, args->omask, sizeof(mask)); 349 } 350 351 return (error); 352 } 353 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 354 355 int 356 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args) 357 { 358 l_sigset_t oset; 359 sigset_t set, *pset; 360 int error; 361 362 error = linux_copyin_sigset(td, args->mask, args->sigsetsize, 363 &set, &pset); 364 if (error != 0) 365 return (EINVAL); 366 367 error = linux_do_sigprocmask(td, args->how, pset, 368 args->omask ? &oset : NULL); 369 370 if (args->omask != NULL && error == 0) { 371 #ifdef KTRACE 372 if (KTRPOINT(td, KTR_STRUCT)) 373 linux_ktrsigset(&oset, sizeof(oset)); 374 #endif 375 error = copyout(&oset, args->omask, sizeof(oset)); 376 } 377 378 return (error); 379 } 380 381 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 382 int 383 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args) 384 { 385 struct proc *p = td->td_proc; 386 l_sigset_t mask; 387 388 PROC_LOCK(p); 389 bsd_to_linux_sigset(&td->td_sigmask, &mask); 390 PROC_UNLOCK(p); 391 td->td_retval[0] = mask.__mask; 392 #ifdef KTRACE 393 if (KTRPOINT(td, KTR_STRUCT)) 394 linux_ktrsigset(&mask, sizeof(mask)); 395 #endif 396 return (0); 397 } 398 399 int 400 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args) 401 { 402 struct proc *p = td->td_proc; 403 l_sigset_t lset; 404 sigset_t bset; 405 406 PROC_LOCK(p); 407 bsd_to_linux_sigset(&td->td_sigmask, &lset); 408 td->td_retval[0] = lset.__mask; 409 LINUX_SIGEMPTYSET(lset); 410 lset.__mask = args->mask; 411 linux_to_bsd_sigset(&lset, &bset); 412 #ifdef KTRACE 413 if (KTRPOINT(td, KTR_STRUCT)) 414 linux_ktrsigset(&lset, sizeof(lset)); 415 #endif 416 td->td_sigmask = bset; 417 SIG_CANTMASK(td->td_sigmask); 418 signotify(td); 419 PROC_UNLOCK(p); 420 return (0); 421 } 422 423 int 424 linux_sigpending(struct thread *td, struct linux_sigpending_args *args) 425 { 426 struct proc *p = td->td_proc; 427 sigset_t bset; 428 l_sigset_t lset; 429 l_osigset_t mask; 430 431 PROC_LOCK(p); 432 bset = p->p_siglist; 433 SIGSETOR(bset, td->td_siglist); 434 SIGSETAND(bset, td->td_sigmask); 435 PROC_UNLOCK(p); 436 bsd_to_linux_sigset(&bset, &lset); 437 #ifdef KTRACE 438 if (KTRPOINT(td, KTR_STRUCT)) 439 linux_ktrsigset(&lset, sizeof(lset)); 440 #endif 441 mask = lset.__mask; 442 return (copyout(&mask, args->mask, sizeof(mask))); 443 } 444 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 445 446 /* 447 * MPSAFE 448 */ 449 int 450 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args) 451 { 452 struct proc *p = td->td_proc; 453 sigset_t bset; 454 l_sigset_t lset; 455 456 if (args->sigsetsize > sizeof(lset)) 457 return (EINVAL); 458 /* NOT REACHED */ 459 460 PROC_LOCK(p); 461 bset = p->p_siglist; 462 SIGSETOR(bset, td->td_siglist); 463 SIGSETAND(bset, td->td_sigmask); 464 PROC_UNLOCK(p); 465 bsd_to_linux_sigset(&bset, &lset); 466 #ifdef KTRACE 467 if (KTRPOINT(td, KTR_STRUCT)) 468 linux_ktrsigset(&lset, sizeof(lset)); 469 #endif 470 return (copyout(&lset, args->set, args->sigsetsize)); 471 } 472 473 int 474 linux_rt_sigtimedwait(struct thread *td, 475 struct linux_rt_sigtimedwait_args *args) 476 { 477 struct timespec ts, *tsa; 478 int error; 479 480 if (args->timeout) { 481 error = linux_get_timespec(&ts, args->timeout); 482 if (error != 0) 483 return (error); 484 tsa = &ts; 485 } else 486 tsa = NULL; 487 488 return (linux_common_rt_sigtimedwait(td, args->mask, tsa, 489 args->ptr, args->sigsetsize)); 490 } 491 492 static int 493 linux_common_rt_sigtimedwait(struct thread *td, l_sigset_t *mask, 494 struct timespec *tsa, l_siginfo_t *ptr, l_size_t sigsetsize) 495 { 496 int error, sig; 497 sigset_t bset; 498 l_siginfo_t lsi; 499 ksiginfo_t ksi; 500 501 error = linux_copyin_sigset(td, mask, sigsetsize, &bset, NULL); 502 if (error != 0) 503 return (error); 504 505 ksiginfo_init(&ksi); 506 error = kern_sigtimedwait(td, bset, &ksi, tsa); 507 if (error != 0) 508 return (error); 509 510 sig = bsd_to_linux_signal(ksi.ksi_signo); 511 512 if (ptr) { 513 memset(&lsi, 0, sizeof(lsi)); 514 siginfo_to_lsiginfo(&ksi.ksi_info, &lsi, sig); 515 error = copyout(&lsi, ptr, sizeof(lsi)); 516 } 517 if (error == 0) 518 td->td_retval[0] = sig; 519 520 return (error); 521 } 522 523 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 524 int 525 linux_rt_sigtimedwait_time64(struct thread *td, 526 struct linux_rt_sigtimedwait_time64_args *args) 527 { 528 struct timespec ts, *tsa; 529 int error; 530 531 if (args->timeout) { 532 error = linux_get_timespec64(&ts, args->timeout); 533 if (error != 0) 534 return (error); 535 tsa = &ts; 536 } else 537 tsa = NULL; 538 539 return (linux_common_rt_sigtimedwait(td, args->mask, tsa, 540 args->ptr, args->sigsetsize)); 541 } 542 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 543 544 int 545 linux_kill(struct thread *td, struct linux_kill_args *args) 546 { 547 int sig; 548 549 /* 550 * Allow signal 0 as a means to check for privileges 551 */ 552 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0) 553 return (EINVAL); 554 555 if (args->signum > 0) 556 sig = linux_to_bsd_signal(args->signum); 557 else 558 sig = 0; 559 560 if (args->pid > PID_MAX) 561 return (linux_psignal(td, args->pid, sig)); 562 else 563 return (kern_kill(td, args->pid, sig)); 564 } 565 566 int 567 linux_tgkill(struct thread *td, struct linux_tgkill_args *args) 568 { 569 int sig; 570 571 if (args->pid <= 0 || args->tgid <=0) 572 return (EINVAL); 573 574 /* 575 * Allow signal 0 as a means to check for privileges 576 */ 577 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0) 578 return (EINVAL); 579 580 if (args->sig > 0) 581 sig = linux_to_bsd_signal(args->sig); 582 else 583 sig = 0; 584 585 return (linux_tdsignal(td, args->pid, args->tgid, sig)); 586 } 587 588 /* 589 * Deprecated since 2.5.75. Replaced by tgkill(). 590 */ 591 int 592 linux_tkill(struct thread *td, struct linux_tkill_args *args) 593 { 594 int sig; 595 596 if (args->tid <= 0) 597 return (EINVAL); 598 599 if (!LINUX_SIG_VALID(args->sig)) 600 return (EINVAL); 601 602 sig = linux_to_bsd_signal(args->sig); 603 604 return (linux_tdsignal(td, args->tid, -1, sig)); 605 } 606 607 static int 608 sigfpe_sicode2lsicode(int si_code) 609 { 610 611 switch (si_code) { 612 case FPE_INTOVF: 613 return (LINUX_FPE_INTOVF); 614 case FPE_INTDIV: 615 return (LINUX_FPE_INTDIV); 616 case FPE_FLTIDO: 617 return (LINUX_FPE_FLTUNK); 618 default: 619 return (si_code); 620 } 621 } 622 623 static int 624 sigbus_sicode2lsicode(int si_code) 625 { 626 627 switch (si_code) { 628 case BUS_OOMERR: 629 return (LINUX_BUS_MCEERR_AR); 630 default: 631 return (si_code); 632 } 633 } 634 635 static int 636 sigsegv_sicode2lsicode(int si_code) 637 { 638 639 switch (si_code) { 640 case SEGV_PKUERR: 641 return (LINUX_SEGV_PKUERR); 642 default: 643 return (si_code); 644 } 645 } 646 647 static int 648 sigtrap_sicode2lsicode(int si_code) 649 { 650 651 switch (si_code) { 652 case TRAP_DTRACE: 653 return (LINUX_TRAP_TRACE); 654 case TRAP_CAP: 655 return (LINUX_TRAP_UNK); 656 default: 657 return (si_code); 658 } 659 } 660 661 static void 662 sicode_to_lsicode(int sig, int si_code, int *lsi_code) 663 { 664 665 switch (si_code) { 666 case SI_USER: 667 *lsi_code = LINUX_SI_USER; 668 break; 669 case SI_KERNEL: 670 *lsi_code = LINUX_SI_KERNEL; 671 break; 672 case SI_QUEUE: 673 *lsi_code = LINUX_SI_QUEUE; 674 break; 675 case SI_TIMER: 676 *lsi_code = LINUX_SI_TIMER; 677 break; 678 case SI_MESGQ: 679 *lsi_code = LINUX_SI_MESGQ; 680 break; 681 case SI_ASYNCIO: 682 *lsi_code = LINUX_SI_ASYNCIO; 683 break; 684 case SI_LWP: 685 *lsi_code = LINUX_SI_TKILL; 686 break; 687 default: 688 switch (sig) { 689 case LINUX_SIGFPE: 690 *lsi_code = sigfpe_sicode2lsicode(si_code); 691 break; 692 case LINUX_SIGBUS: 693 *lsi_code = sigbus_sicode2lsicode(si_code); 694 break; 695 case LINUX_SIGSEGV: 696 *lsi_code = sigsegv_sicode2lsicode(si_code); 697 break; 698 case LINUX_SIGTRAP: 699 *lsi_code = sigtrap_sicode2lsicode(si_code); 700 break; 701 default: 702 *lsi_code = si_code; 703 break; 704 } 705 break; 706 } 707 } 708 709 void 710 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig) 711 { 712 713 /* sig already converted */ 714 lsi->lsi_signo = sig; 715 sicode_to_lsicode(sig, si->si_code, &lsi->lsi_code); 716 717 switch (si->si_code) { 718 case SI_LWP: 719 lsi->lsi_pid = si->si_pid; 720 lsi->lsi_uid = si->si_uid; 721 break; 722 723 case SI_TIMER: 724 lsi->lsi_int = si->si_value.sival_int; 725 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr); 726 lsi->lsi_tid = si->si_timerid; 727 break; 728 729 case SI_QUEUE: 730 lsi->lsi_pid = si->si_pid; 731 lsi->lsi_uid = si->si_uid; 732 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr); 733 break; 734 735 case SI_ASYNCIO: 736 lsi->lsi_int = si->si_value.sival_int; 737 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr); 738 break; 739 740 default: 741 switch (sig) { 742 case LINUX_SIGPOLL: 743 /* XXX si_fd? */ 744 lsi->lsi_band = si->si_band; 745 break; 746 747 case LINUX_SIGCHLD: 748 lsi->lsi_errno = 0; 749 lsi->lsi_pid = si->si_pid; 750 lsi->lsi_uid = si->si_uid; 751 752 if (si->si_code == CLD_STOPPED || si->si_code == CLD_KILLED) 753 lsi->lsi_status = bsd_to_linux_signal(si->si_status); 754 else if (si->si_code == CLD_CONTINUED) 755 lsi->lsi_status = bsd_to_linux_signal(SIGCONT); 756 else 757 lsi->lsi_status = si->si_status; 758 break; 759 760 case LINUX_SIGBUS: 761 case LINUX_SIGILL: 762 case LINUX_SIGFPE: 763 case LINUX_SIGSEGV: 764 lsi->lsi_addr = PTROUT(si->si_addr); 765 break; 766 767 default: 768 lsi->lsi_pid = si->si_pid; 769 lsi->lsi_uid = si->si_uid; 770 if (sig >= LINUX_SIGRTMIN) { 771 lsi->lsi_int = si->si_value.sival_int; 772 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr); 773 } 774 break; 775 } 776 break; 777 } 778 } 779 780 static int 781 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi, 782 siginfo_t *si, int sig) 783 { 784 785 switch (lsi->lsi_code) { 786 case LINUX_SI_TKILL: 787 if (linux_kernver(td) >= LINUX_KERNVER(2,6,39)) { 788 linux_msg(td, "SI_TKILL forbidden since 2.6.39"); 789 return (EPERM); 790 } 791 si->si_code = SI_LWP; 792 break; 793 case LINUX_SI_QUEUE: 794 si->si_code = SI_QUEUE; 795 break; 796 case LINUX_SI_TIMER: 797 si->si_code = SI_TIMER; 798 break; 799 case LINUX_SI_MESGQ: 800 si->si_code = SI_MESGQ; 801 break; 802 case LINUX_SI_ASYNCIO: 803 si->si_code = SI_ASYNCIO; 804 break; 805 default: 806 si->si_code = lsi->lsi_code; 807 break; 808 } 809 810 si->si_signo = sig; 811 si->si_pid = td->td_proc->p_pid; 812 si->si_uid = td->td_ucred->cr_ruid; 813 si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr); 814 return (0); 815 } 816 817 int 818 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args) 819 { 820 l_siginfo_t linfo; 821 ksiginfo_t ksi; 822 int error; 823 int sig; 824 825 if (!LINUX_SIG_VALID(args->sig)) 826 return (EINVAL); 827 828 error = copyin(args->info, &linfo, sizeof(linfo)); 829 if (error != 0) 830 return (error); 831 832 if (linfo.lsi_code >= 0) 833 /* SI_USER, SI_KERNEL */ 834 return (EPERM); 835 836 sig = linux_to_bsd_signal(args->sig); 837 ksiginfo_init(&ksi); 838 error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig); 839 if (error != 0) 840 return (error); 841 842 return (linux_pksignal(td, args->pid, sig, &ksi)); 843 } 844 845 int 846 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args) 847 { 848 l_siginfo_t linfo; 849 ksiginfo_t ksi; 850 int error; 851 int sig; 852 853 if (!LINUX_SIG_VALID(args->sig)) 854 return (EINVAL); 855 856 error = copyin(args->uinfo, &linfo, sizeof(linfo)); 857 if (error != 0) 858 return (error); 859 860 if (linfo.lsi_code >= 0) 861 return (EPERM); 862 863 sig = linux_to_bsd_signal(args->sig); 864 ksiginfo_init(&ksi); 865 error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig); 866 if (error != 0) 867 return (error); 868 869 return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi)); 870 } 871 872 int 873 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 874 { 875 sigset_t sigmask; 876 int error; 877 878 error = linux_copyin_sigset(td, uap->newset, uap->sigsetsize, 879 &sigmask, NULL); 880 if (error != 0) 881 return (error); 882 883 return (kern_sigsuspend(td, sigmask)); 884 } 885 886 static int 887 linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig, 888 ksiginfo_t *ksi) 889 { 890 struct thread *tdt; 891 struct proc *p; 892 int error; 893 894 tdt = linux_tdfind(td, tid, tgid); 895 if (tdt == NULL) 896 return (ESRCH); 897 898 p = tdt->td_proc; 899 AUDIT_ARG_SIGNUM(sig); 900 AUDIT_ARG_PID(p->p_pid); 901 AUDIT_ARG_PROCESS(p); 902 903 error = p_cansignal(td, p, sig); 904 if (error != 0 || sig == 0) 905 goto out; 906 907 tdksignal(tdt, sig, ksi); 908 909 out: 910 PROC_UNLOCK(p); 911 return (error); 912 } 913 914 static int 915 linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig) 916 { 917 ksiginfo_t ksi; 918 919 ksiginfo_init(&ksi); 920 ksi.ksi_signo = sig; 921 ksi.ksi_code = SI_LWP; 922 ksi.ksi_pid = td->td_proc->p_pid; 923 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid; 924 return (linux_tdksignal(td, tid, tgid, sig, &ksi)); 925 } 926 927 static int 928 linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi) 929 { 930 struct thread *tdt; 931 struct proc *p; 932 int error; 933 934 tdt = linux_tdfind(td, pid, -1); 935 if (tdt == NULL) 936 return (ESRCH); 937 938 p = tdt->td_proc; 939 AUDIT_ARG_SIGNUM(sig); 940 AUDIT_ARG_PID(p->p_pid); 941 AUDIT_ARG_PROCESS(p); 942 943 error = p_cansignal(td, p, sig); 944 if (error != 0 || sig == 0) 945 goto out; 946 947 pksignal(p, sig, ksi); 948 949 out: 950 PROC_UNLOCK(p); 951 return (error); 952 } 953 954 static int 955 linux_psignal(struct thread *td, int pid, int sig) 956 { 957 ksiginfo_t ksi; 958 959 ksiginfo_init(&ksi); 960 ksi.ksi_signo = sig; 961 ksi.ksi_code = SI_LWP; 962 ksi.ksi_pid = td->td_proc->p_pid; 963 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid; 964 return (linux_pksignal(td, pid, sig, &ksi)); 965 } 966 967 int 968 linux_copyin_sigset(struct thread *td, l_sigset_t *lset, 969 l_size_t sigsetsize, sigset_t *set, sigset_t **pset) 970 { 971 l_sigset_t lmask; 972 int error; 973 974 if (sigsetsize != sizeof(l_sigset_t)) 975 return (EINVAL); 976 if (lset != NULL) { 977 error = copyin(lset, &lmask, sizeof(lmask)); 978 if (error != 0) 979 return (error); 980 linux_to_bsd_sigset(&lmask, set); 981 if (pset != NULL) 982 *pset = set; 983 #ifdef KTRACE 984 if (KTRPOINT(td, KTR_STRUCT)) 985 linux_ktrsigset(&lmask, sizeof(lmask)); 986 #endif 987 } else if (pset != NULL) 988 *pset = NULL; 989 return (0); 990 } 991