Skip to content

Commit f818a7b

Browse files
committed
build: discover -O0 side-channel components from a source marker
The list of timing-sensitive components built at -O0 was hand-maintained in the Makefile, so adding one required a Makefile edit (breaking the drop-in component model) and the -O0 files emitted a spurious "_FORTIFY_SOURCE requires -O" warning from glibc. Discover the set by grepping the source for a KASLD_BUILD_NO_OPTIMIZE marker (added to each of the 7 side-channel headers) instead, and append -U_FORTIFY_SOURCE to the -O0 recipe to drop the fortify define (a no-op at -O0). Whole-file -O0 is kept deliberately — a per-function no-opt attribute is not a reliable substitute for the timing guarantee.
1 parent 45d74ee commit f818a7b

8 files changed

Lines changed: 41 additions & 6 deletions

File tree

Makefile

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,15 @@ COMP_SRC_DIR := $(SRC_DIR)/components
227227
SRC_FILES := $(wildcard $(COMP_SRC_DIR)/*.c)
228228
BIN_FILES := $(patsubst $(COMP_SRC_DIR)/%.c,$(COMP_DIR)/%,$(SRC_FILES))
229229

230-
# Side-channel components compiled with -O0 (see the recipe below for why).
231-
# Named once here so the build rule and `make print-deps` share one list.
232-
SIDECHANNEL_COMPONENTS := databounce echoload entrybleed mincore prefetch \
233-
prefetch_directmap zombieload
230+
# Side-channel components opt out of optimization by carrying the marker
231+
# KASLD_BUILD_NO_OPTIMIZE in their source header; they are discovered by grep so
232+
# adding one needs no Makefile edit (matching the drop-in component model). The
233+
# same list feeds the build rule below and `make print-deps`. Whole-file -O0 is
234+
# deliberate: it reliably stops the optimizer reordering or eliding the
235+
# rdtsc/mfence timing loops, which a per-function attribute does not guarantee
236+
# (gcc's optimize attribute is documented debugging-only).
237+
SIDECHANNEL_COMPONENTS := $(patsubst $(COMP_SRC_DIR)/%.c,%,\
238+
$(shell grep -l KASLD_BUILD_NO_OPTIMIZE $(COMP_SRC_DIR)/*.c 2>/dev/null))
234239
SIDECHANNEL_BINS := $(addprefix $(COMP_DIR)/,$(SIDECHANNEL_COMPONENTS))
235240

236241
# cc-component <cmd...>: compile one leak component. One line per component.
@@ -305,9 +310,11 @@ endif
305310
# (rdtsc/rdtscp + mfence/lfence), speculative execution gadgets (asm goto),
306311
# or Flush+Reload cache probing via volatile pointer accesses. The static
307312
# pattern rule takes precedence over the generic $(COMP_DIR)/% rule above for
308-
# the listed targets; the list lives in SIDECHANNEL_COMPONENTS.
313+
# the SIDECHANNEL_BINS (discovered by the KASLD_BUILD_NO_OPTIMIZE marker above).
314+
# -U_FORTIFY_SOURCE drops the fortify define inherited from ALL_CFLAGS: it is a
315+
# no-op at -O0 and glibc otherwise warns "_FORTIFY_SOURCE requires -O".
309316
$(SIDECHANNEL_BINS): $(COMP_DIR)/%: $(COMP_SRC_DIR)/%.c $(HDRS) | $(COMP_DIR)
310-
$(call cc-component, $(CC) $(ALL_CFLAGS) -O0 $(ALL_LDFLAGS) -I$(SRC_DIR) $< -o $@)
317+
$(call cc-component, $(CC) $(ALL_CFLAGS) -O0 -U_FORTIFY_SOURCE $(ALL_LDFLAGS) -I$(SRC_DIR) $< -o $@)
311318

312319
# kernelsnitch: needs -lpthread (uses default -O2 for hash timing performance)
313320
$(COMP_DIR)/kernelsnitch: $(COMP_SRC_DIR)/kernelsnitch.c $(HDRS) | $(COMP_DIR)

src/components/databounce.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
// https://cc0x1f.net/publications/kaslr.pdf
4747
// https://github.com/cc0x1f/store-to-leak-forwarding-there-and-back-again
4848
// https://i.blackhat.com/asia-20/Friday/asia-20-Canella-Store-To-Leak-Forwarding-There-And-Back-Again-wp.pdf
49+
//
50+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
51+
// or elide the timing / cache-probe / speculation measurements this technique
52+
// relies on; a per-function no-opt attribute is not a reliable substitute.
4953
// ---
5054
// <bcoles@gmail.com>
5155

src/components/echoload.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
// https://cc0x1f.net/publications/kaslr.pdf
5050
// https://github.com/cc0x1f/store-to-leak-forwarding-there-and-back-again
5151
// https://i.blackhat.com/asia-20/Friday/asia-20-Canella-Store-To-Leak-Forwarding-There-And-Back-Again-wp.pdf
52+
//
53+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
54+
// or elide the timing / cache-probe / speculation measurements this technique
55+
// relies on; a per-function no-opt attribute is not a reliable substitute.
5256
// ---
5357
// <bcoles@gmail.com>
5458

src/components/entrybleed.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
// https://dl.acm.org/doi/pdf/10.1145/3623652.3623669
4545
// https://googleprojectzero.blogspot.com/2022/12/exploiting-CVE-2022-42703-bringing-back-the-stack-attack.html
4646
// https://bugs.chromium.org/p/project-zero/issues/detail?id=2351
47+
//
48+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
49+
// or elide the timing / cache-probe / speculation measurements this technique
50+
// relies on; a per-function no-opt attribute is not a reliable substitute.
4751
// ---
4852
// <bcoles@gmail.com>
4953

src/components/mincore.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
// Mitigations:
2626
// Patched in v4.15. No runtime sysctl could restrict access — the
2727
// bug was an uninitialized byte in the mincore output vector. x86_64 only.
28+
//
29+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
30+
// or elide the timing / cache-probe / speculation measurements this technique
31+
// relies on; a per-function no-opt attribute is not a reliable substitute.
2832
// ---
2933
// <bcoles@gmail.com>
3034

src/components/prefetch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@
126126
// https://github.com/IAIK/prefetch
127127
// https://www.usenix.org/conference/usenixsecurity16/technical-sessions/presentation/gruss
128128
// https://www.amd.com/en/resources/product-security/bulletin/amd-sb-1017.html
129+
//
130+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
131+
// or elide the timing / cache-probe / speculation measurements this technique
132+
// relies on; a per-function no-opt attribute is not a reliable substitute.
129133
// ---
130134
// <bcoles@gmail.com>
131135

src/components/prefetch_directmap.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
//
4242
// References:
4343
// https://gruss.cc/files/prefetch.pdf
44+
//
45+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
46+
// or elide the timing / cache-probe / speculation measurements this technique
47+
// relies on; a per-function no-opt attribute is not a reliable substitute.
4448
// ---
4549
// <bcoles@gmail.com>
4650

src/components/zombieload.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@
7878
// Van Schaik, Milburn, Österlund, Frigo, Maisuradze, Razavi, Bos,
7979
// Giuffrida.
8080
// "RIDL: Rogue In-Flight Data Load" (S&P 2019)
81+
//
82+
// KASLD_BUILD_NO_OPTIMIZE: built -O0 (Makefile) so the optimizer cannot reorder
83+
// or elide the timing / cache-probe / speculation measurements this technique
84+
// relies on; a per-function no-opt attribute is not a reliable substitute.
8185
// ---
8286
// <bcoles@gmail.com>
8387

0 commit comments

Comments
 (0)