netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Björn Töpel" <bjorn.topel@gmail.com>
To: bjorn.topel@gmail.com, magnus.karlsson@intel.com,
	magnus.karlsson@gmail.com, ast@kernel.org, daniel@iogearbox.net,
	netdev@vger.kernel.org
Cc: "Björn Töpel" <bjorn.topel@intel.com>,
	brouer@redhat.com, u9012063@gmail.com, qi.z.zhang@intel.com
Subject: [PATCH bpf-next 5/7] bpf: add function to load builtin BPF program
Date: Fri,  7 Dec 2018 12:44:29 +0100	[thread overview]
Message-ID: <20181207114431.18038-6-bjorn.topel@gmail.com> (raw)
In-Reply-To: <20181207114431.18038-1-bjorn.topel@gmail.com>

From: Björn Töpel <bjorn.topel@intel.com>

The added bpf_prog_load_builtin can be used to load and verify a BPF
program that originates from the kernel. We call this a "builtin BPF
program". A builtin program can be used for convenience, e.g. it
allows for the kernel to use the bpf infrastructure for internal
tasks.

This functionality will be used by AF_XDP sockets in a later commit.

Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
---
 include/linux/bpf.h  |  2 ++
 kernel/bpf/syscall.c | 32 ++++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index e82b7039fc66..e810bfeb6239 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -563,6 +563,8 @@ static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
 int array_map_alloc_check(union bpf_attr *attr);
 
+struct bpf_prog *bpf_prog_load_builtin(union bpf_attr *attr);
+
 #else /* !CONFIG_BPF_SYSCALL */
 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
 {
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ee1328625330..323831e1a1e2 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1461,10 +1461,16 @@ static struct bpf_prog *__bpf_prog_load(union bpf_attr *attr,
 	    !capable(CAP_SYS_ADMIN))
 		return ERR_PTR(-EPERM);
 
-	/* copy eBPF program license from user space */
-	if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
-			      sizeof(license) - 1) < 0)
-		return ERR_PTR(-EFAULT);
+	/* NB! If uattr is NULL, a builtin BPF is being loaded. */
+	if (uattr) {
+		/* copy eBPF program license from user space */
+		if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
+				      sizeof(license) - 1) < 0)
+			return ERR_PTR(-EFAULT);
+	} else {
+		strncpy(license, (const char *)(unsigned long)attr->license,
+			sizeof(license) - 1);
+	}
 	license[sizeof(license) - 1] = 0;
 
 	/* eBPF programs must be GPL compatible to use GPL-ed functions */
@@ -1505,10 +1511,15 @@ static struct bpf_prog *__bpf_prog_load(union bpf_attr *attr,
 
 	prog->len = attr->insn_cnt;
 
-	err = -EFAULT;
-	if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
-			   bpf_prog_insn_size(prog)) != 0)
-		goto free_prog;
+	if (uattr) {
+		err = -EFAULT;
+		if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
+				   bpf_prog_insn_size(prog)) != 0)
+			goto free_prog;
+	} else {
+		memcpy(prog->insns, (void *)(unsigned long)attr->insns,
+		       bpf_prog_insn_size(prog));
+	}
 
 	prog->orig_prog = NULL;
 	prog->jited = 0;
@@ -1584,6 +1595,11 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 	return fd;
 }
 
+struct bpf_prog *bpf_prog_load_builtin(union bpf_attr *attr)
+{
+	return __bpf_prog_load(attr, NULL);
+}
+
 #define BPF_OBJ_LAST_FIELD file_flags
 
 static int bpf_obj_pin(const union bpf_attr *attr)
-- 
2.19.1

  parent reply	other threads:[~2018-12-07 11:45 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-07 11:44 [PATCH bpf-next 0/7] Add XDP_ATTACH bind() flag to AF_XDP sockets Björn Töpel
2018-12-07 11:44 ` [PATCH bpf-next 1/7] xsk: simplify AF_XDP socket teardown Björn Töpel
2018-12-07 11:44 ` [PATCH bpf-next 2/7] xsk: add XDP_ATTACH bind() flag Björn Töpel
2018-12-07 11:44 ` [PATCH bpf-next 3/7] bpf: add bpf_xsk_redirect function Björn Töpel
2018-12-07 11:44 ` [PATCH bpf-next 4/7] bpf: prepare for builtin bpf program Björn Töpel
2018-12-07 11:44 ` Björn Töpel [this message]
2018-12-07 11:44 ` [PATCH bpf-next 6/7] xsk: load a builtin XDP program on XDP_ATTACH Björn Töpel
2018-12-10  2:17   ` Jakub Kicinski
2018-12-10  7:29     ` Björn Töpel
2018-12-07 13:42 ` [PATCH bpf-next 0/7] Add XDP_ATTACH bind() flag to AF_XDP sockets Jesper Dangaard Brouer
2018-12-07 14:01   ` Björn Töpel
2018-12-08 14:52     ` Jesper Dangaard Brouer
2018-12-08 18:43       ` Björn Töpel
2018-12-08 20:42         ` Jesper Dangaard Brouer
2018-12-07 15:27   ` Björn Töpel
2018-12-07 21:21 ` Alexei Starovoitov
2018-12-08  9:31   ` Björn Töpel
2018-12-08 15:12   ` Jesper Dangaard Brouer
2018-12-08 16:55     ` Andrew Lunn
2018-12-08 20:37       ` Jesper Dangaard Brouer
2018-12-08 20:48         ` Andrew Lunn
2018-12-08 18:50     ` Björn Töpel
     [not found] ` <20181207114431.18038-8-bjorn.topel@gmail.com>
2018-12-08 10:08   ` [PATCH bpf-next 7/7] samples: bpf: add support for XDP_ATTACH to xdpsock Zhang, Qi Z
2018-12-10 14:01 ` [PATCH bpf-next 0/7] Add XDP_ATTACH bind() flag to AF_XDP sockets Björn Töpel

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=20181207114431.18038-6-bjorn.topel@gmail.com \
    --to=bjorn.topel@gmail.com \
    --cc=ast@kernel.org \
    --cc=bjorn.topel@intel.com \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=magnus.karlsson@gmail.com \
    --cc=magnus.karlsson@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=qi.z.zhang@intel.com \
    --cc=u9012063@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).