Skip to content

WIP: fix(trap): trap reset disposition paths — reinstall after flip (#2348) + SIGINT REVERT special case (#2347) - #2349

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2348-trap-reset-reinstall
Oct 31, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2348-trap-reset-reinstall

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Audit-fix wave (1.5.0 RC freeze, audit-fix line). Two REVERT-path disposition bugs from the pre-release audit chapter 1:

Regression pins: tests/issue2348_2347_trap_reset_regressions.rs (CI-uncovered target; run locally via the build slot by the cluster). Collector: full lib/regression + CI are yours per the wave split.

…n flip

Refs #2348

GNU's REVERT and SET paths round-trip: trap.c:1398 reset_signal
restores the original handler on `trap - SIG`, and trap.c:838
set_signal installs trap_handler again when a new trap is set, so a
re-set handler receives deliveries. rubash wrote the trap table but
left the flipped OS disposition in place — after `trap - USR1` a
re-set USR1 trap never fired and the shell died by the signal; with
a `trap ''` flip in between it stayed deaf at SIG_IGN instead.

Root cause: the per-signal state machine desynced across its two
records. signal-hook's Handle caches each signal's registration, so
a later add_signal for the same signal is a no-op, while reconcile's
Default/Ignore transitions overwrite the OS disposition with raw
sigaction calls (SIG_DFL / SIG_IGN). After any flip away from the
backend dispatcher, the slot stayed registered but the kernel no
longer routed deliveries to it, and HANDLER_SET bookkeeping claimed
the re-arm had happened.

Fix: kernel_signals::ensure_handler now guarantees the recorded
Handler state against the kernel — it captures the backend
dispatcher verbatim at the first live install, and on a Handler
wish whose OS disposition reads SIG_DFL/SIG_IGN it reinstalls that
dispatcher, reconnecting the still-registered slot (including its
chained previous disposition). The existing SA_RESTART
normalization re-runs on the reinstalled action.

Pins: tests/issue2348_2347_trap_reset_regressions.rs, issue2348_*
shapes (direct/eval/source restores, TERM/HUP/SIG-spelling, the
ignore-flip variant, and the clean boundaries: overwrite without
clear, clear -> ignore, clear -> different signal, clear -> EXIT).
The issue2347_* pins ride in the next commit with their fix.
… default

Refs #2347

GNU trap_builtin's REVERT branch (builtins/trap.def:228-251) is a
deliberate special case citing POSIX interp 751: after
restore_default_signal (trap.c) early-returns on a hard-ignored
signal, the builtin still installs termsig_sighandler for SIGINT in
a non-interactive shell, so `trap - SIGINT` restores the fatal
disposition even when SIGINT was ignored on entry. rubash no-oped
the whole operation — the shell survived a self-sent SIGINT that
kills GNU.

The fix keys on SIGINT alone (SIGQUIT is the opposite hard-code —
forced SIG_IGN, already matched — and TERM/USR1 reverts on
entry-ignored signals stay no-ops in both shells):

- trap.rs: the REVERT branch calls the disposition half after
  remove_trap's (correct) table no-op when SIGINT is entry-ignored.
- kill.rs: a SIGINT_REVERTED flag records the revert. The builtin
  applies SIG_DFL synchronously for a non-interactive shell; an
  interactive shell records the flag and lets its SIGINT pin route
  the signal (GNU's sigint_sighandler seat).
- reconcile: SIGINT's Default base resolves to SIG_DFL once
  reverted, and a first match arm in
  reconcile_kernel_trap_dispositions keeps the seeded entry-ignore
  table entry (an "" action) from re-imposing Ignore at every
  command boundary — the step that otherwise undid the builtin's
  install one command later. The interactive pin arm likewise
  honors the flag. Trap-set on SIGINT stays a hard-ignore no-op,
  and the `trap -p` listing keeps showing the entry-ignore entry,
  both exactly as GNU behaves after the revert.

Pins: issue2347_* in tests/issue2348_2347_trap_reset_regressions.rs
(real kernel entry-ignore via an ignoring launcher that execs the
shell; death forms asserted on the parent wait status), covering
the kill cell, the trap-set no-op, the no-op resets for
TERM/USR1/QUIT, the untouched entry ignore, and the ordinary
SIGINT-reset control.
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