• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
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 "hook_service.h"
17 
18 #include "hook_manager.h"
19 #include <cinttypes>
20 #include <unistd.h>
21 #include <sys/socket.h>
22 #include "common.h"
23 #include "logging.h"
24 #include "parameter.h"
25 #include "socket_context.h"
26 
27 namespace OHOS::Developtools::NativeDaemon {
28 const int VEC_SHIFT = 2;
29 
HookService(const ClientConfig & clientConfig,std::shared_ptr<HookManager> hook,bool multipleProcesses)30 HookService::HookService(const ClientConfig& clientConfig, std::shared_ptr<HookManager> hook, bool multipleProcesses)
31     : clientConfig_(clientConfig), hookMgr_(hook), multipleProcesses_(multipleProcesses)
32 {
33     serviceName_ = "HookService";
34     StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
35 }
36 
~HookService()37 HookService::~HookService()
38 {
39     serviceEntry_ = nullptr;
40 }
41 
StartService(const std::string & unixSocketName)42 bool HookService::StartService(const std::string& unixSocketName)
43 {
44     serviceEntry_ = std::make_shared<ServiceEntry>();
45     if (!serviceEntry_->StartServer(unixSocketName)) {
46         serviceEntry_ = nullptr;
47         PROFILER_LOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
48         return false;
49     }
50     serviceEntry_->RegisterService(*this);
51     return true;
52 }
53 
AddPidInfo(pid_t pid,uid_t uid,gid_t gid)54 void HookService::AddPidInfo(pid_t pid, uid_t uid, gid_t gid)
55 {
56     std::unique_lock<std::mutex> lock(pidInfoMtx_);
57     pidInfo_[pid] = std::make_pair(uid, gid);
58 }
59 
RemovePidInfo(pid_t pid)60 void HookService::RemovePidInfo(pid_t pid)
61 {
62     std::unique_lock<std::mutex> lock(pidInfoMtx_);
63     pidInfo_.erase(pid);
64 }
65 
CloseSocketFd(pid_t pid)66 void HookService::CloseSocketFd(pid_t pid)
67 {
68     std::unique_lock<std::mutex> lock(pidFdMtx_);
69     if ((pidFds_.find(pid) != pidFds_.end()) && (pidFds_[pid] != 0)) {
70         int socketFd = pidFds_[pid];
71         pidFds_.erase(pid);
72         lock.unlock();
73         serviceEntry_->RemoveContext(socketFd);
74     } else {
75         PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaService::CloseSocketFd pid invalid");
76     }
77 }
78 
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)79 bool HookService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
80 {
81     if (size != sizeof(int)) {
82         return false;
83     }
84     int peerConfig = *const_cast<int *>(reinterpret_cast<const int *>(buf));
85     if (peerConfig == -1) {
86         return false;
87     }
88     hookMgr_->SetPid(peerConfig);
89     std::string filePath = "/proc/" + std::to_string(peerConfig) + "/cmdline";
90     std::string bundleName;
91     if (!LoadStringFromFile(filePath, bundleName)) {
92         PROFILER_LOG_ERROR(LOG_CORE, "Get process name by pid failed!, pid: %d", peerConfig);
93         return false;
94     }
95     hookMgr_->SetPid(peerConfig);
96     bundleName.resize(strlen(bundleName.c_str()));
97 
98     if (bundleName.size() >= 2 && bundleName.substr(0, 2) == "./") { // 2: size
99         bundleName = bundleName.substr(2); // 2: point
100     }
101     size_t found = bundleName.rfind("/");
102     std::string procName;
103     if (found != std::string::npos) {
104         procName = bundleName.substr(found + 1);
105     } else {
106         procName = bundleName;
107     }
108 
109     std::lock_guard<std::mutex> lock(mtx_);
110     if ((!firstProcess_) && (!multipleProcesses_)) {
111         return false;
112     }
113     firstProcess_ = false;
114     if (!(hookMgr_ && hookMgr_->GetHookConfig().startup_mode())) {
115         int socketHandle = context.GetSocketHandle();
116         struct ucred cred;
117         socklen_t len = sizeof(struct ucred);
118         if (getsockopt(socketHandle, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) {
119             PROFILER_LOG_ERROR(LOG_CORE, "HookService ProtocolProc getsockopt failed");
120             return false;
121         }
122         if (!((pidInfo_.find(cred.pid) != pidInfo_.end()) && (pidInfo_[cred.pid].first == cred.uid) &&
123             (pidInfo_[cred.pid].second == cred.gid))) {
124             PROFILER_LOG_ERROR(LOG_CORE, "HookService ProtocolProc process uid gid check failed");
125             return false;
126         }
127     }
128 
129     PROFILER_LOG_DEBUG(LOG_CORE, "ProtocolProc, receive message from hook client, and send hook config to process %d",
130                        peerConfig);
131     context.SendHookConfig(reinterpret_cast<uint8_t *>(&clientConfig_), sizeof(clientConfig_));
132     int sharedMemCount = (clientConfig_.offlineSymbolization) ? SHARED_MEMORY_NUM : 1;
133     std::vector<int> fdVec = hookMgr_->GetFds(peerConfig, procName, sharedMemCount);
134     for (int i = 0; i < sharedMemCount; ++i) {
135         int eventFd = fdVec[i * VEC_SHIFT];
136         int smbFd = fdVec[i * VEC_SHIFT + 1];
137         if (eventFd == smbFd) {
138             PROFILER_LOG_ERROR(LOG_CORE, "Get fd failed!, name: %s, pid: %d", procName.c_str(), peerConfig);
139             return false;
140         }
141         context.SendFileDescriptor(smbFd);
142         context.SendFileDescriptor(eventFd);
143     }
144     hookMgr_->ResetStartupParam();
145     std::unique_lock<std::mutex> socketLock(pidFdMtx_);
146     pidFds_[static_cast<pid_t>(peerConfig)] = context.GetSocketHandle();
147     socketLock.unlock();
148     return true;
149 }
150 }