1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dfx_signal_hook.h"
17
18 #include <dlfcn.h>
19 #include <securec.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include "dfx_define.h"
24 #include "dfx_log.h"
25 #include "pthread.h"
26
27 #include "dfx_hook_utils.h"
28
29 #ifdef LOG_DOMAIN
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002D11
32 #endif
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #define LOG_TAG "DfxSigHook"
37 #endif
38
39 #ifndef SIGDUMP
40 #define SIGDUMP 35
41 #endif
42
43 #define MIN_FRAME 4
44 #define MAX_FRAME 64
45 #define BUF_SZ 512
46 #define MAPINFO_SIZE 256
47 #define MAX_SIGNO 63
48
InitHook(void)49 void __attribute__((constructor)) InitHook(void)
50 {
51 StartHookFunc();
52 }
53
54 typedef int (*SigactionFunc)(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact);
55 typedef int (*SigprocmaskFunc)(int how, const sigset_t *restrict set, sigset_t *restrict oldset);
56 typedef int (*PthreadSigmaskFunc)(int how, const sigset_t *restrict set, sigset_t *restrict oldset);
57 typedef sighandler_t (*SignalFunc)(int signum, sighandler_t handler);
58 static SigactionFunc g_hookedSigaction = NULL;
59 static SigprocmaskFunc g_hookedSigprocmask = NULL;
60 static SignalFunc g_hookedSignal = NULL;
61 static PthreadSigmaskFunc g_hookedPthreadSigmask = NULL;
62
IsPlatformHandleSignal(int sig)63 static bool IsPlatformHandleSignal(int sig)
64 {
65 int platformSignals[] = {
66 SIGABRT, SIGBUS, SIGILL, SIGSEGV, SIGDUMP
67 };
68 for (size_t i = 0; i < sizeof(platformSignals) / sizeof(platformSignals[0]); i++) {
69 if (platformSignals[i] == sig) {
70 return true;
71 }
72 }
73 return false;
74 }
75
pthread_sigmask(int how,const sigset_t * restrict set,sigset_t * restrict oldset)76 int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict oldset)
77 {
78 if (set != NULL) {
79 for (int i = 1; i < MAX_SIGNO; i++) {
80 if (sigismember(set, i) && (IsPlatformHandleSignal(i)) &&
81 ((how == SIG_BLOCK) || (how == SIG_SETMASK))) {
82 LOGI("%d:%d pthread_sigmask signal(%d)\n", getpid(), gettid(), i);
83 LogBacktrace();
84 }
85 }
86 }
87
88 if (g_hookedPthreadSigmask == NULL) {
89 LOGE("hooked procmask is NULL?\n");
90 return -1;
91 }
92 return g_hookedPthreadSigmask(how, set, oldset);
93 }
94
sigprocmask(int how,const sigset_t * restrict set,sigset_t * restrict oldset)95 int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oldset)
96 {
97 if (set != NULL) {
98 for (int i = 1; i < MAX_SIGNO; i++) {
99 if (sigismember(set, i) && (IsPlatformHandleSignal(i)) &&
100 ((how == SIG_BLOCK) || (how == SIG_SETMASK))) {
101 LOGI("%d:%d sigprocmask signal(%d)\n", getpid(), gettid(), i);
102 LogBacktrace();
103 }
104 }
105 }
106 if (g_hookedSigprocmask == NULL) {
107 LOGE("hooked procmask is NULL?\n");
108 return -1;
109 }
110 return g_hookedSigprocmask(how, set, oldset);
111 }
112
signal(int signum,sighandler_t handler)113 sighandler_t signal(int signum, sighandler_t handler)
114 {
115 if (IsPlatformHandleSignal(signum)) {
116 LOGI("%d register signal handler for signal(%d)\n", getpid(), signum);
117 LogBacktrace();
118 }
119
120 if (g_hookedSignal == NULL) {
121 LOGE("hooked signal is NULL?\n");
122 return NULL;
123 }
124 return g_hookedSignal(signum, handler);
125 }
126
IsSigactionAddr(uintptr_t sigactionAddr)127 static bool IsSigactionAddr(uintptr_t sigactionAddr)
128 {
129 bool ret = false;
130 char path[NAME_BUF_LEN] = {0};
131 if (snprintf_s(path, sizeof(path), sizeof(path) - 1, PROC_SELF_MAPS_PATH) <= 0) {
132 LOGW("Fail to print path.");
133 return false;
134 }
135
136 FILE *fp = fopen(path, "r");
137 if (fp == NULL) {
138 LOGW("Fail to open maps info.");
139 return false;
140 }
141
142 char mapInfo[MAPINFO_SIZE] = {0};
143 int pos = 0;
144 uint64_t begin = 0;
145 uint64_t end = 0;
146 uint64_t offset = 0;
147 char perms[5] = {0}; // 5:rwxp
148 while (fgets(mapInfo, sizeof(mapInfo), fp) != NULL) {
149 // f79d6000-f7a62000 r-xp 0004b000 b3:06 1605 /system/lib/ld-musl-arm.so.1
150 if (sscanf_s(mapInfo, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %*x:%*x %*d%n", &begin, &end,
151 &perms, sizeof(perms), &offset, &pos) != 4) { // 4:scan size
152 LOGW("Fail to parse maps info.");
153 continue;
154 }
155
156 if ((strstr(mapInfo, "r-xp") != NULL) && (strstr(mapInfo, "ld-musl") != NULL)) {
157 LOGI("begin: %" PRIu64 ", end: %" PRIu64 ", sigactionAddr: %" PRIuPTR "", begin, end, sigactionAddr);
158 if ((sigactionAddr >= begin) && (sigactionAddr <= end)) {
159 ret = true;
160 break;
161 }
162 } else {
163 continue;
164 }
165 }
166 if (fclose(fp) != 0) {
167 LOGW("Fail to close maps info.");
168 }
169 return ret;
170 }
171
sigaction(int sig,const struct sigaction * restrict act,struct sigaction * restrict oact)172 int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact)
173 {
174 if (g_hookedSigaction == NULL) {
175 LOGE("hooked sigaction is NULL?");
176 return -1;
177 }
178
179 if (IsPlatformHandleSignal(sig) && (act == NULL || !IsSigactionAddr((uintptr_t)(act->sa_sigaction)))) {
180 LOGI("%d call sigaction and signo is %d\n", getpid(), sig);
181 LogBacktrace();
182 }
183
184 return g_hookedSigaction(sig, act, oact);
185 }
186
187 GEN_HOOK_FUNC(StartHookSigactionFunction, SigactionFunc, "sigaction", g_hookedSigaction)
188 GEN_HOOK_FUNC(StartHookSignalFunction, SignalFunc, "signal", g_hookedSignal)
189 GEN_HOOK_FUNC(StartHookSigprocmaskFunction, SigprocmaskFunc, "sigprocmask", g_hookedSigprocmask)
190 GEN_HOOK_FUNC(StartHookPthreadSigmaskFunction, PthreadSigmaskFunc, "pthread_sigmask", g_hookedPthreadSigmask)
191
StartHookFunc(void)192 void StartHookFunc(void)
193 {
194 StartHookSigactionFunction();
195 StartHookSignalFunction();
196 StartHookSigprocmaskFunction();
197 StartHookPthreadSigmaskFunction();
198 }
199