@@ -58,6 +58,7 @@ static inline int landlock_restrict_self(const int ruleset_fd,
58
58
#define ENV_TCP_BIND_NAME "LL_TCP_BIND"
59
59
#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
60
60
#define ENV_SCOPED_NAME "LL_SCOPED"
61
+ #define ENV_FORCE_LOG_NAME "LL_FORCE_LOG"
61
62
#define ENV_DELIMITER ":"
62
63
63
64
static int str2num (const char * numstr , __u64 * num_dst )
@@ -295,7 +296,7 @@ static bool check_ruleset_scope(const char *const env_var,
295
296
296
297
/* clang-format on */
297
298
298
- #define LANDLOCK_ABI_LAST 6
299
+ #define LANDLOCK_ABI_LAST 7
299
300
300
301
#define XSTR (s ) #s
301
302
#define STR (s ) XSTR(s)
@@ -322,6 +323,9 @@ static const char help[] =
322
323
" - \"a\" to restrict opening abstract unix sockets\n"
323
324
" - \"s\" to restrict sending signals\n"
324
325
"\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"
325
329
"Example:\n"
326
330
ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
327
331
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)
340
344
const char * cmd_path ;
341
345
char * const * cmd_argv ;
342
346
int ruleset_fd , abi ;
343
- char * env_port_name ;
347
+ char * env_port_name , * env_force_log ;
344
348
__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ ,
345
349
access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE ;
346
350
@@ -351,6 +355,8 @@ int main(const int argc, char *const argv[], char *const *const envp)
351
355
.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
352
356
LANDLOCK_SCOPE_SIGNAL ,
353
357
};
358
+ int supported_restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON ;
359
+ int set_restrict_flags = 0 ;
354
360
355
361
if (argc < 2 ) {
356
362
fprintf (stderr , help , argv [0 ]);
@@ -422,6 +428,13 @@ int main(const int argc, char *const argv[], char *const *const envp)
422
428
/* Removes LANDLOCK_SCOPE_* for ABI < 6 */
423
429
ruleset_attr .scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
424
430
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. */
425
438
fprintf (stderr ,
426
439
"Hint: You should update the running kernel "
427
440
"to leverage Landlock features "
@@ -456,6 +469,24 @@ int main(const int argc, char *const argv[], char *const *const envp)
456
469
if (check_ruleset_scope (ENV_SCOPED_NAME , & ruleset_attr ))
457
470
return 1 ;
458
471
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
+
459
490
ruleset_fd =
460
491
landlock_create_ruleset (& ruleset_attr , sizeof (ruleset_attr ), 0 );
461
492
if (ruleset_fd < 0 ) {
@@ -483,7 +514,7 @@ int main(const int argc, char *const argv[], char *const *const envp)
483
514
perror ("Failed to restrict privileges" );
484
515
goto err_close_ruleset ;
485
516
}
486
- if (landlock_restrict_self (ruleset_fd , 0 )) {
517
+ if (landlock_restrict_self (ruleset_fd , set_restrict_flags )) {
487
518
perror ("Failed to enforce ruleset" );
488
519
goto err_close_ruleset ;
489
520
}
0 commit comments