• 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 
SetAsanEnabledEnv(const AppSpawnMgr * content,const AppSpawningCtx * property)49 static int SetAsanEnabledEnv(const AppSpawnMgr *content, const AppSpawningCtx *property)
50 {
51     size_t configTableSize = sizeof(g_configTable) / sizeof(g_configTable[0]);
52     for (size_t i = 0; i < configTableSize; ++i) {
53         if (CheckAppMsgFlagsSet(property, g_configTable[i].flag)) {
54             if (g_configTable[i].ldPreload) {
55                 setenv("LD_PRELOAD", g_configTable[i].ldPreload, 1);
56             }
57             if (g_configTable[i].asanOptions) {
58                 setenv("ASAN_OPTIONS", g_configTable[i].asanOptions, 1);
59             } else {
60                 unsetenv("ASAN_OPTIONS");
61             }
62             if (g_configTable[i].tsanOptions) {
63                 setenv("TSAN_OPTIONS", g_configTable[i].tsanOptions, 1);
64             } else {
65                 unsetenv("TSAN_OPTIONS");
66             }
67             if (g_configTable[i].ubsanOptions) {
68                 setenv("UBSAN_OPTIONS", g_configTable[i].ubsanOptions, 1);
69             } else {
70                 unsetenv("UBSAN_OPTIONS");
71             }
72             if (g_configTable[i].hwasanOptions) {
73                 setenv("HWASAN_OPTIONS", g_configTable[i].hwasanOptions, 1);
74             } else {
75                 unsetenv("HWASAN_OPTIONS");
76             }
77             APPSPAWN_LOGV("SetAsanEnabledEnv %{public}d,%{public}s,%{public}s,%{public}s,%{public}s,%{public}s",
78                 g_configTable[i].flag, g_configTable[i].ldPreload, g_configTable[i].asanOptions,
79                 g_configTable[i].tsanOptions, g_configTable[i].ubsanOptions, g_configTable[i].hwasanOptions);
80             return 0;
81         }
82     }
83     return -1;
84 }
85 #endif
86 
SetGwpAsanEnabled(const AppSpawnMgr * content,const AppSpawningCtx * property)87 static void SetGwpAsanEnabled(const AppSpawnMgr *content, const AppSpawningCtx *property)
88 {
89     int enforce = CheckAppMsgFlagsSet(property, APP_FLAGS_GWP_ENABLED_FORCE);
90     APPSPAWN_LOGV("SetGwpAsanEnabled with flags: %{public}d", enforce);
91     may_init_gwp_asan(enforce);
92 }
93 
94 #ifdef ASAN_DETECTOR
95 #define WRAP_VALUE_MAX_LENGTH 96
CheckSupportColdStart(const char * bundleName)96 static int CheckSupportColdStart(const char *bundleName)
97 {
98     char wrapBundleNameKey[WRAP_VALUE_MAX_LENGTH] = {0};
99     char wrapBundleNameValue[WRAP_VALUE_MAX_LENGTH] = {0};
100 
101     int len = sprintf_s(wrapBundleNameKey, WRAP_VALUE_MAX_LENGTH, "wrap.%s", bundleName);
102     APPSPAWN_CHECK(len > 0 && (len < WRAP_VALUE_MAX_LENGTH), return -1, "Invalid to format wrapBundleNameKey");
103 
104     int ret = GetParameter(wrapBundleNameKey, "", wrapBundleNameValue, WRAP_VALUE_MAX_LENGTH);
105     APPSPAWN_CHECK(ret > 0 && (!strcmp(wrapBundleNameValue, "asan_wrapper")), return -1,
106         "Not wrap %{public}s.", bundleName);
107     APPSPAWN_LOGI("Asan: GetParameter %{public}s the value is %{public}s.", wrapBundleNameKey, wrapBundleNameValue);
108     return 0;
109 }
110 #endif
111 
AsanSpawnGetSpawningFlag(AppSpawnMgr * content,AppSpawningCtx * property)112 static int AsanSpawnGetSpawningFlag(AppSpawnMgr *content, AppSpawningCtx *property)
113 {
114     APPSPAWN_LOGV("Prepare spawn app %{public}s", GetProcessName(property));
115 #ifdef ASAN_DETECTOR
116     if (CheckSupportColdStart(GetBundleName(property)) == 0) {
117         property->client.flags |= APP_COLD_START;
118         property->client.flags |= APP_ASAN_DETECTOR;
119         if (property->forkCtx.coldRunPath) {
120             free(property->forkCtx.coldRunPath);
121         }
122 #ifndef CJAPP_SPAWN
123         property->forkCtx.coldRunPath = strdup("/system/asan/bin/appspawn");
124 #elif NATIVE_SPAWN
125         property->forkCtx.coldRunPath = strdup("/system/asan/bin/nativespawn");
126 #else
127         property->forkCtx.coldRunPath = strdup("/system/asan/bin/cjappspawn");
128 #endif
129         if (property->forkCtx.coldRunPath == NULL) {
130             APPSPAWN_LOGE("Failed to set asan exec path %{public}s", GetProcessName(property));
131         }
132     }
133 #endif
134     return 0;
135 }
136 
AsanSpawnInitSpawningEnv(AppSpawnMgr * content,AppSpawningCtx * property)137 static int AsanSpawnInitSpawningEnv(AppSpawnMgr *content, AppSpawningCtx *property)
138 {
139     if (GetAppSpawnMsgType(property) == MSG_SPAWN_NATIVE_PROCESS) {
140         return 0;
141     }
142 #ifndef ASAN_DETECTOR
143     int ret = SetAsanEnabledEnv(content, property);
144     if (ret == 0) {
145         APPSPAWN_LOGI("SetAsanEnabledEnv cold start app %{public}s", GetProcessName(property));
146         property->client.flags |= APP_COLD_START;
147     }
148 #endif
149     (void)SetGwpAsanEnabled(content, property);
150     return 0;
151 }
152 
MODULE_CONSTRUCTOR(void)153 MODULE_CONSTRUCTOR(void)
154 {
155     APPSPAWN_LOGV("Load asan module ...");
156     AddAppSpawnHook(STAGE_CHILD_PRE_COLDBOOT, HOOK_PRIO_COMMON, AsanSpawnInitSpawningEnv);
157     AddAppSpawnHook(STAGE_PARENT_PRE_FORK, HOOK_PRIO_COMMON, AsanSpawnGetSpawningFlag);
158 }
159