xref: /linux/tools/testing/selftests/exec/Makefile (revision b9cba7ebfe539f3e4bbdd03a1e0efa3b30b3f592)
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
2425757bc8SChristian Brauner# binfmt_misc must not be reachable as an exec source or as a stacking layer,
2525757bc8SChristian Brauner# or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf.
2625757bc8SChristian BraunerTEST_GEN_PROGS += binfmt_misc_selfpin
2725757bc8SChristian Brauner
28*f2b69ea2SChristian Brauner# The interpreters an 'F' or 'B' entry pre-opens are charged against
29*f2b69ea2SChristian Brauner# UCOUNT_BINFMT_MISC_INTERPRETERS. Unprivileged, no bpf.
30*f2b69ea2SChristian BraunerTEST_GEN_PROGS += binfmt_misc_interplimit
31*f2b69ea2SChristian Brauner
326bd0c7abSChristian Brauner# 'D' (register disabled) binfmt_misc test: an entry that exists but does
336bd0c7abSChristian Brauner# not dispatch until it is enabled. Static magic entry, no bpf toolchain.
346bd0c7abSChristian BraunerTEST_GEN_PROGS += binfmt_misc_disabled
356bd0c7abSChristian Brauner
367baee96fSChristian Brauner# Static ('T' flag) transparent binfmt_misc test; the asserting interpreter
377baee96fSChristian Brauner# is shared with the bpf harness's transparent case. No bpf toolchain needed.
387baee96fSChristian BraunerTEST_GEN_PROGS += binfmt_misc_transparent
397baee96fSChristian BraunerTEST_GEN_FILES += binfmt_transparent_interp
407baee96fSChristian Brauner
4187c50a58SChristian Brauner# 'L' (loader substitution) binfmt_misc test: the payload runs as the main
4287c50a58SChristian Brauner# image with a copy of the system loader substituted for its PT_INTERP and
4387c50a58SChristian Brauner# asserts the native identity from inside; the static build proves the
4487c50a58SChristian Brauner# override is dropped for a binary without PT_INTERP.
4587c50a58SChristian BraunerTEST_GEN_PROGS += binfmt_misc_loader
4687c50a58SChristian BraunerTEST_GEN_FILES += binfmt_loader_payload binfmt_loader_payload_static
4787c50a58SChristian Brauner
48277d787fSFarid Zakaria# binfmt_misc bpf-backed ('B') handler test: a libbpf harness plus its
49277d787fSFarid Zakaria# struct_ops objects and the test interpreter/app it routes between. Only
50277d787fSFarid Zakaria# built when clang, bpftool, the vmlinux BTF and libbpf are all present
51277d787fSFarid Zakaria# (HAVE_BPF_TOOLCHAIN=y forces it) so the other exec selftests don't grow
52277d787fSFarid Zakaria# a bpf toolchain dependency.
53277d787fSFarid ZakariaCLANG ?= clang
54277d787fSFarid ZakariaBPFTOOL ?= bpftool
55277d787fSFarid ZakariaVMLINUX_BTF ?= /sys/kernel/btf/vmlinux
56277d787fSFarid ZakariaHAVE_BPF_TOOLCHAIN ?= $(shell command -v $(CLANG) >/dev/null 2>&1 && \
57277d787fSFarid Zakaria			command -v $(BPFTOOL) >/dev/null 2>&1 && \
58277d787fSFarid Zakaria			test -r $(VMLINUX_BTF) && \
59277d787fSFarid Zakaria			pkg-config --exists libbpf 2>/dev/null && echo y)
60277d787fSFarid Zakariaifeq ($(HAVE_BPF_TOOLCHAIN),y)
61277d787fSFarid ZakariaTEST_GEN_PROGS += binfmt_misc_bpf
627baee96fSChristian BraunerTEST_GEN_FILES += bpf_interp.bpf.o nix_origin.bpf.o transparent.bpf.o
637404b147SChristian BraunerTEST_GEN_FILES += loader.bpf.o interp_bind.bpf.o
647404b147SChristian BraunerTEST_GEN_FILES += binfmt_bpf_interp binfmt_bpf_app binfmt_bind_interp
65277d787fSFarid Zakariaelse
66277d787fSFarid Zakaria$(info exec selftests: skipping binfmt_misc_bpf, needs clang, bpftool, vmlinux BTF and libbpf)
67277d787fSFarid Zakariaendif
68277d787fSFarid Zakaria
690f71241aSKees CookEXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*	\
700f71241aSKees Cook	       $(OUTPUT)/S_I*.test
7180d443e8Sbamvor.zhangjian@huawei.com
7226860105SChristian BraunerLOCAL_HDRS += binfmt_misc_common.h
7326860105SChristian Brauner
7480d443e8Sbamvor.zhangjian@huawei.cominclude ../lib.mk
7580d443e8Sbamvor.zhangjian@huawei.com
762a69962bSMickaël SalaünCHECK_EXEC_SAMPLES := $(top_srcdir)/samples/check-exec
772a69962bSMickaël Salaün
78a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/subdir:
79c9b26b81SDavid Drysdale	mkdir -p $@
80472874cfSKees Cook$(OUTPUT)/script: Makefile
81472874cfSKees Cook	echo '#!/bin/bash' > $@
82c9b26b81SDavid Drysdale	echo 'exit $$*' >> $@
83c9b26b81SDavid Drysdale	chmod +x $@
84a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/execveat.symlink: $(OUTPUT)/execveat
85a8ba798bSbamvor.zhangjian@huawei.com	cd $(OUTPUT) && ln -s -f $(shell basename $<) $(shell basename $@)
86a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat
87c9b26b81SDavid Drysdale	cp $< $@
88c9b26b81SDavid Drysdale	chmod -x $@
89b57a2907SKees Cook$(OUTPUT)/load_address.0x%: load_address.c
90b57a2907SKees Cook	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \
91b57a2907SKees Cook		-fPIE -pie $< -o $@
92b57a2907SKees Cook$(OUTPUT)/load_address.static.0x%: load_address.c
93b57a2907SKees Cook	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \
94b57a2907SKees Cook		-fPIE -static-pie $< -o $@
95b083cc81SMickaël Salaün$(OUTPUT)/false: false.c
96b083cc81SMickaël Salaün	$(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
972a69962bSMickaël Salaün$(OUTPUT)/inc: $(CHECK_EXEC_SAMPLES)/inc.c
982a69962bSMickaël Salaün	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
992a69962bSMickaël Salaün$(OUTPUT)/set-exec: $(CHECK_EXEC_SAMPLES)/set-exec.c
1002a69962bSMickaël Salaün	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
1012a69962bSMickaël Salaün$(OUTPUT)/script-exec.inc: $(CHECK_EXEC_SAMPLES)/script-exec.inc
1022a69962bSMickaël Salaün	cp $< $@
1032a69962bSMickaël Salaün$(OUTPUT)/script-noexec.inc: $(CHECK_EXEC_SAMPLES)/script-noexec.inc
1042a69962bSMickaël Salaün	cp $< $@
105277d787fSFarid Zakaria
10625757bc8SChristian Brauner# Reuses setup_userns()/write_file() from the filesystems selftests. Their
10725757bc8SChristian Brauner# wrappers.h wants the uapi headers, so ask for them here rather than widening
10825757bc8SChristian Brauner# CFLAGS for every program in this directory.
10925757bc8SChristian Brauner$(OUTPUT)/binfmt_misc_selfpin: CFLAGS += $(TOOLS_INCLUDES)
11025757bc8SChristian Brauner$(OUTPUT)/binfmt_misc_selfpin: ../filesystems/utils.c
111*f2b69ea2SChristian Brauner$(OUTPUT)/binfmt_misc_interplimit: CFLAGS += $(TOOLS_INCLUDES)
112*f2b69ea2SChristian Brauner$(OUTPUT)/binfmt_misc_interplimit: ../filesystems/utils.c
11325757bc8SChristian Brauner
114277d787fSFarid Zakaria# --- binfmt_misc bpf ('B') handler test ---------------------------------
115277d787fSFarid Zakaria# The struct_ops bpf objects are compiled against the running kernel's BTF.
116277d787fSFarid Zakaria# CLANG/BPFTOOL/VMLINUX_BTF are set above next to the toolchain check;
117277d787fSFarid Zakaria# override LIBBPF_CFLAGS/LDLIBS to point at a libbpf install.
118277d787fSFarid ZakariaBPF_CFLAGS ?= -I$(OUTPUT)
119277d787fSFarid ZakariaLIBBPF_CFLAGS ?=
120277d787fSFarid ZakariaLIBBPF_LDLIBS ?= -lbpf -lelf -lz
121277d787fSFarid Zakaria
122277d787fSFarid Zakaria$(OUTPUT)/vmlinux.h:
123277d787fSFarid Zakaria	$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
124277d787fSFarid Zakaria
12526860105SChristian Brauner# BPF_NO_KFUNC_PROTOTYPES: the programs declare the kfuncs they use themselves.
126277d787fSFarid Zakaria$(OUTPUT)/%.bpf.o: %.bpf.c $(OUTPUT)/vmlinux.h
12726860105SChristian Brauner	$(CLANG) -g -O2 -target bpf -mcpu=v3 -DBPF_NO_KFUNC_PROTOTYPES \
12826860105SChristian Brauner		$(BPF_CFLAGS) $(LIBBPF_CFLAGS) -c $< -o $@
129277d787fSFarid Zakaria
13026860105SChristian Brauner$(OUTPUT)/binfmt_misc_bpf: binfmt_misc_bpf.c binfmt_misc_common.h
131277d787fSFarid Zakaria	$(CC) $(CFLAGS) $(LIBBPF_CFLAGS) $(LDFLAGS) $< $(LIBBPF_LDLIBS) -o $@
132277d787fSFarid Zakaria
133277d787fSFarid Zakaria$(OUTPUT)/binfmt_bpf_interp: binfmt_bpf_interp.c
134277d787fSFarid Zakaria	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
135277d787fSFarid Zakaria
1367404b147SChristian Brauner$(OUTPUT)/binfmt_bind_interp: binfmt_bind_interp.c
1377404b147SChristian Brauner	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
1387404b147SChristian Brauner
13987c50a58SChristian Brauner$(OUTPUT)/binfmt_loader_payload: binfmt_loader_payload.c binfmt_misc_common.h
14087c50a58SChristian Brauner	$(CC) $(CFLAGS) $(LDFLAGS) -fPIE -pie $< -o $@
14187c50a58SChristian Brauner
14287c50a58SChristian Brauner$(OUTPUT)/binfmt_loader_payload_static: binfmt_loader_payload.c binfmt_misc_common.h
14387c50a58SChristian Brauner	$(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
14487c50a58SChristian Brauner
145277d787fSFarid Zakaria# PT_INTERP is set to the literal "$ORIGIN/binfmt_bpf_interp"; the nix_origin
146277d787fSFarid Zakaria# handler resolves it relative to the binary at run time.
147277d787fSFarid Zakaria$(OUTPUT)/binfmt_bpf_app: binfmt_bpf_app.c
148277d787fSFarid Zakaria	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,--dynamic-linker,'$$ORIGIN/binfmt_bpf_interp' $< -o $@
149277d787fSFarid Zakaria
15026860105SChristian BraunerEXTRA_CLEAN += $(OUTPUT)/vmlinux.h $(OUTPUT)/*.bpf.o
151