libintrovirt v0.57.4
IntroVirt introspection library
Loading...
Searching...
No Matches
SystemCallMonitor.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 Assured Information Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
17
18using namespace introvirt;
19using namespace introvirt::windows;
20
21class SystemCallMonitor final : public EventCallback {
22 public:
23 void process_event(Event& event) override {
24 switch (event.type()) {
25 case EventType::EVENT_FAST_SYSCALL: {
26 SystemCall* syscall = event.syscall().handler();
27 if (unlikely(syscall == nullptr))
28 break; // Shouldn't happen I believe
29
30 if (!syscall->supported()) {
31 // Handle system calls that aren't technically supported
32 if (unsupported_)
33 event.syscall().hook_return(true);
34 break;
35 }
36
37 if (likely(syscall->will_return())) {
38 // The most common case
39 event.syscall().hook_return(true);
40 } else {
41 if (json_)
42 write_json(event);
43 else {
44 write_syscall(event);
45 }
46 }
47
48 break;
49 }
50 case EventType::EVENT_FAST_SYSCALL_RET: {
51
52 if (json_)
53 write_json(event);
54 else
55 write_syscall(event);
56
57 break;
58 }
59 default:
60 // Some other event we don't care about
61 break;
62 }
63 }
64
65 SystemCallMonitor(bool flush, bool json, bool unsupported)
66 : flush_(flush), json_(json), unsupported_(unsupported) {}
67 ~SystemCallMonitor() { std::cout.flush(); }
68
69 private:
70 void write_syscall(const Event& event) {
71 std::lock_guard lock(mtx_);
72
73 const Vcpu& vcpu = event.vcpu();
74 std::cout << "Vcpu " << vcpu.id() << ": [" << event.task().pid() << ":"
75 << event.task().tid() << "] " << event.task().process_name() << '\n';
76 std::cout << event.syscall().name() << '\n';
77 if (event.syscall().handler())
78 event.syscall().handler()->write();
79 if (flush_)
80 std::cout.flush();
81 }
82
83 void write_json(const Event& event) {
84 std::lock_guard lock(mtx_);
85 std::cout << event.json() << '\n';
86 if (flush_)
87 std::cout.flush();
88 }
89
90 std::mutex mtx_;
91 const bool flush_;
92 const bool json_;
93 const bool unsupported_;
94};
Definition SystemCallMonitor.hh:21
SystemCallMonitor(bool flush, bool json, bool unsupported)
Definition SystemCallMonitor.hh:65
~SystemCallMonitor()
Definition SystemCallMonitor.hh:67
void process_event(Event &event) override
Process an incoming event.
Definition SystemCallMonitor.hh:23
Interface for an event poller callback.
Definition EventCallback.hh:29
Interface class for hypervisor events.
Definition Event.hh:43
virtual SystemCallEvent & syscall()=0
Get system call event information.
virtual EventType type() const =0
Get the type of event.
virtual SystemCall * handler()=0
Gets the associated system call handler with this event.
Definition SystemCall.hh:31
virtual bool will_return() const =0
virtual bool supported() const =0
Check if this system call is supported by a more specific handler.
A class representing a single virtual processor.
Definition Vcpu.hh:33
virtual uint32_t id() const =0
Get the number of this Vcpu.
#define likely(x)
Definition compiler.hh:26
#define unlikely(x)
Definition compiler.hh:27
Classes related to Microsoft Windows guests.
Definition LanguageId.hh:21
Core IntroVirt classes.
Definition Cr0.hh:20