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 247baee96fSChristian Brauner# Static ('T' flag) transparent binfmt_misc test; the asserting interpreter 257baee96fSChristian Brauner# is shared with the bpf harness's transparent case. No bpf toolchain needed. 267baee96fSChristian BraunerTEST_GEN_PROGS += binfmt_misc_transparent 277baee96fSChristian BraunerTEST_GEN_FILES += binfmt_transparent_interp 287baee96fSChristian Brauner 29*87c50a58SChristian Brauner# 'L' (loader substitution) binfmt_misc test: the payload runs as the main 30*87c50a58SChristian Brauner# image with a copy of the system loader substituted for its PT_INTERP and 31*87c50a58SChristian Brauner# asserts the native identity from inside; the static build proves the 32*87c50a58SChristian Brauner# override is dropped for a binary without PT_INTERP. 33*87c50a58SChristian BraunerTEST_GEN_PROGS += binfmt_misc_loader 34*87c50a58SChristian BraunerTEST_GEN_FILES += binfmt_loader_payload binfmt_loader_payload_static 35*87c50a58SChristian Brauner 36277d787fSFarid Zakaria# binfmt_misc bpf-backed ('B') handler test: a libbpf harness plus its 37277d787fSFarid Zakaria# struct_ops objects and the test interpreter/app it routes between. Only 38277d787fSFarid Zakaria# built when clang, bpftool, the vmlinux BTF and libbpf are all present 39277d787fSFarid Zakaria# (HAVE_BPF_TOOLCHAIN=y forces it) so the other exec selftests don't grow 40277d787fSFarid Zakaria# a bpf toolchain dependency. 41277d787fSFarid ZakariaCLANG ?= clang 42277d787fSFarid ZakariaBPFTOOL ?= bpftool 43277d787fSFarid ZakariaVMLINUX_BTF ?= /sys/kernel/btf/vmlinux 44277d787fSFarid ZakariaHAVE_BPF_TOOLCHAIN ?= $(shell command -v $(CLANG) >/dev/null 2>&1 && \ 45277d787fSFarid Zakaria command -v $(BPFTOOL) >/dev/null 2>&1 && \ 46277d787fSFarid Zakaria test -r $(VMLINUX_BTF) && \ 47277d787fSFarid Zakaria pkg-config --exists libbpf 2>/dev/null && echo y) 48277d787fSFarid Zakariaifeq ($(HAVE_BPF_TOOLCHAIN),y) 49277d787fSFarid ZakariaTEST_GEN_PROGS += binfmt_misc_bpf 507baee96fSChristian BraunerTEST_GEN_FILES += bpf_interp.bpf.o nix_origin.bpf.o transparent.bpf.o 51*87c50a58SChristian BraunerTEST_GEN_FILES += loader.bpf.o 52277d787fSFarid ZakariaTEST_GEN_FILES += binfmt_bpf_interp binfmt_bpf_app 53277d787fSFarid Zakariaelse 54277d787fSFarid Zakaria$(info exec selftests: skipping binfmt_misc_bpf, needs clang, bpftool, vmlinux BTF and libbpf) 55277d787fSFarid Zakariaendif 56277d787fSFarid Zakaria 570f71241aSKees CookEXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx* \ 580f71241aSKees Cook $(OUTPUT)/S_I*.test 5980d443e8Sbamvor.zhangjian@huawei.com 6026860105SChristian BraunerLOCAL_HDRS += binfmt_misc_common.h 6126860105SChristian Brauner 6280d443e8Sbamvor.zhangjian@huawei.cominclude ../lib.mk 6380d443e8Sbamvor.zhangjian@huawei.com 642a69962bSMickaël SalaünCHECK_EXEC_SAMPLES := $(top_srcdir)/samples/check-exec 652a69962bSMickaël Salaün 66a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/subdir: 67c9b26b81SDavid Drysdale mkdir -p $@ 68472874cfSKees Cook$(OUTPUT)/script: Makefile 69472874cfSKees Cook echo '#!/bin/bash' > $@ 70c9b26b81SDavid Drysdale echo 'exit $$*' >> $@ 71c9b26b81SDavid Drysdale chmod +x $@ 72a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/execveat.symlink: $(OUTPUT)/execveat 73a8ba798bSbamvor.zhangjian@huawei.com cd $(OUTPUT) && ln -s -f $(shell basename $<) $(shell basename $@) 74a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat 75c9b26b81SDavid Drysdale cp $< $@ 76c9b26b81SDavid Drysdale chmod -x $@ 77b57a2907SKees Cook$(OUTPUT)/load_address.0x%: load_address.c 78b57a2907SKees Cook $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \ 79b57a2907SKees Cook -fPIE -pie $< -o $@ 80b57a2907SKees Cook$(OUTPUT)/load_address.static.0x%: load_address.c 81b57a2907SKees Cook $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \ 82b57a2907SKees Cook -fPIE -static-pie $< -o $@ 83b083cc81SMickaël Salaün$(OUTPUT)/false: false.c 84b083cc81SMickaël Salaün $(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@ 852a69962bSMickaël Salaün$(OUTPUT)/inc: $(CHECK_EXEC_SAMPLES)/inc.c 862a69962bSMickaël Salaün $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ 872a69962bSMickaël Salaün$(OUTPUT)/set-exec: $(CHECK_EXEC_SAMPLES)/set-exec.c 882a69962bSMickaël Salaün $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ 892a69962bSMickaël Salaün$(OUTPUT)/script-exec.inc: $(CHECK_EXEC_SAMPLES)/script-exec.inc 902a69962bSMickaël Salaün cp $< $@ 912a69962bSMickaël Salaün$(OUTPUT)/script-noexec.inc: $(CHECK_EXEC_SAMPLES)/script-noexec.inc 922a69962bSMickaël Salaün cp $< $@ 93277d787fSFarid Zakaria 94277d787fSFarid Zakaria# --- binfmt_misc bpf ('B') handler test --------------------------------- 95277d787fSFarid Zakaria# The struct_ops bpf objects are compiled against the running kernel's BTF. 96277d787fSFarid Zakaria# CLANG/BPFTOOL/VMLINUX_BTF are set above next to the toolchain check; 97277d787fSFarid Zakaria# override LIBBPF_CFLAGS/LDLIBS to point at a libbpf install. 98277d787fSFarid ZakariaBPF_CFLAGS ?= -I$(OUTPUT) 99277d787fSFarid ZakariaLIBBPF_CFLAGS ?= 100277d787fSFarid ZakariaLIBBPF_LDLIBS ?= -lbpf -lelf -lz 101277d787fSFarid Zakaria 102277d787fSFarid Zakaria$(OUTPUT)/vmlinux.h: 103277d787fSFarid Zakaria $(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@ 104277d787fSFarid Zakaria 10526860105SChristian Brauner# BPF_NO_KFUNC_PROTOTYPES: the programs declare the kfuncs they use themselves. 106277d787fSFarid Zakaria$(OUTPUT)/%.bpf.o: %.bpf.c $(OUTPUT)/vmlinux.h 10726860105SChristian Brauner $(CLANG) -g -O2 -target bpf -mcpu=v3 -DBPF_NO_KFUNC_PROTOTYPES \ 10826860105SChristian Brauner $(BPF_CFLAGS) $(LIBBPF_CFLAGS) -c $< -o $@ 109277d787fSFarid Zakaria 11026860105SChristian Brauner$(OUTPUT)/binfmt_misc_bpf: binfmt_misc_bpf.c binfmt_misc_common.h 111277d787fSFarid Zakaria $(CC) $(CFLAGS) $(LIBBPF_CFLAGS) $(LDFLAGS) $< $(LIBBPF_LDLIBS) -o $@ 112277d787fSFarid Zakaria 113277d787fSFarid Zakaria$(OUTPUT)/binfmt_bpf_interp: binfmt_bpf_interp.c 114277d787fSFarid Zakaria $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ 115277d787fSFarid Zakaria 116*87c50a58SChristian Brauner$(OUTPUT)/binfmt_loader_payload: binfmt_loader_payload.c binfmt_misc_common.h 117*87c50a58SChristian Brauner $(CC) $(CFLAGS) $(LDFLAGS) -fPIE -pie $< -o $@ 118*87c50a58SChristian Brauner 119*87c50a58SChristian Brauner$(OUTPUT)/binfmt_loader_payload_static: binfmt_loader_payload.c binfmt_misc_common.h 120*87c50a58SChristian Brauner $(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@ 121*87c50a58SChristian Brauner 122277d787fSFarid Zakaria# PT_INTERP is set to the literal "$ORIGIN/binfmt_bpf_interp"; the nix_origin 123277d787fSFarid Zakaria# handler resolves it relative to the binary at run time. 124277d787fSFarid Zakaria$(OUTPUT)/binfmt_bpf_app: binfmt_bpf_app.c 125277d787fSFarid Zakaria $(CC) $(CFLAGS) $(LDFLAGS) -Wl,--dynamic-linker,'$$ORIGIN/binfmt_bpf_interp' $< -o $@ 126277d787fSFarid Zakaria 12726860105SChristian BraunerEXTRA_CLEAN += $(OUTPUT)/vmlinux.h $(OUTPUT)/*.bpf.o 128