Skip to content

Commit

Permalink
parse: Reject -{exec,ok}dir if $PATH contains a relative path
Browse files Browse the repository at this point in the history
This matches the behaviour of GNU find.
  • Loading branch information
tavianator committed Nov 7, 2023
1 parent ce90dc9 commit 163baf1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,28 @@ static struct bfs_expr *parse_exec(struct parser_state *state, int flags, int ar
expr->ephemeral_fds = 2;

if (execbuf->flags & BFS_EXEC_CHDIR) {
// Check for relative paths in $PATH
const char *path = getenv("PATH");
while (path) {
if (*path != '/') {
size_t len = strcspn(path, ":");
char *comp = strndup(path, len);
if (comp) {
parse_expr_error(state, expr,
"This action would be unsafe, since ${bld}$$PATH${rs} contains the relative path ${bld}%pq${rs}\n", comp);
free(comp);
} else {
parse_perror(state, "strndup()");
}
goto fail;
}

path = strchr(path, ':');
if (path) {
++path;
}
}

// To dup() the parent directory
if (execbuf->flags & BFS_EXEC_MULTI) {
++expr->persistent_fds;
Expand All @@ -1276,6 +1298,10 @@ static struct bfs_expr *parse_exec(struct parser_state *state, int flags, int ar
}

return expr;

fail:
bfs_expr_free(expr);
return NULL;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/gnu/execdir_path_dot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
! PATH=".:$PATH" invoke_bfs basic -execdir echo {} +
1 change: 1 addition & 0 deletions tests/gnu/execdir_path_empty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
! PATH=":$PATH" invoke_bfs basic -execdir echo {} +
1 change: 1 addition & 0 deletions tests/gnu/execdir_path_relative.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
! PATH="foo:$PATH" invoke_bfs basic -execdir echo {} +
1 change: 1 addition & 0 deletions tests/gnu/okdir_path_dot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
! PATH=".:$PATH" invoke_bfs basic -okdir echo {} \;
1 change: 1 addition & 0 deletions tests/gnu/okdir_path_empty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
! PATH=":$PATH" invoke_bfs basic -okdir echo {} \;
1 change: 1 addition & 0 deletions tests/gnu/okdir_path_relative.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
! PATH="foo:$PATH" invoke_bfs basic -okdir echo {} \;

0 comments on commit 163baf1

Please sign in to comment.