-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
50 lines (41 loc) · 1.09 KB
/
common.cpp
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
#include <stdexcept>
#include "common.h"
fs::path nsMntDir(MNTDIR);
fs::path linksDir(LINKSDIR);
fs::path executorPath(EXECUTORPATH);
fs::path config(CONFIGPATH);
class CapWrapper {
public:
~CapWrapper() {
if (caps) {
cap_free(caps);
}
}
void init() {
caps = cap_init();
if (!caps) {
throw std::runtime_error(
"Couldn't initialize capabilities context");
}
}
void allowCaps(const std::vector<cap_value_t> &capList, cap_flag_t set) {
if (cap_set_flag(caps, set, capList.size(), capList.data(), CAP_SET) ==
-1) {
throw std::runtime_error("Couldn't set capabiliets on context");
}
}
void enableCaps() {
if (cap_set_proc(caps) == -1) {
throw std::runtime_error("Couldn't drop capabilities");
}
}
private:
cap_t caps;
};
void dropToCapabilities(const std::vector<cap_value_t> &capList) {
CapWrapper cw;
cw.init();
cw.allowCaps(capList, CAP_PERMITTED);
cw.allowCaps(capList, CAP_EFFECTIVE);
cw.enableCaps();
}