Skip to content

aarch64: sign the return address saved in jmp_buf - #7

Merged
alexey-milovidov merged 3 commits into
ClickHouse/v1.2.6from
pac-ret-setjmp
Oct 26, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse/v1.2.6from
pac-ret-setjmp

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

setjmp and sigsetjmp store x30 in the jmp_buf, and longjmp branches through it, so a jmp_buf reachable by an overflow is a return address in memory, like a spilled one. -mbranch-protection=pac-ret (ClickHouse/ClickHouse#119685) signs compiler-generated spills but not raw .s inputs.

This signs x30 with paciasp before storing it and authenticates it with autiasp after loading it back. The modifier is sp, which is the same at both ends. Both instructions are hint-space no-ops on cores without FEAT_PAuth.

The other aarch64 asm sources do not spill a return address: dlsym.s passes x30 as a value, clone.s saves a function pointer, and the rest never touch x30.

I checked this on Neoverse V2: round trips work, and corrupting the saved slot makes longjmp fault.

Related: ClickHouse/ClickHouse#119685

🤖 Generated with Claude Code

`setjmp` and `sigsetjmp` store `x30` in the `jmp_buf`, and `longjmp` and
the second return of `sigsetjmp` branch through what they load back, so a
`jmp_buf` reachable by an overflow is a return address in memory exactly
like a spilled one. ClickHouse builds with `-mbranch-protection=pac-ret`,
which signs the compiler-generated spills but says nothing about raw `.s`
inputs, so these stayed unsigned.

Sign `x30` with `paciasp` before storing it and authenticate it with
`autiasp` after loading it back. The modifier is `sp`, which is the same
at both ends: `longjmp` restores the `sp` that `setjmp` saved before it
authenticates, and `sigsetjmp` resumes with its own `sp` whether control
comes back from `setjmp` or from `longjmp`. Both instructions are in the
hint space, so cores without `FEAT_PAuth` execute them as no-ops.

Checked on Neoverse V2: round trips through `setjmp`/`longjmp` and
`sigsetjmp` work, the saved slot carries a signature in the upper bits,
and flipping one bit of it makes `longjmp` fault.

Related: ClickHouse/ClickHouse#119685

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Oct 5, 2026
Under `USE_MUSL` the `aarch64` asm sources from `contrib/musl` are raw `.s`
inputs, which `-mbranch-protection` does not instrument. The only return
address among them that lives in memory is `x30` in a `jmp_buf`, written
by `setjmp` and `sigsetjmp` and branched through by `longjmp`; bump musl to
sign it with `paciasp` and authenticate it with `autiasp`, both with `sp`
as the modifier, which `longjmp` restores before authenticating. `dlsym.s`
and `clone.s` save `x30` only as a value or a function pointer, so they
need nothing.

ClickHouse/musl#7

Addresses review: #119685 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
thevar1able and others added 2 commits October 21, 2026 17:16
…untime

Companion to __pthread_current_tsd_range. The sanitizer runtime resets the
shadow of a starting thread's stack and static TLS block only; musl carves
the stack, the TLS image and the tsd array out of one mapping with internal
__mmap/__munmap/__unmapself calls the runtime never sees, so a mapping
recycled for a new thread keeps the previous thread's shadow for everything
else. On TLS_ABOVE_TP targets (aarch64) struct pthread sits just below the
thread pointer, outside the static TLS block, and TSan reported its
errno_val as a data race between an exited tokio blocking-pool thread and
its replacement. `__pthread_current_thread_range` lets GetTls cover the
descriptor, as ThreadDescriptorSize() does on glibc.

(cherry picked from commit b147430)
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Oct 24, 2026
Resolve the `contrib/musl` conflict: master moved the submodule to
`7ad08176` (`sanitizer-thread-range`) while this branch points at
`34664698` (`pac-ret-setjmp`). The two touch disjoint files, so they are
merged in `ClickHouse/musl` as `ca24ff58` on the `pac-ret-setjmp` branch
(ClickHouse/musl#7), and the gitlink points there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Oct 26, 2026
@alexey-milovidov
alexey-milovidov merged commit 388a78b into ClickHouse/v1.2.6 Oct 26, 2026
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Oct 26, 2026
thevar1able added a commit to ClickHouse/ClickHouse that referenced this pull request Nov 3, 2026
It now contains the per-arch rseq signature fix merged in
ClickHouse/musl#8. It also brings
ClickHouse/musl#7, which signs the return address
that the aarch64 `setjmp` stores, using hint-space instructions that are
no-ops on cores without `FEAT_PAuth`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants