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                 DFXLOGI("%{public}d:%{public}d pthread_sigmask signal(%{public}d)\n", getpid(), gettid(), i);
83                 LogBacktrace();
84             }
85         }
86     }
87 
88     if (g_hookedPthreadSigmask == NULL) {
89         DFXLOGE("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                 DFXLOGI("%{public}d:%{public}d sigprocmask signal(%{public}d)\n", getpid(), gettid(), i);
102             }
103         }
104     }
105     if (g_hookedSigprocmask == NULL) {
106         DFXLOGE("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         DFXLOGI("%{public}d register signal handler for signal(%{public}d)\n", getpid(), signum);
116         LogBacktrace();
117     }
118 
119     if (g_hookedSignal == NULL) {
120         DFXLOGE("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         DFXLOGW("Fail to print path.");
132         return false;
133     }
134 
135     FILE *fp = fopen(path, "r");
136     if (fp == NULL) {
137         DFXLOGW("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             DFXLOGW("Fail to parse maps info.");
152             continue;
153         }
154 
155         if ((strstr(mapInfo, "r-xp") != NULL) && (strstr(mapInfo, "ld-musl") != NULL)) {
156             DFXLOGI("begin: %{public}" PRIu64 ", end: %{public}" PRIu64 ", sigactionAddr: %{public}" PRIuPTR  "",
157                 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         DFXLOGW("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         DFXLOGE("hooked sigaction is NULL?");
176         return -1;
177     }
178 
179     if (IsPlatformHandleSignal(sig) && (act == NULL || !IsSigactionAddr((uintptr_t)(act->sa_sigaction)))) {
180         DFXLOGI("%{public}d call sigaction and signo is %{public}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