Skip to content

Experiment with dropping capabilities #1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions collector/collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ void initialChecks() {
}

int main(int argc, char** argv) {
// Drop not needed capabilities. Depending on the environment they might be
// already dropped, but still make sure we use as little as possible.
capng_clear(CAPNG_SELECT_ALL);
capng_type_t cap_types = static_cast<capng_type_t>(CAPNG_EFFECTIVE |
CAPNG_PERMITTED);
capng_updatev(CAPNG_ADD, cap_types,
// BPF is needed to load bpf programs and maps
CAP_BPF,
// PERFMON needed for using kprobes and tracepoints
CAP_PERFMON,
// DAC_READ_SEARCH is needed to check tracefs
CAP_DAC_READ_SEARCH,
// SYS_RESOURCE is needed for setrlimits
CAP_SYS_RESOURCE,
// SYS_PTRACE and SYS_ADMIN are needed to read /proc/$PID/ns
CAP_SYS_PTRACE,
CAP_SYS_ADMIN, -1);

if (capng_apply(CAPNG_SELECT_ALL) != 0) {
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
}

// Print system information before doing actual work.
auto& host_info = HostInfo::Instance();
CLOG(INFO) << "Collector Version: " << GetCollectorVersion();
Expand Down
16 changes: 16 additions & 0 deletions collector/lib/CollectorStatsExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <iostream>
#include <math.h>

extern "C" {
#include <cap-ng.h>
}

#include "Containers.h"
#include "EventNames.h"
#include "Logging.h"
Expand Down Expand Up @@ -105,6 +109,18 @@ class CollectorTimerGauge {
};

void CollectorStatsExporter::run() {
capng_clear(CAPNG_SELECT_ALL);

capng_type_t cap_types = static_cast<capng_type_t>(CAPNG_EFFECTIVE |
CAPNG_PERMITTED);
capng_updatev(CAPNG_ADD, cap_types,
// BPF is needed to read maps with stats
CAP_BPF, -1);

if (capng_apply(CAPNG_SELECT_ALL) != 0) {
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
}

auto& collectorEventCounters = prometheus::BuildGauge()
.Name("rox_collector_events")
.Help("Collector events")
Expand Down
18 changes: 18 additions & 0 deletions collector/lib/NetworkStatusNotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

extern "C" {
#include <cap-ng.h>
}

#include "CollectorStats.h"
#include "DuplexGRPC.h"
#include "GRPCUtil.h"
Expand Down Expand Up @@ -108,6 +112,20 @@ void NetworkStatusNotifier::ReceiveIPNetworks(const sensor::IPNetworkList& netwo
}

void NetworkStatusNotifier::Run() {
capng_clear(CAPNG_SELECT_ALL);
capng_type_t cap_types = static_cast<capng_type_t>(CAPNG_EFFECTIVE |
CAPNG_PERMITTED);
capng_updatev(CAPNG_ADD, cap_types,
// DAC_READ_SEARCH is needed to check tracefs
CAP_DAC_READ_SEARCH,
// SYS_PTRACE and SYS_ADMIN are needed to read /proc/$PID/ns
CAP_SYS_PTRACE,
CAP_SYS_ADMIN, -1);

if (capng_apply(CAPNG_SELECT_ALL) != 0) {
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
}

Profiler::RegisterCPUThread();
auto next_attempt = std::chrono::system_clock::now();

Expand Down
9 changes: 9 additions & 0 deletions collector/lib/SignalServiceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <fstream>

extern "C" {
#include <cap-ng.h>
}

#include "GRPCUtil.h"
#include "Logging.h"
#include "ProtoUtil.h"
Expand Down Expand Up @@ -43,6 +47,11 @@ bool SignalServiceClient::EstablishGRPCStreamSingle() {
}

void SignalServiceClient::EstablishGRPCStream() {
capng_clear(CAPNG_SELECT_ALL);
if (capng_apply(CAPNG_SELECT_ALL) != 0) {
CLOG(WARNING) << "Failed to drop capabilities: " << StrError();
}

while (EstablishGRPCStreamSingle());
CLOG(INFO) << "Signal service client terminating.";
}
Expand Down
Loading