| 1 | |
| 2 | |
| 3 | * binfmt_misc_ops handler for the loader-substitution case: match the
|
| 4 | * marker the harness poked into the payload's e_ident padding and ask for
|
| 5 | * the selected interpreter to be substituted for the binary's PT_INTERP,
|
| 6 | * so the binary itself runs as a fully native exec.
|
| 7 |
|
| 8 | #include "vmlinux.h"
|
| 9 | #include <bpf/bpf_helpers.h>
|
| 10 | #include <bpf/bpf_tracing.h>
|
| 11 |
|
| 12 | char _license[] SEC("license") = "GPL";
|
| 13 |
|
| 14 | #define EI_CLASS 4
|
| 15 | #define EI_PAD 9
|
| 16 | #define ELFCLASS64 2
|
| 17 |
|
| 18 | extern int bpf_binprm_set_interp(struct linux_binprm *bprm, const char *path,
|
| 19 | size_t path__sz) __ksym;
|
| 20 | extern int bpf_binprm_set_flags(struct linux_binprm *bprm,
|
| 21 | enum bpf_binprm_flags flags) __ksym;
|
| 22 |
|
| 23 | SEC("struct_ops.s/match")
|
| 24 | bool BPF_PROG(loader_match, struct linux_binprm *bprm)
|
| 25 | {
|
| 26 | if (bprm->buf[0] != 0x7f || bprm->buf[1] != 'E' ||
|
| 27 | bprm->buf[2] != 'L' || bprm->buf[3] != 'F' ||
|
| 28 | bprm->buf[EI_CLASS] != ELFCLASS64)
|
| 29 | return false;
|
| 30 |
|
| 31 |
|
| 32 | return bprm->buf[EI_PAD + 0] == 'L' && bprm->buf[EI_PAD + 1] == 'D' &&
|
| 33 | bprm->buf[EI_PAD + 2] == 'R' && bprm->buf[EI_PAD + 3] == 'T' &&
|
| 34 | bprm->buf[EI_PAD + 4] == 'S' && bprm->buf[EI_PAD + 5] == 'T';
|
| 35 | }
|
| 36 |
|
| 37 | SEC("struct_ops.s/load")
|
| 38 | int BPF_PROG(loader_load, struct linux_binprm *bprm)
|
| 39 | {
|
| 40 | char interp[] = "/tmp/binfmt_loader_interp";
|
| 41 | int err;
|
| 42 |
|
| 43 | err = bpf_binprm_set_flags(bprm, BPF_BINPRM_LOADER);
|
| 44 | if (err)
|
| 45 | return err;
|
| 46 |
|
| 47 |
|
| 48 | return bpf_binprm_set_interp(bprm, interp, sizeof(interp));
|
| 49 | }
|
| 50 |
|
| 51 | SEC(".struct_ops.link")
|
| 52 | struct binfmt_misc_ops loader = {
|
| 53 | .match = (void *)loader_match,
|
| 54 | .load = (void *)loader_load,
|
| 55 | .name = "loader",
|
| 56 | };
|