From 673cf464b02cf64ad5bf6c57b8ad0625a0127e65 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Wed, 7 Oct 2026 17:52:49 +0800 Subject: [PATCH] fix(nix): authorize desktop service outside the FHS sandbox --- .github/workflows/nix.yaml | 3 ++ .github/workflows/update-flake-hashes.yaml | 3 ++ flake.nix | 5 +++ scripts/tests/nix-desktop-authorization.sh | 41 ++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 scripts/tests/nix-desktop-authorization.sh diff --git a/.github/workflows/nix.yaml b/.github/workflows/nix.yaml index 0608239..2e08e2c 100644 --- a/.github/workflows/nix.yaml +++ b/.github/workflows/nix.yaml @@ -88,6 +88,9 @@ jobs: test -x result/bin/gpgui-helper test -x result/bin/gpgui + - name: Verify desktop authorization runs on the host + run: bash scripts/tests/nix-desktop-authorization.sh result + nixos-module: name: nixos-module (${{ matrix.platform.system }}, ${{ matrix.nixpkgs }}) strategy: diff --git a/.github/workflows/update-flake-hashes.yaml b/.github/workflows/update-flake-hashes.yaml index 636c02a..6a398ec 100644 --- a/.github/workflows/update-flake-hashes.yaml +++ b/.github/workflows/update-flake-hashes.yaml @@ -122,6 +122,9 @@ jobs: exit 1 fi + - name: Verify desktop authorization runs on the host + run: bash scripts/tests/nix-desktop-authorization.sh result + open-pull-request: needs: - validate-nix-prebuilt diff --git a/flake.nix b/flake.nix index a7ad52a..0c49aa2 100644 --- a/flake.nix +++ b/flake.nix @@ -401,6 +401,11 @@ export GP_SERVICE_BINARY='@gpservice_public@' export GP_AUTH_BINARY='@gpauth_public@' export GP_GUI_BINARY='${hostGuiLauncher}' + if [ "''${1:-}" = "launch-gui" ]; then + # Authorization must run before entering Bubblewrap, which disables setuid elevation. + export PATH=/run/wrappers/bin:$PATH + exec '${prebuiltFiles}/bin/gpclient' "$@" + fi exec '${prebuiltCommands.gpclient}/bin/gpclient' "$@" EOF substituteInPlace $out/bin/gpclient \ diff --git a/scripts/tests/nix-desktop-authorization.sh b/scripts/tests/nix-desktop-authorization.sh new file mode 100644 index 0000000..8e89a1b --- /dev/null +++ b/scripts/tests/nix-desktop-authorization.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +set -euo pipefail + +package_path="$(readlink -f "${1:-result}")" +test_workspace="$(mktemp -d)" +trap 'rm -rf "$test_workspace"' EXIT + +# This probe runs on CI without a system Polkit wrapper and never authorizes a service. +if [ -x /run/wrappers/bin/pkexec ]; then + echo "Run this probe on a host without /run/wrappers/bin/pkexec" >&2 + exit 1 +fi + +mkdir -p "$test_workspace/bin" +cat > "$test_workspace/bin/pkexec" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +test "$#" -eq 4 +test "$1" = --user +test "$2" = root +test "$3" = "$GP_TEST_SERVICE_BINARY" +test "$4" = --desktop-credentials +if ! grep -Eq '^NoNewPrivs:[[:space:]]+0$' /proc/self/status; then + echo "Authorization was attempted inside a sandbox" >&2 + exit 1 +fi +echo "Authorization reached the host without NoNewPrivs" >&2 +exit 126 +EOF +chmod +x "$test_workspace/bin/pkexec" + +if PATH="$test_workspace/bin:$PATH" \ + XDG_DATA_HOME="$test_workspace/data" \ + GP_TEST_SERVICE_BINARY="$package_path/bin/gpservice" \ + "$package_path/bin/gpclient" launch-gui --minimized; then + echo "The desktop launcher ignored the authorization rejection" >&2 + exit 1 +fi + +grep -Fx 'Authorization reached the host without NoNewPrivs' \ + "$test_workspace/data/gpclient/gpclient.log"