Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030283AbbEOHw4 (ORCPT ); Fri, 15 May 2015 03:52:56 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:13545 "EHLO szxga03-in.huawei.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S934282AbbEOHwt (ORCPT ); Fri, 15 May 2015 03:52:49 -0400 From: Wang Nan To: , , , , , , , , , , , CC: , , , Subject: [RFC PATCH v2 08/37] tools lib bpf: open eBPF object file and do basic validation. Date: Fri, 15 May 2015 07:51:01 +0000 Message-ID: <1431676290-1230-9-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431676290-1230-1-git-send-email-wangnan0@huawei.com> References: <1431676290-1230-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.5555A599.00B3,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 303a6d99e9bea5f275abeb754284cb5b Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4287 Lines: 202 This patch adds basic 'struct bpf_object' which will be used for eBPF object file loading. eBPF object files are compiled by LLVM as ELF format. In this patch, libelf is used to open those files, read EHDR and do basic validation according to e_type and e_machine. All elf related staffs are grouped together and reside in elf field of 'struct bpf_object'. bpf_obj_clear_elf() is introduced to clear it. Signed-off-by: Wang Nan --- tools/lib/bpf/libbpf.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index f8decff..43c16bc 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -12,9 +12,12 @@ #include #include #include +#include #include #include #include +#include +#include #include "libbpf.h" @@ -58,12 +61,161 @@ void libbpf_set_print(int (*warn)(const char *format, ...), __pr_debug = debug; } -struct bpf_object *bpf_open_object(const char *path) +#ifdef HAVE_LIBELF_MMAP_SUPPORT +# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP +#else +# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ +#endif + +struct bpf_object { + char *path; + + /* + * Information when doing elf related work. Only valid if fd + * is valid. + */ + struct { + int fd; + Elf *elf; + GElf_Ehdr ehdr; + } elf; +}; +#define obj_elf_valid(o) ((o)->elf.fd >= 0) + +static struct bpf_object *__bpf_obj_alloc(const char *path) +{ + struct bpf_object *obj; + + obj = calloc(1, sizeof(struct bpf_object)); + if (!obj) { + pr_warning("alloc memory failed for %s\n", path); + return NULL; + } + + obj->path = strdup(path); + if (!obj->path) { + pr_warning("failed to strdup '%s'\n", path); + free(obj); + return NULL; + } + return obj; +} + +static struct bpf_object *bpf_obj_alloc(const char *path) { + struct bpf_object *obj; + + obj = __bpf_obj_alloc(path); + if (!obj) + goto out; + + obj->elf.fd = -1; + return obj; +out: + bpf_close_object(obj); return NULL; } -void bpf_close_object(struct bpf_object *object) +static void bpf_obj_clear_elf(struct bpf_object *obj) +{ + if (!obj_elf_valid(obj)) + return; + + if (obj->elf.elf) { + elf_end(obj->elf.elf); + obj->elf.elf = NULL; + } + if (obj->elf.fd >= 0) { + close(obj->elf.fd); + obj->elf.fd = -1; + } +} + +static int bpf_obj_elf_init(struct bpf_object *obj) { + int err = 0; + GElf_Ehdr *ep; + + if (obj_elf_valid(obj)) { + pr_warning("elf init: internal error\n"); + return -EEXIST; + } + + obj->elf.fd = open(obj->path, O_RDONLY); + if (obj->elf.fd < 0) { + pr_warning("failed to open %s: %s\n", obj->path, + strerror(errno)); + return -errno; + } + + obj->elf.elf = elf_begin(obj->elf.fd, + LIBBPF_ELF_C_READ_MMAP, + NULL); + if (!obj->elf.elf) { + pr_warning("failed to open %s as ELF file\n", + obj->path); + err = -EINVAL; + goto errout; + } + + if (!gelf_getehdr(obj->elf.elf, &obj->elf.ehdr)) { + pr_warning("failed to get EHDR from %s\n", + obj->path); + err = -EINVAL; + goto errout; + } + ep = &obj->elf.ehdr; + + if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) { + pr_warning("%s is not an eBPF object file\n", + obj->path); + err = -EINVAL; + goto errout; + } + return 0; +errout: + bpf_obj_clear_elf(obj); + return err; +} + +struct bpf_object *bpf_open_object(const char *path) +{ + struct bpf_object *obj; + + /* param validation */ + if (!path) + return NULL; + + pr_debug("loading %s\n", path); + + if (elf_version(EV_CURRENT) == EV_NONE) { + pr_warning("failed to init libelf for %s\n", path); + return NULL; + } + + obj = bpf_obj_alloc(path); + if (!obj) + return NULL; + + if (bpf_obj_elf_init(obj)) + goto out; + + return obj; + +out: + bpf_close_object(obj); + return NULL; +} + +void bpf_close_object(struct bpf_object *obj) +{ + if (!obj) + return; + + bpf_obj_clear_elf(obj); + + if (obj->path) + free(obj->path); + free(obj); } -- 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/