Skip to contentSkip to conversation

bpftool: Always disable stack protection for clang

PATCH In review Holger Hoffstätte 3 participants 3 messages Jan 13, 2023 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: Always disable stack protection for clang

When the clang toolchain has stack protection enabled in order to be consistent with gcc - which just happens to be the case on Gentoo - the bpftool build fails:

clang \
	-I. \
	-I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/include/uapi/ \
	-I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/bpf/bpftool/bootstrap/libbpf/include \
	-g -O2 -Wall -target bpf -c skeleton/pid_iter.bpf.c -o pid_iter.bpf.o
clang \
	-I. \
	-I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/include/uapi/ \
	-I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/bpf/bpftool/bootstrap/libbpf/include \
	-g -O2 -Wall -target bpf -c skeleton/profiler.bpf.c -o profiler.bpf.o
skeleton/profiler.bpf.c:40:14: error: A call to built-in function '__stack_chk_fail' is not supported.
int BPF_PROG(fentry_XXX)
             ^
skeleton/profiler.bpf.c:94:14: error: A call to built-in function '__stack_chk_fail' is not supported.
int BPF_PROG(fexit_XXX)
             ^

2 errors generated.

Since stack-protector makes no sense for the BPF bits just unconditionally disable it.

--snip--

src/Makefile +1 -1
diff a/src/Makefile b/src/Makefile
@@ -205,7 +205,7 @@ $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
 		-I$(or $(OUTPUT),.) \
 		-I$(srctree)/include/uapi/ \
 		-I$(LIBBPF_BOOTSTRAP_INCLUDE) \
-		-g -O2 -Wall -target bpf -c $< -o $@
+		-g -O2 -Wall -fno-stack-protector -target bpf -c $< -o $@
 	$(Q)$(LLVM_STRIP) -g $@
 
 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
Acked-by on v1 1/1
#1 lore raw

2023-01-13 14:49 UTC+0100 ~ Holger Hoffstätte
<[email protected]>

Quoted 44 lines

When the clang toolchain has stack protection enabled in order to be consistent
with gcc - which just happens to be the case on Gentoo - the bpftool build fails:

clang \
    -I. \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/include/uapi/ \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/bpf/bpftool/bootstrap/libbpf/include \
    -g -O2 -Wall -target bpf -c skeleton/pid_iter.bpf.c -o pid_iter.bpf.o
clang \
    -I. \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/include/uapi/ \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/bpf/bpftool/bootstrap/libbpf/include \
    -g -O2 -Wall -target bpf -c skeleton/profiler.bpf.c -o profiler.bpf.o skeleton/profiler.bpf.c:40:14: error: A call to built-in function
'__stack_chk_fail' is not supported.
int BPF_PROG(fentry_XXX)
             ^
skeleton/profiler.bpf.c:94:14: error: A call to built-in function
'__stack_chk_fail' is not supported.
int BPF_PROG(fexit_XXX)
             ^
2 errors generated.

Since stack-protector makes no sense for the BPF bits just unconditionally disable it.

--snip--

diff a/src/Makefile b/src/Makefile

src/Makefile +1 -1
@@ -205,7 +205,7 @@ $(OUTPUT)%.bpf.o: skeleton/%.bpf.c
 $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
          -I$(or $(OUTPUT),.) \
          -I$(srctree)/include/uapi/ \
          -I$(LIBBPF_BOOTSTRAP_INCLUDE) \
-        -g -O2 -Wall -target bpf -c $< -o $@
+        -g -O2 -Wall -fno-stack-protector -target bpf -c $< -o $@
Quoted 3 lines

     $(Q)$(LLVM_STRIP) -g $@

 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)

Right, I understand we don't want it when compiling the BPF program from the skeleton. Looks good, thank you!

maintainer
#2 lore raw
Quoted 48 lines · Quentin Monnet

2023-01-13 14:49 UTC+0100 ~ Holger Hoffstätte
<[email protected]>

Quoted 46 lines

When the clang toolchain has stack protection enabled in order to be consistent
with gcc - which just happens to be the case on Gentoo - the bpftool build fails:

clang \
    -I. \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/include/uapi/ \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/bpf/bpftool/bootstrap/libbpf/include \
    -g -O2 -Wall -target bpf -c skeleton/pid_iter.bpf.c -o pid_iter.bpf.o
clang \
    -I. \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/include/uapi/ \
    -I/tmp/portage/dev-util/bpftool-6.0.12/work/linux-6.0/tools/bpf/bpftool/bootstrap/libbpf/include \
    -g -O2 -Wall -target bpf -c skeleton/profiler.bpf.c -o profiler.bpf.o
skeleton/profiler.bpf.c:40:14: error: A call to built-in function
'__stack_chk_fail' is not supported.
int BPF_PROG(fentry_XXX)
             ^
skeleton/profiler.bpf.c:94:14: error: A call to built-in function
'__stack_chk_fail' is not supported.
int BPF_PROG(fexit_XXX)
             ^
2 errors generated.

Since stack-protector makes no sense for the BPF bits just unconditionally disable it.

--snip--

diff a/src/Makefile b/src/Makefile

src/Makefile +1 -1
@@ -205,7 +205,7 @@ $(OUTPUT)%.bpf.o: skeleton/%.bpf.c
 $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
          -I$(or $(OUTPUT),.) \
          -I$(srctree)/include/uapi/ \
          -I$(LIBBPF_BOOTSTRAP_INCLUDE) \
-        -g -O2 -Wall -target bpf -c $< -o $@
+        -g -O2 -Wall -fno-stack-protector -target bpf -c $< -o $@
      $(Q)$(LLVM_STRIP) -g $@
 
Quoted 6 lines
Quoted 1 line

 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)

Right, I understand we don't want it when compiling the BPF program from the skeleton. Looks good, thank you!

LGTM, thanks Holger! Looks like this patch is against GH mirror (https://github.com/libbpf/bpftool).
Manually applied it to the upstream tree:
https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit/?id=878625e1c7a10dfbb1fdaaaae2c4d2a58fbce627

Thanks,
Daniel