quickshell icon indicating copy to clipboard operation
quickshell copied to clipboard

PAM subprocess outlives a killed shell and inherits the crash handler (a crash in a PAM module starts a second shell)

Open alvarosth opened this issue 2 months ago • 1 comments

Quickshell 0.3.0. I reproduced it there and checked the relevant code on master (41651d7).

The subprocess that PamConversation::createSubprocess forks has two problems, and neither is touched by the fix for the wrong free() in 0fed22a.

  1. It is not tied to its parent's lifetime. If the shell dies mid-conversation (I SIGKILLed it), the subprocess keeps running the PAM stack until the module sends its next message. With pam_fprintd that can be 30 s or more, and during that time it keeps the fingerprint reader claimed and holds every fd it inherited from the shell. That includes stdout/stderr, so a supervisor that waits for EOF on the shell's stderr pipe doesn't notice the shell is gone. That's how I ran into it: DankMaterialShell's dms run sat there with no shell and no lock prompt for ~30 s.

  2. It keeps the shell's crash signal handlers across fork(). A crash anywhere in the subprocess, including inside a PAM module, runs qs::crash::signalHandler. That writes a crash report for the parent's instance and then execve()s a relaunch of the whole shell inside the PAM subprocess. If the parent is still alive you end up with two instances of the shell; if it was killed, the "restarted" shell is the former PAM helper, with its pid, parent and fds. The 10 s no-relaunch guard is measured from the parent's start, so it doesn't help.

On 0.3.0 both showed up together: the orphaned helper hit the wrong free() when pam_fprintd relayed "Verification timed out" to the dead parent, then relaunched the shell from the helper.

Repro for 2 (no hardware, original shell left running):

shell.qml:

import QtQuick
import Quickshell
import Quickshell.Services.Pam

ShellRoot {
    PamContext { id: pam; configDirectory: Quickshell.shellDir; config: "crashing-module" }
    // start after the 10 s relaunch guard
    Timer { interval: 12000; running: true; onTriggered: console.log("pam start:", pam.start()) }
}

crashing-module, next to it (adjust the paths):

auth required pam_exec.so quiet /bin/sh -c [kill -SEGV $PPID]
QT_QPA_PLATFORM=offscreen quickshell -p ./shell.qml

After 12 s the output is:

 DEBUG qml: pam start: true
 ERROR: Quickshell has crashed under pid 83161 (Coredumps will be available under that pid.)
 ERROR: Quickshell has been restarted.
  INFO: Launching config: "/tmp/qs-pam-crash.midu/shell.qml"

The original instance is still running, and a second one is now running in the PAM subprocess.

For 1, replace the module with pam_exec.so quiet /bin/sleep 20 followed by pam_echo.so hello, and kill -9 the shell during the sleep. The subprocess keeps running (reparented to init) until pam_echo's message, and holds the shell's stdout/stderr until then.

Related: #960 and #969 are the wrong free() (fixed by 0fed22a). #1167 might be this mechanism too: its repeated restarts with overlapping pam_fprintd subprocesses look like what happens when helpers outlive the shell and relaunch it.

#1256 uses a lifeline pipe instead of prctl(PR_SET_PDEATHSIG): a crash relaunches the shell in place, so its PID stays the same and the parent-death signal doesn't fire. Closing the shell's end of the pipe on exit or exec signals the helper to terminate. The helper also resets inherited signal handlers as exec would, so a PAM module crash no longer relaunches the shell.

alvarosth avatar Oct 03 '26 09:10 alvarosth

Another data point, from 0.3.1 (Arch package quickshell 0.3.1-1, which does not include 0fed22a). It adds one consequence not covered above: the relaunched second shell can end up holding the session lock.

Setup: Omarchy 4 on Hyprland. The lock screen is WlSessionLock + PamContext with a pam_fprintd-compatible module.

  1. The shell was SIGKILLed while the lock's PAM helper was waiting for a finger (the cause of the kill is still unknown).
  2. The wrapper relaunched the shell. About 23 s later, the orphaned helper relayed "Verification timed out" and segfaulted in the conversation relay (the wrong free()). Its crash handler then relaunched a full second shell inside the helper process.
  3. Both shells kept running their idle logic. At the next idle timeout both requested a lock, and the instance living in the former helper process became the session-lock client.
  4. From outside, that instance looks like a harmless duplicate: it has duplicate bar and background layers. Terminating it while the screen was locked left Hyprland locked with no lock client, and the only way out was a hard power-off.

So until #1256 lands, a shell running inside a former PAM helper can own the session lock, and killing "the duplicate" is not safe while locked. A 0.3.x point release containing 0fed22a would at least remove the crash that triggers the relaunch on this path.

HemangKumawat avatar Oct 17 '26 16:10 HemangKumawat