xref: /linux/tools/testing/selftests/exec/Makefile (revision 14c897740388216acd59477a382de327d5e27ba4)
1# SPDX-License-Identifier: GPL-2.0
2CFLAGS = -Wall
3CFLAGS += -Wno-nonnull
4CFLAGS += $(KHDR_INCLUDES)
5
6LDLIBS += -lcap
7
8ALIGNS := 0x1000 0x200000 0x1000000
9ALIGN_PIES        := $(patsubst %,load_address.%,$(ALIGNS))
10ALIGN_STATIC_PIES := $(patsubst %,load_address.static.%,$(ALIGNS))
11ALIGNMENT_TESTS   := $(ALIGN_PIES) $(ALIGN_STATIC_PIES)
12
13TEST_PROGS := binfmt_script.py check-exec-tests.sh
14TEST_GEN_PROGS := execveat non-regular $(ALIGNMENT_TESTS)
15TEST_GEN_PROGS_EXTENDED := false inc set-exec script-exec.inc script-noexec.inc
16TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir
17# Makefile is a run-time dependency, since it's accessed by the execveat test
18TEST_FILES := Makefile
19
20TEST_GEN_PROGS += recursion-depth
21TEST_GEN_PROGS += null-argv
22TEST_GEN_PROGS += check-exec
23
24# binfmt_misc must not be reachable as an exec source or as a stacking layer,
25# or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf.
26TEST_GEN_PROGS += binfmt_misc_selfpin
27
28# The interpreters an 'F' or 'B' entry pre-opens are charged against
29# UCOUNT_BINFMT_MISC_INTERPRETERS. Unprivileged, no bpf.
30TEST_GEN_PROGS += binfmt_misc_interplimit
31
32# 'D' (register disabled) binfmt_misc test: an entry that exists but does
33# not dispatch until it is enabled. Static magic entry, no bpf toolchain.
34TEST_GEN_PROGS += binfmt_misc_disabled
35
36# Static ('T' flag) transparent binfmt_misc test; the asserting interpreter
37# is shared with the bpf harness's transparent case. No bpf toolchain needed.
38TEST_GEN_PROGS += binfmt_misc_transparent
39TEST_GEN_FILES += binfmt_transparent_interp
40
41# 'L' (loader substitution) binfmt_misc test: the payload runs as the main
42# image with a copy of the system loader substituted for its PT_INTERP and
43# asserts the native identity from inside; the static build proves the
44# override is dropped for a binary without PT_INTERP.
45TEST_GEN_PROGS += binfmt_misc_loader
46TEST_GEN_FILES += binfmt_loader_payload binfmt_loader_payload_static
47
48# Only ASCII punctuation delimits the fields of a register string, so a new
49# flag character cannot change which strings register. No bpf toolchain.
50TEST_GEN_PROGS += binfmt_misc_delim
51
52# binfmt_misc bpf-backed ('B') handler test: a libbpf harness plus its
53# struct_ops objects and the test interpreter/app it routes between. Only
54# built when clang, bpftool, the vmlinux BTF and libbpf are all present
55# (HAVE_BPF_TOOLCHAIN=y forces it) so the other exec selftests don't grow
56# a bpf toolchain dependency.
57CLANG ?= clang
58BPFTOOL ?= bpftool
59VMLINUX_BTF ?= /sys/kernel/btf/vmlinux
60HAVE_BPF_TOOLCHAIN ?= $(shell command -v $(CLANG) >/dev/null 2>&1 && \
61			command -v $(BPFTOOL) >/dev/null 2>&1 && \
62			test -r $(VMLINUX_BTF) && \
63			pkg-config --exists libbpf 2>/dev/null && echo y)
64ifeq ($(HAVE_BPF_TOOLCHAIN),y)
65TEST_GEN_PROGS += binfmt_misc_bpf
66TEST_GEN_FILES += bpf_interp.bpf.o nix_origin.bpf.o transparent.bpf.o
67TEST_GEN_FILES += loader.bpf.o interp_bind.bpf.o
68TEST_GEN_FILES += binfmt_bpf_interp binfmt_bpf_app binfmt_bind_interp
69else
70$(info exec selftests: skipping binfmt_misc_bpf, needs clang, bpftool, vmlinux BTF and libbpf)
71endif
72
73EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*	\
74	       $(OUTPUT)/S_I*.test
75
76LOCAL_HDRS += binfmt_misc_common.h
77
78include ../lib.mk
79
80CHECK_EXEC_SAMPLES := $(top_srcdir)/samples/check-exec
81
82$(OUTPUT)/subdir:
83	mkdir -p $@
84$(OUTPUT)/script: Makefile
85	echo '#!/bin/bash' > $@
86	echo 'exit $$*' >> $@
87	chmod +x $@
88$(OUTPUT)/execveat.symlink: $(OUTPUT)/execveat
89	cd $(OUTPUT) && ln -s -f $(shell basename $<) $(shell basename $@)
90$(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat
91	cp $< $@
92	chmod -x $@
93$(OUTPUT)/load_address.0x%: load_address.c
94	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \
95		-fPIE -pie $< -o $@
96$(OUTPUT)/load_address.static.0x%: load_address.c
97	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \
98		-fPIE -static-pie $< -o $@
99$(OUTPUT)/false: false.c
100	$(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
101$(OUTPUT)/inc: $(CHECK_EXEC_SAMPLES)/inc.c
102	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
103$(OUTPUT)/set-exec: $(CHECK_EXEC_SAMPLES)/set-exec.c
104	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
105$(OUTPUT)/script-exec.inc: $(CHECK_EXEC_SAMPLES)/script-exec.inc
106	cp $< $@
107$(OUTPUT)/script-noexec.inc: $(CHECK_EXEC_SAMPLES)/script-noexec.inc
108	cp $< $@
109
110# Reuses setup_userns()/write_file() from the filesystems selftests. Their
111# wrappers.h wants the uapi headers, so ask for them here rather than widening
112# CFLAGS for every program in this directory.
113$(OUTPUT)/binfmt_misc_selfpin: CFLAGS += $(TOOLS_INCLUDES)
114$(OUTPUT)/binfmt_misc_selfpin: ../filesystems/utils.c
115$(OUTPUT)/binfmt_misc_interplimit: CFLAGS += $(TOOLS_INCLUDES)
116$(OUTPUT)/binfmt_misc_interplimit: ../filesystems/utils.c
117
118# --- binfmt_misc bpf ('B') handler test ---------------------------------
119# The struct_ops bpf objects are compiled against the running kernel's BTF.
120# CLANG/BPFTOOL/VMLINUX_BTF are set above next to the toolchain check;
121# override LIBBPF_CFLAGS/LDLIBS to point at a libbpf install.
122BPF_CFLAGS ?= -I$(OUTPUT)
123LIBBPF_CFLAGS ?=
124LIBBPF_LDLIBS ?= -lbpf -lelf -lz
125
126$(OUTPUT)/vmlinux.h:
127	$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
128
129# BPF_NO_KFUNC_PROTOTYPES: the programs declare the kfuncs they use themselves.
130$(OUTPUT)/%.bpf.o: %.bpf.c $(OUTPUT)/vmlinux.h
131	$(CLANG) -g -O2 -target bpf -mcpu=v3 -DBPF_NO_KFUNC_PROTOTYPES \
132		$(BPF_CFLAGS) $(LIBBPF_CFLAGS) -c $< -o $@
133
134$(OUTPUT)/binfmt_misc_bpf: binfmt_misc_bpf.c binfmt_misc_common.h
135	$(CC) $(CFLAGS) $(LIBBPF_CFLAGS) $(LDFLAGS) $< $(LIBBPF_LDLIBS) -o $@
136
137$(OUTPUT)/binfmt_bpf_interp: binfmt_bpf_interp.c
138	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
139
140$(OUTPUT)/binfmt_bind_interp: binfmt_bind_interp.c
141	$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
142
143$(OUTPUT)/binfmt_loader_payload: binfmt_loader_payload.c binfmt_misc_common.h
144	$(CC) $(CFLAGS) $(LDFLAGS) -fPIE -pie $< -o $@
145
146$(OUTPUT)/binfmt_loader_payload_static: binfmt_loader_payload.c binfmt_misc_common.h
147	$(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
148
149# PT_INTERP is set to the literal "$ORIGIN/binfmt_bpf_interp"; the nix_origin
150# handler resolves it relative to the binary at run time.
151$(OUTPUT)/binfmt_bpf_app: binfmt_bpf_app.c
152	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,--dynamic-linker,'$$ORIGIN/binfmt_bpf_interp' $< -o $@
153
154EXTRA_CLEAN += $(OUTPUT)/vmlinux.h $(OUTPUT)/*.bpf.o
155