-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_plugin.cc
296 lines (261 loc) · 11 KB
/
process_plugin.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "secagentd/plugins.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "absl/status/status.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "missive/proto/record_constants.pb.h"
#include "secagentd/bpf/bpf_types.h"
#include "secagentd/device_user.h"
#include "secagentd/message_sender.h"
#include "secagentd/metrics_sender.h"
#include "secagentd/policies_features_broker.h"
#include "secagentd/proto/security_xdr_events.pb.h"
namespace secagentd {
namespace pb = cros_xdr::reporting;
namespace {
// Fills a Namespaces proto with contents from bpf namespace_info.
void FillNamespaces(const bpf::cros_namespace_info& ns,
pb::Namespaces* ns_proto) {
ns_proto->set_cgroup_ns(ns.cgroup_ns);
ns_proto->set_pid_ns(ns.pid_ns);
ns_proto->set_user_ns(ns.user_ns);
ns_proto->set_uts_ns(ns.uts_ns);
ns_proto->set_mnt_ns(ns.mnt_ns);
ns_proto->set_net_ns(ns.net_ns);
ns_proto->set_ipc_ns(ns.ipc_ns);
}
std::string GetBatchedEventKey(
const pb::ProcessEventAtomicVariant& process_event) {
switch (process_event.variant_type_case()) {
case cros_xdr::reporting::ProcessEventAtomicVariant::kProcessExec:
return process_event.process_exec().spawn_process().process_uuid();
case cros_xdr::reporting::ProcessEventAtomicVariant::kProcessTerminate:
return process_event.process_terminate().process().process_uuid();
case cros_xdr::reporting::ProcessEventAtomicVariant::VARIANT_TYPE_NOT_SET:
return "";
}
}
bool SetTerminateTimestamp(pb::ProcessEventAtomicVariant* exec) {
if (exec->has_process_exec()) {
exec->mutable_process_exec()->set_terminate_timestamp_us(
base::Time::Now().ToJavaTime() *
base::Time::kMicrosecondsPerMillisecond);
return true;
}
return false;
}
} // namespace
ProcessPlugin::ProcessPlugin(
scoped_refptr<BpfSkeletonFactoryInterface> bpf_skeleton_factory,
scoped_refptr<MessageSenderInterface> message_sender,
scoped_refptr<ProcessCacheInterface> process_cache,
scoped_refptr<PoliciesFeaturesBrokerInterface> policies_features_broker,
scoped_refptr<DeviceUserInterface> device_user,
uint32_t batch_interval_s)
: weak_ptr_factory_(this),
process_cache_(process_cache),
policies_features_broker_(policies_features_broker),
device_user_(device_user),
batch_sender_(
std::make_unique<BatchSender<std::string,
pb::XdrProcessEvent,
pb::ProcessEventAtomicVariant>>(
base::BindRepeating(&GetBatchedEventKey),
message_sender,
reporting::Destination::CROS_SECURITY_PROCESS,
batch_interval_s)) {
CHECK(message_sender != nullptr);
CHECK(process_cache != nullptr);
CHECK(bpf_skeleton_factory);
factory_ = std::move(bpf_skeleton_factory);
}
std::string ProcessPlugin::GetName() const {
return "Process";
}
void ProcessPlugin::HandleRingBufferEvent(const bpf::cros_event& bpf_event) {
auto atomic_event = std::make_unique<pb::ProcessEventAtomicVariant>();
if (bpf_event.type != bpf::kProcessEvent) {
LOG(ERROR) << "ProcessBPF: unknown BPF event type.";
return;
}
const bpf::cros_process_event& pe = bpf_event.data.process_event;
if (pe.type == bpf::kProcessStartEvent) {
const bpf::cros_process_start& process_start = pe.data.process_start;
// Record the newly spawned process into our cache.
process_cache_->PutFromBpfExec(process_start);
auto exec_event = MakeExecEvent(process_start);
const pb::Process* parent_process =
exec_event->has_process() ? exec_event->mutable_process() : nullptr;
const pb::Process* process = exec_event->has_spawn_process()
? exec_event->mutable_spawn_process()
: nullptr;
if (process_cache_->IsEventFiltered(parent_process, process)) {
return;
}
atomic_event->set_allocated_process_exec(exec_event.release());
} else if (pe.type == bpf::kProcessExitEvent) {
const bpf::cros_process_exit& process_exit = pe.data.process_exit;
auto terminate_event = MakeTerminateEvent(process_exit);
if (process_exit.is_leaf) {
process_cache_->EraseProcess(process_exit.task_info.pid,
process_exit.task_info.start_time);
}
const pb::Process* parent_process =
terminate_event->has_parent_process()
? terminate_event->mutable_parent_process()
: nullptr;
const pb::Process* process = terminate_event->has_process()
? terminate_event->mutable_process()
: nullptr;
if (process_cache_->IsEventFiltered(parent_process, process)) {
return;
}
atomic_event->set_allocated_process_terminate(terminate_event.release());
} else {
LOG(ERROR) << "ProcessBPF: unknown BPF process event type.";
return;
}
device_user_->GetDeviceUserAsync(
base::BindOnce(&ProcessPlugin::OnDeviceUserRetrieved,
weak_ptr_factory_.GetWeakPtr(), std::move(atomic_event)));
}
void ProcessPlugin::OnDeviceUserRetrieved(
std::unique_ptr<pb::ProcessEventAtomicVariant> atomic_event,
const std::string& device_user) {
atomic_event->mutable_common()->set_device_user(device_user);
EnqueueBatchedEvent(std::move(atomic_event));
}
void ProcessPlugin::HandleBpfRingBufferReadReady() const {
skeleton_wrapper_->ConsumeEvent();
}
absl::Status ProcessPlugin::Activate() {
// If already called do nothing and report Ok.
if (skeleton_wrapper_) {
return absl::OkStatus();
}
struct BpfCallbacks callbacks;
callbacks.ring_buffer_event_callback = base::BindRepeating(
&ProcessPlugin::HandleRingBufferEvent, weak_ptr_factory_.GetWeakPtr());
callbacks.ring_buffer_read_ready_callback =
base::BindRepeating(&ProcessPlugin::HandleBpfRingBufferReadReady,
weak_ptr_factory_.GetWeakPtr());
skeleton_wrapper_ =
factory_->Create(Types::BpfSkeleton::kProcess, std::move(callbacks), 0);
if (skeleton_wrapper_ == nullptr) {
return absl::InternalError("Process BPF program loading error.");
}
batch_sender_->Start();
return absl::OkStatus();
}
absl::Status ProcessPlugin::Deactivate() {
return absl::UnimplementedError(
"Deactivate not implemented for ProcessPlugin.");
}
void ProcessPlugin::EnqueueBatchedEvent(
std::unique_ptr<pb::ProcessEventAtomicVariant> atomic_event) {
if (atomic_event->has_process_terminate() &&
policies_features_broker_->GetFeature(
PoliciesFeaturesBroker::Feature::
kCrOSLateBootSecagentdCoalesceTerminates)) {
if (batch_sender_->Visit(pb::ProcessEventAtomicVariant::kProcessExec,
GetBatchedEventKey(*atomic_event),
base::BindOnce(&SetTerminateTimestamp))) {
// Successfully visited and presumably also coalesced.
return;
}
}
batch_sender_->Enqueue(std::move(atomic_event));
}
bool ProcessPlugin::IsActive() const {
return skeleton_wrapper_ != nullptr;
}
std::unique_ptr<pb::ProcessExecEvent> ProcessPlugin::MakeExecEvent(
const bpf::cros_process_start& process_start) {
auto process_exec_event = std::make_unique<pb::ProcessExecEvent>();
FillNamespaces(process_start.spawn_namespace,
process_exec_event->mutable_spawn_namespaces());
// Fetch information on process that was just spawned, the parent process
// that spawned that process, and its parent process. I.e a total of
// three.
auto hierarchy = process_cache_->GetProcessHierarchy(
process_start.task_info.pid, process_start.task_info.start_time, 3);
if (hierarchy.empty()) {
LOG(ERROR) << "PID:" << process_start.task_info.pid
<< " not found in the process cache.";
}
if (hierarchy.size() > 0) {
process_exec_event->set_allocated_spawn_process(hierarchy[0].release());
}
if (hierarchy.size() > 1) {
process_exec_event->set_allocated_process(hierarchy[1].release());
}
if (hierarchy.size() > 2) {
process_exec_event->set_allocated_parent_process(hierarchy[2].release());
}
// Exec event metrics.
metrics::ProcessEvent exec_event_metric = metrics::ProcessEvent::kFullEvent;
if (hierarchy.empty()) {
exec_event_metric = metrics::ProcessEvent::kSpawnPidNotInCache;
} else if (hierarchy.size() == 1) {
exec_event_metric = metrics::ProcessEvent::kProcessPidNotInCache;
} else if (hierarchy.size() == 2 && process_exec_event->has_process() &&
process_exec_event->process().canonical_pid() > 1) {
exec_event_metric = metrics::ProcessEvent::kParentPidNotInCache;
}
MetricsSender::GetInstance().IncrementBatchedMetric(metrics::kExecEvent,
exec_event_metric);
return process_exec_event;
}
std::unique_ptr<pb::ProcessTerminateEvent> ProcessPlugin::MakeTerminateEvent(
const bpf::cros_process_exit& process_exit) {
auto process_terminate_event = std::make_unique<pb::ProcessTerminateEvent>();
// Try to fetch from the process cache if possible. The cache has more
// complete information.
auto hierarchy = process_cache_->GetProcessHierarchy(
process_exit.task_info.pid, process_exit.task_info.start_time, 2);
// If that fails, fill in the task info that we got from BPF.
if (hierarchy.empty()) {
ProcessCache::PartiallyFillProcessFromBpfTaskInfo(
process_exit.task_info, process_terminate_event->mutable_process(),
device_user_->GetUsernamesForRedaction());
// Maybe the parent is still alive and in procfs.
auto parent = process_cache_->GetProcessHierarchy(
process_exit.task_info.ppid, process_exit.task_info.parent_start_time,
1);
if (parent.size() != 0) {
process_terminate_event->set_allocated_parent_process(
parent[0].release());
}
}
if (hierarchy.size() > 0) {
process_terminate_event->set_allocated_process(hierarchy[0].release());
}
if (hierarchy.size() > 1) {
process_terminate_event->set_allocated_parent_process(
hierarchy[1].release());
}
// Terminate event metrics.
metrics::ProcessEvent terminate_event_metric =
metrics::ProcessEvent::kFullEvent;
if (hierarchy.empty()) {
if (process_terminate_event->has_process()) {
terminate_event_metric = metrics::ProcessEvent::kParentStillAlive;
} else {
terminate_event_metric = metrics::ProcessEvent::kProcessPidNotInCache;
}
} else if (hierarchy.size() == 1 && process_terminate_event->has_process() &&
process_terminate_event->process().canonical_pid() > 1) {
terminate_event_metric = metrics::ProcessEvent::kParentPidNotInCache;
}
MetricsSender::GetInstance().IncrementBatchedMetric(metrics::kTerminateEvent,
terminate_event_metric);
return process_terminate_event;
}
} // namespace secagentd