1 /*
2 * Copyright (c) 2021-2022 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 /* This files contains faultlog secure module. */
17
18 #include "fault_logger_secure.h"
19
20 #include <cstdio>
21 #include <cstdlib>
22 #include <securec.h>
23 #include <string>
24 #include "dfx_log.h"
25 #include "iosfwd"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 static const std::string FAULTLOGGERSECURE_TAG = "FaultLoggerSecure";
31 static constexpr int32_t ROOT_UID = 0;
32 static constexpr int32_t BMS_UID = 1000;
33 static constexpr int32_t HIVIEW_UID = 1201;
34 static constexpr int32_t HIDUMPER_SERVICE_UID = 1212;
35 static constexpr int32_t MAX_RESP_LEN = 128;
36 static constexpr int32_t MAX_CMD_LEN = 1024;
37 }
38
FaultLoggerSecure()39 FaultLoggerSecure::FaultLoggerSecure()
40 {
41 }
42
~FaultLoggerSecure()43 FaultLoggerSecure::~FaultLoggerSecure()
44 {
45 }
46
DelSpace(char * src)47 static int DelSpace(char *src)
48 {
49 char* pos = src;
50 unsigned int count = 0;
51
52 while (*src != '\0') {
53 if (*src != ' ') {
54 *pos++ = *src;
55 } else {
56 count++;
57 }
58 src++;
59 }
60 *pos = '\0';
61 return count;
62 }
63
CheckUidAndPid(const int uid,const int32_t pid)64 bool FaultLoggerSecure::CheckUidAndPid(const int uid, const int32_t pid)
65 {
66 bool ret = false;
67 char resp[MAX_RESP_LEN] = { '\0' };
68 char cmd[MAX_CMD_LEN] = { '\0' };
69
70 DfxLogInfo("%s :: CheckUidAndPid :: uid(%d), pid(%d).\n",
71 FAULTLOGGERSECURE_TAG.c_str(), uid, pid);
72
73 errno_t err = memset_s(resp, sizeof(resp), '\0', sizeof(resp));
74 if (err != EOK) {
75 DfxLogError("%s :: memset_s resp failed..", __func__);
76 }
77 err = memset_s(cmd, sizeof(cmd), '\0', sizeof(cmd));
78 if (err != EOK) {
79 DfxLogError("%s :: memset_s cmd failed..", __func__);
80 }
81 auto pms = sprintf_s(cmd, sizeof(cmd), "/bin/ps -u %d -o PID", uid);
82 if (pms <= 0) {
83 return ret;
84 }
85 DfxLogInfo("%s :: CheckUidAndPid :: cmd(%s).\n", FAULTLOGGERSECURE_TAG.c_str(), cmd);
86
87 FILE *fp = popen(cmd, "r");
88 if (fp == nullptr) {
89 return ret;
90 }
91
92 int count = static_cast<int>(fread(resp, 1, MAX_RESP_LEN - 1, fp));
93 pclose(fp);
94 if (count < 0) {
95 return ret;
96 }
97
98 DelSpace(reinterpret_cast<char *>(resp));
99
100 char delim[] = "\n";
101 char *buf;
102 char *token = strtok_s(reinterpret_cast<char *>(resp), delim, &buf);
103 if (token == nullptr) {
104 return ret;
105 }
106 token = strtok_s(nullptr, delim, &buf);
107 while (token != nullptr) {
108 int tokenPID = atoi(token);
109 if (pid == tokenPID) {
110 ret = true;
111 break;
112 }
113 token = strtok_s(nullptr, delim, &buf);
114 }
115
116 DfxLogInfo("%s :: CheckUidAndPid :: ret(%d).\n",
117 FAULTLOGGERSECURE_TAG.c_str(), ret);
118 return ret;
119 }
120
CheckCallerUID(const int callingUid,const int32_t pid)121 bool FaultLoggerSecure::CheckCallerUID(const int callingUid, const int32_t pid)
122 {
123 bool ret = false;
124 if ((callingUid < 0) || (pid <= 0)) {
125 return false;
126 }
127
128 // If caller's is BMS / root or caller's uid/pid is validate, just return true
129 if ((callingUid == BMS_UID) ||
130 (callingUid == ROOT_UID) ||
131 (callingUid == HIVIEW_UID) ||
132 (callingUid == HIDUMPER_SERVICE_UID)) {
133 ret = true;
134 } else {
135 ret = false;
136 }
137
138 DfxLogInfo("%s :: CheckCallerUID :: ret(%d).\n", FAULTLOGGERSECURE_TAG.c_str(), ret);
139 return ret;
140 }
141 } // namespace HiviewDfx
142 } // namespace OHOS
143