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