Skip to content

portal: Fix --env-fd regressions - #4283

Merged
alexlarsson merged 8 commits into
flatpak:masterfrom
smcv:portal-env-fd-leak
May 25, 2021
Merged

alexlarsson merged 8 commits into
flatpak:masterfrom
smcv:portal-env-fd-leak

Conversation

@smcv

@smcv smcv commented May 20, 2021 •

Copy link
Copy Markdown
Collaborator
  • portal: Don't leak fd used for serialized environment

    Otherwise we'll run out of file descriptors eventually, when starting
    a sufficiently large number of subsandboxes.

    Resolves: portal: --env-fd is leaked #4285
    Fixes: aeb6a7a "portal: Convert --env in extra-args into --env-fd"

  • portal: Use a GArray to store fds

    This will allow us to add additional mapping entries for fds to be
    used internally by flatpak run, in particular --env-fd.

    Defer the second pass through the fd array until the last possible
    moment, so that any extra fds we want to add (like the --env-fd) have
    already been added by then.

    Helps: portal: --env-fd can collide with fds passed across D-Bus #4286

  • portal: Remap --env-fd into child process's fd space

    Just because we can allocate a new, unused fd in the portal's fd space,
    that doesn't mean that fd number is going to be unused in the child
    process's fd space: we might need to remap it.

    Resolves: portal: --env-fd can collide with fds passed across D-Bus #4286
    Fixes: aeb6a7a "portal: Convert --env in extra-args into --env-fd"

  • context: Factor out functions to parse an environment block

    This makes them easier to test, and easier to use in related tests.

  • tests: Test environment block parsing

  • portal: Add NONE values for flags sets

  • portal: Add some design-for-test

    During unit testing we don't have a complete Flatpak app or runtime
    available, and flatpak run is not necessarily in FLATPAK_BINDIR yet;
    but we can run the portal with this environment variable set, to
    specify a mock implementation of Flatpak.

    This helps to reproduce portal: --env-fd can collide with fds passed across D-Bus #4286.

  • portal: Add some test coverage

    This exercises Spawn() and reproduces portal: --env-fd can collide with fds passed across D-Bus #4286.


The first 3 commits should also be applied to 1.10.x, 1.8.x and anything else that got aeb6a7a backported to it.

@smcv
smcv marked this pull request as draft May 21, 2021 13:16
@smcv

smcv commented May 21, 2021 •

Copy link
Copy Markdown
Collaborator Author

It looks like fixing #4285 was masking a different bug in the portal (#4286), which was also introduced while we were fixing CVE-2021-21261: --env-fd can collide with one of the fds that is being passed through by the D-Bus caller. If it does, we end up reading 0 bytes from --env-fd and not setting the required environment variables. In particular, this breaks Steam's pressure-vessel subsandboxes, but only intermittently (!)

Fixing #4285 accidentally mitigated that bug: as we leak more --env-fd file descriptors, the next --env-fd becomes numerically larger, making it less likely to collide with one of the ones from D-Bus and making starting the subsandbox more likely to succeed.

I'm now trying to write a unit test for the portal that we can use as a reproducer for both of these.

[edited: I've opened separate issues for the two things I was fixing and edited this comment to refer to them]

@smcv smcv mentioned this pull request May 21, 2021
smcv added 6 commits May 21, 2021 18:44
Otherwise we'll run out of file descriptors eventually, when starting
a sufficiently large number of subsandboxes.

Resolves: flatpak#4285
Fixes: aeb6a7a "portal: Convert --env in extra-args into --env-fd"
Signed-off-by: Simon McVittie <[email protected]>
This will allow us to add additional mapping entries for fds to be
used internally by `flatpak run`, in particular --env-fd.

Defer the second pass through the fd array until the last possible
moment, so that any extra fds we want to add (like the --env-fd) have
already been added by then.

Helps: flatpak#4286
Signed-off-by: Simon McVittie <[email protected]>
Just because we can allocate a new, unused fd in the portal's fd space,
that doesn't mean that fd number is going to be unused in the child
process's fd space: we might need to remap it.

Resolves: flatpak#4286
Fixes: aeb6a7a "portal: Convert --env in extra-args into --env-fd"
Signed-off-by: Simon McVittie <[email protected]>
This makes them easier to test, and easier to use in related tests.

Signed-off-by: Simon McVittie <[email protected]>
@smcv
smcv force-pushed the portal-env-fd-leak branch from 23d83a7 to 2fa44c7 Compare May 21, 2021 17:46
@smcv

smcv commented May 21, 2021

Copy link
Copy Markdown
Collaborator Author

OK, try this. Originally, this branch was just the first commit, fixing #4285; but that makes #4286 more likely to happen, and I think we should fix both at the same time (meaning at least the first three commits).

The refactoring in flatpak-context is not strictly necessary but is an enabler for test coverage.

The test coverage is also not strictly necessary, but is good to have.

@smcv smcv changed the title portal: Don't leak fd used for serialized environment portal: Fix --env-fd regressions May 21, 2021
@smcv
smcv marked this pull request as ready for review May 21, 2021 18:09
Comment thread tests/test-portal.c Resolved
@smcv

smcv commented May 21, 2021

Copy link
Copy Markdown
Collaborator Author

/cc @RyuzakiKK

Comment thread portal/flatpak-portal.c Resolved
@smcv
smcv marked this pull request as draft May 24, 2021 11:49
smcv added 2 commits May 24, 2021 13:30
During unit testing we don't have a complete Flatpak app or runtime
available, and `flatpak run` is not necessarily in FLATPAK_BINDIR yet;
but we can run the portal with this environment variable set, to
specify a mock implementation of Flatpak.

This helps to reproduce flatpak#4286.

Signed-off-by: Simon McVittie <[email protected]>
This exercises Spawn() and reproduces flatpak#4286.

Signed-off-by: Simon McVittie <[email protected]>
@smcv
smcv force-pushed the portal-env-fd-leak branch from 2fa44c7 to 8fed236 Compare May 24, 2021 12:31
@smcv
smcv marked this pull request as ready for review May 24, 2021 12:40
@alexlarsson

Copy link
Copy Markdown
Member

LGTM

@alexlarsson
alexlarsson merged commit 412c157 into flatpak:master May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

portal: --env-fd can collide with fds passed across D-Bus portal: --env-fd is leaked

4 participants