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

[Bug]: input methods that use WAYLAND_SOCKET environment variable regress in 1.15.6 #5614

Closed
4 tasks done
wengxt opened this issue Dec 1, 2023 · 5 comments · Fixed by #5615
Closed
4 tasks done

[Bug]: input methods that use WAYLAND_SOCKET environment variable regress in 1.15.6 #5614

wengxt opened this issue Dec 1, 2023 · 5 comments · Fixed by #5615
Labels

Comments

@wengxt
Copy link
Contributor

wengxt commented Dec 1, 2023

Checklist

  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for a bug that matches the one I want to file, without success.
  • If this is an issue with a particular app, I have tried filing it in the appropriate issue tracker for the app (e.g. under https://github.com/flathub/) and determined that it is an issue with Flatpak itself.
  • This issue is not a report of a security vulnerability (see here if you need to report a security issue).

Flatpak version

1.15.6

What Linux distribution are you using?

Arch Linux

Linux distribution version

Archlinux

What architecture are you using?

x86_64

How to reproduce

I have an application that may use WAYLAND_SOCKET environment to connect to wayland in certain cases. With WAYLAND_SOCKET, the compositor can identify the client and may expose special wayland object to the client, like granting a certain permission.

Specifically, this is used by weston and kwin to allow input method process to be only forked by compositor.

To reproduce this with weston, install org.fcitx.Fcitx5 from flathub.

Put following content in ~/.config/weston.ini

[core]
xwayland=false

[input-method]
path=/home/user/.local/share/flatpak/exports/bin/org.fcitx.Fcitx5

and start weston (make sure there's no fcitx5 running).

Expected Behavior

fcitx connects to wayland correctly.

Actual Behavior

fcitx quits from weston output saying something about wayland disconnected.

Additional Information

I believe this is relevant to the new wayland security context feature's implementation.

https://gitlab.freedesktop.org/wayland/wayland/-/blob/main/src/wayland-client.c#L1297

wl_display_connect will unset WAYLAND_SOCKET when it's called. The new wayland security context actually calls wl_display_connect and it will effectively unset WAYLAND_SOCKET for the sub process.

WAYLAND_SOCKET should not be used by flatpak for this, because the fd specified by WAYLAND_SOCKET can be only used once.

@wengxt wengxt added the bug label Dec 1, 2023
@wengxt wengxt changed the title [Bug]: WAYLAND_SOCKET break between 1.15.4 and 1.15.6 [Bug]: application relies on WAYLAND_SOCKET environment variable breaks between 1.15.4 and 1.15.6 Dec 1, 2023
@smcv
Copy link
Collaborator

smcv commented Dec 5, 2023

cc @emersion @swick

I have an application that may use WAYLAND_SOCKET environment to connect to wayland in certain cases. With WAYLAND_SOCKET, the compositor can identify the client and may expose special wayland object to the client, like granting a certain permission.

It sounds as though the compositor is implementing an ad-hoc precursor of the "security context" extension, so that instead of Flatpak giving the app an identifiable socket, the Wayland compositor is giving Flatpak an identifiable socket.

wl_display_connect will unset WAYLAND_SOCKET when it's called

This is unsafe to do in a multi-threaded process: any thread could be calling getenv() concurrently.

@smcv
Copy link
Collaborator

smcv commented Dec 5, 2023

It seems odd to me to be using an input method installed as a Flatpak app and giving it special privileges that the rest of the desktop environment - even parts of it that are explicitly in the trusted computing base, like the desktop shell - do not have. Normally, Flatpak apps have fewer privileges than un-sandboxed desktop apps.

In particular, the input method seems like something very "powerful" that needs to be absolutely trusted: it can do anything that you would be able to input with the keyboard.

@swick
Copy link
Contributor

swick commented Dec 5, 2023

Indeed a bit weird. I was aware that compositors like weston create special privileged sockets and exec apps which then have access to them. For this to be useful all apps must be separated from the trusted computing base the compositor is running in, and flatpak gives you this separation. So, I kind of get the use case here and I think this is a genuine bug.

@wengxt
Copy link
Contributor Author

wengxt commented Dec 5, 2023

It seems odd to me to be using an input method installed as a Flatpak app and giving it special privileges that the rest of the desktop environment - even parts of it that are explicitly in the trusted computing base, like the desktop shell - do not have. Normally, Flatpak apps have fewer privileges than un-sandboxed desktop apps.

In particular, the input method seems like something very "powerful" that needs to be absolutely trusted: it can do anything that you would be able to input with the keyboard.

In the real world, android allows third-party input method from the very beginning, and iOS allows that in some recent versions. I don't think Input method and sandbox really conflicts. I think the "trust" comes from how you install it and set it as input method, not by the fact whether it runs within a sandbox or not.

On other hand, sandbox may help enhance the security (I'm not saying that this is guaranteed today with flatpak though), e.g. preventing other app from accessing the sensitive data that input method saves because input method will use user typing history to adapt to improve user experience.

Right now security context and WAYLAND_SOCKET provides two similar but somewhat different capabilities:

  1. WAYLAND_SOCKET can be only used by ONE wayland client.
  2. security context is applied to ALL wayland clients in the sandbox.

While there is some conflict with the implementation (listener fd and WAYLAND_SOCKET fd). If we put away the implementation details aside, as today WAYLAND_SOCKET indeed provides something that security context doesn't and I hope at least it can be used with flatpak for now until there's some other alternatives (or maybe extend security context) implemented.

@smcv
Copy link
Collaborator

smcv commented Dec 8, 2023

In the real world, android allows third-party input method from the very beginning, and iOS allows that in some recent versions

Yes, and they're very attractive to attackers on those platforms, because a malicious application that says "I'm an input method" is the perfect combination of user-installable and extremely powerful.

@smcv smcv changed the title [Bug]: application relies on WAYLAND_SOCKET environment variable breaks between 1.15.4 and 1.15.6 [Bug]: input methods that use WAYLAND_SOCKET environment variable regress in 1.15.6 Dec 8, 2023
smcv pushed a commit that referenced this issue Feb 14, 2024
1. For security context creation, only relies on WAYLAND_DISPLAY, do not
   use WAYLAND_SOCKET since the file descriptor defined by WAYLAND_SOCKET
   can be only consumed once.
2. Due to the incompatiblity between WAYLAND_SOCKET and the security
   context, add a new permission --socket=inherit-wayland-socket
   to limit the usage of WAYLAND_SOCKET to an opt-in feature. Only when
   this flag is set, WAYLAND_SOCKET will be passed to the sandbox.
3. When WAYLAND_SOCKET is not inherited, set FD_CLOEXEC to avoid it to
   be leaked the to sandbox.

Closes: #5614
GeorgesStavracas pushed a commit to GeorgesStavracas/flatpak that referenced this issue Apr 26, 2024
1. For security context creation, only relies on WAYLAND_DISPLAY, do not
   use WAYLAND_SOCKET since the file descriptor defined by WAYLAND_SOCKET
   can be only consumed once.
2. Due to the incompatiblity between WAYLAND_SOCKET and the security
   context, add a new permission --socket=inherit-wayland-socket
   to limit the usage of WAYLAND_SOCKET to an opt-in feature. Only when
   this flag is set, WAYLAND_SOCKET will be passed to the sandbox.
3. When WAYLAND_SOCKET is not inherited, set FD_CLOEXEC to avoid it to
   be leaked the to sandbox.

Closes: flatpak#5614
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants