xref: /linux/tools/testing/selftests/exec/Makefile (revision 25757bc855e388eedf86c69c382857ef2c67b08e)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2c9b26b81SDavid DrysdaleCFLAGS = -Wall
34e7301e6SAlexey DobriyanCFLAGS += -Wno-nonnull
4b083cc81SMickaël SalaünCFLAGS += $(KHDR_INCLUDES)
5b083cc81SMickaël Salaün
6b083cc81SMickaël SalaünLDLIBS += -lcap
7c9b26b81SDavid Drysdale
8b57a2907SKees CookALIGNS := 0x1000 0x200000 0x1000000
9b57a2907SKees CookALIGN_PIES        := $(patsubst %,load_address.%,$(ALIGNS))
10b57a2907SKees CookALIGN_STATIC_PIES := $(patsubst %,load_address.static.%,$(ALIGNS))
113545deffSKees CookALIGNMENT_TESTS   := $(ALIGN_PIES) $(ALIGN_STATIC_PIES)
12b57a2907SKees Cook
132a69962bSMickaël SalaünTEST_PROGS := binfmt_script.py check-exec-tests.sh
14b57a2907SKees CookTEST_GEN_PROGS := execveat non-regular $(ALIGNMENT_TESTS)
152a69962bSMickaël SalaünTEST_GEN_PROGS_EXTENDED := false inc set-exec script-exec.inc script-noexec.inc
16908a26e1SMuhammad Usama AnjumTEST_GEN_FILES := execveat.symlink execveat.denatured script subdir
1780d443e8Sbamvor.zhangjian@huawei.com# Makefile is a run-time dependency, since it's accessed by the execveat test
1880d443e8Sbamvor.zhangjian@huawei.comTEST_FILES := Makefile
1980d443e8Sbamvor.zhangjian@huawei.com
204e7301e6SAlexey DobriyanTEST_GEN_PROGS += recursion-depth
219132c394SKees CookTEST_GEN_PROGS += null-argv
22b083cc81SMickaël SalaünTEST_GEN_PROGS += check-exec
234e7301e6SAlexey Dobriyan
24*25757bc8SChristian Brauner# binfmt_misc must not be reachable as an exec source or as a stacking layer,
25*25757bc8SChristian Brauner# or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf.
26*25757bc8SChristian BraunerTEST_GEN_PROGS += binfmt_misc_selfpin
27*25757bc8SChristian Brauner
287baee96fSChristian Brauner# Static ('T' flag) transparent binfmt_misc test; the asserting interpreter
297baee96fSChristian Brauner# is shared with the bpf harness's transparent case. No bpf toolchain needed.
307baee96fSChristian BraunerTEST_GEN_PROGS += binfmt_misc_transparent
317baee96fSChristian BraunerTEST_GEN_FILES += binfmt_transparent_interp
327baee96fSChristian Brauner
3387c50a58SChristian Brauner# 'L' (loader substitution) binfmt_misc test: the payload runs as the main
3487c50a58SChristian Brauner# image with a copy of the system loader substituted for its PT_INTERP and
3587c50a58SChristian Brauner# asserts the native identity from inside; the static build proves the
3687c50a58SChristian Brauner# override is dropped for a binary without PT_INTERP.
3787c50a58SChristian BraunerTEST_GEN_PROGS += binfmt_misc_loader
3887c50a58SChristian BraunerTEST_GEN_FILES += binfmt_loader_payload binfmt_loader_payload_static
3987c50a58SChristian Brauner
40277d787fSFarid Zakaria# binfmt_misc bpf-backed ('B') handler test: a libbpf harness plus its
41277d787fSFarid Zakaria# struct_ops objects and the test interpreter/app it routes between. Only
42277d787fSFarid Zakaria# built when clang, bpftool, the vmlinux BTF and libbpf are all present
43277d787fSFarid Zakaria# (HAVE_BPF_TOOLCHAIN=y forces it) so the other exec selftests don't grow
44277d787fSFarid Zakaria# a bpf toolchain dependency.
45277d787fSFarid ZakariaCLANG ?= clang
46277d787fSFarid ZakariaBPFTOOL ?= bpftool
47277d787fSFarid ZakariaVMLINUX_BTF ?= /sys/kernel/btf/vmlinux
48277d787fSFarid ZakariaHAVE_BPF_TOOLCHAIN ?= $(shell command -v $(CLANG) >/dev/null 2>&1 && \
49277d787fSFarid Zakaria			command -v $(BPFTOOL) >/dev/null 2>&1 && \
50277d787fSFarid Zakaria			test -r $(VMLINUX_BTF) && \
51277d787fSFarid Zakaria			pkg-config --exists libbpf 2>/dev/null && echo y)
52277d787fSFarid Zakariaifeq ($(HAVE_BPF_TOOLCHAIN),y)
53277d787fSFarid ZakariaTEST_GEN_PROGS += binfmt_misc_bpf
547baee96fSChristian BraunerTEST_GEN_FILES += bpf_interp.bpf.o nix_origin.bpf.o transparent.bpf.o
5587c50a58SChristian BraunerTEST_GEN_FILES += loader.bpf.o
56277d787fSFarid ZakariaTEST_GEN_FILES += binfmt_bpf_interp binfmt_bpf_app
57277d787fSFarid Zakariaelse
58277d787fSFarid Zakaria$(info exec selftests: skipping binfmt_misc_bpf, needs clang, bpftool, vmlinux BTF and libbpf)
59277d787fSFarid Zakariaendif
60277d787fSFarid Zakaria
610f71241aSKees CookEXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*	\
620f71241aSKees Cook	       $(OUTPUT)/S_I*.test
6380d443e8Sbamvor.zhangjian@huawei.com
6426860105SChristian BraunerLOCAL_HDRS += binfmt_misc_common.h
6526860105SChristian Brauner
6680d443e8Sbamvor.zhangjian@huawei.cominclude ../lib.mk
6780d443e8Sbamvor.zhangjian@huawei.com
682a69962bSMickaël SalaünCHECK_EXEC_SAMPLES := $(top_srcdir)/samples/check-exec
692a69962bSMickaël Salaün
70a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/subdir:
71c9b26b81SDavid Drysdale	mkdir -p $@
72472874cfSKees Cook$(OUTPUT)/script: Makefile
73472874cfSKees Cook	echo '#!/bin/bash' > $@
74c9b26b81SDavid Drysdale	echo 'exit $$*' >> $@
75c9b26b81SDavid Drysdale	chmod +x $@
76a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/execveat.symlink: $(OUTPUT)/execveat
77a8ba798bSbamvor.zhangjian@huawei.com	cd $(OUTPUT) && ln -s -f $(shell basename $<) $(shell basename $@)
78a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat
79c9b26b81SDavid Drysdale	cp $< $@
80c9b26b81SDavid Drysdale	chmod -x $@
81b57a2907SKees Cook$(OUTPUT)/load_address.0x%: load_address.c
82b57a2907SKees Cook	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \
83b57a2907SKees Cook		-fPIE -pie $< -o $@
84b57a2907SKees Cook$(OUTPUT)/load_address.static.0x%: load_address.c
85b57a2907SKees Cook	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \
86b57a2907SKees Cook		-fPIE -static-pie $< -o $@
87b083cc81SMickaël Salaün$(OUTPUT)/false: false.c
88b083cc81SMickaël Salaün	$(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
892a69962bSMickaël Salaün$(OUTPUT)/inc: $(CHECK_EXEC_SAMPLES)/inc.c
902a69962bSMickaël Salaün	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
912a69962bSMickaël Salaün$(OUTPUT)/set-exec: $(CHECK_EXEC_SAMPLES)/set-exec.c
922a69962bSMickaël Salaün	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
932a69962bSMickaël Salaün$(OUTPUT)/script-exec.inc: $(CHECK_EXEC_SAMPLES)/script-exec.inc
942a69962bSMickaël Salaün	cp $< $@
952a69962bSMickaël Salaün$(OUTPUT)/script-noexec.inc: $(CHECK_EXEC_SAMPLES)/script-noexec.inc
962a69962bSMickaël Salaün	cp $< $@
97277d787fSFarid Zakaria
98*25757bc8SChristian Brauner# Reuses setup_userns()/write_file() from the filesystems selftests. Their
99*25757bc8SChristian Brauner# wrappers.h wants the uapi headers, so ask for them here rather than widening
100*25757bc8SChristian Brauner# CFLAGS for every program in this directory.
101*25757bc8SChristian Brauner$(OUTPUT)/binfmt_misc_selfpin: CFLAGS += $(TOOLS_INCLUDES)
102*25757bc8SChristian Brauner$(OUTPUT)/binfmt_misc_selfpin: ../filesystems/utils.c
103*25757bc8SChristian Brauner
104277d787fSFarid Zakaria# --- binfmt_misc bpf ('B') handler test ---------------------------------
105277d787fSFarid Zakaria# The struct_ops bpf objects are compiled against the running kernel's BTF.
106277d787fSFarid Zakaria# CLANG/BPFTOOL/VMLINUX_BTF are set above next to the toolchain check;
107277d787fSFarid Zakaria# override LIBBPF_CFLAGS/LDLIBS to point at a libbpf install.
108277d787fSFarid ZakariaBPF_CFLAGS ?= -I$(OUTPUT)
109277d787fSFarid ZakariaLIBBPF_CFLAGS ?=
110277d787fSFarid ZakariaLIBBPF_LDLIBS ?= -lbpf -lelf -lz
111277d787fSFarid Zakaria
112277d787fSFarid Zakaria$(OUTPUT)/vmlinux.h:
113277d787fSFarid Zakaria	$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
114277d787fSFarid Zakaria
11526860105SChristian Brauner# BPF_NO_KFUNC_PROTOTYPES: the programs declare the kfuncs they use themselves.
116277d787fSFarid Zakaria$(OUTPUT)/%.bpf.o: %.bpf.c $(OUTPUT)/vmlinux.h
11726860105SChristian Brauner	$(CLANG) -g -O2 -target bpf -mcpu=v3 -DBPF_NO_KFUNC_PROTOTYPES \
11826860105SChristian Brauner		$(BPF_CFLAGS) $(LIBBPF_CFLAGS) -c $< -o $@
119277d787fSFarid Zakaria
12026860105SChristian Brauner$(OUTPUT)/binfmt_misc_bpf: binfmt_misc_bpf.c binfmt_misc_common.h
121277d787fSFarid Zakaria	$(CC) $(CFLAGS) $(LIBBPF_CFLAGS) $(LDFLAGS) $< $(LIBBPF_LDLIBS) -o $@
122277d787fSFarid Zakaria
123277d787fSFarid Zakaria$(OUTPUT)/binfmt_bpf_interp: binfmt_bpf_interp.c
124277d787fSFarid Zakaria	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
125277d787fSFarid Zakaria
12687c50a58SChristian Brauner$(OUTPUT)/binfmt_loader_payload: binfmt_loader_payload.c binfmt_misc_common.h
12787c50a58SChristian Brauner	$(CC) $(CFLAGS) $(LDFLAGS) -fPIE -pie $< -o $@
12887c50a58SChristian Brauner
12987c50a58SChristian Brauner$(OUTPUT)/binfmt_loader_payload_static: binfmt_loader_payload.c binfmt_misc_common.h
13087c50a58SChristian Brauner	$(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
13187c50a58SChristian Brauner
132277d787fSFarid Zakaria# PT_INTERP is set to the literal "$ORIGIN/binfmt_bpf_interp"; the nix_origin
133277d787fSFarid Zakaria# handler resolves it relative to the binary at run time.
134277d787fSFarid Zakaria$(OUTPUT)/binfmt_bpf_app: binfmt_bpf_app.c
135277d787fSFarid Zakaria	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,--dynamic-linker,'$$ORIGIN/binfmt_bpf_interp' $< -o $@
136277d787fSFarid Zakaria
13726860105SChristian BraunerEXTRA_CLEAN += $(OUTPUT)/vmlinux.h $(OUTPUT)/*.bpf.o
138