Skip to content

Avoid leaking --app-fd, etc. down to wrapped command - #6589

Merged
smcv merged 2 commits into
flatpak:mainfrom
smcv:avoid-leaking-fds
Apr 10, 2026
Merged

smcv merged 2 commits into
flatpak:mainfrom
smcv:avoid-leaking-fds

Conversation

@smcv

@smcv smcv commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator
  • utils: Add flatpak_set_cloexec()

    Helps: [Bug]: Chromium-based browsers (including Brave) crash with 1.16.5 #6582

  • run, context: Mark fd arguments as close-on-exec

    On entry to flatpak run, these fds have been inheritable (not
    FD_CLOEXEC), otherwise they would not have been inherited; but we don't
    want the "payload" command to inherit them, so set them as
    non-close-on-exec as soon as we receive them. In the cases where we pass
    them down to the underlying bwrap command, we'll either dup them, or
    set them to be inheritable again (in practice we dup them).

    In particular, Chromium-derived web browsers get very upset when their
    subsandbox processes inherit unexpected fds, which has been causing crashes
    with no useful diagnostic information since CVE-2026-34078 was fixed.

    Fixes: 1b5e886 "run: Add --usr-fd and --app-fd options"
    Fixes: b5ae89e "run: Add --(ro-)bind-fd options"
    Resolves: [Bug]: Chromium-based browsers (including Brave) crash with 1.16.5 #6582


This works for me with at least Ungoogled Chromium. Tests with other Chromium-derived browsers welcome. Ideally we'd have an automated test for this, but I thought it would be better to get a potential solution available first.

With many thanks to @swick for figuring out why Chromium subprocesses were crashing!

smcv added 2 commits April 10, 2026 09:58
Helps: flatpak#6582
Signed-off-by: Simon McVittie <[email protected]>
On entry to `flatpak run`, these fds have been inheritable (not
FD_CLOEXEC), otherwise they would not have been inherited; but we don't
want the "payload" command to inherit them, so set them as
non-close-on-exec as soon as we receive them. In the cases where we pass
them down to the underlying bwrap command, we'll either dup them, or
set them to be inheritable again (in practice we dup them).

In particular, Chromium-derived web browsers get very upset when their
subsandbox processes inherit unexpected fds, which has been causing crashes
with no useful diagnostic information since CVE-2026-34078 was fixed.

Fixes: 1b5e886 "run: Add --usr-fd and --app-fd options"
Fixes: b5ae89e "run: Add --(ro-)bind-fd options"
Resolves: flatpak#6582
Signed-off-by: Simon McVittie <[email protected]>
@smcv
smcv requested review from alexlarsson, bbhtt and swick April 10, 2026 09:13
@smcv

smcv commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator Author

Also works for org.chromium.Chromium and com.brave.Browser.

com.vivaldi.Vivaldi was briefly crashing with Settings schema 'org.gnome.shell' is not installed for me, but I can't see how that could be related, and it seems to work now... so probably that was something unrelated? 🤷 I don't normally use any of these browsers, so I'm not familiar with how they should be expected to behave.

@smcv

smcv commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator Author

Or maybe flatpak_parse_fd() should be responsible for setting the fd CLOEXEC? That would centralize this further.

@smcv

smcv commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator Author

Or maybe flatpak_parse_fd() should be responsible for setting the fd CLOEXEC? That would centralize this further.

... but that does seem out-of-scope for its name, and we also definitely don't want to be making stdin, stdout or stderr be CLOEXEC.

Comment on lines 82 to 83
if (fd < 3)
return glnx_throw (error, "File descriptors 0, 1, 2 are reserved");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit unrelated, but I noticed that the autofd will close fd 0, 1, 2 here which isn't the intent; maybe worth fixing in this PR as well

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I do that as a followup? I'd like to keep the regression fix relatively minimal/simple, and I'm less concerned about misbehaviour when invoking Flatpak in ways that don't make sense anyway.

@swick

swick commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator

... but that does seem out-of-scope for its name, and we also definitely don't want to be making stdin, stdout or stderr be CLOEXEC.

Yeah, this PR is good for now. As a follow up we could create another function which does the check for <=3 and sets cloexec.

@swick

swick commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator

This took way too long to figure out. I really think we should try to test for this.

@smcv

smcv commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator Author

This took way too long to figure out. I really think we should try to test for this.

Yeah, I have some ideas. I'd like to get the actual regression fix merged so we can stop the bleeding, but I can work on a test (and the other followups mentioned above) today.

@swick

swick commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator
exec 3< custom-app/files
exec 4< custom-runtime/files
exec 5< "${path}"
exec 6< "${path}"
run --app-fd=3 --usr-fd=4 --bind-fd=5 --ro-bind-fd=6 \
    --command=sh org.test.Hello \
    -c 'for fd in $(ls /proc/self/fd); do readlink -f /proc/self/fd/$fd; done' > hello_out
exec 6>&-
exec 5>&-
exec 4>&-
exec 3>&-

wd="$(readlink -f .)"
while read fdpath; do
  if [[ "$fdpath" == "$wd"* && "$fdpath" != "$wd/hello_out" ]]; then
    assert_not_reached "A fd for '$fdpath' unexpectedly made it to the app"
  fi
done < hello_out

ok "check no fd leak"

Came up with this, but every now and then it seems to succeed when it should not (but never the other way around at least).

@smcv
smcv added this pull request to the merge queue Apr 10, 2026
Merged via the queue into flatpak:main with commit 0902090 Apr 10, 2026
11 checks passed
@smcv
smcv deleted the avoid-leaking-fds branch April 10, 2026 12:36
algitbot pushed a commit to alpinelinux/aports that referenced this pull request Apr 10, 2026
Backport upstream github PR #6589 (commits 79d3e09, e0cd581) which fixes
file descriptor leakage to sandboxed child processes.

The CVE-2026-34078 fix in 1.16.4 inadvertently caused --app-fd,
--usr-fd, --bind-fd, and --ro-bind-fd to lose their FD_CLOEXEC flag,
resulting in unexpected fd inheritance by subsandbox processes.
Chromium-based browsers crash on every launch after the first run
due to this.

The fix sets close-on-exec on these fds immediately upon receipt.

Upstream: flatpak/flatpak#6589
Fixes: flatpak/flatpak#6582
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Chromium-based browsers (including Brave) crash with 1.16.5

2 participants