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 "app_spawn_stub.h"
17
18 #include <cerrno>
19 #include <csignal>
20 #include <cstdarg>
21 #include <cstdbool>
22 #include <cstdlib>
23 #include <ctime>
24 #include <fcntl.h>
25 #include <pthread.h>
26 #include <pwd.h>
27 #include <grp.h>
28
29 #include <linux/capability.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/un.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37
38 #include "access_token.h"
39 #include "hilog/log.h"
40 #include "securec.h"
41 #include "token_setproc.h"
42 #include "tokenid_kit.h"
43
44 #ifdef WITH_SELINUX
45 #include "hap_restorecon.h"
46 #endif
47 #ifdef WITH_SECCOMP
48 #include "seccomp_policy.h"
49 #include <sys/prctl.h>
50 #endif
51
52 static uint32_t g_preloadParamResult = 0;
53 static uint32_t g_preloadEtsParamResult = 0;
SetBoolParamResult(const char * key,bool flag)54 void SetBoolParamResult(const char *key, bool flag)
55 {
56 if (strcmp(key, "persist.appspawn.preload") == 0) {
57 flag ? (g_preloadParamResult = true) : (g_preloadParamResult = false);
58 }
59 if (strcmp(key, "persist.appspawn.preloadets") == 0) {
60 flag ? (g_preloadEtsParamResult = true) : (g_preloadEtsParamResult = false);
61 }
62 }
63
64 namespace OHOS {
65 namespace system {
GetIntParameter(const std::string & key,bool def,bool arg1=false,bool arg2=false)66 bool GetIntParameter(const std::string &key, bool def, bool arg1 = false, bool arg2 = false)
67 {
68 return def;
69 }
70
GetBoolParameter(const std::string & key,bool def)71 bool GetBoolParameter(const std::string &key, bool def)
72 {
73 if (strcmp(key.c_str(), "persist.appspawn.preload") == 0) {
74 return g_preloadParamResult ? true : false;
75 }
76 if (strcmp(key.c_str(), "persist.appspawn.preloadets") == 0) {
77 return g_preloadEtsParamResult ? true : false;
78 }
79 return def;
80 }
81 } // namespace system
82
83 namespace Security {
84 namespace AccessToken {
GetRenderTokenID(uint64_t tokenId)85 uint64_t TokenIdKit::GetRenderTokenID(uint64_t tokenId)
86 {
87 return tokenId;
88 }
89 } // namespace AccessToken
90 } // namespace Security
91 } // namespace OHOS
92
93 #ifdef WITH_SELINUX
HapContext()94 HapContext::HapContext() {}
~HapContext()95 HapContext::~HapContext() {}
HapDomainSetcontext(HapDomainInfo & hapDomainInfo)96 int HapContext::HapDomainSetcontext(HapDomainInfo &hapDomainInfo)
97 {
98 return 0;
99 }
100 #endif
101
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
ResetParamSecurityLabel()105 void ResetParamSecurityLabel() {}
106
SetSelfTokenID(uint64_t tokenId)107 int SetSelfTokenID(uint64_t tokenId)
108 {
109 return 0;
110 }
111
SetTraceDisabled(int disable)112 void SetTraceDisabled(int disable) {}
113
114 #ifdef WITH_SECCOMP
SetSeccompPolicyWithName(SeccompFilterType filter,const char * filterName)115 bool SetSeccompPolicyWithName(SeccompFilterType filter, const char *filterName)
116 {
117 static int result = 0;
118 result++;
119 return true; // (result % 3) == 0; // 3 is test data
120 }
121
IsEnableSeccomp(void)122 bool IsEnableSeccomp(void)
123 {
124 return true;
125 }
126 #endif
127
GetControlSocket(const char * name)128 int GetControlSocket(const char *name)
129 {
130 return -1;
131 }
132
133 static bool g_developerMode = true;
SetDeveloperMode(bool mode)134 void SetDeveloperMode(bool mode)
135 {
136 g_developerMode = mode;
137 }
138
GetParameter(const char * key,const char * def,char * value,uint32_t len)139 int GetParameter(const char *key, const char *def, char *value, uint32_t len)
140 {
141 static uint32_t count = 0;
142 count++;
143 if (strcmp(key, "startup.appspawn.cold.boot") == 0) {
144 return strcpy_s(value, len, "true") == 0 ? strlen("true") : -1;
145 }
146 if (strcmp(key, "persist.appspawn.reqMgr.timeout") == 0) {
147 const char *tmp = def;
148 if ((count % 3) == 0) { // 3 test
149 return -1;
150 } else if ((count % 3) == 1) { // 3 test
151 tmp = "a";
152 } else {
153 tmp = "5";
154 }
155 return strcpy_s(value, len, tmp) == 0 ? strlen(tmp) : -1;
156 }
157 if (strcmp(key, "const.security.developermode.state") == 0) {
158 return g_developerMode ? (strcpy_s(value, len, "true") == 0 ? strlen("true") : -1) : -1;
159 }
160 if (strcmp(key, "persist.nweb.sandbox.src_path") == 0) {
161 return strcpy_s(value, len, def) == 0 ? strlen(def) : -1;
162 }
163 if (strcmp(key, "test.variable.001") == 0) {
164 return strcpy_s(value, len, "test.variable.001") == 0 ? strlen("test.variable.001") : -1;
165 }
166 if (strcmp(key, "persist.arkwebcore.package_name") == 0) {
167 return strcpy_s(value, len, "com.ohos.arkwebcore") == 0 ? strlen("com.ohos.arkwebcore") : -1;
168 }
169 if (strcmp(key, "persist.arkwebcore.install_path") == 0) {
170 return strcpy_s(value, len, "/data/app/el1/bundle/public/com.ohos.nweb/ArkWWebCore.hap") == 0 ?
171 strlen("/data/app/el1/bundle/public/com.ohos.nweb/ArkWWebCore.hap") : -1;
172 }
173 return -1;
174 }
175
SetParameter(const char * key,const char * value)176 int SetParameter(const char *key, const char *value)
177 {
178 return 0;
179 }
180
InUpdaterMode(void)181 int InUpdaterMode(void)
182 {
183 return 0;
184 }
185
186
187 #ifdef __cplusplus
188 }
189 #endif
190