• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "appspawn_hook.h"
16 #include "appspawn_msg.h"
17 #include "appspawn_manager.h"
18 #include "appspawn_utils.h"
19 #include "parameter.h"
20 #include "securec.h"
21 
22 // for stub
23 extern bool may_init_gwp_asan(bool forceInit);
24 
25 // ide-asan
26 #ifndef ASAN_DETECTOR
27 
28 #if defined(__aarch64__) || defined(__x86_64__)
29 #define ASAN_LD_PRELOAD "/system/lib64/libclang_rt.asan.so"
30 #else
31 #define ASAN_LD_PRELOAD "/system/lib/libclang_rt.asan.so"
32 #endif
33 #define HWASAN_LD_PRELOAD "/system/lib64/libclang_rt.hwasan.so"
34 #define TSAN_LD_PRELOAD "/system/lib64/libclang_rt.tsan.so"
35 
36 #define ASAN_OPTIONS "include=/system/etc/asan.options"
37 #define HWASAN_OPTIONS "include=/system/etc/asan.options"
38 #define TSAN_OPTIONS "include=/system/etc/tsan.options"
39 #define UBSAN_OPTIONS "print_stacktrace=1:print_module_map=2:log_exe_name=1"
40 
41 // 配置表数据
42 static const EnvConfig g_configTable[] = {
43     { APP_FLAGS_HWASAN_ENABLED, HWASAN_LD_PRELOAD, NULL, NULL, NULL, HWASAN_OPTIONS },
44     { APP_FLAGS_ASANENABLED, ASAN_LD_PRELOAD, ASAN_OPTIONS, NULL, NULL, NULL },
45     { APP_FLAGS_TSAN_ENABLED, TSAN_LD_PRELOAD, NULL, TSAN_OPTIONS, NULL, NULL },
46     { APP_FLAGS_UBSAN_ENABLED, NULL, NULL, NULL, UBSAN_OPTIONS, NULL },
47 };
48 
IsInRenderProcess(const AppSpawningCtx * property)49 static bool IsInRenderProcess(const AppSpawningCtx *property)
50 {
51     static bool isRender = false;
52     static bool firstIn = true;
53     if (!firstIn) {
54         return isRender;
55     }
56     firstIn = false;
57     char *processType = (char *)GetAppPropertyExt(property, MSG_EXT_NAME_PROCESS_TYPE, NULL);
58     if (processType == NULL) {
59         APPSPAWN_LOGE("GetAppPropertyExt ProcessType is null");
60         return false;
61     }
62     if (strcmp(processType, "render") == 0 || strcmp(processType, "gpu") == 0) {
63         isRender = true;
64     }
65     return isRender;
66 }
67 
SetAsanEnabledEnv(const AppSpawnMgr * content,const AppSpawningCtx * property)68 static int SetAsanEnabledEnv(const AppSpawnMgr *content, const AppSpawningCtx *property)
69 {
70     size_t configTableSize = sizeof(g_configTable) / sizeof(g_configTable[0]);
71     for (size_t i = 0; i < configTableSize; ++i) {
72         if (CheckAppMsgFlagsSet(property, g_configTable[i].flag) == 0) {
73             continue;
74         }
75         if (g_configTable[i].flag == APP_FLAGS_TSAN_ENABLED && IsInRenderProcess(property)) {
76             APPSPAWN_LOGI("SetAsanEnabledEnv skip tsan setting if render process");
77             continue;
78         }
79         if (g_configTable[i].ldPreload) {
80             setenv("LD_PRELOAD", g_configTable[i].ldPreload, 1);
81         }
82         if (g_configTable[i].asanOptions) {
83             setenv("ASAN_OPTIONS", g_configTable[i].asanOptions, 1);
84         } else {
85             unsetenv("ASAN_OPTIONS");
86         }
87         if (g_configTable[i].tsanOptions) {
88             setenv("TSAN_OPTIONS", g_configTable[i].tsanOptions, 1);
89         } else {
90             unsetenv("TSAN_OPTIONS");
91         }
92         if (g_configTable[i].ubsanOptions) {
93             setenv("UBSAN_OPTIONS", g_configTable[i].ubsanOptions, 1);
94         } else {
95             unsetenv("UBSAN_OPTIONS");
96         }
97         if (g_configTable[i].hwasanOptions) {
98             setenv("HWASAN_OPTIONS", g_configTable[i].hwasanOptions, 1);
99         } else {
100             unsetenv("HWASAN_OPTIONS");
101         }
102         APPSPAWN_LOGV("SetAsanEnabledEnv %{public}d,%{public}s,%{public}s,%{public}s,%{public}s,%{public}s",
103             g_configTable[i].flag, g_configTable[i].ldPreload, g_configTable[i].asanOptions,
104             g_configTable[i].tsanOptions, g_configTable[i].ubsanOptions, g_configTable[i].hwasanOptions);
105         return 0;
106     }
107     return -1;
108 }
109 #endif
110 
SetGwpAsanEnabled(const AppSpawnMgr * content,const AppSpawningCtx * property)111 static void SetGwpAsanEnabled(const AppSpawnMgr *content, const AppSpawningCtx *property)
112 {
113     int enforce = CheckAppMsgFlagsSet(property, APP_FLAGS_GWP_ENABLED_FORCE);
114     APPSPAWN_LOGV("SetGwpAsanEnabled with flags: %{public}d", enforce);
115     may_init_gwp_asan(enforce);
116 }
117 
118 #define WRAP_VALUE_MAX_LENGTH 96
CheckSupportColdStart(const char * bundleName)119 static int CheckSupportColdStart(const char *bundleName)
120 {
121     char wrapBundleNameKey[WRAP_VALUE_MAX_LENGTH] = {0};
122     char wrapBundleNameValue[WRAP_VALUE_MAX_LENGTH] = {0};
123 
124     int len = sprintf_s(wrapBundleNameKey, WRAP_VALUE_MAX_LENGTH, "wrap.%s", bundleName);
125     APPSPAWN_CHECK(len > 0 && (len < WRAP_VALUE_MAX_LENGTH), return -1, "Invalid to format wrapBundleNameKey");
126 
127     int ret = GetParameter(wrapBundleNameKey, "", wrapBundleNameValue, WRAP_VALUE_MAX_LENGTH);
128     APPSPAWN_CHECK_LOGV(ret > 0 && (!strcmp(wrapBundleNameValue, "asan_wrapper")), return -1,
129         "Not wrap %{public}s.", bundleName);
130     APPSPAWN_LOGI("Asan: GetParameter %{public}s the value is %{public}s.", wrapBundleNameKey, wrapBundleNameValue);
131     return 0;
132 }
133 
AsanSpawnGetSpawningFlag(AppSpawnMgr * content,AppSpawningCtx * property)134 static int AsanSpawnGetSpawningFlag(AppSpawnMgr *content, AppSpawningCtx *property)
135 {
136     APPSPAWN_LOGV("Prepare spawn app %{public}s", GetProcessName(property));
137 #ifdef ASAN_DETECTOR
138     if (CheckSupportColdStart(GetBundleName(property)) == 0) {
139         property->client.flags |= APP_COLD_START;
140         property->client.flags |= APP_ASAN_DETECTOR;
141         if (property->forkCtx.coldRunPath) {
142             free(property->forkCtx.coldRunPath);
143         }
144 #ifdef CJAPP_SPAWN
145         property->forkCtx.coldRunPath = strdup("/system/asan/bin/cjappspawn");
146 #elif NATIVE_SPAWN
147         property->forkCtx.coldRunPath = strdup("/system/asan/bin/nativespawn");
148 #elif NWEB_SPAWN
149         property->forkCtx.coldRunPath = strdup("/system/asan/bin/nwebspawn");
150 #elif HYBRID_SPAWN
151         property->forkCtx.coldRunPath = strdup("/system/asan/bin/hybridspawn");
152 #else
153         property->forkCtx.coldRunPath = strdup("/system/asan/bin/appspawn");
154 #endif
155         if (property->forkCtx.coldRunPath == NULL) {
156             APPSPAWN_LOGE("Failed to set asan exec path %{public}s", GetProcessName(property));
157         }
158     }
159 #endif
160     return 0;
161 }
162 
AsanSpawnInitSpawningEnv(AppSpawnMgr * content,AppSpawningCtx * property)163 static int AsanSpawnInitSpawningEnv(AppSpawnMgr *content, AppSpawningCtx *property)
164 {
165     if (GetAppSpawnMsgType(property) == MSG_SPAWN_NATIVE_PROCESS) {
166         return 0;
167     }
168 #ifndef ASAN_DETECTOR
169     if (CheckSupportColdStart(GetBundleName(property)) == 0) {
170         setenv("LD_PRELOAD", "/system/lib64/libclang_rt.hwasan.so", 1);
171         setenv("HWASAN_OPTIONS", "include=/system/etc/asan.options", 1);
172         property->client.flags |= APP_COLD_START;
173     }
174     int ret = SetAsanEnabledEnv(content, property);
175     if (ret == 0) {
176         APPSPAWN_LOGI("SetAsanEnabledEnv cold start app %{public}s", GetProcessName(property));
177         property->client.flags |= APP_COLD_START;
178     }
179 #endif
180     (void)SetGwpAsanEnabled(content, property);
181     return 0;
182 }
183 
MODULE_CONSTRUCTOR(void)184 MODULE_CONSTRUCTOR(void)
185 {
186     APPSPAWN_LOGV("Load asan module ...");
187     AddAppSpawnHook(STAGE_CHILD_PRE_COLDBOOT, HOOK_PRIO_COMMON, AsanSpawnInitSpawningEnv);
188     AddAppSpawnHook(STAGE_PARENT_PRE_FORK, HOOK_PRIO_COMMON, AsanSpawnGetSpawningFlag);
189 }
190