| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * load-signal.c: loaded signal functions |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | |
|---|
| 21 | /* Needed for sighandler_t on glibc systems */ |
|---|
| 22 | #define _GNU_SOURCE |
|---|
| 23 | /* Needed for struct sigaction on HP-UX */ |
|---|
| 24 | #define _INCLUDE_POSIX_SOURCE |
|---|
| 25 | |
|---|
| 26 | #if defined HAVE_STDINT_H |
|---|
| 27 | # include <stdint.h> |
|---|
| 28 | #elif defined HAVE_INTTYPES_H |
|---|
| 29 | # include <inttypes.h> |
|---|
| 30 | #endif |
|---|
| 31 | #include <stdlib.h> |
|---|
| 32 | |
|---|
| 33 | #include <string.h> |
|---|
| 34 | #include <signal.h> |
|---|
| 35 | |
|---|
| 36 | #include "libzzuf.h" |
|---|
| 37 | #include "lib-load.h" |
|---|
| 38 | #include "debug.h" |
|---|
| 39 | #include "fuzz.h" |
|---|
| 40 | |
|---|
| 41 | #if defined HAVE_SIGHANDLER_T |
|---|
| 42 | # define SIG_T sighandler_t |
|---|
| 43 | #elif defined HAVE_SIG_T |
|---|
| 44 | # define SIG_T sig_t |
|---|
| 45 | #else |
|---|
| 46 | typedef void (*SIG_T) (int); |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | /* Library functions that we divert */ |
|---|
| 50 | static SIG_T (*ORIG(signal)) (int signum, SIG_T handler); |
|---|
| 51 | #if defined HAVE_SIGACTION |
|---|
| 52 | static int (*ORIG(sigaction)) (int signum, const struct sigaction *act, |
|---|
| 53 | struct sigaction *oldact); |
|---|
| 54 | #endif |
|---|
| 55 | /* Local functions */ |
|---|
| 56 | static int isfatal(int signum); |
|---|
| 57 | |
|---|
| 58 | static int isfatal(int signum) |
|---|
| 59 | { |
|---|
| 60 | switch(signum) |
|---|
| 61 | { |
|---|
| 62 | case SIGABRT: |
|---|
| 63 | case SIGFPE: |
|---|
| 64 | case SIGILL: |
|---|
| 65 | #if defined SIGQUIT |
|---|
| 66 | case SIGQUIT: |
|---|
| 67 | #endif |
|---|
| 68 | case SIGSEGV: |
|---|
| 69 | #if defined SIGTRAP |
|---|
| 70 | case SIGTRAP: |
|---|
| 71 | #endif |
|---|
| 72 | #if defined SIGSYS |
|---|
| 73 | case SIGSYS: |
|---|
| 74 | #endif |
|---|
| 75 | #if defined SIGEMT |
|---|
| 76 | case SIGEMT: |
|---|
| 77 | #endif |
|---|
| 78 | #if defined SIGBUS |
|---|
| 79 | case SIGBUS: |
|---|
| 80 | #endif |
|---|
| 81 | #if defined SIGXCPU |
|---|
| 82 | case SIGXCPU: |
|---|
| 83 | #endif |
|---|
| 84 | #if defined SIGXFSZ |
|---|
| 85 | case SIGXFSZ: |
|---|
| 86 | #endif |
|---|
| 87 | return 1; |
|---|
| 88 | default: |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | SIG_T NEW(signal)(int signum, SIG_T handler) |
|---|
| 94 | { |
|---|
| 95 | SIG_T ret; |
|---|
| 96 | |
|---|
| 97 | LOADSYM(signal); |
|---|
| 98 | |
|---|
| 99 | if(!_zz_signal) |
|---|
| 100 | return ORIG(signal)(signum, handler); |
|---|
| 101 | |
|---|
| 102 | ret = ORIG(signal)(signum, isfatal(signum) ? SIG_DFL : handler); |
|---|
| 103 | |
|---|
| 104 | debug("%s(%i, %p) = %p", __func__, signum, handler, ret); |
|---|
| 105 | |
|---|
| 106 | return ret; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | #if defined HAVE_SIGACTION |
|---|
| 110 | int NEW(sigaction)(int signum, const struct sigaction *act, |
|---|
| 111 | struct sigaction *oldact) |
|---|
| 112 | { |
|---|
| 113 | int ret; |
|---|
| 114 | |
|---|
| 115 | LOADSYM(sigaction); |
|---|
| 116 | |
|---|
| 117 | if(!_zz_signal) |
|---|
| 118 | return ORIG(sigaction)(signum, act, oldact); |
|---|
| 119 | |
|---|
| 120 | if(act && isfatal(signum)) |
|---|
| 121 | { |
|---|
| 122 | struct sigaction newact; |
|---|
| 123 | memcpy(&newact, act, sizeof(struct sigaction)); |
|---|
| 124 | newact.sa_handler = SIG_DFL; |
|---|
| 125 | ret = ORIG(sigaction)(signum, &newact, oldact); |
|---|
| 126 | } |
|---|
| 127 | else |
|---|
| 128 | ret = ORIG(sigaction)(signum, act, oldact); |
|---|
| 129 | |
|---|
| 130 | debug("%s(%i, %p, %p) = %i", __func__, signum, act, oldact, ret); |
|---|
| 131 | |
|---|
| 132 | return ret; |
|---|
| 133 | } |
|---|
| 134 | #endif |
|---|
| 135 | |
|---|