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

wayland: Privileged protocol security #208

Merged
merged 3 commits into from
Nov 7, 2023

Conversation

Drakulix
Copy link
Member

Draft until everything is updated to not break. Perhaps this should be split in two commits. One that enables security-contexts to inherit the privileged flag and one that enables all the enforcing, that can be merged later.

In any case this breaks parts of the desktop today, but once cosmic-panel makes use of security-context applets could even enjoy more privileges. E.g. always creating valid xdg-activiation tokens via #207.

@Drakulix Drakulix force-pushed the panel-sec-context_jammy branch from ee2462c to d6f2c81 Compare October 31, 2023 10:02
@ids1024
Copy link
Member

ids1024 commented Oct 31, 2023

At some point we'll likely want to not just have a binary privileged flag, but a way to give permission for a protocol to specific apps. Not sure how that would work though, since Flatpak itself would not be privileged. And likewise dealing with apps that aren't started by a sandbox engine or by cosmic-session/cosmic-panel and thus have no security context...

For development, would it make sense to have a command line argument or env var to disable enforcement of privileged protocols?

I'd still note that we can't expect this to actually be secure for apps that aren't sandboxed, but it's good not to encourage apps to use sensitive compositor-specific protocols that aren't available in Flatpak, etc.

@Drakulix
Copy link
Member Author

At some point we'll likely want to not just have a binary privileged flag, but a way to give permission for a protocol to specific apps. Not sure how that would work though, since Flatpak itself would not be privileged. And likewise dealing with apps that aren't started by a sandbox engine or by cosmic-session/cosmic-panel and thus have no security context...

Right, I think the only reasonable thing to do here is get_client_credentials and try to match the binary. (Sadlyy this is not portable.)

If we are reasonably sure it is the same the user originally allow-listed, we can allow additional protocols. Check should e.g. include the binary is not user-writable imo.

If we figure out the flatpak-sandbox is actually the flatpak binary, we can also match on the app-id to allow specific protocols.

For development, would it make sense to have a command line argument or env var to disable enforcement of privileged protocols?

Yeah I also thought about just generally enabling all protocols for debug builds. Though those might be too slow for debugging, so an environment variable sounds like a good idea.

@ids1024
Copy link
Member

ids1024 commented Oct 31, 2023

Yeah. The BSDs all seem to support getpeereid to get the uid/gid of a Unix socket peer, but unlike Linux or FreeBSD, I'm not sure there's a way to get the pid on NetBSD and OpenBSD. It may be possible to identify the binary path through /proc from pid. But that is theoretically racy.

Restricting it to debug builds also isn't ideal because someone working on the panel, lock screen, or an applet doesn't necessarily want to have to rebuild the compositor.

@Drakulix Drakulix force-pushed the panel-sec-context_jammy branch from d6f2c81 to 5cdfe46 Compare November 1, 2023 12:00
@Drakulix
Copy link
Member Author

Drakulix commented Nov 1, 2023

Restricting it to debug builds also isn't ideal because someone working on the panel, lock screen, or an applet doesn't necessarily want to have to rebuild the compositor.

Probably this should also be a toggle from the debug interface. Maybe we should make a "debug-interface wishlist" issue at some point.

Anyway COSMIC_DISABLE_WAYLAND_SECURITY exists now and can be easily added to our start-cosmic script or , if desired. Though just as I write this, I realize that we also source the user's profile, so we still need to make sure we don't ship this flag eventually.

Maybe just in the debug-interface would be enough until we have proper configuration for security aspects?

@Drakulix Drakulix force-pushed the panel-sec-context_jammy branch from 5cdfe46 to 0b66651 Compare November 6, 2023 17:41
@Drakulix Drakulix changed the title wayland: Enforce privileged protocol security wayland: Privileged protocol security Nov 6, 2023
@Drakulix Drakulix marked this pull request as ready for review November 6, 2023 17:42
@Drakulix
Copy link
Member Author

Drakulix commented Nov 6, 2023

Ok, this PR now only includes inheriting the privileged flag for security-context clients already having it (and allowlisted for cosmic-panel for now), as well as a COSMIC_ENABLE_WAYLAND_SECURITY flag for testing, so this can be merged early before all pieces for proper security handling are in place.

@Drakulix Drakulix requested a review from ids1024 November 6, 2023 17:43
src/state.rs Outdated
XWaylandKeyboardGrabState::new::<Self>(&dh);
PointerConstraintsState::new::<Self>(&dh);
PointerGesturesState::new::<Self>(&dh);
SecurityContextState::new::<Self, _>(&dh, client_has_security_context);
SecurityContextState::new::<Self, _>(&dh, client_should_see_privileged_protocols);
Copy link
Member

Choose a reason for hiding this comment

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

This would prevent flatpak from using the security context protocol if COSMIC_ENABLE_WAYLAND_SECURITY is set, since it wouldn't be started with a privileged socket. Would it be better to use client_has_no_security_context here?

Though it doesn't make a difference until we start taking into account the security context of Flatpak apps.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it be better to use client_has_no_security_context here?

Right, that wasn't on purpose, changing it back.

Though it doesn't make a difference until we start taking into account the security context of Flatpak apps.

Well it should already block all "privileged" protocols. Though once we enforce security there is indeed no difference here, but given there might be in the future this is still a good change.

if let Err(err) = state.common.display_handle.insert_client(
client_stream,
Arc::new(ClientState {
security_context: Some(security_context.clone()),
privileged: privileged
&& security_context.sandbox_engine.as_deref()
== Some("com.system76.CosmicPanel"),
Copy link
Member

Choose a reason for hiding this comment

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

We may want to further restrict which privileged protocols an applet has access to (define required permissions in desktop files?). But this is reasonable for now.

(And for that to really be secure, we'd also want to sandbox applets.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Defining necessary protocols in the desktop file would already be a good idea, I agree.

@Drakulix Drakulix force-pushed the panel-sec-context_jammy branch from 0b66651 to 9fd521d Compare November 7, 2023 11:28
@Drakulix Drakulix force-pushed the panel-sec-context_jammy branch from 9fd521d to 9576154 Compare November 7, 2023 11:28
@Drakulix Drakulix merged commit f7759a7 into master_jammy Nov 7, 2023
1 check passed
@jackpot51 jackpot51 deleted the panel-sec-context_jammy branch November 7, 2023 23:12
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.

2 participants