Skip to content

Commit ec2798d

Browse files
committed
samples/landlock: Enable users to log sandbox denials
By default, denials from within the sandbox are not logged. Indeed, the sandboxer's security policy might not be fitted to the set of sandboxed processes that could be spawned (e.g. from a shell). For test purpose, parse the LL_FORCE_LOG environment variable to log every sandbox denials, including after launching the initial sandboxed program thanks to LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON. Cc: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20250320190717.2287696-20-mic@digikod.net [mic: Remove inappropriate hunk] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent ead9079 commit ec2798d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

samples/landlock/sandboxer.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static inline int landlock_restrict_self(const int ruleset_fd,
5858
#define ENV_TCP_BIND_NAME "LL_TCP_BIND"
5959
#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
6060
#define ENV_SCOPED_NAME "LL_SCOPED"
61+
#define ENV_FORCE_LOG_NAME "LL_FORCE_LOG"
6162
#define ENV_DELIMITER ":"
6263

6364
static int str2num(const char *numstr, __u64 *num_dst)
@@ -295,7 +296,7 @@ static bool check_ruleset_scope(const char *const env_var,
295296

296297
/* clang-format on */
297298

298-
#define LANDLOCK_ABI_LAST 6
299+
#define LANDLOCK_ABI_LAST 7
299300

300301
#define XSTR(s) #s
301302
#define STR(s) XSTR(s)
@@ -322,6 +323,9 @@ static const char help[] =
322323
" - \"a\" to restrict opening abstract unix sockets\n"
323324
" - \"s\" to restrict sending signals\n"
324325
"\n"
326+
"A sandboxer should not log denied access requests to avoid spamming logs, "
327+
"but to test audit we can set " ENV_FORCE_LOG_NAME "=1\n"
328+
"\n"
325329
"Example:\n"
326330
ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
327331
ENV_FS_RW_NAME "=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
@@ -340,7 +344,7 @@ int main(const int argc, char *const argv[], char *const *const envp)
340344
const char *cmd_path;
341345
char *const *cmd_argv;
342346
int ruleset_fd, abi;
343-
char *env_port_name;
347+
char *env_port_name, *env_force_log;
344348
__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
345349
access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
346350

@@ -351,6 +355,8 @@ int main(const int argc, char *const argv[], char *const *const envp)
351355
.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
352356
LANDLOCK_SCOPE_SIGNAL,
353357
};
358+
int supported_restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
359+
int set_restrict_flags = 0;
354360

355361
if (argc < 2) {
356362
fprintf(stderr, help, argv[0]);
@@ -422,6 +428,13 @@ int main(const int argc, char *const argv[], char *const *const envp)
422428
/* Removes LANDLOCK_SCOPE_* for ABI < 6 */
423429
ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
424430
LANDLOCK_SCOPE_SIGNAL);
431+
__attribute__((fallthrough));
432+
case 6:
433+
/* Removes LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON for ABI < 7 */
434+
supported_restrict_flags &=
435+
~LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
436+
437+
/* Must be printed for any ABI < LANDLOCK_ABI_LAST. */
425438
fprintf(stderr,
426439
"Hint: You should update the running kernel "
427440
"to leverage Landlock features "
@@ -456,6 +469,24 @@ int main(const int argc, char *const argv[], char *const *const envp)
456469
if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
457470
return 1;
458471

472+
/* Enables optional logs. */
473+
env_force_log = getenv(ENV_FORCE_LOG_NAME);
474+
if (env_force_log) {
475+
if (strcmp(env_force_log, "1") != 0) {
476+
fprintf(stderr, "Unknown value for " ENV_FORCE_LOG_NAME
477+
" (only \"1\" is handled)\n");
478+
return 1;
479+
}
480+
if (!(supported_restrict_flags &
481+
LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON)) {
482+
fprintf(stderr,
483+
"Audit logs not supported by current kernel\n");
484+
return 1;
485+
}
486+
set_restrict_flags |= LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
487+
unsetenv(ENV_FORCE_LOG_NAME);
488+
}
489+
459490
ruleset_fd =
460491
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
461492
if (ruleset_fd < 0) {
@@ -483,7 +514,7 @@ int main(const int argc, char *const argv[], char *const *const envp)
483514
perror("Failed to restrict privileges");
484515
goto err_close_ruleset;
485516
}
486-
if (landlock_restrict_self(ruleset_fd, 0)) {
517+
if (landlock_restrict_self(ruleset_fd, set_restrict_flags)) {
487518
perror("Failed to enforce ruleset");
488519
goto err_close_ruleset;
489520
}

0 commit comments

Comments
 (0)