1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: lib-signal.c 1683 2007-01-17 13:46:38Z sam $ |
---|
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 */ |
---|
22 | #define _GNU_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 | #include <dlfcn.h> |
---|
31 | |
---|
32 | #include <string.h> |
---|
33 | #include <signal.h> |
---|
34 | |
---|
35 | #include "libzzuf.h" |
---|
36 | #include "lib-load.h" |
---|
37 | #include "debug.h" |
---|
38 | #include "fuzz.h" |
---|
39 | |
---|
40 | #if defined HAVE_SIGHANDLER_T |
---|
41 | # define SIG_T sighandler_t |
---|
42 | #elif defined HAVE_SIG_T |
---|
43 | # define SIG_T sig_t |
---|
44 | #else |
---|
45 | typedef void (*SIG_T) (int); |
---|
46 | #endif |
---|
47 | |
---|
48 | /* Library functions that we divert */ |
---|
49 | static SIG_T (*signal_orig) (int signum, SIG_T handler); |
---|
50 | static int (*sigaction_orig) (int signum, const struct sigaction *act, |
---|
51 | struct sigaction *oldact); |
---|
52 | /* Local functions */ |
---|
53 | static int isfatal(int signum); |
---|
54 | |
---|
55 | static int isfatal(int signum) |
---|
56 | { |
---|
57 | switch(signum) |
---|
58 | { |
---|
59 | case SIGABRT: |
---|
60 | case SIGFPE: |
---|
61 | case SIGILL: |
---|
62 | case SIGQUIT: |
---|
63 | case SIGSEGV: |
---|
64 | case SIGTRAP: |
---|
65 | #ifdef SIGSYS |
---|
66 | case SIGSYS: |
---|
67 | #endif |
---|
68 | #ifdef SIGEMT |
---|
69 | case SIGEMT: |
---|
70 | #endif |
---|
71 | #ifdef SIGBUS |
---|
72 | case SIGBUS: |
---|
73 | #endif |
---|
74 | #ifdef SIGXCPU |
---|
75 | case SIGXCPU: |
---|
76 | #endif |
---|
77 | #ifdef SIGXFSZ |
---|
78 | case SIGXFSZ: |
---|
79 | #endif |
---|
80 | return 1; |
---|
81 | default: |
---|
82 | return 0; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | SIG_T signal(int signum, SIG_T handler) |
---|
87 | { |
---|
88 | SIG_T ret; |
---|
89 | |
---|
90 | LOADSYM(signal); |
---|
91 | |
---|
92 | if(!_zz_signal) |
---|
93 | return signal_orig(signum, handler); |
---|
94 | |
---|
95 | ret = signal_orig(signum, isfatal(signum) ? SIG_DFL : handler); |
---|
96 | |
---|
97 | debug("signal(%i, %p) = %p", signum, handler, ret); |
---|
98 | |
---|
99 | return ret; |
---|
100 | } |
---|
101 | |
---|
102 | int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) |
---|
103 | { |
---|
104 | int ret; |
---|
105 | |
---|
106 | LOADSYM(sigaction); |
---|
107 | |
---|
108 | if(!_zz_signal) |
---|
109 | return sigaction_orig(signum, act, oldact); |
---|
110 | |
---|
111 | if(act && isfatal(signum)) |
---|
112 | { |
---|
113 | struct sigaction newact; |
---|
114 | memcpy(&newact, act, sizeof(struct sigaction)); |
---|
115 | newact.sa_handler = SIG_DFL; |
---|
116 | ret = sigaction_orig(signum, &newact, oldact); |
---|
117 | } |
---|
118 | else |
---|
119 | ret = sigaction_orig(signum, act, oldact); |
---|
120 | |
---|
121 | debug("sigaction(%i, %p, %p) = %i", signum, act, oldact, ret); |
---|
122 | |
---|
123 | return ret; |
---|
124 | } |
---|
125 | |
---|