@@ -18,29 +18,38 @@ namespace ml {
18
18
namespace seccomp {
19
19
20
20
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.
23
26
// (allow file-write*) is required for mkfifo and that permission
24
27
// 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{" \
26
32
(version 1) \
27
33
(deny default) \
34
+ (allow signal (target self)) \
35
+ (allow system-sched (target self)) \
28
36
(allow file-read*) \
29
37
(allow file-read-data) \
30
38
(allow file-write*) \
31
- (allow file-write-data)" );
39
+ (allow file-write-data) \
40
+ (debug deny)" };
32
41
33
42
// 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" } ;
35
44
// The length of the suffix '.sb'
36
- const int FILE_NAME_TEMPLATE_SUFFIX_LEN = 3 ;
45
+ const int FILE_NAME_TEMPLATE_SUFFIX_LEN{ 3 } ;
37
46
38
47
std::string getTempDir () {
39
48
// Prefer to use the temporary directory set by the Elasticsearch JVM
40
- const char * tmpDir ( ::getenv (" TMPDIR" )) ;
49
+ const char * tmpDir{ ::getenv (" TMPDIR" )} ;
41
50
42
51
// 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} ;
44
53
// Make sure path ends with a slash so it's ready to have a file name appended
45
54
if (path[path.length () - 1 ] != ' /' ) {
46
55
path += ' /' ;
@@ -49,37 +58,37 @@ std::string getTempDir() {
49
58
}
50
59
51
60
std::string writeTempRulesFile () {
52
- std::string profileFilename = getTempDir () + FILE_NAME_TEMPLATE;
61
+ std::string profileFilename{ getTempDir () + FILE_NAME_TEMPLATE} ;
53
62
54
63
// Create and open a temporary file with a random name
55
64
// 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)} ;
57
66
if (fd == -1 ) {
58
67
LOG_ERROR (<< " Opening a temporary file with mkstemps failed: "
59
68
<< std::strerror (errno));
60
69
return std::string ();
61
70
}
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);
64
73
65
74
return profileFilename;
66
75
}
67
76
}
68
77
69
78
void CSystemCallFilter::installSystemCallFilter () {
70
- std::string profileFilename = writeTempRulesFile ();
79
+ std::string profileFilename{ writeTempRulesFile ()} ;
71
80
if (profileFilename.empty ()) {
72
81
LOG_WARN (<< " Cannot write sandbox rules. macOS sandbox will not be initialized" );
73
82
return ;
74
83
}
75
84
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 ) {
78
87
std::string msg (" Error initializing macOS sandbox" );
79
88
if (errorbuf != nullptr ) {
80
89
msg += " : " ;
81
90
msg += errorbuf;
82
- sandbox_free_error (errorbuf);
91
+ :: sandbox_free_error (errorbuf);
83
92
}
84
93
LOG_ERROR (<< msg);
85
94
} else {
0 commit comments