Skip to content

fix(linux): grant X11 access to the elevated instance without xhost - #6

Open
erasmus74 wants to merge 2 commits into
CardputerZero:mainfrom
erasmus74:fix/x11-root-grant-without-xhost
Open

erasmus74 wants to merge 2 commits into
CardputerZero:mainfrom
erasmus74:fix/x11-root-grant-without-xhost

Conversation

@erasmus74

Copy link
Copy Markdown

Summary

On Linux, M5 Imager 3.0.0 (AppImage) aborts right after the polkit authentication when xhost(1) is not installed: no window appears, and the elevated instance dies with SIGABRT.

This replaces the xhost +SI:localuser:root subprocess with the same X11 exchange done in process, so the elevated instance no longer depends on an optional package. The result is unchanged where xhost exists.

Background

What happens on a system without xhost:

  1. The AppImage starts unelevated, runs pkexec <AppImage> --install-elevation-policy, then re-executes itself through pkexec as root.
  2. As root, PlatformQuirks::applyQuirks() calls grantRootDisplayAccess(), which forks, drops back to the original user and runs xhost +SI:localuser:root. The result is never checked: the waitpid status is ignored and a missing binary ends in _exit(127).
  3. The AppImage only ships the xcb platform plugin, so root then has to connect to the user's X display. With access control enabled and root not on the host list, the connection is refused and Qt aborts (qt.qpa.xcb: could not connect to display).

xhost is an optional package (xorg-xhost on Arch and derivatives). AppRun.wrapped has a second attempt, but guarded by command -v xhost and || true, so on such systems nothing grants access and nothing says why.

Observed on CachyOS with Hyprland and Xwayland, xhost not installed:

pkexec[1630673]: master: Executing command [USER=root] ... [COMMAND=.../M5-Imager-3.0.0-linux-x64.appimage]
systemd[1]: Started Process Core Dump (PID 1631085/UID 0).      # 70 ms later
coredumpctl: UID: 0 (root)  Signal: 6 (ABRT)  Command Line: /tmp/.mount_.../usr/bin/rpi-imager
host access list: access control enabled, hosts (1): si:localuser:master

Changes

  • New src/linux/x11hostaccess.{h,cpp}: speaks the three pieces of the X11 protocol that are needed (connection setup, ChangeHosts, ListHosts) and sends what xhost +SI:localuser:<user> sends: ChangeHosts(Insert, FamilyServerInterpreted, "localuser\0root\0"). It then reads the list back to confirm the entry. POSIX sockets only, no new dependency.
  • grantRootDisplayAccess() uses it in the forked child, as before after setgid/setuid to the original user. All parsing and allocation happens in the parent before fork(); the child reports through a pipe, so the outcome is logged instead of swallowed (Granted root access to X11 display :0 (...) or WARNING: could not grant root access to X11 display :0: <reason>).
  • When the Xauthority file has a MIT-MAGIC-COOKIE-1 entry for the display it is used, as xhost does; otherwise the connection relies on the host access list.
  • The exchange is bounded: a per-read timeout and an overall deadline, so a wedged server cannot hang startup.

Compatibility

  • Same access as before, granted the same way: si:localuser:root, added by a process running as the original user.
  • $DISPLAY naming a remote or TCP display is declined with a log line; host access lists only apply to local connections.
  • AppRun.wrapped still tries xhost when it is present. That is now redundant but harmless, so I left the packaging alone.

Testing

  • New [x11hostaccess] cases in platformquirks_test drive the exchange against a fake X server on a unix socket, so they need no display: insert and read back, cookie selection, access control disabled, refused ChangeHosts, refused connection, server closing, stalling and trickling bytes, DISPLAY parsing, outcome messages.
  • With X11HOSTACCESS_TEST_DISPLAY set, a read-only case lists the host access list of a real X server. Ran against Xwayland 24.1.15.
  • platformquirks_test passes on qt6-base 6.12 / GCC 16 (253 assertions, 23 test cases), with and without the real display.
  • Not tested by me: a complete elevated run of a rebuilt AppImage, because it needs an interactive polkit authentication. The diagnosis comes from the journal entries above, the live host access list and the source.

The first commit (test: include <unistd.h> for access()/F_OK) is an unrelated one-line fix: platformquirks_test does not build with Qt 6.12 without it. Happy to drop it from this PR.

raspberrypi/rpi-imager has the same xhost call in src/linux/platformquirks_linux.cpp. I can open the same PR there if you would rather it went upstream first.

platformquirks_test.cpp calls access() and uses F_OK without declaring
them. It only compiled because an included Qt header pulled in
<unistd.h> on Qt 6.9. With qt6-base 6.12 the target fails to build:

    error: 'F_OK' was not declared
    error: 'access' was not declared
On Linux the elevated instance is started through pkexec or sudo and then
adds "si:localuser:root" to the X server's host access list, because the
AppImage only ships the xcb platform plugin and root has no other way to
reach the user's display. That was done by running
`xhost +SI:localuser:root`, and the result of that call was never checked.

xhost(1) is an optional package (xorg-xhost on Arch and derivatives, for
example). Where it is missing, the elevated instance silently gets no
access and aborts right after the authentication dialog with
"qt.qpa.xcb: could not connect to display".

Do the same X11 exchange in process instead: connect to the display as the
original user, send ChangeHosts(Insert, FamilyServerInterpreted,
"localuser\0root\0") and verify the entry by reading the list back. That
needs nothing beyond POSIX sockets, so no runtime dependency is added.

The exchange runs in the forked child as before, after dropping back to
the original user. All parsing and allocation happens in the parent before
fork(); the child reports what it saw through a pipe, so a failure is now
logged instead of being swallowed. Remote and TCP displays are declined
explicitly: host access lists only apply to local connections there.

Tests drive the exchange against a fake X server on a unix socket, so they
need no display. With X11HOSTACCESS_TEST_DISPLAY set, a read-only case also
lists the host access list of a real X server.
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.

1 participant