bpf: allow loading instructions from a fd
PATCH 1/1 bpf: allow loading instructions from a fd
From: Matteo Croce <[email protected]>
Allow to load the BPF instructons from a file descriptor, other than a pointer.
This is required by the Integrity Subsystem to validate the source of the instructions.
In bpf_attr replace 'insns', which is an u64, to a union containing also the file descriptor as int.
A new BPF_F_LOAD_BY_FD flag tells bpf_prog_load() to load the instructions from file descriptor and ignore the pointer.
As BPF files usually are regular ELF files, start reading from the current file position, so the userspace can skip the ELF header and jump to the right section.
--- include/uapi/linux/bpf.h | 10 +++++- kernel/bpf/syscall.c | 69 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 6 deletions(-)
include/uapi/linux/bpf.h +9 -1
@@ -332,6 +332,11 @@ enum bpf_link_type {/* The verifier internal test flag. Behavior is undefined */ #define BPF_F_TEST_STATE_FREQ (1U << 3) +/* The BPF is loaded by the file descriptor in `prog_bpf_fd` +* instead of the buffer pointed by `insns`. +*/ +#define BPF_F_LOAD_BY_FD (1U << 4) +/* When BPF ldimm64's insn[0].src_reg != 0 then this can have * two extensions: *
@@ -482,7 +487,10 @@ union bpf_attr {struct {/* anonymous struct used by BPF_PROG_LOAD command */ __u32 prog_type;/* one of enum bpf_prog_type */ __u32 insn_cnt; -__aligned_u64 insns; +union { +__aligned_u64 insns;/* BPF instructions */ +__u32 prog_bpf_fd;/* fd pointing to BPF program */ + };__aligned_u64 license;__u32 log_level;/* verbosity level of verifier */ __u32 log_size;/* size of user buffer */
kernel/bpf/syscall.c +64 -5
93 lines not on this page · raw message
Signature · 1 line
2.26.2
Quoted 8 lines · Matteo Croce
From: Matteo Croce <[email protected]>
Allow to load the BPF instructons from a file descriptor, other than a pointer.
This is required by the Integrity Subsystem to validate the source of the instructions.
Quoted 8 lines
In
bpf_attrreplace 'insns', which is an u64, to a union containing also the file descriptor as int.
A newBPF_F_LOAD_BY_FDflag tellsbpf_prog_load()to load the instructions from file descriptor and ignore the pointer.As BPF files usually are regular ELF files, start reading from the current file position, so the userspace can skip the ELF header and jump to the right section.
That is not the case at all.
Have you looked at amount of work libbpf is doing with elf file before raw instructions become suitable to be loaded by the kernel?
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Matteo Croce
From: Matteo Croce <[email protected]>
Allow to load the BPF instructons from a file descriptor, other than a pointer.
This is required by the Integrity Subsystem to validate the source of the instructions.
In
bpf_attrreplace 'insns', which is an u64, to a union containing also the file descriptor as int.
A newBPF_F_LOAD_BY_FDflag tellsbpf_prog_load()to load the instructions from file descriptor and ignore the pointer.
Quoted 7 lines
Quoted 3 lines
As BPF files usually are regular ELF files, start reading from the current file position, so the userspace can skip the ELF header and jump to the right section.
That is not the case at all.
Have you looked at amount of work libbpf is doing with elf file before raw instructions become suitable to be loaded by the kernel?
I see now what bpf_object__relocate() and all the *reloc* functions do, so it can't be done this way, I see.
A malicious BPF file can be as bad as a malicious binary. Let's say I want to assert code integrity for BPF files, what could be a viable option?
Perhaps a signature in the object file as we do with modules?
Regards,
Signature · 1 line
per aspera ad upstream
Quoted 25 lines · Matteo Croce
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Matteo Croce
From: Matteo Croce <[email protected]>
Allow to load the BPF instructons from a file descriptor, other than a pointer.
This is required by the Integrity Subsystem to validate the source of the instructions.
In
bpf_attrreplace 'insns', which is an u64, to a union containing also the file descriptor as int.
A newBPF_F_LOAD_BY_FDflag tellsbpf_prog_load()to load the instructions from file descriptor and ignore the pointer.Quoted 7 lines
Quoted 3 lines
As BPF files usually are regular ELF files, start reading from the current file position, so the userspace can skip the ELF header and jump to the right section.
That is not the case at all.
Have you looked at amount of work libbpf is doing with elf file before raw instructions become suitable to be loaded by the kernel?
Quoted 7 lines
I see now what
bpf_object__relocate()and all the *reloc* functions do, so it can't be done this way, I see.A malicious BPF file can be as bad as a malicious binary. Let's say I want to assert code integrity for BPF files, what could be a viable option?
Perhaps a signature in the object file as we do with modules?
It's a hard problem to solve.
Signing bpf programs was proposed in the past. It may work, but challenging to implement, since even simplest programs are being modified by the user space loader before kernel sees them. The signature would have to skip all such instructions which makes the signature verification 'best effort'. Some instructions won't be covered by signature (like ld_imm64 that points to a map).