• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 #define _GNU_SOURCE
16 #include "appspawn_process.h"
17 #include <errno.h>
18 #include <sys/prctl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 
22 #ifdef OHOS_DEBUG
23 #include <time.h>
24 #endif // OHOS_DEBUG
25 
26 #include <unistd.h>
27 #include "ability_main.h"
28 #include "appspawn_adapter.h"
29 #include "log.h"
30 #include "securec.h"
31 
32 #ifdef __LINUX__
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 
36 static const rlim_t DEFAULT_RLIMIT = 40;
37 #endif // __LINUX__
38 
39 #define DEFAULT_UMASK 022
40 #define CAP_NUM 2
41 #define ENV_TITLE "LD_LIBRARY_PATH="
42 #define UPPER_BOUND_GID   999
43 #define LOWER_BOUND_GID   100
44 #define GRP_NUM 2
45 #define DEVMGR_GRP 99
46 
SetCapability(unsigned int capsCnt,const unsigned int * caps)47 static int SetCapability(unsigned int capsCnt, const unsigned int* caps)
48 {
49     struct __user_cap_header_struct capHeader;
50     capHeader.version = _LINUX_CAPABILITY_VERSION_3;
51     capHeader.pid = 0;
52 
53     // common user, clear all caps
54     struct __user_cap_data_struct capData[CAP_NUM] = {0};
55     for (unsigned int i = 0; i < capsCnt; ++i) {
56         capData[CAP_TO_INDEX(caps[i])].effective |= CAP_TO_MASK(caps[i]);
57         capData[CAP_TO_INDEX(caps[i])].permitted |= CAP_TO_MASK(caps[i]);
58         capData[CAP_TO_INDEX(caps[i])].inheritable |= CAP_TO_MASK(caps[i]);
59     }
60 
61     if (capset(&capHeader, capData) != 0) {
62         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] capset failed, err: %{public}d.", errno);
63         return -1;
64     }
65     for (unsigned int i = 0; i < capsCnt; ++i) {
66         if (SetAmbientCapability(caps[i]) != 0) {
67             HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] SetAmbientCapability failed, err: %{public}d.", errno);
68             return -1;
69         }
70     }
71     return 0;
72 }
73 
SetPerms(uid_t uID,gid_t gID,unsigned int capsCnt,const unsigned int * caps)74 static int SetPerms(uid_t uID, gid_t gID, unsigned int capsCnt, const unsigned int* caps)
75 {
76     gid_t groups[GRP_NUM];
77 
78     if (KeepCapability() != 0) {
79         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] KeepCapability failed, uID %{public}u, err: %{public}d.", \
80             uID, errno);
81         return -1;
82     }
83 
84     if (setgid(gID) != 0) {
85         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setgid failed, gID %{public}u, err: %{public}d.", \
86             gID, errno);
87         return -1;
88     }
89 
90     if (setuid(uID) != 0) {
91         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setuid failed, uID %{public}u, err: %{public}d.", \
92             uID, errno);
93         return -1;
94     }
95 
96     // add device groups for system app
97     if (gID >= LOWER_BOUND_GID && gID <= UPPER_BOUND_GID) {
98         groups[0] = gID;
99         groups[1] = DEVMGR_GRP;
100         if (setgroups(GRP_NUM, groups)) {
101             HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setgroups failed, uID %{public}u, err: %{public}d.", \
102                 uID, errno);
103             return -1;
104         }
105     }
106 
107     // umask call always succeeds and return the previous mask value which is not needed here
108     (void)umask(DEFAULT_UMASK);
109 
110     // set rlimit
111 #ifdef __LINUX__
112     struct rlimit rlim = {0};
113     rlim.rlim_cur = DEFAULT_RLIMIT;
114     rlim.rlim_max = DEFAULT_RLIMIT;
115     if (setrlimit(RLIMIT_NICE, &rlim) != 0) {
116         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setrlimit failed, err: %{public}d.", errno);
117         return -1;
118     }
119 
120     unsigned int tmpCaps[] = {17};  // 17 means CAP_SYS_RAWIO
121     unsigned int tmpsCapCnt = sizeof(tmpCaps) / sizeof(tmpCaps[0]);
122     if (SetCapability(tmpsCapCnt, tmpCaps) != 0) {
123         return -1;
124     }
125 #else
126     if (SetCapability(capsCnt, caps) != 0) {
127         return -1;
128     }
129 #endif // __LINUX__
130 
131     return 0;
132 }
133 
CreateProcess(const MessageSt * msgSt)134 pid_t CreateProcess(const MessageSt* msgSt)
135 {
136     pid_t newPID = fork();
137     if (newPID < 0) {
138         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] create process, fork failed! err %{public}d.", errno);
139         return -1;
140     }
141 
142     // in child process
143     if (newPID == 0) {
144 #ifdef OHOS_DEBUG
145         struct timespec tmStart = {0};
146         if (clock_gettime(CLOCK_REALTIME, &tmStart) != 0) {
147             HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process, pid %{public}d. get time err %{public}d.", \
148                 getpid(), errno);
149         }
150 #endif // OHOS_DEBUG
151 
152         // set permissions
153         if (SetPerms(msgSt->uID, msgSt->gID, msgSt->capsCnt, msgSt->caps) != 0) {
154             HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process %{public}s exit!", msgSt->bundleName);
155             exit(0x7f); // 0x7f: user specified
156         }
157 
158         (void)prctl(PR_SET_NAME, msgSt->bundleName);
159 
160 #ifdef OHOS_DEBUG
161         struct timespec tmEnd = {0};
162         if (clock_gettime(CLOCK_REALTIME, &tmEnd) != 0) {
163             HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process, pid %{public}d. get time2 err %{public}d.", \
164                 getpid(), errno);
165         }
166         // 1s = 1000000000ns
167         long timeUsed = (tmEnd.tv_sec - tmStart.tv_sec) * 1000000000L + (tmEnd.tv_nsec - tmStart.tv_nsec);
168         HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] sub-process, pid %{public}d, timeused %ld ns.", \
169             getpid(), timeUsed);
170 #endif // OHOS_DEBUG
171 
172         if (AbilityMain(msgSt->identityID) != 0) {
173             HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] AbilityMain execute failed, pid %{public}d.", getpid());
174             exit(0x7f); // 0x7f: user specified
175         }
176 
177         HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process exit, pid %{public}d.", getpid());
178         exit(0x7f); // 0x7f: user specified
179     }
180 
181     return newPID;
182 }
183 
184