1 /*
2 * Copyright (c) 2021-2023 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 "hook_service.h"
17
18 #include <cinttypes>
19 #include <unistd.h>
20
21 #include "common.h"
22 #include "logging.h"
23 #include "parameter.h"
24 #include "socket_context.h"
25
26 namespace OHOS::Developtools::NativeDaemon {
HookService(int smbFd,int eventFd,int pid,const std::string & processName,const ClientConfig & clientConfig)27 HookService::HookService(int smbFd,
28 int eventFd,
29 int pid,
30 const std::string& processName,
31 const ClientConfig& clientConfig)
32 : smbFd_(smbFd), eventFd_(eventFd), pid_(pid), processName_(processName), clientConfig_(clientConfig)
33 {
34 serviceName_ = "HookService";
35 if (getuid() == 0) {
36 StartService(DEFAULT_UNIX_SOCKET_HOOK_FULL_PATH);
37 } else {
38 StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
39 }
40 }
41
~HookService()42 HookService::~HookService()
43 {
44 serviceEntry_ = nullptr;
45 }
46
StartService(const std::string & unixSocketName)47 bool HookService::StartService(const std::string& unixSocketName)
48 {
49 serviceEntry_ = std::make_shared<ServiceEntry>();
50 if (!serviceEntry_->StartServer(unixSocketName)) {
51 serviceEntry_ = nullptr;
52 HILOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
53 return false;
54 }
55 serviceEntry_->RegisterService(*this);
56 return true;
57 }
58
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)59 bool HookService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
60 {
61 if (size != sizeof(int)) {
62 HILOG_ERROR(LOG_CORE, "ProtocolProc hook config error, pid_ = %d, size = %d", pid_, size);
63 }
64 int peerConfig = *const_cast<int *>(reinterpret_cast<const int *>(buf));
65 if (peerConfig == -1) {
66 return true;
67 }
68 if (pid_ == 0) {
69 printf("Process %d hook started.\n", peerConfig);
70 pid_ = peerConfig;
71 } else if (peerConfig != pid_) {
72 HILOG_ERROR(LOG_CORE, "ProtocolProc receive peerConfig:%d not expected", peerConfig);
73 return false;
74 }
75 CHECK_TRUE(getuid() == 0 || COMMON::CheckApplicationPermission(pid_, ""), false,
76 "Application debug permisson denied!");
77 HILOG_DEBUG(LOG_CORE, "ProtocolProc, receive message from hook client, and send hook config to process %d", pid_);
78 context.SendHookConfig(reinterpret_cast<uint8_t *>(&clientConfig_), sizeof(clientConfig_));
79 context.SendFileDescriptor(smbFd_);
80 context.SendFileDescriptor(eventFd_);
81 return true;
82 }
83 }