1 /* $NetBSD: linux32_signal.c,v 1.20 2019/08/23 08:31:11 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Emmanuel Dreyfus, 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 Emmanuel Dreyfus 17 * 4. The name of the author may not be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: linux32_signal.c,v 1.20 2019/08/23 08:31:11 maxv Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/ucred.h> 39 #include <sys/signalvar.h> 40 #include <sys/lwp.h> 41 #include <sys/time.h> 42 #include <sys/proc.h> 43 #include <sys/wait.h> 44 45 #include <compat/netbsd32/netbsd32.h> 46 47 #include <compat/linux/common/linux_types.h> 48 #include <compat/linux/common/linux_signal.h> 49 50 #include <compat/linux32/common/linux32_types.h> 51 #include <compat/linux32/common/linux32_signal.h> 52 #include <compat/linux32/common/linux32_siginfo.h> 53 #include <compat/linux32/linux32_syscallargs.h> 54 #include <compat/linux32/common/linux32_errno.h> 55 #include <compat/linux32/common/linux32_sched.h> 56 57 #define linux32_sigemptyset(s) memset((s), 0, sizeof(*(s))) 58 #define linux32_sigismember(s, n) ((s)->sig[((n) - 1) / LINUX32__NSIG_BPW] \ 59 & (1 << ((n) - 1) % LINUX32__NSIG_BPW)) 60 #define linux32_sigaddset(s, n) ((s)->sig[((n) - 1) / LINUX32__NSIG_BPW] \ 61 |= (1 << ((n) - 1) % LINUX32__NSIG_BPW)) 62 63 extern const int native_to_linux32_signo[]; 64 extern const int linux32_to_native_signo[]; 65 66 #ifdef DEBUG_LINUX 67 #define DPRINTF(a) uprintf a 68 #else 69 #define DPRINTF(a) 70 #endif 71 72 void 73 linux32_to_native_sigset(sigset_t *bss, const linux32_sigset_t *lss) 74 { 75 int i, newsig; 76 77 sigemptyset(bss); 78 for (i = 1; i < LINUX32__NSIG; i++) { 79 if (linux32_sigismember(lss, i)) { 80 newsig = linux32_to_native_signo[i]; 81 if (newsig) 82 sigaddset(bss, newsig); 83 } 84 } 85 } 86 87 void 88 native_to_linux32_sigset(linux32_sigset_t *lss, const sigset_t *bss) 89 { 90 int i, newsig; 91 92 linux32_sigemptyset(lss); 93 for (i = 1; i < NSIG; i++) { 94 if (sigismember(bss, i)) { 95 newsig = native_to_linux32_signo[i]; 96 if (newsig) 97 linux32_sigaddset(lss, newsig); 98 } 99 } 100 } 101 102 void 103 native_to_linux32_siginfo(linux32_siginfo_t *lsi, const struct _ksiginfo *ksi) 104 { 105 memset(lsi, 0, sizeof(*lsi)); 106 107 lsi->lsi_signo = native_to_linux32_signo[ksi->_signo]; 108 lsi->lsi_errno = native_to_linux32_errno[ksi->_errno]; 109 lsi->lsi_code = native_to_linux32_si_code(ksi->_code); 110 111 switch (ksi->_code) { 112 case SI_NOINFO: 113 break; 114 115 case SI_USER: 116 lsi->lsi_pid = ksi->_reason._rt._pid; 117 lsi->lsi_uid = ksi->_reason._rt._uid; 118 if (lsi->lsi_signo == LINUX_SIGALRM || 119 lsi->lsi_signo >= LINUX_SIGRTMIN) 120 NETBSD32PTR32(lsi->lsi_value.sival_ptr, 121 ksi->_reason._rt._value.sival_ptr); 122 break; 123 124 case SI_TIMER: 125 case SI_QUEUE: 126 lsi->lsi_uid = ksi->_reason._rt._uid; 127 lsi->lsi_uid = ksi->_reason._rt._uid; 128 NETBSD32PTR32(lsi->lsi_value.sival_ptr, 129 ksi->_reason._rt._value.sival_ptr); 130 break; 131 132 case SI_ASYNCIO: 133 case SI_MESGQ: 134 NETBSD32PTR32(lsi->lsi_value.sival_ptr, 135 ksi->_reason._rt._value.sival_ptr); 136 break; 137 138 default: 139 switch (ksi->_signo) { 140 case SIGCHLD: 141 lsi->lsi_uid = ksi->_reason._child._uid; 142 lsi->lsi_pid = ksi->_reason._child._pid; 143 lsi->lsi_status = native_to_linux32_si_status( 144 ksi->_code, ksi->_reason._child._status); 145 lsi->lsi_utime = ksi->_reason._child._utime; 146 lsi->lsi_stime = ksi->_reason._child._stime; 147 break; 148 149 case SIGILL: 150 case SIGFPE: 151 case SIGSEGV: 152 case SIGBUS: 153 case SIGTRAP: 154 NETBSD32PTR32(lsi->lsi_addr, ksi->_reason._fault._addr); 155 break; 156 157 case SIGIO: 158 lsi->lsi_fd = ksi->_reason._poll._fd; 159 lsi->lsi_band = ksi->_reason._poll._band; 160 break; 161 default: 162 break; 163 } 164 } 165 } 166 167 unsigned int 168 native_to_linux32_sigflags(const int bsf) 169 { 170 unsigned int lsf = 0; 171 if ((bsf & SA_NOCLDSTOP) != 0) 172 lsf |= LINUX32_SA_NOCLDSTOP; 173 if ((bsf & SA_NOCLDWAIT) != 0) 174 lsf |= LINUX32_SA_NOCLDWAIT; 175 if ((bsf & SA_ONSTACK) != 0) 176 lsf |= LINUX32_SA_ONSTACK; 177 if ((bsf & SA_RESTART) != 0) 178 lsf |= LINUX32_SA_RESTART; 179 if ((bsf & SA_NODEFER) != 0) 180 lsf |= LINUX32_SA_NOMASK; 181 if ((bsf & SA_RESETHAND) != 0) 182 lsf |= LINUX32_SA_ONESHOT; 183 if ((bsf & SA_SIGINFO) != 0) 184 lsf |= LINUX32_SA_SIGINFO; 185 return lsf; 186 } 187 188 int 189 linux32_to_native_sigflags(const unsigned long lsf) 190 { 191 int bsf = 0; 192 if ((lsf & LINUX32_SA_NOCLDSTOP) != 0) 193 bsf |= SA_NOCLDSTOP; 194 if ((lsf & LINUX32_SA_NOCLDWAIT) != 0) 195 bsf |= SA_NOCLDWAIT; 196 if ((lsf & LINUX32_SA_ONSTACK) != 0) 197 bsf |= SA_ONSTACK; 198 if ((lsf & LINUX32_SA_RESTART) != 0) 199 bsf |= SA_RESTART; 200 if ((lsf & LINUX32_SA_ONESHOT) != 0) 201 bsf |= SA_RESETHAND; 202 if ((lsf & LINUX32_SA_NOMASK) != 0) 203 bsf |= SA_NODEFER; 204 if ((lsf & LINUX32_SA_SIGINFO) != 0) 205 bsf |= SA_SIGINFO; 206 if ((lsf & ~LINUX32_SA_ALLBITS) != 0) { 207 #ifdef DEBUG_LINUX 208 printf("linux32_old_to_native_sigflags: " 209 "%lx extra bits ignored\n", lsf); 210 #endif 211 } 212 return bsf; 213 } 214 215 void 216 linux32_to_native_sigaction(struct sigaction *bsa, const struct linux32_sigaction *lsa) 217 { 218 bsa->sa_handler = NETBSD32PTR64(lsa->linux_sa_handler); 219 linux32_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask); 220 bsa->sa_flags = linux32_to_native_sigflags(lsa->linux_sa_flags); 221 } 222 223 void 224 native_to_linux32_sigaction(struct linux32_sigaction *lsa, const struct sigaction *bsa) 225 { 226 NETBSD32PTR32(lsa->linux_sa_handler, bsa->sa_handler); 227 native_to_linux32_sigset(&lsa->linux_sa_mask, &bsa->sa_mask); 228 lsa->linux_sa_flags = native_to_linux32_sigflags(bsa->sa_flags); 229 NETBSD32PTR32(lsa->linux_sa_restorer, NULL); 230 } 231 232 void 233 native_to_linux32_sigaltstack(struct linux32_sigaltstack *lss, const struct sigaltstack *bss) 234 { 235 memset(lss, 0, sizeof(*lss)); 236 NETBSD32PTR32(lss->ss_sp, bss->ss_sp); 237 lss->ss_size = bss->ss_size; 238 if (bss->ss_flags & SS_ONSTACK) 239 lss->ss_flags = LINUX32_SS_ONSTACK; 240 else if (bss->ss_flags & SS_DISABLE) 241 lss->ss_flags = LINUX32_SS_DISABLE; 242 else 243 lss->ss_flags = 0; 244 } 245 246 247 void 248 native_to_linux32_old_sigset(linux32_old_sigset_t *lss, const sigset_t *bss) 249 { 250 linux32_sigset_t lsnew; 251 252 native_to_linux32_sigset(&lsnew, bss); 253 254 /* convert new sigset to old sigset */ 255 *lss = lsnew.sig[0]; 256 } 257 258 void 259 linux32_old_to_native_sigset(sigset_t *bss, const linux32_old_sigset_t *lss) 260 { 261 linux32_sigset_t ls; 262 263 memset(&ls, 0, sizeof(ls)); 264 ls.sig[0] = *lss; 265 266 linux32_to_native_sigset(bss, &ls); 267 } 268 269 int 270 linux32_sys_rt_sigaction(struct lwp *l, const struct linux32_sys_rt_sigaction_args *uap, register_t *retval) 271 { 272 /* { 273 syscallarg(int) signum; 274 syscallarg(const linux32_sigactionp_t) nsa; 275 syscallarg(linux32_sigactionp_t) osa; 276 syscallarg(netbsd32_size_t) sigsetsize; 277 } */ 278 struct linux32_sigaction nls32; 279 struct linux32_sigaction ols32; 280 struct sigaction ns; 281 struct sigaction os; 282 int error; 283 int sig; 284 int vers = 0; 285 void *tramp = NULL; 286 287 if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t)) { 288 DPRINTF(("rt_sigaction: Inconsistent sigsetsize %u %zu\n", 289 SCARG(uap, sigsetsize), sizeof(linux32_sigset_t))); 290 return EINVAL; 291 } 292 293 if (SCARG_P32(uap, nsa) != NULL) { 294 if ((error = copyin(SCARG_P32(uap, nsa), 295 &nls32, sizeof(nls32))) != 0) { 296 DPRINTF(("rt_sigaction: Copyin %d\n", error)); 297 return error; 298 } 299 linux32_to_native_sigaction(&ns, &nls32); 300 } 301 302 sig = SCARG(uap, signum); 303 /* 304 * XXX: Linux has 33 realtime signals, the go binary wants to 305 * reset all of them; nothing else uses the last RT signal, so for 306 * now ignore it. 307 */ 308 if (sig == LINUX__NSIG) { 309 uprintf("%s: setting signal %d ignored\n", __func__, sig); 310 sig--; /* back to 63 which is ignored */ 311 } 312 if (sig < 0 || sig >= LINUX32__NSIG) { 313 DPRINTF(("rt_sigaction: Bad signal number %d %d\n", 314 sig, LINUX32__NSIG)); 315 return EINVAL; 316 } 317 if (sig > 0 && !linux32_to_native_signo[sig]) { 318 /* unknown signal... */ 319 os.sa_handler = SIG_IGN; 320 sigemptyset(&os.sa_mask); 321 os.sa_flags = 0; 322 } else { 323 if ((error = sigaction1(l, 324 linux32_to_native_signo[sig], 325 SCARG_P32(uap, nsa) ? &ns : NULL, 326 SCARG_P32(uap, osa) ? &os : NULL, 327 tramp, vers)) != 0) { 328 DPRINTF(("rt_sigaction: sigaction %d\n", error)); 329 return error; 330 } 331 } 332 333 if (SCARG_P32(uap, osa) != NULL) { 334 native_to_linux32_sigaction(&ols32, &os); 335 336 if ((error = copyout(&ols32, SCARG_P32(uap, osa), 337 sizeof(ols32))) != 0) { 338 DPRINTF(("rt_sigaction: Copyout %d\n", error)); 339 return error; 340 } 341 } 342 343 return 0; 344 } 345 346 int 347 linux32_sys_rt_sigprocmask(struct lwp *l, const struct linux32_sys_rt_sigprocmask_args *uap, register_t *retval) 348 { 349 /* { 350 syscallarg(int) how; 351 syscallarg(const linux32_sigsetp_t) set; 352 syscallarg(linux32_sigsetp_t) oset; 353 syscallarg(netbsd32_size_t) sigsetsize; 354 } */ 355 struct proc *p = l->l_proc; 356 linux32_sigset_t nls32, ols32; 357 sigset_t ns, os; 358 int error; 359 int how; 360 361 if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t)) 362 return EINVAL; 363 364 switch (SCARG(uap, how)) { 365 case LINUX32_SIG_BLOCK: 366 how = SIG_BLOCK; 367 break; 368 case LINUX32_SIG_UNBLOCK: 369 how = SIG_UNBLOCK; 370 break; 371 case LINUX32_SIG_SETMASK: 372 how = SIG_SETMASK; 373 break; 374 default: 375 return EINVAL; 376 break; 377 } 378 379 if (SCARG_P32(uap, set) != NULL) { 380 if ((error = copyin(SCARG_P32(uap, set), 381 &nls32, sizeof(nls32))) != 0) 382 return error; 383 linux32_to_native_sigset(&ns, &nls32); 384 } 385 386 mutex_enter(p->p_lock); 387 error = sigprocmask1(l, how, 388 SCARG_P32(uap, set) ? &ns : NULL, 389 SCARG_P32(uap, oset) ? &os : NULL); 390 mutex_exit(p->p_lock); 391 392 if (error != 0) 393 return error; 394 395 if (SCARG_P32(uap, oset) != NULL) { 396 native_to_linux32_sigset(&ols32, &os); 397 if ((error = copyout(&ols32, 398 SCARG_P32(uap, oset), sizeof(ols32))) != 0) 399 return error; 400 } 401 402 return 0; 403 } 404 405 int 406 linux32_sys_kill(struct lwp *l, const struct linux32_sys_kill_args *uap, register_t *retval) 407 { 408 /* { 409 syscallarg(int) pid; 410 syscallarg(int) signum; 411 } */ 412 413 struct sys_kill_args ka; 414 int sig; 415 416 SCARG(&ka, pid) = SCARG(uap, pid); 417 sig = SCARG(uap, signum); 418 if (sig < 0 || sig >= LINUX32__NSIG) 419 return (EINVAL); 420 SCARG(&ka, signum) = linux32_to_native_signo[sig]; 421 return sys_kill(l, &ka, retval); 422 } 423 424 int 425 linux32_sys_rt_sigsuspend(struct lwp *l, const struct linux32_sys_rt_sigsuspend_args *uap, register_t *retval) 426 { 427 /* { 428 syscallarg(linux32_sigsetp_t) unewset; 429 syscallarg(netbsd32_size_t) sigsetsize; 430 } */ 431 linux32_sigset_t lss; 432 sigset_t bss; 433 int error; 434 435 if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t)) 436 return EINVAL; 437 438 if ((error = copyin(SCARG_P32(uap, unewset), 439 &lss, sizeof(linux32_sigset_t))) != 0) 440 return error; 441 442 linux32_to_native_sigset(&bss, &lss); 443 444 return sigsuspend1(l, &bss); 445 } 446 447 static int 448 fetchss(const void *u, void *s, size_t len) 449 { 450 int error; 451 linux32_sigset_t lss; 452 453 if ((error = copyin(u, &lss, sizeof(lss))) != 0) 454 return error; 455 456 linux32_to_native_sigset(s, &lss); 457 return 0; 458 } 459 460 static int 461 fetchts(const void *u, void *s, size_t len) 462 { 463 int error; 464 struct linux32_timespec lts; 465 466 if ((error = copyin(u, <s, sizeof(lts))) != 0) 467 return error; 468 469 linux32_to_native_timespec(s, <s); 470 return 0; 471 } 472 473 static int 474 fakestorets(const void *u, void *s, size_t len) 475 { 476 /* Do nothing, sigtimedwait does not alter timeout like ours */ 477 return 0; 478 } 479 480 static int 481 storeinfo(const void *s, void *u, size_t len) 482 { 483 linux32_siginfo_t lsi; 484 485 486 native_to_linux32_siginfo(&lsi, &((const siginfo_t *)s)->_info); 487 return copyout(&lsi, u, sizeof(lsi)); 488 } 489 490 int 491 linux32_sys_rt_sigtimedwait(struct lwp *l, 492 const struct linux32_sys_rt_sigtimedwait_args *uap, register_t *retval) 493 { 494 /* { 495 syscallarg(const linux32_sigset_t *) set; 496 syscallarg(linux32_siginfo_t *) info); 497 syscallarg(const struct linux32_timespec *) timeout; 498 } */ 499 struct sys_____sigtimedwait50_args ap; 500 501 SCARG(&ap, set) = SCARG_P32(uap, set); 502 SCARG(&ap, info) = SCARG_P32(uap, info); 503 SCARG(&ap, timeout) = SCARG_P32(uap, timeout); 504 505 return sigtimedwait1(l, &ap, 506 retval, fetchss, storeinfo, fetchts, fakestorets); 507 } 508 509 int 510 linux32_sys_signal(struct lwp *l, const struct linux32_sys_signal_args *uap, register_t *retval) 511 { 512 /* { 513 syscallarg(int) signum; 514 syscallarg(linux32_handlerp_t) handler; 515 } */ 516 struct sigaction nbsa, obsa; 517 int error, sig; 518 519 *retval = -1; 520 521 sig = SCARG(uap, signum); 522 if (sig < 0 || sig >= LINUX32__NSIG) 523 return EINVAL; 524 525 nbsa.sa_handler = SCARG_P32(uap, handler); 526 sigemptyset(&nbsa.sa_mask); 527 nbsa.sa_flags = SA_RESETHAND | SA_NODEFER; 528 529 if ((error = sigaction1(l, linux32_to_native_signo[sig], 530 &nbsa, &obsa, NULL, 0)) != 0) 531 return error; 532 533 *retval = (int)(long)obsa.sa_handler; 534 return 0; 535 } 536 537 int 538 linux32_sys_rt_sigpending(struct lwp *l, const struct linux32_sys_rt_sigpending_args *uap, register_t *retval) 539 { 540 /* { 541 syscallarg(linux32_sigsetp_t) set; 542 syscallarg(netbsd32_size_t) sigsetsize; 543 } */ 544 sigset_t bss; 545 linux32_sigset_t lss; 546 547 if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t)) 548 return EINVAL; 549 550 sigpending1(l, &bss); 551 native_to_linux32_sigset(&lss, &bss); 552 return copyout(&lss, SCARG_P32(uap, set), sizeof(lss)); 553 } 554 555 int 556 linux32_sys_siggetmask(struct lwp *l, const void *v, register_t *retval) 557 { 558 struct proc *p = l->l_proc; 559 sigset_t bss; 560 linux32_old_sigset_t lss; 561 int error; 562 563 mutex_enter(p->p_lock); 564 error = sigprocmask1(l, SIG_SETMASK, 0, &bss); 565 mutex_exit(p->p_lock); 566 if (error) 567 return error; 568 native_to_linux32_old_sigset(&lss, &bss); 569 *retval = lss; 570 return 0; 571 } 572 573 int 574 linux32_sys_sigsetmask(struct lwp *l, const struct linux32_sys_sigsetmask_args *uap, register_t *retval) 575 { 576 /* { 577 syscallarg(linux32_old_sigset_t) mask; 578 } */ 579 sigset_t nbss, obss; 580 linux32_old_sigset_t nlss, olss; 581 struct proc *p = l->l_proc; 582 int error; 583 584 nlss = SCARG(uap, mask); 585 linux32_old_to_native_sigset(&nbss, &nlss); 586 mutex_enter(p->p_lock); 587 error = sigprocmask1(l, SIG_SETMASK, &nbss, &obss); 588 mutex_exit(p->p_lock); 589 if (error) 590 return error; 591 native_to_linux32_old_sigset(&olss, &obss); 592 *retval = olss; 593 return 0; 594 } 595 596 int 597 linux32_sys_rt_queueinfo(struct lwp *l, const struct linux32_sys_rt_queueinfo_args *uap, register_t *retval) 598 { 599 /* 600 syscallarg(int) pid; 601 syscallarg(int) sig; 602 syscallarg(linux32_siginfop_t) uinfo; 603 */ 604 int error; 605 linux32_siginfo_t info; 606 607 error = copyin(SCARG_P32(uap, uinfo), &info, sizeof(info)); 608 if (error) 609 return error; 610 if (info.lsi_code >= 0) 611 return EPERM; 612 613 /* XXX To really implement this we need to */ 614 /* XXX keep a list of queued signals somewhere. */ 615 return linux32_sys_kill(l, (const void *)uap, retval); 616 } 617 618 int 619 native_to_linux32_si_code(int code) 620 { 621 int si_codes[] = { 622 LINUX32_SI_USER, LINUX32_SI_QUEUE, LINUX32_SI_TIMER, 623 LINUX32_SI_ASYNCIO, LINUX32_SI_MESGQ, LINUX32_SI_TKILL /* SI_LWP */ 624 }; 625 626 if (code <= 0 && -code < __arraycount(si_codes)) 627 return si_codes[-code]; 628 629 return code; 630 } 631 632 int 633 native_to_linux32_si_status(int code, int status) 634 { 635 int sts; 636 637 switch (code) { 638 case CLD_CONTINUED: 639 sts = LINUX_SIGCONT; 640 break; 641 case CLD_EXITED: 642 sts = WEXITSTATUS(status); 643 break; 644 case CLD_STOPPED: 645 case CLD_TRAPPED: 646 case CLD_DUMPED: 647 case CLD_KILLED: 648 default: 649 sts = native_to_linux32_signo[WTERMSIG(status)]; 650 break; 651 } 652 653 return sts; 654 } 655