Skip to content

Commit 4abacd7

Browse files
authored
[7.x][ML] Fix macOS sandbox rules (elastic#1147)
Two rules were missing: 1. We were blocking our own SIGIO signals used to wake up blocking file reads. (This is quite nasty as this functionality is only used in edge cases.) 2. We were blocking the setpriority() calls made by nice(). I added the (debug deny) rule so that any blocked calls get logged to the system console. This makes it easier to see which rules are required but missing. I also changed the code style to match what our current style guide says. Backport of elastic#1146
1 parent 3c20a45 commit 4abacd7

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

lib/seccomp/CSystemCallFilter_MacOSX.cc

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,38 @@ namespace ml {
1818
namespace seccomp {
1919

2020
namespace {
21-
// The Sandbox rules deny all actions apart from creating fifos,
22-
// opening files, reading and writing.
21+
// The Sandbox rules deny all actions not explicitly listed.
22+
// (allow signal (target self)) is required for the SIGIO used
23+
// to wake up blocking reads.
24+
// (allow system-sched (target self)) is required for "nice".
25+
// (allow file-read*) is required for reading config files.
2326
// (allow file-write*) is required for mkfifo and that permission
2427
// can not be set using the more granular controls.
25-
const std::string SANDBOX_RULES("\
28+
// (debug deny) makes it easier to see which calls need adding
29+
// when one that is required is not in the list - they show up in
30+
// the macOS console.
31+
const std::string SANDBOX_RULES{"\
2632
(version 1) \
2733
(deny default) \
34+
(allow signal (target self)) \
35+
(allow system-sched (target self)) \
2836
(allow file-read*) \
2937
(allow file-read-data) \
3038
(allow file-write*) \
31-
(allow file-write-data)");
39+
(allow file-write-data) \
40+
(debug deny)"};
3241

3342
// mkstemps will replace the Xs with random characters
34-
const std::string FILE_NAME_TEMPLATE("ml.XXXXXX.sb");
43+
const std::string FILE_NAME_TEMPLATE{"ml.XXXXXX.sb"};
3544
// The length of the suffix '.sb'
36-
const int FILE_NAME_TEMPLATE_SUFFIX_LEN = 3;
45+
const int FILE_NAME_TEMPLATE_SUFFIX_LEN{3};
3746

3847
std::string getTempDir() {
3948
// Prefer to use the temporary directory set by the Elasticsearch JVM
40-
const char* tmpDir(::getenv("TMPDIR"));
49+
const char* tmpDir{::getenv("TMPDIR")};
4150

4251
// If TMPDIR is not set use _PATH_VARTMP
43-
std::string path((tmpDir == nullptr) ? _PATH_VARTMP : tmpDir);
52+
std::string path{(tmpDir == nullptr) ? _PATH_VARTMP : tmpDir};
4453
// Make sure path ends with a slash so it's ready to have a file name appended
4554
if (path[path.length() - 1] != '/') {
4655
path += '/';
@@ -49,37 +58,37 @@ std::string getTempDir() {
4958
}
5059

5160
std::string writeTempRulesFile() {
52-
std::string profileFilename = getTempDir() + FILE_NAME_TEMPLATE;
61+
std::string profileFilename{getTempDir() + FILE_NAME_TEMPLATE};
5362

5463
// Create and open a temporary file with a random name
5564
// profileFilename is updated with the new filename.
56-
int fd = mkstemps(&profileFilename[0], FILE_NAME_TEMPLATE_SUFFIX_LEN);
65+
int fd{::mkstemps(&profileFilename[0], FILE_NAME_TEMPLATE_SUFFIX_LEN)};
5766
if (fd == -1) {
5867
LOG_ERROR(<< "Opening a temporary file with mkstemps failed: "
5968
<< std::strerror(errno));
6069
return std::string();
6170
}
62-
write(fd, SANDBOX_RULES.c_str(), SANDBOX_RULES.size());
63-
close(fd);
71+
::write(fd, SANDBOX_RULES.c_str(), SANDBOX_RULES.size());
72+
::close(fd);
6473

6574
return profileFilename;
6675
}
6776
}
6877

6978
void CSystemCallFilter::installSystemCallFilter() {
70-
std::string profileFilename = writeTempRulesFile();
79+
std::string profileFilename{writeTempRulesFile()};
7180
if (profileFilename.empty()) {
7281
LOG_WARN(<< "Cannot write sandbox rules. macOS sandbox will not be initialized");
7382
return;
7483
}
7584

76-
char* errorbuf = nullptr;
77-
if (sandbox_init(profileFilename.c_str(), SANDBOX_NAMED, &errorbuf) != 0) {
85+
char* errorbuf{nullptr};
86+
if (::sandbox_init(profileFilename.c_str(), SANDBOX_NAMED, &errorbuf) != 0) {
7887
std::string msg("Error initializing macOS sandbox");
7988
if (errorbuf != nullptr) {
8089
msg += ": ";
8190
msg += errorbuf;
82-
sandbox_free_error(errorbuf);
91+
::sandbox_free_error(errorbuf);
8392
}
8493
LOG_ERROR(<< msg);
8594
} else {

0 commit comments

Comments
 (0)