Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always allow app to inherit redirected fds from flatpak-run(1) #5626

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Always allow app to inherit redirected fds from flatpak-run(1)
As noticed on #5615, under normal circumstances, flatpak-run(1)
replaces itself with the bwrap process via execve(), and does not
close any fds that it might have inherited from its parent. This
allows for patterns like:

    flatpak run com.example.App --in-fd=3 --out-fd=5 3<foo 5>bar

However, using execve() is annoying when trying to analyze code
coverage, because the coverage instrumentation does not get the
opportunity to write out its data during exit, so it is possible to
set FLATPAK_TEST_COVERAGE=1 to make flatpak run the app as a child
process and wait for it. This puts us on the code path normally used
for apps launched in the background by flatpak_installation_launch_full(),
which *don't* inherit arbitrary fds from their parent.

Detect this situation and use a different child setup function,
avoiding closing fds that we were meant to inherit.

Fixes: 88a928e "run: Avoid execve() when measuring test coverage"
Signed-off-by: Simon McVittie <[email protected]>
  • Loading branch information
smcv committed Feb 13, 2024
commit 43e033e45a2fcf12d2852b0c6ca5d36f42549d82
1 change: 1 addition & 0 deletions common/flatpak-bwrap-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void flatpak_bwrap_populate_runtime_dir (FlatpakBwrap *bwrap,
const char *shared_xdg_runtime_dir);

void flatpak_bwrap_child_setup_cb (gpointer user_data);
void flatpak_bwrap_child_setup_inherit_fds_cb (gpointer user_data);
void flatpak_bwrap_child_setup (GArray *fd_array,
gboolean close_fd_workaround);

Expand Down
10 changes: 10 additions & 0 deletions common/flatpak-bwrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,16 @@ flatpak_bwrap_child_setup_cb (gpointer user_data)
flatpak_bwrap_child_setup (fd_array, TRUE);
}

/* Unset FD_CLOEXEC on the array of fds passed in @user_data,
* but do not set FD_CLOEXEC on all other fds */
void
flatpak_bwrap_child_setup_inherit_fds_cb (gpointer user_data)
{
GArray *fd_array = user_data;

flatpak_bwrap_child_setup (fd_array, FALSE);
}

/* Add a --sync-fd argument for bwrap(1). Returns the write end of the pipe on
* success, or -1 on error. */
int
Expand Down
8 changes: 7 additions & 1 deletion common/flatpak-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -3441,12 +3441,18 @@ flatpak_run_app (FlatpakDecomposed *app_ref,
char pid_str[64];
g_autofree char *pid_path = NULL;
GSpawnFlags spawn_flags;
GSpawnChildSetupFunc child_setup;

spawn_flags = G_SPAWN_SEARCH_PATH;
if (flags & FLATPAK_RUN_FLAG_DO_NOT_REAP ||
(flags & FLATPAK_RUN_FLAG_BACKGROUND) == 0)
spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;

if (flags & FLATPAK_RUN_FLAG_BACKGROUND)
child_setup = flatpak_bwrap_child_setup_cb;
else
child_setup = flatpak_bwrap_child_setup_inherit_fds_cb;

/* We use LEAVE_DESCRIPTORS_OPEN to work around dead-lock, see flatpak_close_fds_workaround */
spawn_flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN;

Expand All @@ -3460,7 +3466,7 @@ flatpak_run_app (FlatpakDecomposed *app_ref,
(char **) bwrap->argv->pdata,
bwrap->envp,
spawn_flags,
flatpak_bwrap_child_setup_cb, bwrap->fds,
child_setup, bwrap->fds,
&child_pid,
error))
return FALSE;
Expand Down