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             }
103         }
104     }
105     if (g_hookedSigprocmask == NULL) {
106         LOGE("hooked procmask is NULL?\n");
107         return -1;
108     }
109     return g_hookedSigprocmask(how, set, oldset);
110 }
111 
signal(int signum,sighandler_t handler)112 sighandler_t signal(int signum, sighandler_t handler)
113 {
114     if (IsPlatformHandleSignal(signum)) {
115         LOGI("%d register signal handler for signal(%d)\n", getpid(), signum);
116         LogBacktrace();
117     }
118 
119     if (g_hookedSignal == NULL) {
120         LOGE("hooked signal is NULL?\n");
121         return NULL;
122     }
123     return g_hookedSignal(signum, handler);
124 }
125 
IsSigactionAddr(uintptr_t sigactionAddr)126 static bool IsSigactionAddr(uintptr_t sigactionAddr)
127 {
128     bool ret = false;
129     char path[NAME_BUF_LEN] = {0};
130     if (snprintf_s(path, sizeof(path), sizeof(path) - 1, PROC_SELF_MAPS_PATH) <= 0) {
131         LOGW("Fail to print path.");
132         return false;
133     }
134 
135     FILE *fp = fopen(path, "r");
136     if (fp == NULL) {
137         LOGW("Fail to open maps info.");
138         return false;
139     }
140 
141     char mapInfo[MAPINFO_SIZE] = {0};
142     int pos = 0;
143     uint64_t begin = 0;
144     uint64_t end = 0;
145     uint64_t offset = 0;
146     char perms[5] = {0}; // 5:rwxp
147     while (fgets(mapInfo, sizeof(mapInfo), fp) != NULL) {
148         // f79d6000-f7a62000 r-xp 0004b000 b3:06 1605                               /system/lib/ld-musl-arm.so.1
149         if (sscanf_s(mapInfo, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %*x:%*x %*d%n", &begin, &end,
150             &perms, sizeof(perms), &offset, &pos) != 4) { // 4:scan size
151             LOGW("Fail to parse maps info.");
152             continue;
153         }
154 
155         if ((strstr(mapInfo, "r-xp") != NULL) && (strstr(mapInfo, "ld-musl") != NULL)) {
156             LOGI("begin: %" PRIu64 ", end: %" PRIu64 ", sigactionAddr: %" PRIuPTR  "", begin, end, sigactionAddr);
157             if ((sigactionAddr >= begin) && (sigactionAddr <= end)) {
158                 ret = true;
159                 break;
160             }
161         } else {
162             continue;
163         }
164     }
165     if (fclose(fp) != 0) {
166         LOGW("Fail to close maps info.");
167     }
168     return ret;
169 }
170 
sigaction(int sig,const struct sigaction * restrict act,struct sigaction * restrict oact)171 int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact)
172 {
173     if (g_hookedSigaction == NULL) {
174         LOGE("hooked sigaction is NULL?");
175         return -1;
176     }
177 
178     if (IsPlatformHandleSignal(sig) && (act == NULL || !IsSigactionAddr((uintptr_t)(act->sa_sigaction)))) {
179         LOGI("%d call sigaction and signo is %d\n", getpid(), sig);
180         LogBacktrace();
181     }
182 
183     return g_hookedSigaction(sig, act, oact);
184 }
185 
186 GEN_HOOK_FUNC(StartHookSigactionFunction, SigactionFunc, "sigaction", g_hookedSigaction)
187 GEN_HOOK_FUNC(StartHookSignalFunction, SignalFunc, "signal", g_hookedSignal)
188 GEN_HOOK_FUNC(StartHookSigprocmaskFunction, SigprocmaskFunc, "sigprocmask", g_hookedSigprocmask)
189 GEN_HOOK_FUNC(StartHookPthreadSigmaskFunction, PthreadSigmaskFunc, "pthread_sigmask", g_hookedPthreadSigmask)
190 
191 void StartHookFunc(void)
192 {
193     StartHookSigactionFunction();
194     StartHookSignalFunction();
195     StartHookSigprocmaskFunction();
196     StartHookPthreadSigmaskFunction();
197 }
198