Skip to content

Commit

Permalink
landlock: expand simple macros in commands
Browse files Browse the repository at this point in the history
This includes macros such as `${HOME}` and `${RUNUSER}`, but not
`${PATH}`, which may expand to multiple strings.

Relates to #6078.
  • Loading branch information
kmk3 committed Dec 12, 2023
1 parent d44be8e commit 19e1082
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/firejail/landlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ static int ll_create_full_ruleset(void) {
return ruleset_fd;
}

static int ll_fs(const char *allowed_path, const __u64 allowed_access,
const char *caller) {
static int _ll_fs(const char *allowed_path, const __u64 allowed_access,
const char *caller) {
if (!ll_is_supported())
return 0;

Expand Down Expand Up @@ -155,6 +155,16 @@ static int ll_fs(const char *allowed_path, const __u64 allowed_access,
return error;
}

// TODO: Add support for the ${PATH} macro.
static int ll_fs(const char *allowed_path, const __u64 allowed_access,
const char *caller) {
char *expanded_path = expand_macros(allowed_path);
int error = _ll_fs(expanded_path, allowed_access, caller);

free(expanded_path);
return error;
}

int ll_read(const char *allowed_path) {
__u64 allowed_access =
LANDLOCK_ACCESS_FS_READ_DIR |
Expand Down Expand Up @@ -193,28 +203,21 @@ int ll_exec(const char *allowed_path) {
}

int ll_basic_system(void) {
assert(cfg.homedir);

if (!ll_is_supported())
return 0;

if (ll_ruleset_fd == -1)
ll_ruleset_fd = ll_create_full_ruleset();

int error;
char *rundir;
if (asprintf(&rundir, "/run/user/%d", getuid()) == -1)
errExit("asprintf");

error =
int error =
ll_read("/") || // whole system read
ll_special("/") || // sockets etc.

ll_write("/tmp") || // write access
ll_write("/dev") ||
ll_write("/run/shm") ||
ll_write(cfg.homedir) ||
ll_write(rundir) ||
ll_write("${HOME}") ||
ll_write("${RUNUSER}") ||

ll_exec("/opt") || // exec access
ll_exec("/bin") ||
Expand All @@ -240,7 +243,7 @@ int ll_basic_system(void) {
fprintf(stderr, "Error: %s: failed to set --landlock rules\n",
__func__);
}
free(rundir);

return error;
}

Expand Down

0 comments on commit 19e1082

Please sign in to comment.