Skip to content

fix: read KWin tablet emulation env from wrapper, Xwayland, or systemd when non-dumpable - #499

Merged
avifenesh merged 1 commit into
mainfrom
fix/kwin-environ-cap-sys-nice
Nov 22, 2026
Merged

avifenesh merged 1 commit into
mainfrom
fix/kwin-environ-cap-sys-nice

Conversation

@avifenesh

@avifenesh avifenesh commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #498.

Why

Distros such as Fedora ship /usr/bin/kwin_wayland with file capabilities (cap_sys_nice=ep). A process exec'd from a binary with file capabilities is non-dumpable (dumpable = 0), so /proc/<pid>/environ is 0400 root:root and returns EACCES (or ENOENT under hidepid=2) to the desktop user. kwin_blocker() previously read only /proc/<kwin_pid>/environ and failed closed, refusing every KDE background click, drag and scroll.

What changed

When /proc/<kwin_pid>/environ fails with PermissionDenied or NotFound, kwin_blocker() checks verified sources in order before failing closed:

  1. kwin_wayland_wrapper parent (or org.kde.KWinWrapper under hidepid): Both systemd (plasma-kwin_wayland.service) and non-systemd (plasma-session) Plasma Wayland sessions start kwin_wayland as a direct child of kwin_wayland_wrapper, which carries no file capabilities and launches kwin_wayland with its environment verbatim (including plasma-kwin_wayland.service drop-ins, environment.d, and plasma-workspace/env). When /proc/<kwin_pid>/status is readable, PPid must match kwin_wayland_wrapper (or .kwin_wayland_wrapper-wrapped) or the session bus owner of org.kde.KWinWrapper; an unrelated parent (such as a shell) is rejected. When /proc/<kwin_pid> is hidden, org.kde.KWinWrapper is verified against task/*/children when readable.
  2. Direct Xwayland child of KWin: KWin spawns Xwayland with QProcessEnvironment::systemEnvironment() after lowering CAP_SYS_NICE, so a direct dumpable Xwayland child (verified via PPid == kwin_pid) carries KWin's environment even without kwin_wayland_wrapper.
  3. KWin's systemd user .service unit (org.freedesktop.systemd1): When GetUnitByPID returns a .service (not a .scope), ExecStart runs kwin_wayland or kwin_wayland_wrapper directly, and PAMName is empty, the check evaluates Manager.Environment, Service.Environment, Service.EnvironmentFiles (following systemd.exec(5) quoting/continuation rules, failing closed on missing required files, globs or malformed quotes), and Service.UnsetEnvironment. Unlike /proc snapshots, systemd reports its current loaded state.
  4. Fail closed: If none of those sources can prove KWin's environment, the check still refuses with the same message.

Also lists KWIN_WAYLAND_EMULATE_TABLET in scripts/check_env_forwarding.py NOT_ENVIRONMENT (read from KWin's environment, not the server's) and adds a CHANGELOG.md entry under [Unreleased].

Tests

  • Real Linux kernel PR_SET_DUMPABLE=0 fixtures verifying /proc/<pid>/environ returns EACCES, kwin_wayland_wrapper parent fallback detects unset/0/1, a non-wrapper sh parent fails closed, and a direct Xwayland child fallback detects unset/1.
  • Synthetic /proc + private D-Bus fixtures covering hidepid=2 with task/*/children verification, NixOS .kwin_wayland_wrapper-wrapped, PPid mismatch rejection, and direct /proc/<kwin_pid>/environ precedence.
  • Unit tests for systemd.exec(5) EnvironmentFile parsing (comments, single/double quotes, escapes, multi-line quoted values, =\ continuations, malformed input) and precedence (Manager.Environment -> Environment -> EnvironmentFile -> UnsetEnvironment).
  • Private D-Bus org.freedesktop.systemd1 integration tests exercising GetUnitByPID, Properties.GetAll, .service vs .scope rejection, and EnvironmentFile override.
  • Local gates passed: cargo fmt --all -- --check, cargo clippy --locked --all-targets -- -D warnings, RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps --document-private-items, cargo test --locked --no-fail-fast, scripts/mcp_safety_check.py, scripts/check_plugins.sh, cargo publish --dry-run --locked.

Review

  • Clean-context self-review completed (two passes: initial findings fixed before pushing, final pass clean and posted as a PR comment).
  • All 18 CI checks passed. External reviewers did not run (Bugbot usage limit reached; no revuto review posted), so landing on CI plus self-review.

…d when non-dumpable

Distros such as Fedora ship /usr/bin/kwin_wayland with cap_sys_nice=ep, making the process non-dumpable so /proc/<pid>/environ returns EACCES (or ENOENT under hidepid=2) and breaking KDE background pointer input.

When /proc/<kwin_pid>/environ is unreadable, fall back to:
1. KWin's kwin_wayland_wrapper parent (or org.kde.KWinWrapper verified via task/*/children when /proc/<kwin_pid> is hidden), which carries no file capabilities and spawns kwin_wayland with its environment verbatim.
2. A direct dumpable Xwayland child of KWin (verified via PPid).
3. KWin's systemd user .service unit when ExecStart runs KWin or its wrapper directly and PAMName is empty.

Otherwise fail closed as before.

Fixes #498
@cursor

cursor Bot commented Nov 22, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 43f20606-cf55-44ce-af59-a09f3658ea8c)

@avifenesh

Copy link
Copy Markdown
Collaborator Author

Self-review (clean context)

Self-review complete: no blocking findings remain.

Verified

  • Correctness and distro coverage (false negatives / false positives):
    • Direct /proc/<kwin_pid>/environ still runs first and takes precedence whenever readable, so existing behavior on dumpable installs is unchanged.
    • On non-dumpable kwin_wayland installs (cap_sys_nice=ep on Fedora, Arch, openSUSE, NixOS, and non-systemd startplasma-wayland sessions), Fallback 1 reads kwin_wayland_wrapper (including NixOS .kwin_wayland_wrapper-wrapped) directly via PPid without an extra D-Bus round trip when argv[0] matches, or via org.kde.KWinWrapper when PPid matches or /proc/<kwin_pid> is hidden.
    • Fallback 2 reads a direct Xwayland / .Xwayland-wrapped child after re-checking PPid == kwin_pid.
    • Fallback 3 evaluates KWin's systemd user .service unit only when GetUnitByPID resolves to a .service (_2eservice), every ExecStart binary is KWin or its wrapper (rejecting shell/env launchers), and PAMName is empty; otherwise the check fails closed.
    • KWIN_WAYLAND_EMULATE_TABLET evaluation remains fail-closed (!value.is_empty() && value != b"0"), and scripts/check_env_forwarding.py passes with the new NOT_ENVIRONMENT entry.
  • Linux procfs, hidepid, PID reuse, musl, and root-container edge cases:
    • Only PermissionDenied and NotFound trigger fallbacks; other I/O errors fail closed.
    • When /proc/<kwin_pid>/status is readable and names a non-wrapper parent, the code never jumps to org.kde.KWinWrapper. Direct Xwayland children are re-verified via read_proc_ppid(child_pid) == Some(kwin_pid).
    • Production code uses portable std/Unix/zbus APIs (musl build passed in CI). Permission-based tests skip under geteuid() == 0 while parser and precedence unit tests still execute.
  • Systemd D-Bus Properties.GetAll and EnvironmentFile parsing:
    • Properties.GetAll types match org.freedesktop.systemd1.Service (ExecStart: a(sasbttttuii), PAMName: s, Environment: as, EnvironmentFiles: a(sb), UnsetEnvironment: as).
    • Precedence matches systemd.exec(5): Manager.Environment -> Service.Environment -> EnvironmentFiles -> UnsetEnvironment.
    • apply_systemd_env_file handles systemd.exec(5) quoting, comments, CRLF/LF continuations (including immediately after =), double-quote escapes, and unquoted escapes, failing closed on malformed quoting, globs, or unreadable required files.
  • Tests and cleanup:
    • Real kernel PR_SET_DUMPABLE=0 tests verify actual EACCES on /proc/<pid>/environ and exercise kwin_wayland_wrapper parent, non-wrapper sh parent refusal, and Xwayland child paths.
    • Fake procfs and private D-Bus tests cover hidepid=2 with task/*/children, NixOS .kwin_wayland_wrapper-wrapped, PPid mismatch, renamed wrapper owning org.kde.KWinWrapper, direct-environ precedence, and end-to-end systemd D-Bus evaluation.
    • ChildGuard + PR_SET_PDEATHSIG(SIGTERM) and TempDirGuard prevent leaked processes and directories.

Documented limits (non-blocking)

  • Systemd fallback is not a launch-time snapshot: Fallback 3 reads systemd's current loaded manager/unit/file state rather than KWin's frozen launch environment; it is only reached when KWin's own environ, kwin_wayland_wrapper, and any direct Xwayland child are all unavailable.
  • hidepid without CONFIG_PROC_CHILDREN: If /proc/<kwin_pid> is hidden and /proc/<wpid>/task/*/children is absent, org.kde.KWinWrapper is accepted by D-Bus name ownership and wrapper argv[0].
  • Wildcard EnvironmentFile=: Any wildcard path (*, ?, [) in EnvironmentFiles fails closed.

@avifenesh
avifenesh merged commit 3aed969 into main Nov 22, 2026
25 checks passed
@avifenesh
avifenesh deleted the fix/kwin-environ-cap-sys-nice branch November 22, 2026 20:35
@avifenesh avifenesh mentioned this pull request Nov 22, 2026
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.

KDE background click always refused on Fedora: kwin_wayland environ unreadable (cap_sys_nice)

1 participant