Skip to content

Commit ee7bedc

Browse files
committed
samples/landlock: Add FS quiet flag support to sandboxer
Signed-off-by: Tingmao Wang <[email protected]>
1 parent 3b3e547 commit ee7bedc

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

samples/landlock/sandboxer.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static inline int landlock_restrict_self(const int ruleset_fd,
5858

5959
#define ENV_FS_RO_NAME "LL_FS_RO"
6060
#define ENV_FS_RW_NAME "LL_FS_RW"
61+
#define ENV_FS_QUIET_NAME "LL_FS_QUIET"
6162
#define ENV_TCP_BIND_NAME "LL_TCP_BIND"
6263
#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
6364
#define ENV_SCOPED_NAME "LL_SCOPED"
@@ -116,7 +117,7 @@ static int parse_path(char *env_path, const char ***const path_list)
116117
/* clang-format on */
117118

118119
static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
119-
const __u64 allowed_access)
120+
const __u64 allowed_access, bool quiet)
120121
{
121122
int num_paths, i, ret = 1;
122123
char *env_path_name;
@@ -166,7 +167,8 @@ static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
166167
if (!S_ISDIR(statbuf.st_mode))
167168
path_beneath.allowed_access &= ACCESS_FILE;
168169
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
169-
&path_beneath, 0)) {
170+
&path_beneath,
171+
quiet ? LANDLOCK_ADD_RULE_QUIET : 0)) {
170172
fprintf(stderr,
171173
"Failed to update the ruleset with \"%s\": %s\n",
172174
path_list[i], strerror(errno));
@@ -328,6 +330,7 @@ static const char help[] =
328330
"\n"
329331
"A sandboxer should not log denied access requests to avoid spamming logs, "
330332
"but to test audit we can set " ENV_FORCE_LOG_NAME "=1\n"
333+
ENV_FS_QUIET_NAME " can then be used to make access to some denied paths not trigger audit logging.\n"
331334
"\n"
332335
"Example:\n"
333336
ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
@@ -497,12 +500,21 @@ int main(const int argc, char *const argv[], char *const *const envp)
497500
return 1;
498501
}
499502

500-
if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
503+
if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro,
504+
false)) {
501505
goto err_close_ruleset;
502506
}
503-
if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
507+
if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw,
508+
false)) {
504509
goto err_close_ruleset;
505510
}
511+
/* Don't require this env to be present */
512+
if (getenv(ENV_FS_QUIET_NAME)) {
513+
if (populate_ruleset_fs(ENV_FS_QUIET_NAME, ruleset_fd, 0,
514+
true)) {
515+
goto err_close_ruleset;
516+
}
517+
}
506518

507519
if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
508520
LANDLOCK_ACCESS_NET_BIND_TCP)) {

0 commit comments

Comments
 (0)