Repository navigation
Enable stack canaries - #119039
Enable stack canaries#119039
Conversation
`our_syscall` reads its seventh argument from `8(%rsp)` and `remapToHugeStep3` corrects its own return address at the same offset. A stack canary is placed between the locals and the return address, so it shifts what that offset refers to. `-fstack-protector-strong` does not instrument either function, since neither has locals, but relying on that heuristic is fragile. Mark them explicitly instead. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`-fno-stack-protector` is transcribed from upstream `SANITIZER_COMMON_CFLAGS`, it is not a choice of ours. Say so, and say why upstream needs it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Build with `-fstack-protector-strong`, so that a guard value is placed between the local variables and the return address and checked before returning. An overflow of a local buffer then aborts instead of returning to an address the attacker wrote. `strong` instruments only the functions that can actually be overflowed: those with a local array, an address-taken local or an alloca. `all` instruments every function, including leaves that cannot overflow, which is not worth the cost. The flag is set before `add_subdirectory (contrib)`, so it applies to the bundled libraries as well. The sanitizer runtimes keep their own `-fno-stack-protector`, which comes from upstream compiler-rt. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [632f473] Summary: ❌
AI ReviewSummaryThis PR enables Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 632f473.
Changed lines: Changed C/C++ lines covered: 280/317 (88.33%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
The `musl` target had a blanket `-fno-stack-protector`, so in the static musl build libc itself was the only part left without canaries. Restrict the opt-out to upstream musl's own `NOSSP_OBJS` list: the CRT objects, `__init_ssp` and the thread-pointer setup it runs through, and the `memcpy`/`memset` called from that path. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`-fstack-protector-strong` instruments a whole function whenever a local array or an escaping local survives to codegen, and it charges every call for it even when the reason sits on a cold path. Remove the reason in the functions the perf run flagged: outline the throws in `WriteBuffer::write`, outline the paths above 128 bits in `itoa` and take the 256 bit values by reference, use `PODArray` for the `LIMIT BY` slice buffer, and share one stateless `NullOutput` in `skipFieldByEscapingRule`. `clock_gettime_ns` cannot drop the `timespec` it hands to the kernel, so it opts out with `NO_STACK_PROTECTOR`, which moves from `remapExecutable.cpp` to `base/defines.h`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`no_stack_protector` on an inline function does nothing for its callers: the stack protector pass runs after inlining, so a caller that inlined the `timespec` still got a canary, and only the unused out-of-line copies were clean. Move the definition into `Stopwatch.cpp` so the buffer never reaches a caller, which drops the canary from `StepWallClock::onEnter` and `onLeave`, `Stopwatch` construction, `ProfileEvents::Timer` and `WorkloadResources`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Interesting, why is it so much more complex than https://github.com/ClickHouse/ClickHouse/pull/119684/changes? |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The code changes are very good!
|
But the WASM build has failed. |
`clock_gettime_ns` moved out of line into `src/Common/Stopwatch.cpp`, and the standalone wasm parser build names every translation unit it links explicitly, so `Build (wasm_parser)` failed with `undefined symbol: clock_gettime_ns`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119039&sha=091c46e6720f16725f9a620b229ee25d3c371fdc&name_0=PR&name_1=Build%20%28wasm_parser%29 PR: ClickHouse#119039 Co-Authored-By: Claude Fable 5.1 <[email protected]>
`throwWriteToFinalizedBuffer` and `throwWriteToCanceledBuffer` are defined in the same translation unit as `WriteBuffer::write`, so without `NO_INLINE` the optimizer can inline them back into the hot path and restore the exception-construction stack slot and the stack canary that moving them out of line was meant to remove. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Appending the flag to `CMAKE_C_FLAGS`/`CMAKE_CXX_FLAGS` after `cmake/linux/default_libs.cmake` left every subdirectory that file adds without a canary: libc++, libc++abi, libunwind, `base/glibc-compatibility` and `base/harmful` (a subdirectory sees the flags as they were when it was added). Verified from `compile_commands.json`: 145 translation units in those directories lacked the flag before the move, and only compiler-rt lacks it afterwards, which is deliberate, since `contrib/compiler-rt-cmake` resets inherited flags to an allowlist and its sanitizer runtimes add `-fno-stack-protector` on top. Co-Authored-By: Claude Fable 5.1 <[email protected]>
`JSONExtract` into a named tuple skipped the fixup whenever the number of insertions matched the
tuple size, but a repeated key inserts into the same element twice, so `{"a":1,"a":2}` for
`Tuple(a UInt8, b UInt8)` left `a` two rows long and `b` empty. Reading that result goes out of
bounds: a `LOGICAL_ERROR` in the lucky case and a crash in the rest. Track which elements took a
value in a bit set instead of counting insertions.
ClickHouse#119039 (comment)
Co-Authored-By: Claude Opus 5 <[email protected]>
|
The rest of the reports are unrelated. Merging |
cb1f202
|
Correction to my headroom figure above: 29.1 MiB was right for master on 2026-09-20, but it fell to 15.4 MiB by 2026-09-27 (nightly |
`ColumnArray::insertFrom` calls the element column's virtual `insertRangeFrom` once per row (e.g. `FunctionIf::executeGeneric` for `if(cond, [DateTime], [Date])`). Its bound check threw inline and formatted `start`/`length` through `toString(const T &)`, which takes their addresses, so since `-fstack-protector-strong` (ClickHouse#119039) every call paid a canary store and check, a 0xa8-byte frame and two spills. The `conditional` ClickHouse#2/ClickHouse#3 perf queries stepped +9% on amd and arm at that merge. Move the throws of `insertRangeFrom` in `ColumnVector`, `ColumnDecimal`, `ColumnFixedString`, `ColumnString` and `ColumnArray` into `[[noreturn]] NO_INLINE` helpers taking values by value, as ClickHouse#119039 does for its own hot paths (`DataTypesDecimal.cpp`). `NO_INLINE` keeps them out of line under ThinLTO. Conditions, positions and error codes are unchanged; the out-of-bound message of `ColumnString` and `ColumnArray` now carries start, length and size. Measured on an x86 release build against master 7cbb8ad: the 27 `insertRangeFrom` instantiations of these columns go from 5-7 `%fs:0x28` references to 0 (aarch64 `ColumnVector.cpp`: 18/18 `__stack_chk_guard` sequences gone), and `conditional` ClickHouse#2/ClickHouse#3 run at 0.91-0.93x of master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…` on a quarter of the rows
The `JSONEachRow` and `Avro` expansions of queries 0 and 2-10 (20 entries) took 1.00-2.51 s
on MasterCI amd and 1.09-2.90 s on arm: each reads all 2M rows of its `File` table, `Avro` on
one thread, `JSONEachRow` from a 1 GB file.
No existing query number or text changes: the 11 queries (entries 0-87) are disabled and 22
(88 entries) appended as 88-175. The 66 sub-second entries return verbatim through
`{fast_format}`; the 22 `JSONEachRow` / `Avro` ones run through `{slow_format}` on new `_500k`
tables written by the same create and fill on `id < 250000` (`Avro` still 2 files,
`JSONEachRow` one, read whole with default parallel parsing). Master's 2M-row `JSONEachRow` /
`Avro` tables are no longer created: only the disabled entries read them.
Local A/B on official builds across ClickHouse#119039 (845c709 -> 8a7ac31):
- `Avro` (2 swapped passes, 12 runs): originals moved 1.048-1.056, copies 1.044-1.058
(retention 0.87-1.12); per-row user time copy / original 0.998-1.024.
- `JSONEachRow` (4 swapped passes, 20 runs): median over the ten, originals 1.0172, copies
1.0145 (pooled retention 0.84), no pair apart by more than 0.015; per row 1.000-1.016.
On the base build the copies take 0.25-0.30 of the originals' time (per row 0.98-1.04), at
most 0.635 s projected on amd and 0.733 s on arm; the 66 verbatim copies 0.88-1.31 of their
originals (median 1.000).
About 21 s less query time per master perf job on amd and 23 s on arm (8 runs per copy;
26 / 28 s at CI's 5), and about 2.6 s less setup.
Related: ClickHouse#129258
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r of their rows No existing query number or text changes: 2 queries (entries 2-7) are disabled and 4 (6 entries) appended as 8-13. The `disabled` marker covers every expansion of a query, so the four low-distinctness patterns return verbatim as 9, 10, 12, 13 through two new substitutions. Entries 2 and 5 (`like` / `match` with an all-unique non-constant pattern) compile one regexp per row of `numbers_mt(1500000)` and take 1.79 / 1.57 s on MasterCI amd, 1.30 / 1.15 s on arm. Their copies (8, 11) run on every 4th number of the same range, `numbers_mt(0, 1500000, 4)`, with `max_block_size = 16384`: the same 23 blocks, a quarter of the rows each, one compilation per row (375000 regexp cache misses) and a full 1000-bucket local cache per block. Probe on official builds (paired user time, n = 12): per row the copies take 0.97-1.01 of the originals' user time; the ClickHouse#119039 merge moved the originals by 1.029 / 1.028 and the copies by 1.027 / 1.027, the ClickHouse#117346 merge 0.960 / 0.976 and 0.964 / 0.970 (MasterCI: 1.03-1.05 and 0.97-0.99). In a local perf.py run the copies take 0.253 / 0.254 of the originals' time; projected onto MasterCI that is 0.45 / 0.40 s on amd and 0.33 / 0.29 s on arm, about 30 s (amd) and 22 s (arm) less query time per master perf job. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #119037
Build with
-fstack-protector-strong. A guard value is placed between the local variables and the return address and checked before returning, so that an overflow of a local buffer aborts instead of returning to an address the attacker wrote.stronginstruments only the functions that can actually be overflowed: those with a local array, an address-taken local or an alloca.allinstruments every function, including leaves that cannot overflow, which is not worth the cost.The flag is set through
COMPILER_FLAGS, which is folded into the compiler flags beforecmake/linux/default_libs.cmakeandadd_subdirectory (contrib)run, so it applies to the bundled libraries as well, including libc++, libc++abi, libunwind andbase/glibc-compatibility. The sanitizer and XRay runtimes keep their own-fno-stack-protector, which is transcribed from upstream compiler-rt: the sanitizer runtimes execute before thread local storage is set up and inside interceptors and signal handlers, and XRay inherits the same flag set upstream.In the static musl build, libc is compiled with canaries too, except for the objects upstream musl itself excludes (
NOSSP_OBJSin itsMakefile): the CRT objects,__init_sspand the thread-pointer setup it runs through (__libc_start_main,__init_tls,__set_thread_area), and thememcpy/memsetcalled from that path. Instrumenting those would read the canary before it exists, or see a different value on return than the one__init_ssphad installed on entry.The compiler-rt runtime objects and musl's early-init objects are built without canaries. So are, on x86-64, the
memcpy/memmove/memsetentry points of the bundled LLVM libc (contrib/libllvmlibc-cmake), which already carry an explicitno_stack_protectorso that their CPU dispatch stays a plain tail call. No other bundled object is left out.our_syscallandremapToHugeStep3insrc/Common/remapExecutable.cppread and write the return address at a fixed offset from the stack pointer, which a canary would shift, so they are markedno_stack_protectorexplicitly.Kept separate from the full RELRO change so that the performance cost of each can be measured on its own.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Harden official builds by compiling with
-fstack-protector-strong.Workflow [PR]
Sync PR [sync-upstream/pr/119039]
Version info
26.10.1.596-master(included in26.10and later)