Skip to content

Commit 1e81e8e

Browse files
committed
Experiment with dropping capabilities
1 parent b73d914 commit 1e81e8e

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

collector/collector.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ void initialChecks() {
119119
}
120120

121121
int main(int argc, char** argv) {
122+
// Drop not needed capabilities. Depending on the environment they might be
123+
// already dropped, but still make sure we use as little as possible.
124+
capng_clear(CAPNG_SELECT_ALL);
125+
capng_type_t cap_types = static_cast<capng_type_t>(CAPNG_EFFECTIVE |
126+
CAPNG_PERMITTED);
127+
capng_updatev(CAPNG_ADD, cap_types,
128+
// BPF is needed to load bpf programs and maps
129+
CAP_BPF,
130+
// PERFMON needed for using kprobes and tracepoints
131+
CAP_PERFMON,
132+
// DAC_READ_SEARCH is needed to check tracefs
133+
CAP_DAC_READ_SEARCH,
134+
// SYS_RESOURCE is needed for setrlimits
135+
CAP_SYS_RESOURCE,
136+
// SYS_PTRACE and SYS_ADMIN are needed to read /proc/$PID/ns
137+
CAP_SYS_PTRACE,
138+
CAP_SYS_ADMIN, -1);
139+
140+
if (capng_apply(CAPNG_SELECT_ALL) != 0) {
141+
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
142+
}
143+
122144
// Print system information before doing actual work.
123145
auto& host_info = HostInfo::Instance();
124146
CLOG(INFO) << "Collector Version: " << GetCollectorVersion();

collector/lib/CollectorStatsExporter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <iostream>
55
#include <math.h>
66

7-
#include <sys/capability.h>
7+
extern "C" {
8+
#include <cap-ng.h>
9+
}
810

911
#include "Containers.h"
1012
#include "EventNames.h"
@@ -107,7 +109,10 @@ class CollectorTimerGauge {
107109
};
108110

109111
void CollectorStatsExporter::run() {
110-
capng_clear(CAPNG_SELECT_BOTH);
112+
capng_clear(CAPNG_SELECT_ALL);
113+
if (capng_apply(CAPNG_SELECT_ALL) != 0) {
114+
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
115+
}
111116

112117
auto& collectorEventCounters = prometheus::BuildGauge()
113118
.Name("rox_collector_events")

collector/lib/NetworkStatusNotifier.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <google/protobuf/util/time_util.h>
44

5+
extern "C" {
6+
#include <cap-ng.h>
7+
}
8+
59
#include "CollectorStats.h"
610
#include "DuplexGRPC.h"
711
#include "GRPCUtil.h"
@@ -108,6 +112,20 @@ void NetworkStatusNotifier::ReceiveIPNetworks(const sensor::IPNetworkList& netwo
108112
}
109113

110114
void NetworkStatusNotifier::Run() {
115+
capng_clear(CAPNG_SELECT_ALL);
116+
capng_type_t cap_types = static_cast<capng_type_t>(CAPNG_EFFECTIVE |
117+
CAPNG_PERMITTED);
118+
capng_updatev(CAPNG_ADD, cap_types,
119+
// DAC_READ_SEARCH is needed to check tracefs
120+
CAP_DAC_READ_SEARCH,
121+
// SYS_PTRACE and SYS_ADMIN are needed to read /proc/$PID/ns
122+
CAP_SYS_PTRACE,
123+
CAP_SYS_ADMIN, -1);
124+
125+
if (capng_apply(CAPNG_SELECT_ALL) != 0) {
126+
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
127+
}
128+
111129
Profiler::RegisterCPUThread();
112130
auto next_attempt = std::chrono::system_clock::now();
113131

collector/lib/SignalServiceClient.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <fstream>
44

5+
extern "C" {
6+
#include <cap-ng.h>
7+
}
8+
59
#include "GRPCUtil.h"
610
#include "Logging.h"
711
#include "ProtoUtil.h"
@@ -43,6 +47,11 @@ bool SignalServiceClient::EstablishGRPCStreamSingle() {
4347
}
4448

4549
void SignalServiceClient::EstablishGRPCStream() {
50+
capng_clear(CAPNG_SELECT_ALL);
51+
if (capng_apply(CAPNG_SELECT_ALL) != 0) {
52+
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
53+
}
54+
4655
while (EstablishGRPCStreamSingle());
4756
CLOG(INFO) << "Signal service client terminating.";
4857
}

0 commit comments

Comments
 (0)