From: Blaise Boscaccy <bboscaccy@linux.microsoft.com>
To: bpf@vger.kernel.org
Cc: nkapron@google.com, teknoraver@meta.com,
roberto.sassu@huawei.com, gregkh@linuxfoundation.org,
paul@paul-moore.com, code@tyhicks.com,
flaniel@linux.microsoft.com
Subject: [PATCH 14/14] bpf: Augment BPF_PROG_LOAD to use in-kernel relocations
Date: Thu, 9 Jan 2025 13:43:56 -0800 [thread overview]
Message-ID: <20250109214617.485144-15-bboscaccy@linux.microsoft.com> (raw)
In-Reply-To: <20250109214617.485144-1-bboscaccy@linux.microsoft.com>
The basic algorithm here is to allow the user to supply a sysfs entry
corresponding to a previously in-kernel relocated elf object, and a
symbol name that they wish to load. From there the loader ignores any
supplied bpf instruction buffers and relies on the in-kernel
representation. However, maps and other associated file descriptors
passed in from userspace are handled as normal.
Signed-off-by: Blaise Boscaccy <bboscaccy@linux.microsoft.com>
---
kernel/bpf/syscall.c | 56 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 49 insertions(+), 7 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ea0401634e752..8159fe75cd359 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2740,9 +2740,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
struct bpf_prog *prog, *dst_prog = NULL;
struct btf *attach_btf = NULL;
struct bpf_token *token = NULL;
+ struct bpf_obj *obj = NULL;
+ struct bpf_prog_obj *prog_obj = NULL;
bool bpf_cap;
- int err;
+ int err, i;
char license[128];
+ char symbol_name[32];
+ struct fd loader_fd;
if (CHECK_ATTR(BPF_PROG_LOAD))
return -EINVAL;
@@ -2855,8 +2859,40 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
goto put_token;
}
+ if (attr->prog_loader_fd) {
+ loader_fd = fdget(attr->prog_loader_fd);
+ if (!fd_file(loader_fd)) {
+ err = -EBADF;
+ goto put_token;
+ }
+
+ obj = fd_file(loader_fd)->private_data;
+
+ /* copy eBPF program symbol name from user space */
+ if (strncpy_from_bpfptr(symbol_name,
+ make_bpfptr(attr->symbol_loader_name, uattr.is_kernel),
+ sizeof(symbol_name) - 1) < 0)
+ goto put_token;
+
+ symbol_name[sizeof(symbol_name) - 1] = 0;
+
+ for (i = 0; i < obj->nr_programs; i++) {
+ if (strcmp(symbol_name, obj->progs[i].name) == 0) {
+ prog_obj = &obj->progs[i];
+ break;
+ }
+ }
+
+ if (!prog_obj)
+ goto put_token;
+ }
+
/* plain bpf_prog allocation */
- prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
+ if (prog_obj)
+ prog = bpf_prog_alloc(bpf_prog_size(prog_obj->insn_cnt), GFP_USER);
+ else
+ prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
+
if (!prog) {
if (dst_prog)
bpf_prog_put(dst_prog);
@@ -2879,13 +2915,19 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
token = NULL;
prog->aux->user = get_current_user();
- prog->len = attr->insn_cnt;
err = -EFAULT;
- if (copy_from_bpfptr(prog->insns,
- make_bpfptr(attr->insns, uattr.is_kernel),
- bpf_prog_insn_size(prog)) != 0)
- goto free_prog;
+ if (prog_obj) {
+ prog->len = prog_obj->insn_cnt;
+ memcpy(prog->insnsi, prog_obj->insn, prog_obj->insn_cnt * sizeof(struct bpf_insn));
+ } else {
+ prog->len = attr->insn_cnt;
+ if (copy_from_bpfptr(prog->insns,
+ make_bpfptr(attr->insns, uattr.is_kernel),
+ bpf_prog_insn_size(prog)) != 0)
+ goto free_prog;
+ }
+
/* copy eBPF program license from user space */
if (strncpy_from_bpfptr(license,
make_bpfptr(attr->license, uattr.is_kernel),
--
2.47.1
next prev parent reply other threads:[~2025-01-09 21:48 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-09 21:43 [POC][RFC][PATCH] bpf: in-kernel bpf relocations on raw elf files Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 01/14] bpf: Port prerequiste BTF handling functions from userspace Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 02/14] bpf: Add data structures for managing in-kernel eBPF relocations Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 03/14] bpf: Port .btf.ext parsing functions from userspace Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 04/14] bpf: Port elf and btf utility helper " Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 05/14] fs/kernel_read_file: Add an eBPF specifier to kernel_read_file Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 06/14] bpf: Add BPF_LOAD_FD subcommand Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 07/14] bpf: Implement BPF_LOAD_FD subcommand handler Blaise Boscaccy
2025-01-10 6:05 ` Greg KH
2025-01-10 22:41 ` Blaise Boscaccy
2025-01-11 0:41 ` kernel test robot
2025-01-09 21:43 ` [PATCH 08/14] bpf: Add elf parsing support to the BPF_LOAD_FD subcommand Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 09/14] bpf: Collect extern relocations Blaise Boscaccy
2025-01-11 1:35 ` kernel test robot
2025-01-09 21:43 ` [PATCH 10/14] bpf: Implement BTF fixup functionality Blaise Boscaccy
2025-01-11 3:19 ` kernel test robot
2025-01-09 21:43 ` [PATCH 11/14] bpf: Implement relocation collection Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 12/14] bpf: Resolve external relocations Blaise Boscaccy
2025-01-09 21:43 ` [PATCH 13/14] bpf: Apply in-kernel bpf instruction relocations Blaise Boscaccy
2025-01-09 21:43 ` Blaise Boscaccy [this message]
2025-01-10 18:40 ` [POC][RFC][PATCH] bpf: in-kernel bpf relocations on raw elf files Alexei Starovoitov
2025-01-10 23:27 ` Blaise Boscaccy
2025-01-13 17:54 ` Alexei Starovoitov
2025-01-14 18:24 ` Blaise Boscaccy
2025-01-24 5:08 ` bpf signing. " Alexei Starovoitov
2025-01-24 7:05 ` John Fastabend
2025-01-28 22:32 ` Blaise Boscaccy
2025-01-30 1:13 ` Cong Wang
2025-01-30 19:22 ` Blaise Boscaccy
2025-02-01 22:24 ` Cong Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250109214617.485144-15-bboscaccy@linux.microsoft.com \
--to=bboscaccy@linux.microsoft.com \
--cc=bpf@vger.kernel.org \
--cc=code@tyhicks.com \
--cc=flaniel@linux.microsoft.com \
--cc=gregkh@linuxfoundation.org \
--cc=nkapron@google.com \
--cc=paul@paul-moore.com \
--cc=roberto.sassu@huawei.com \
--cc=teknoraver@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.