Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: SIGSEGV in makezleparams when SIGHUP arrives during ZLE parameter initialization



Hello,
from time to time, I’ve been getting automated reports from
Fedora users about one-off crashes in makezleparams and always
without any kind of reproducer.

I’ve finally decided to harness the power of Claude and after
some crunching on the backtraces, it was able to come up with
a reliable reproducer that produces an identical back trace.

The rest of this message was generated by Claude because, sadly,
I don't know much about zsh’s internals.  Nonetheless, the reproducer
and the proposed fix have been independently tested by myself.

Regards,
Lukas

- - -

When vared is called from a shell function and a SIGHUP arrives while
makezleparams() is initializing ZLE special parameters, zsh crashes with
a NULL pointer dereference in unsetparam_pm().

The root cause is a race between parameter initialization and signal
delivery.  makezleparams() creates parameters via createparam(), which
uses zshcalloc — so pm->gsu.s is initially NULL.  It then sets
pm->level = locallevel + 1 (making endparamscope consider the parameter
in-scope for cleanup), and only afterwards assigns pm->gsu.s.  If a
signal arrives in that window and triggers parameter scope teardown, the
NULL gsu pointer is dereferenced.

The normal interactive prompt is safe because loop() in init.c calls
queue_signals() before entering ZLE.  But when vared is called from a
shell function, execsimple() calls dont_queue_signals() before running
the builtin, so zleread -> zlecore -> makezleparams runs with
queueing_enabled=0, making signals deliverable immediately.

The crash path is:
  SIGHUP -> zhandler -> zexit -> callhookfunc("zshexit") -> doshfunc
  -> runshfunc -> endparamscope -> scanendscope -> unsetparam_pm
  -> pm->gsu.s->unsetfn  [pm->gsu.s is NULL -> SIGSEGV]

The fix adds queue_signals()/unqueue_signals() around the body of
makezleparams() itself, which protects all three call sites
(execzlefunc, zlebeforetrap, and compcore) uniformly.

Reproducer: the race window is only a few instructions wide, so a
deterministic reproducer requires injecting kill(getpid(), SIGHUP) at
the vulnerable point.  The following .zshrc triggers the crash when used
with the reproducer patch below:

--- .zshrc ---
zshexit() { : }
my-widget() { : }
zle -N my-widget
bindkey 'a' my-widget
my-redraw() { : }
zle -N zle-line-pre-redraw my-redraw
do_vared() {
    local foo=""
    vared foo
}
do_vared
--- end .zshrc ---

Build with the reproducer patch applied and --disable-dynamic, start
zsh interactively with ZDOTDIR pointing to the directory containing
the .zshrc above, and press 'a'.  Without the fix, zsh crashes with
SIGSEGV.  With the fix applied on top, it exits cleanly on SIGHUP.

Reproducer patch (apply first to trigger the bug deterministically):

diff --git a/Src/Zle/zle_params.c b/Src/Zle/zle_params.c
index bf1e90470..7a0618abc 100644
--- a/Src/Zle/zle_params.c
+++ b/Src/Zle/zle_params.c
@@ -207,6 +207,21 @@ makezleparams(int ro)
 	DPUTS1(!pm, "param %s not set in makezleparams", zp->name);

 	pm->level = locallevel + 1;
+
+	/* bz#2449976 reproducer: send SIGHUP after pm->level is set but
+	 * before pm->gsu is assigned.  At this point pm->gsu.s is NULL
+	 * (from zshcalloc in createparam).  The SIGHUP handler calls
+	 * zexit -> callhookfunc("zshexit") -> runshfunc -> endparamscope
+	 * -> scanendscope -> unsetparam_pm, which dereferences pm->gsu.s
+	 * -> NULL pointer dereference -> SIGSEGV. */
+	{
+	    static int sighup_sent = 0;
+	    if (!sighup_sent && !ro) {
+		sighup_sent = 1;
+		kill(getpid(), SIGHUP);
+	    }
+	}
+
 	pm->u.data = zp->data;
 	switch(PM_TYPE(zp->type)) {
 	    case PM_SCALAR:

Fix:

diff --git a/Src/Zle/zle_params.c b/Src/Zle/zle_params.c
index bf1e90470..72e878230 100644
--- a/Src/Zle/zle_params.c
+++ b/Src/Zle/zle_params.c
@@ -199,6 +199,8 @@ makezleparams(int ro)
     struct zleparam *zp;
     Param reg_param;

+    queue_signals();
+
     for(zp = zleparams; zp->name; zp++) {
 	Param pm = createparam(zp->name, (zp->type |PM_SPECIAL|PM_REMOVABLE|
 		PM_LOCAL|((ro && zp->data != &custom_cursor_form) ? PM_READONLY : 0)));
@@ -228,6 +230,8 @@ makezleparams(int ro)
 	    PM_LOCAL|PM_REMOVABLE);
     reg_param->gsu.h = &registers_gsu;
     reg_param->level = locallevel + 1;
+
+    unqueue_signals();
 }

/* Special unset function for ZLE special parameters: act like the standard *





Messages sorted by: Reverse Date, Date, Thread, Author