kexec: Add bpf light skeleton to load zboot image
A message that is not in the archive (sent off-list or not yet received): [email protected]
RFC 5/7 kexec: Add bpf light skeleton to load zboot image
This light skeleton is generated by 'bpftool gen skeleton -L '.
The bpf-progrom C code is [1].
The interface between bpf-prog and the kernel are constituted by:
four ringbuf
an array
SEC("fentry/bpf_handle_pefile") SEC("fentry/bpf_post_handle_pefile")
They are fixed and provided for all kinds of bpf-prog which interacts with the kexec kernel component.
[1]:
// SPDX-License-Identifier: GPL-2.0
static long (* const bpf_kexec_decompress)(char *image_gz_payload, int image_gz_sz, unsigned int decompressed_sz, struct bpf_map *map, unsigned int *key) = (void *)212; static long (* const bpf_kexec_carrier)(char *name, struct bpf_map *map, void *buf) = (void *)213;
/* 1GB = 1^28 * sizeof(__uint) */
/* 512MB is big enough to hold either kernel or initramfs */
/* ringbuf is safe since the user space has no write access to them */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, MAX_BUF_SIZE);
} ringbuf_1 SEC(".maps");struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, MAX_BUF_SIZE);
} ringbuf_2 SEC(".maps");struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, MAX_BUF_SIZE);
} ringbuf_3 SEC(".maps");struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, MAX_BUF_SIZE);
} ringbuf_4 SEC(".maps");struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 10);
__type(key, unsigned int);
__type(value, struct mem_range_result);
} mem_range_map SEC(".maps");char LICENSE[] SEC("license") = "GPL";
/* see drivers/firmware/efi/libstub/zboot-header.S */
struct linux_pe_zboot_header {
unsigned int mz_magic;
char image_type[4];
unsigned int payload_offset;
unsigned int payload_size;
unsigned int reserved[2];
char comp_type[4];
unsigned int linux_pe_magic;
unsigned int pe_header_offset;
} __attribute__((packed));SEC("fentry/bpf_handle_pefile")
int BPF_PROG(parse_pe, char *image_buf, unsigned int image_sz, char *unused_initrd,
unsigned int unused_initrd_sz, char *unused_cmd)
{
struct linux_pe_zboot_header *zboot_header;
char *image_gz_payload;
int image_gz_sz;
unsigned int decompressed_sz;
char *decompressed_buf;
char *buf;
unsigned int key = 0;
bpf_printk("begin parse PE\n");
if (!image_buf || (image_sz > MAX_RECORD_SIZE)) {
bpf_printk("Err: image size is greater than 0x%lx\n", MAX_RECORD_SIZE);
return 0;
}
/* In order to access bytes not aligned on 2 order, copy into ringbuf */
buf = (char *)bpf_ringbuf_reserve(&ringbuf_1, sizeof(struct linux_pe_zboot_header), 0);
if (!buf) {
bpf_printk("Err: fail to reserve ringbuf to parse zboot header\n");
return 0;
}
/* Ensure the second parameter for bpf_probe_read() is Positive */
image_sz = image_sz & (MAX_RECORD_SIZE - 1);
bpf_probe_read((void *)buf, sizeof(struct linux_pe_zboot_header), image_buf);
zboot_header = (struct linux_pe_zboot_header *)buf;
if (!!__builtin_memcmp(&zboot_header->image_type, "zimg",
sizeof(zboot_header->image_type))) {
bpf_printk("Err: image is not zboot image\n");
bpf_ringbuf_discard(buf, BPF_RB_NO_WAKEUP);
return 0;
}
unsigned int payload_offset = zboot_header->payload_offset;
unsigned int payload_size = zboot_header->payload_size;
bpf_ringbuf_discard(buf, BPF_RB_NO_WAKEUP);
image_gz_sz = payload_size - 4;
if (image_gz_sz <= 0 || image_gz_sz + 4 > image_sz) {
bpf_printk("Invalid offset for decompressed size\n");
return 0;
}
/* Ensure the boundary to make verifier satisfied */
unsigned int d_pos = (payload_offset + image_gz_sz) & (MAX_RECORD_SIZE - 1);
/* appended le32 is the size */
bpf_probe_read((void *)&decompressed_sz, sizeof(int), image_buf + d_pos);
decompressed_sz = le32toh(decompressed_sz);
bpf_printk("payload_offset:0x%lx, payload_size:0x%lx, decompressed size:0x%lx\n",
payload_offset, payload_size, decompressed_sz);
if (decompressed_sz == 0) {
bpf_printk("decompressed size %d is wrong\n", decompressed_sz);
return 0;
}
/* Strict check on pointer */
if (payload_offset >= MAX_RECORD_SIZE ) {
bpf_printk("Err: payload_offset > 0x%lx\n", MAX_RECORD_SIZE);
return 0;
}
buf = (char *)bpf_ringbuf_reserve(&ringbuf_1, MAX_RECORD_SIZE, 0);
if (!buf) {
bpf_printk("Err: fail to reserve from ringbuf_1 for reading payload\n");
return 0;
}
bpf_probe_read((void *)buf, payload_size, image_buf + payload_offset);
struct bpf_map *map_ptr = (struct bpf_map *)&mem_range_map;
bpf_printk("Calling bpf_kexec_decompress()\n");
bpf_kexec_decompress(buf, payload_size - 4, decompressed_sz, map_ptr, &key);
bpf_ringbuf_discard(buf, BPF_RB_NO_WAKEUP);
struct mem_range_result *result;
result = bpf_map_lookup_elem(&mem_range_map, &key);
if (!!result) {
bpf_printk("Calling bpf_kexec_carrier()\n");
bpf_kexec_carrier(KEXEC_RES_KERNEL_NAME, &mem_range_map, result);
}
return 0;
}SEC("fentry/bpf_post_handle_pefile")
int BPF_PROG(post_parse_pe, char *image_buf, int buf_sz)
{
return 0;
}--- kernel/Makefile | 1 + kernel/kexec_pe_image.c | 12 +- kernel/kexec_pe_parser_lskel.h | 620 +++++++++++++++++++++++++++++++++ 3 files changed, 632 insertions(+), 1 deletion(-) create mode 100644 kernel/kexec_pe_parser_lskel.h
kernel/Makefile +1 -0
@@ -134,6 +134,7 @@ obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o CFLAGS_stackleak.o += $(DISABLE_STACKLEAK_PLUGIN) +CFLAGS_kexec_pe_image.o += -I$(srctree)/tools/lib obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o KASAN_SANITIZE_stackleak.o := n KCSAN_SANITIZE_stackleak.o := n
kernel/kexec_pe_image.c +11 -1
@@ -13,6 +13,7 @@#include <linux/kernel.h> #include <linux/vmalloc.h> #include <linux/kexec.h> +#include <linux/elf.h> #include <linux/pe.h> #include <linux/string.h> #include <linux/bpf.h>
@@ -22,7 +23,7 @@#include <asm/cpufeature.h> #include <asm/image.h> #include <asm/memory.h> - +#include "kexec_pe_parser_lskel.h" static LIST_HEAD (phase_head);
@@ -288,15 +289,24 @@ static bool pe_has_bpf_section(char *file_buf, unsigned long pe_sz)return true; } +static struct kexec_pe_parser_bpf *pe_parser; +/* Load a ELF */ static int arm_bpf_prog (char *bpf_elf,unsigned long sz) { + pe_parser =kexec_pe_parser_bpf__open_and_load (); +if (!pe_parser) +return -1 ; +kexec_pe_parser_bpf__attach (pe_parser); +return 0 ; }//todo, release memory resource used by bpf_kexec_decompress() static void disarm_bpf_prog (void ) { +kexec_pe_parser_bpf__destroy (pe_parser); + pe_parser =NULL ; }/*
kernel/kexec_pe_parser_lskel.h new file +620 -0
620 lines not on this page · raw message
Signature · 1 line
2.47.0
RFC 6/7 kexec: Loading bpf opts_insn and opts_data from user space
In reply to a message not in the archiveThe interface between bpf-prog and the kernel are constituted by:
four ringbuf
an array
SEC("fentry/bpf_handle_pefile") SEC("fentry/bpf_post_handle_pefile")
They are fixed and shared by all kexec bpf-prog.
For different kexec bpf-prog, the opts_data[] and opts_insn[] generated by 'bpftool gen skeleton -L ' contains the bytecode to parse different file format. They can be split out and compiled as an ELF. The ELF can be embeded in a PE file (e.g. vmlinuz.efi or UKI) as a '.bpf' section. So opts_data[] and opts_insn[] can be extracted and run in kernel.
By this way, the integrity of bpf-prog can be protected by the signature of the outermost PE file.
--- kernel/kexec_pe_image.c | 60 ++++ kernel/kexec_pe_parser_lskel.h | 484 +-------------------------------- 2 files changed, 65 insertions(+), 479 deletions(-)
kernel/kexec_pe_image.c +60 -0
@@ -291,9 +291,67 @@ static bool pe_has_bpf_section(char *file_buf, unsigned long pe_sz)static struct kexec_pe_parser_bpf *pe_parser; +static void *get_symbol_from_elf (const char *elf_data,size_t elf_size, +const char *symbol_name,unsigned int *symbol_size) +{ +Elf_Ehdr *ehdr = (Elf_Ehdr *)elf_data; +Elf_Shdr *shdr, *symtab_shdr, *strtab_shdr, *dst_shdr; +Elf64_Sym *sym, *symtab =NULL ; +char *strtab =NULL ; +void *symbol_data =NULL ; +int i; + + symtab_shdr = strtab_shdr =NULL ; +if (memcmp (ehdr->e_ident,ELFMAG ,SELFMAG ) !=0 ) { +pr_err ("Not a valid ELF file\n" ); + goto out; + } + + shdr = (struct elf_shdr *)(elf_data + ehdr->e_shoff); +for (i =0 ; i < ehdr->e_shnum; i++) { +if (shdr[i].sh_type ==SHT_SYMTAB ) { + symtab_shdr = &shdr[i]; + }else if (shdr[i].sh_type ==SHT_STRTAB && i != ehdr->e_shstrndx) { + strtab_shdr = &shdr[i]; + } + } + +if (!symtab_shdr || !strtab_shdr) { +pr_err ("Symbol table or string table not found\n" ); + goto out; + } + symtab = (Elf64_Sym *)(elf_data + symtab_shdr->sh_offset); + strtab = (char *)(elf_data + strtab_shdr->sh_offset); +for (i =0 ; i < symtab_shdr->sh_size /sizeof (Elf64_Sym); i++) { + sym = &symtab[i]; +if (strcmp (&strtab[sym->st_name], symbol_name) ==0 ) { +if (sym->st_shndx >=SHN_LORESERVE ) +return NULL ;// No section data for these + dst_shdr = &shdr[sym->st_shndx]; + symbol_data = (void *)(elf_data + dst_shdr->sh_offset + sym->st_value); + *symbol_size = symtab[i].st_size; +break ; + } + } + +out: +return symbol_data; +} +/* Load a ELF */ static int arm_bpf_prog (char *bpf_elf,unsigned long sz) { + opts_data =get_symbol_from_elf (bpf_elf, sz,"opts_data" , &opts_data_sz); + opts_insn =get_symbol_from_elf (bpf_elf, sz,"opts_insn" , &opts_insn_sz); +if (!opts_data || !opts_insn) +return -1 ; +/* +* When light skeleton generates opts_data[] and opts_insn[], it appends a +* NULL terminator at the end of string +*/ + opts_data_sz = opts_data_sz -1 ; + opts_insn_sz = opts_insn_sz -1 ; + pe_parser =kexec_pe_parser_bpf__open_and_load ();if (!pe_parser)return -1 ;
@@ -307,6 +365,8 @@ static void disarm_bpf_prog(void) {kexec_pe_parser_bpf__destroy (pe_parser); pe_parser =NULL ; + opts_data =NULL ; + opts_insn =NULL ; }/*
kernel/kexec_pe_parser_lskel.h +5 -479
498 lines not on this page · raw message
Signature · 1 line
2.47.0