Skip to content
API Documentation
BastionGuard™
anonymous_namespace{ArchiveSandbox.cpp} Namespace Reference

Functions

bool drain_fd_nonblocking (int fd, std::string &out, bool &got_eof, bool &truncated)
bool set_nonblocking (int fd)
void set_worker_rlimits ()
bool json_has_true (const std::string &jsonjson, const std::string &key)
int json_read_int (const std::string &jsonjson, const std::string &key, int fallback=0)
std::string json_read_string (const std::string &jsonjson, const std::string &key)

Function Documentation

◆ drain_fd_nonblocking()

bool anonymous_namespace{ArchiveSandbox.cpp}::drain_fd_nonblocking ( int fd,
std::string & out,
bool & got_eof,
bool & truncated )

Definition at line 50 of file ArchiveSandbox.cpp.

50 {
51 constexpr std::size_t max_output_bytes = 1024 * 1024;
52 got_eof = false;
53 std::array<char, 4096> buf{};
54 while (true) {
55 const ssize_t n = ::read(fd, buf.data(), buf.size());
56 if (n == 0) {
57 got_eof = true;
58 return true;
59 }
60 if (n < 0) {
61 if (errno == EINTR) continue;
62 if (errno == EAGAIN || errno == EWOULDBLOCK) return true;
63 return false;
64 }
65 const auto length = static_cast<std::size_t>(n);
66 if (out.size() < max_output_bytes) {
67 const auto copy = std::min(length, max_output_bytes - out.size());
68 out.append(buf.data(), copy);
69 if (copy != length) truncated = true;
70 } else {
71 truncated = true;
72 }
73 }
74}

References drain_fd_nonblocking().

Referenced by drain_fd_nonblocking(), and run_archive_worker_bwrap().

◆ json_has_true()

bool anonymous_namespace{ArchiveSandbox.cpp}::json_has_true ( const std::string & json,
const std::string & key )

Definition at line 104 of file ArchiveSandbox.cpp.

104 {
105 const std::string pat = "\"" + key + "\":true";
106 return json.find(pat) != std::string::npos;
107}
nlohmann::json json
Definition Backend.cpp:2908

References json_has_true().

Referenced by analyze_archive_in_sandbox(), and json_has_true().

◆ json_read_int()

int anonymous_namespace{ArchiveSandbox.cpp}::json_read_int ( const std::string & json,
const std::string & key,
int fallback = 0 )

Definition at line 109 of file ArchiveSandbox.cpp.

109 {
110 const std::string pat = "\"" + key + "\":";
111 std::size_t pos = json.find(pat);
112 if (pos == std::string::npos) {
113 return fallback;
114 }
115
116 pos += pat.size();
117 while (pos < json.size() && std::isspace(static_cast<unsigned char>(json[pos]))) {
118 ++pos;
119 }
120
121 bool neg = false;
122 if (pos < json.size() && json[pos] == '-') {
123 neg = true;
124 ++pos;
125 }
126
127 int value = 0;
128 bool seen = false;
129 while (pos < json.size() && std::isdigit(static_cast<unsigned char>(json[pos]))) {
130 seen = true;
131 value = value * 10 + (json[pos] - '0');
132 ++pos;
133 }
134
135 if (!seen) {
136 return fallback;
137 }
138
139 return neg ? -value : value;
140}

References json_read_int().

Referenced by analyze_archive_in_sandbox(), and json_read_int().

◆ json_read_string()

std::string anonymous_namespace{ArchiveSandbox.cpp}::json_read_string ( const std::string & json,
const std::string & key )

Definition at line 142 of file ArchiveSandbox.cpp.

142 {
143 const std::string pat = "\"" + key + "\":\"";
144 std::size_t pos = json.find(pat);
145 if (pos == std::string::npos) {
146 return {};
147 }
148
149 pos += pat.size();
150
151 std::string out;
152 while (pos < json.size()) {
153 char c = json[pos++];
154 if (c == '\\') {
155 if (pos < json.size()) {
156 out.push_back(json[pos++]);
157 }
158 continue;
159 }
160 if (c == '"') {
161 break;
162 }
163 out.push_back(c);
164 }
165
166 return out;
167}

References json_read_string().

Referenced by analyze_archive_in_sandbox(), and json_read_string().

◆ set_nonblocking()

bool anonymous_namespace{ArchiveSandbox.cpp}::set_nonblocking ( int fd)

Definition at line 76 of file ArchiveSandbox.cpp.

76 {
77 const int flags = ::fcntl(fd, F_GETFL, 0);
78 if (flags < 0) return false;
79 return ::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0;
80}

References set_nonblocking().

Referenced by run_archive_worker_bwrap(), and set_nonblocking().

◆ set_worker_rlimits()

void anonymous_namespace{ArchiveSandbox.cpp}::set_worker_rlimits ( )

Definition at line 82 of file ArchiveSandbox.cpp.

82 {
83 struct rlimit cpu_lim {};
84 cpu_lim.rlim_cur = 10;
85 cpu_lim.rlim_max = 10;
86 ::setrlimit(RLIMIT_CPU, &cpu_lim);
87
88 struct rlimit as_lim {};
89 as_lim.rlim_cur = 512ULL * 1024ULL * 1024ULL;
90 as_lim.rlim_max = 512ULL * 1024ULL * 1024ULL;
91 ::setrlimit(RLIMIT_AS, &as_lim);
92
93 struct rlimit fsize_lim {};
94 fsize_lim.rlim_cur = 128ULL * 1024ULL * 1024ULL;
95 fsize_lim.rlim_max = 128ULL * 1024ULL * 1024ULL;
96 ::setrlimit(RLIMIT_FSIZE, &fsize_lim);
97
98 struct rlimit nofile_lim {};
99 nofile_lim.rlim_cur = 64;
100 nofile_lim.rlim_max = 64;
101 ::setrlimit(RLIMIT_NOFILE, &nofile_lim);
102}

References set_worker_rlimits().

Referenced by run_archive_worker_bwrap(), and set_worker_rlimits().