• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
20 #include "logging.h"
21 #include "socket_context.h"
22 
23 namespace {
24 const int BUF_MAX_LEN = 10;
25 const int MOVE_BIT = 32;
26 }  // namespace
27 
HookService(int smbFd,int eventFd,uint32_t filterSize,uint32_t smbSize,int pid,std::string processName)28 HookService::HookService(int smbFd,
29                          int eventFd,
30                          uint32_t filterSize,
31                          uint32_t smbSize,
32                          int pid,
33                          std::string processName)
34     : smbFd_(smbFd), eventFd_(eventFd), filterSize_(filterSize), smbSize_(smbSize), pid_(pid), processName_(processName)
35 {
36     serviceName_ = "HookService";
37     StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
38 }
39 
~HookService()40 HookService::~HookService()
41 {
42     serviceEntry_ = nullptr;
43 }
44 
StartService(const std::string & unixSocketName)45 bool HookService::StartService(const std::string& unixSocketName)
46 {
47     serviceEntry_ = std::make_shared<ServiceEntry>();
48     if (!serviceEntry_->StartServer(unixSocketName)) {
49         serviceEntry_ = nullptr;
50         HILOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
51         return false;
52     }
53     serviceEntry_->RegisterService(*this);
54     return true;
55 }
56 
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)57 bool HookService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
58 {
59     if (size != sizeof(uint64_t)) {
60         HILOG_ERROR(LOG_CORE, "ProtocolProc hook config error");
61     }
62     uint64_t peerconfig = *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(buf));
63     if (pid_ == -1) {
64         // check if the pid and process name is consistency
65         std::string findpid = "pidof " + processName_;
66         std::unique_ptr<char []> buffer {new (std::nothrow) char[BUF_MAX_LEN]};
67         HILOG_INFO(LOG_CORE, "find pid command : %s", findpid.c_str());
68         std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(findpid.c_str(), "r"), pclose);
69 
70         char line[LINE_SIZE];
71         do {
72             if (fgets(line, sizeof(line), pipe.get()) == nullptr) {
73                 return false;
74             } else if (strlen(line) > 0 && isdigit((unsigned char)(line[0]))) {
75                 pid_ = (int)atoi(line);
76                 if (peerconfig != (uint64_t)pid_) {
77                     return false;
78                 }
79                 printf("Process %" PRIu64 " hook started.\n", peerconfig);
80                 break;
81             }
82         } while (1);
83     } else if (peerconfig != (uint64_t)pid_) {
84         HILOG_ERROR(LOG_CORE, "ProtocolProc receive peerconfig %" PRIu64 " not expected", peerconfig);
85         return false;
86     }
87 
88     HILOG_DEBUG(LOG_CORE, "ProtocolProc, receive message from hook client, and send hook config to process %d", pid_);
89     uint64_t config = filterSize_;
90     config <<= MOVE_BIT;
91     config |= smbSize_;
92     context.SendHookConfig(config);
93     context.SendFileDescriptor(smbFd_);
94     context.SendFileDescriptor(eventFd_);
95     return true;
96 }
97