forked from VirusTotal/yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyara_transaction.cc
173 lines (148 loc) · 5.67 KB
/
yara_transaction.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright (c) 2019. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "sandbox/yara_transaction.h"
#include <poll.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include "absl/strings/str_cat.h"
#include "libyara/include/yara/error.h"
#include "sandboxed_api/util/status_macros.h"
namespace yara
{
absl::Mutex YaraTransaction::mutex_(absl::kConstInit);
::sapi::StatusOr<std::unique_ptr<YaraTransaction>> YaraTransaction::Create(
Options options)
{
auto transaction = absl::WrapUnique(
new YaraTransaction(options.scan_timeout));
// "Run" the transaction in order to initialize the underlying sandbox.
SAPI_RETURN_IF_ERROR(transaction->Run());
sandbox::YaraApi api(transaction->sandbox());
SAPI_RETURN_IF_ERROR(
api.YaraInitWorkers(options.num_workers >= 1 ? options.num_workers : 1));
return transaction;
}
::sapi::StatusOr<int> YaraTransaction::LoadRules(const std::string& rule_string)
{
absl::MutexLock lock(&mutex_);
sandbox::YaraApi api(sandbox());
::sapi::v::ConstCStr rule_string_sapi(rule_string.c_str());
YaraStatus error_status;
::sapi::v::Proto<YaraStatus> error_status_sapi(error_status);
SAPI_ASSIGN_OR_RETURN(
int num_rules,
api.YaraLoadRules(
rule_string_sapi.PtrBefore(), error_status_sapi.PtrBoth()));
if (num_rules <= 0)
{
auto error_status_copy = error_status_sapi.GetProtoCopy();
if (!error_status_copy)
{
return absl::UnknownError("Deserialization of response failed");
}
return absl::InvalidArgumentError(error_status_copy->message());
}
return num_rules;
}
::sapi::StatusOr<YaraMatches> YaraTransaction::ScanFd(int fd)
{
int local_event_fd = eventfd(0 /* initval */, 0 /* flags */);
if (local_event_fd == -1)
{
return absl::InternalError(
absl::StrCat("eventfd() error: ", strerror(errno)));
}
struct FDCloser
{
~FDCloser() { close(event_fd); }
int event_fd;
} event_fd_closer = {local_event_fd};
sandbox::YaraApi api(sandbox());
uint64_t result_id;
{
absl::MutexLock lock(&mutex_);
// Note: These SAPI Fd objects use the underlying sandbox comms to
// synchronize. Hence they must live within this locked scope.
::sapi::v::Fd event_fd(local_event_fd);
SAPI_RETURN_IF_ERROR(sandbox()->TransferToSandboxee(&event_fd));
event_fd.OwnLocalFd(false); // Needs to be valid during poll()
event_fd.OwnRemoteFd(false); // Sandboxee will close
::sapi::v::Fd data_fd(fd);
SAPI_RETURN_IF_ERROR(sandbox()->TransferToSandboxee(&data_fd));
data_fd.OwnLocalFd(false); // To be closed by caller
data_fd.OwnRemoteFd(false); // Sandboxee will close
SAPI_ASSIGN_OR_RETURN(
result_id,
api.YaraAsyncScanFd(
data_fd.GetRemoteFd(),
event_fd.GetRemoteFd(),
absl::ToInt64Seconds(scan_timeout_)));
}
pollfd poll_events{local_event_fd, POLLIN};
int poll_result;
// TEMP_FAILURE_RETRY is a GNU extension that retries if the call returns
// EINTR.
poll_result = TEMP_FAILURE_RETRY(poll(
&poll_events,
1 /* nfds */,
// Add extra time to allow code inside the sandbox to time out first.
absl::ToInt64Milliseconds(scan_timeout_ + absl::Seconds(10))));
if (poll_result == 0)
{
return absl::DeadlineExceededError("Scan timeout during poll()");
}
if (poll_result == -1)
{
return absl::InternalError(absl::StrCat("poll() error: ", strerror(errno)));
}
if (poll_events.revents & POLLHUP || poll_events.revents & POLLERR ||
poll_events.revents & POLLNVAL)
{
return absl::InternalError(
absl::StrCat("poll() error, revents: ", poll_events.revents));
}
absl::MutexLock lock(&mutex_);
YaraMatches matches;
::sapi::v::Proto<YaraMatches> matches_sapi(matches);
SAPI_ASSIGN_OR_RETURN(
int scan_result,
api.YaraGetScanResult(result_id, matches_sapi.PtrBoth()));
switch (scan_result)
{
case ERROR_SUCCESS:
case ERROR_TOO_MANY_MATCHES:
{
auto matches_copy = matches_sapi.GetProtoCopy();
if (!matches_copy)
{
return absl::UnknownError("Deserialization of response failed");
}
return *matches_copy;
}
case ERROR_SCAN_TIMEOUT:
return absl::DeadlineExceededError("Scan timeout");
}
return absl::InternalError(absl::StrCat("Error during scan: ", scan_result));
}
} // namespace yara