• 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 
16 #include "appspawn_adapter.h"
17 
18 #include "access_token.h"
19 #include "appspawn_hook.h"
20 #include "appspawn_manager.h"
21 #include "appspawn_utils.h"
22 #include "cJSON.h"
23 #include "token_setproc.h"
24 #include "tokenid_kit.h"
25 #include "securec.h"
26 
27 #ifdef WITH_SELINUX
28 #include "hap_restorecon.h"
29 #include "selinux/selinux.h"
30 #endif
31 #ifdef WITH_SECCOMP
32 #include "seccomp_policy.h"
33 #include <sys/prctl.h>
34 #ifdef SECCOMP_PRIVILEGE
35 #include <dlfcn.h>
36 #define GET_ALL_PROCESSES "ohos.permission.GET_ALL_PROCESSES"
37 #define GET_PERMISSION_INDEX "GetPermissionIndex"
38 using GetPermissionFunc = int32_t (*)(void *, const char *);
39 #endif
40 #endif
41 #define MSG_EXT_NAME_PROCESS_TYPE "ProcessType"
42 #define NWEBSPAWN_SERVER_NAME "nwebspawn"
43 #define MAX_USERID_LEN  32
44 using namespace OHOS::Security::AccessToken;
45 
SetAppAccessToken(const AppSpawnMgr * content,const AppSpawningCtx * property)46 int SetAppAccessToken(const AppSpawnMgr *content, const AppSpawningCtx *property)
47 {
48     int32_t ret = 0;
49     uint64_t tokenId = 0;
50     AppSpawnMsgAccessToken *tokenInfo =
51         reinterpret_cast<AppSpawnMsgAccessToken *>(GetAppProperty(property, TLV_ACCESS_TOKEN_INFO));
52     APPSPAWN_CHECK(tokenInfo != NULL, return APPSPAWN_MSG_INVALID,
53         "No access token in msg %{public}s", GetProcessName(property));
54 
55     if (IsNWebSpawnMode(content) || IsIsolatedNativeSpawnMode(content, property)) {
56         TokenIdKit tokenIdKit;
57         tokenId = tokenIdKit.GetRenderTokenID(tokenInfo->accessTokenIdEx);
58     } else {
59         tokenId = tokenInfo->accessTokenIdEx;
60     }
61     ret = SetSelfTokenID(tokenId);
62     APPSPAWN_CHECK(ret == 0, return APPSPAWN_ACCESS_TOKEN_INVALID,
63         "set access token id failed, ret: %{public}d %{public}s", ret, GetProcessName(property));
64     APPSPAWN_LOGV("SetAppAccessToken success for %{public}s", GetProcessName(property));
65     return 0;
66 }
67 
SetSelinuxConNweb(const AppSpawnMgr * content,const AppSpawningCtx * property)68 int SetSelinuxConNweb(const AppSpawnMgr *content, const AppSpawningCtx *property)
69 {
70 #if defined(WITH_SELINUX) && !defined(APPSPAWN_TEST)
71     uint32_t len = 0;
72     std::string processType =
73         reinterpret_cast<char *>(GetAppPropertyExt(property, MSG_EXT_NAME_PROCESS_TYPE, &len));
74     int32_t ret;
75     if (processType == "render") {
76         ret = setcon("u:r:isolated_render:s0");
77     } else {
78         ret = setcon("u:r:isolated_gpu:s0");
79     }
80     APPSPAWN_CHECK_ONLY_LOG(ret == 0, "Setcon failed, errno: %{public}d", errno);
81 #endif
82     return 0;
83 }
84 
85 #ifdef WITH_SELINUX
SetHapDomainInfo(HapDomainInfo * hapDomainInfo,const AppSpawningCtx * property,AppSpawnMsgDomainInfo * msgDomainInfo,AppDacInfo * appInfo)86 void SetHapDomainInfo(HapDomainInfo *hapDomainInfo, const AppSpawningCtx *property,
87     AppSpawnMsgDomainInfo *msgDomainInfo, AppDacInfo *appInfo)
88 {
89     hapDomainInfo->apl = msgDomainInfo->apl;
90     hapDomainInfo->packageName = GetBundleName(property);
91     hapDomainInfo->hapFlags = msgDomainInfo->hapFlags;
92     // The value of 0 is invalid. Its purpose is to initialize.
93     hapDomainInfo->uid = appInfo == nullptr ? 0 : appInfo->uid;
94     if (CheckAppMsgFlagsSet(property, APP_FLAGS_DEBUGGABLE)) {
95         hapDomainInfo->hapFlags |= SELINUX_HAP_DEBUGGABLE;
96     }
97     if (CheckAppMsgFlagsSet(property, APP_FLAGS_DLP_MANAGER)) {
98         hapDomainInfo->hapFlags |= SELINUX_HAP_DLP;
99     }
100     if (CheckAppMsgFlagsSet(property, APP_FLAGS_ISOLATED_SANDBOX)) {
101         hapDomainInfo->hapFlags |= SELINUX_HAP_INPUT_ISOLATE;
102     }
103 #ifdef CUSTOM_SANDBOX
104     if (CheckAppMsgFlagsSet(property, APP_FLAGS_CUSTOM_SANDBOX)) {
105         hapDomainInfo->hapFlags |= SELINUX_HAP_CUSTOM_SANDBOX;
106     }
107 #endif
108     if (CheckAppMsgFlagsSet(property, APP_FLAGS_ISOLATED_SELINUX_LABEL)) {
109         uint32_t len = 0;
110         std::string extensionType =
111             reinterpret_cast<char *>(GetAppPropertyExt(property, MSG_EXT_NAME_EXTENSION_TYPE, &len));
112         hapDomainInfo->extensionType = extensionType;
113     }
114 }
115 #endif
116 
SetSelinuxCon(const AppSpawnMgr * content,const AppSpawningCtx * property)117 int SetSelinuxCon(const AppSpawnMgr *content, const AppSpawningCtx *property)
118 {
119 #ifdef WITH_SELINUX
120     APPSPAWN_LOGV("SetSelinuxCon IsDeveloperModeOn %{public}d", IsDeveloperModeOn(property));
121     if (GetAppSpawnMsgType(property) == MSG_SPAWN_NATIVE_PROCESS) {
122         if (!IsDeveloperModeOn(property)) {
123             APPSPAWN_LOGE("Denied Launching a native process: not in developer mode");
124             return APPSPAWN_NATIVE_NOT_SUPPORT;
125         }
126         return 0;
127     }
128     if (IsNWebSpawnMode(content)) {
129 #ifndef APPSPAWN_TEST
130         return SetSelinuxConNweb(content, property);
131 #else
132         return 0;
133 #endif
134     } else if (IsIsolatedNativeSpawnMode(content, property)) {
135         return setcon("u:r:isolated_render:s0");
136     }
137     AppSpawnMsgDomainInfo *msgDomainInfo =
138         reinterpret_cast<AppSpawnMsgDomainInfo *>(GetAppProperty(property, TLV_DOMAIN_INFO));
139     APPSPAWN_CHECK(msgDomainInfo != NULL, return APPSPAWN_TLV_NONE,
140         "No domain info in req form %{public}s", GetProcessName(property));
141     AppDacInfo *appInfo = reinterpret_cast<AppDacInfo *>(GetAppProperty(property, TLV_DAC_INFO));
142     HapContext hapContext;
143     HapDomainInfo hapDomainInfo;
144     SetHapDomainInfo(&hapDomainInfo, property, msgDomainInfo, appInfo);
145     int32_t ret = hapContext.HapDomainSetcontext(hapDomainInfo);
146     APPSPAWN_CHECK(ret == 0, return APPSPAWN_ACCESS_TOKEN_INVALID,
147         "Set domain context failed, ret: %{public}d %{public}s", ret, GetProcessName(property));
148     APPSPAWN_LOGV("SetSelinuxCon success for %{public}s", GetProcessName(property));
149 #endif
150     return 0;
151 }
152 
SetUidGidFilter(const AppSpawnMgr * content)153 int SetUidGidFilter(const AppSpawnMgr *content)
154 {
155 #ifdef WITH_SECCOMP
156     bool ret = false;
157     if (IsNWebSpawnMode(content)) {
158         if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
159             APPSPAWN_LOGE("Failed to set no new privs");
160         }
161         ret = SetSeccompPolicyWithName(INDIVIDUAL, NWEBSPAWN_NAME);
162     } else {
163 #ifdef SECCOMP_PRIVILEGE
164         return 0;
165 #endif
166         ret = SetSeccompPolicyWithName(INDIVIDUAL, APPSPAWN_NAME);
167     }
168     if (!ret) {
169         APPSPAWN_LOGE("Failed to set APPSPAWN seccomp filter and exit");
170         _exit(0x7f);
171     }
172     APPSPAWN_LOGV("SetUidGidFilter success");
173 #endif
174     return 0;
175 }
176 
SetSeccompFilter(const AppSpawnMgr * content,const AppSpawningCtx * property)177 int SetSeccompFilter(const AppSpawnMgr *content, const AppSpawningCtx *property)
178 {
179 #ifdef WITH_SECCOMP
180     APPSPAWN_CHECK(property != nullptr, return 0, "property is NULL");
181     const char *appName = APP_NAME;
182     SeccompFilterType type = APP;
183 
184     if (IsNWebSpawnMode(content)) {
185         uint32_t len = 0;
186         std::string processType =
187             reinterpret_cast<char *>(GetAppPropertyExt(property, MSG_EXT_NAME_PROCESS_TYPE, &len));
188         if (processType == "render") {
189             return 0;
190         }
191     }
192 
193 #ifdef SECCOMP_PRIVILEGE
194     if (IsDeveloperModeOpen()) {
195         // Enable high permission seccomp policy for hishell in developer mode.
196         if (CheckAppMsgFlagsSet(property, APP_FLAGS_GET_ALL_PROCESSES) != 0) {
197             appName = APP_PRIVILEGE;
198         }
199     }
200 #endif
201 
202 #ifdef CUSTOM_SANDBOX
203     // Set seccomp policy for custom process.
204     if (CheckAppMsgFlagsSet(property, APP_FLAGS_CUSTOM_SANDBOX) != 0) {
205         appName = APP_CUSTOM;
206     }
207 #endif
208 
209     // Set seccomp policy for input method security mode.
210     if (CheckAppMsgFlagsSet(property, APP_FLAGS_ISOLATED_SANDBOX) != 0) {
211         appName = IMF_EXTENTOIN_NAME;
212     }
213 
214     // Set seccomp policy for atomic service process.
215     if (CheckAppMsgFlagsSet(property, APP_FLAGS_ATOMIC_SERVICE) != 0) {
216         appName = APP_ATOMIC;
217     }
218 
219     // Set seccomp policy for processes that have ohos.permission.ALLOW_IOURING.
220     if (CheckAppMsgFlagsSet(property, APP_FLAGS_ALLOW_IOURING) != 0) {
221         appName = APP_ALLOW_IOURING;
222     }
223 
224     if (!SetSeccompPolicyWithName(type, appName)) {
225         APPSPAWN_LOGE("Failed to set %{public}s seccomp filter and exit %{public}d", appName, errno);
226         return -EINVAL;
227     }
228     APPSPAWN_LOGV("SetSeccompPolicyWithName success for %{public}s", appName);
229 #endif
230     return 0;
231 }
232 
SetInternetPermission(const AppSpawningCtx * property)233 int SetInternetPermission(const AppSpawningCtx *property)
234 {
235     AppSpawnMsgInternetInfo *info =
236         reinterpret_cast<AppSpawnMsgInternetInfo *>(GetAppProperty(property, TLV_INTERNET_INFO));
237     APPSPAWN_CHECK(info != NULL, return 0,
238         "No tlv internet permission info in req form %{public}s", GetProcessName(property));
239     APPSPAWN_LOGV("Set internet permission %{public}d %{public}d", info->setAllowInternet, info->allowInternet);
240     if (info->setAllowInternet == 1 && info->allowInternet == 0) {
241 #ifndef APPSPAWN_ALLOW_INTERNET_PERMISSION
242         DisallowInternet();
243 #endif
244     }
245     return 0;
246 }
247 
InitAppCommonEnv(const AppSpawningCtx * property)248 static void InitAppCommonEnv(const AppSpawningCtx *property)
249 {
250     AppDacInfo *appInfo = reinterpret_cast<AppDacInfo *>(GetAppProperty(property, TLV_DAC_INFO));
251     if (appInfo == NULL) {
252         return;
253     }
254     const uint32_t userId = appInfo->uid / UID_BASE;
255     char user[MAX_USERID_LEN] = {0};
256     int len = sprintf_s(user, MAX_USERID_LEN, "%u", userId);
257     APPSPAWN_CHECK(len > 0, return, "Failed to format userid: %{public}u", userId);
258     int ret = setenv("USER", user, 1);
259     APPSPAWN_CHECK(ret == 0, return, "setenv failed, userid:%{public}u, errno: %{public}d", userId, errno);
260 }
261 
SetEnvInfo(const AppSpawnMgr * content,const AppSpawningCtx * property)262 int32_t SetEnvInfo(const AppSpawnMgr *content, const AppSpawningCtx *property)
263 {
264     InitAppCommonEnv(property);
265 
266     uint32_t size = 0;
267     char *envStr = reinterpret_cast<char *>(GetAppPropertyExt(property, "AppEnv", &size));
268     if (size == 0 || envStr == NULL) {
269         return 0;
270     }
271     int ret = 0;
272     cJSON *root = cJSON_Parse(envStr);
273     APPSPAWN_CHECK(root != nullptr, return -1, "SetEnvInfo: json parse failed %{public}s", envStr);
274     cJSON *config = nullptr;
275     cJSON_ArrayForEach(config, root) {
276         const char *name = config->string;
277         const char *value = cJSON_GetStringValue(config);
278         APPSPAWN_LOGV("SetEnvInfo name: %{public}s value: %{public}s", name, value);
279         ret = setenv(name, value, 1);
280         APPSPAWN_CHECK(ret == 0, break, "setenv failed, errno: %{public}d", errno);
281     }
282     cJSON_Delete(root);
283     APPSPAWN_LOGV("SetEnvInfo success");
284     return ret;
285 }
286