All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, "ast@kernel.org, stable@vger.kernel.org,
	Daniel Borkmann" <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH 4.14 65/71] bpf: introduce BPF_JIT_ALWAYS_ON config
Date: Mon, 29 Jan 2018 13:57:33 +0100	[thread overview]
Message-ID: <20180129123831.965109266@linuxfoundation.org> (raw)
In-Reply-To: <20180129123827.271171825@linuxfoundation.org>

4.14-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Alexei Starovoitov <ast@kernel.org>

[ upstream commit 290af86629b25ffd1ed6232c4e9107da031705cb ]

The BPF interpreter has been used as part of the spectre 2 attack CVE-2017-5715.

A quote from goolge project zero blog:
"At this point, it would normally be necessary to locate gadgets in
the host kernel code that can be used to actually leak data by reading
from an attacker-controlled location, shifting and masking the result
appropriately and then using the result of that as offset to an
attacker-controlled address for a load. But piecing gadgets together
and figuring out which ones work in a speculation context seems annoying.
So instead, we decided to use the eBPF interpreter, which is built into
the host kernel - while there is no legitimate way to invoke it from inside
a VM, the presence of the code in the host kernel's text section is sufficient
to make it usable for the attack, just like with ordinary ROP gadgets."

To make attacker job harder introduce BPF_JIT_ALWAYS_ON config
option that removes interpreter from the kernel in favor of JIT-only mode.
So far eBPF JIT is supported by:
x64, arm64, arm32, sparc64, s390, powerpc64, mips64

The start of JITed program is randomized and code page is marked as read-only.
In addition "constant blinding" can be turned on with net.core.bpf_jit_harden

v2->v3:
- move __bpf_prog_ret0 under ifdef (Daniel)

v1->v2:
- fix init order, test_bpf and cBPF (Daniel's feedback)
- fix offloaded bpf (Jakub's feedback)
- add 'return 0' dummy in case something can invoke prog->bpf_func
- retarget bpf tree. For bpf-next the patch would need one extra hunk.
  It will be sent when the trees are merged back to net-next

Considered doing:
  int bpf_jit_enable __read_mostly = BPF_EBPF_JIT_DEFAULT;
but it seems better to land the patch as-is and in bpf-next remove
bpf_jit_enable global variable from all JITs, consolidate in one place
and remove this jit_init() function.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 init/Kconfig               |    7 +++++++
 kernel/bpf/core.c          |   19 +++++++++++++++++++
 lib/test_bpf.c             |   11 +++++++----
 net/core/filter.c          |    6 ++----
 net/core/sysctl_net_core.c |    6 ++++++
 net/socket.c               |    9 +++++++++
 6 files changed, 50 insertions(+), 8 deletions(-)

--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1342,6 +1342,13 @@ config BPF_SYSCALL
 	  Enable the bpf() system call that allows to manipulate eBPF
 	  programs and maps via file descriptors.
 
+config BPF_JIT_ALWAYS_ON
+	bool "Permanently enable BPF JIT and remove BPF interpreter"
+	depends on BPF_SYSCALL && HAVE_EBPF_JIT && BPF_JIT
+	help
+	  Enables BPF JIT and removes BPF interpreter to avoid
+	  speculative execution of BPF instructions by the interpreter
+
 config SHMEM
 	bool "Use full shmem filesystem" if EXPERT
 	default y
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -760,6 +760,7 @@ noinline u64 __bpf_call_base(u64 r1, u64
 }
 EXPORT_SYMBOL_GPL(__bpf_call_base);
 
+#ifndef CONFIG_BPF_JIT_ALWAYS_ON
 /**
  *	__bpf_prog_run - run eBPF program on a given context
  *	@ctx: is the data we are operating on
@@ -1310,6 +1311,14 @@ EVAL6(PROG_NAME_LIST, 224, 256, 288, 320
 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
 };
 
+#else
+static unsigned int __bpf_prog_ret0(const void *ctx,
+				    const struct bpf_insn *insn)
+{
+	return 0;
+}
+#endif
+
 bool bpf_prog_array_compatible(struct bpf_array *array,
 			       const struct bpf_prog *fp)
 {
@@ -1357,9 +1366,13 @@ static int bpf_check_tail_call(const str
  */
 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
 {
+#ifndef CONFIG_BPF_JIT_ALWAYS_ON
 	u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
 
 	fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
+#else
+	fp->bpf_func = __bpf_prog_ret0;
+#endif
 
 	/* eBPF JITs can rewrite the program in case constant
 	 * blinding is active. However, in case of error during
@@ -1368,6 +1381,12 @@ struct bpf_prog *bpf_prog_select_runtime
 	 * be JITed, but falls back to the interpreter.
 	 */
 	fp = bpf_int_jit_compile(fp);
+#ifdef CONFIG_BPF_JIT_ALWAYS_ON
+	if (!fp->jited) {
+		*err = -ENOTSUPP;
+		return fp;
+	}
+#endif
 	bpf_prog_lock_ro(fp);
 
 	/* The tail call compatibility check can only be done at
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -6207,9 +6207,8 @@ static struct bpf_prog *generate_filter(
 				return NULL;
 			}
 		}
-		/* We don't expect to fail. */
 		if (*err) {
-			pr_cont("FAIL to attach err=%d len=%d\n",
+			pr_cont("FAIL to prog_create err=%d len=%d\n",
 				*err, fprog.len);
 			return NULL;
 		}
@@ -6233,6 +6232,10 @@ static struct bpf_prog *generate_filter(
 		 * checks.
 		 */
 		fp = bpf_prog_select_runtime(fp, err);
+		if (*err) {
+			pr_cont("FAIL to select_runtime err=%d\n", *err);
+			return NULL;
+		}
 		break;
 	}
 
@@ -6418,8 +6421,8 @@ static __init int test_bpf(void)
 				pass_cnt++;
 				continue;
 			}
-
-			return err;
+			err_cnt++;
+			continue;
 		}
 
 		pr_cont("jited:%u ", fp->jited);
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -1053,11 +1053,9 @@ static struct bpf_prog *bpf_migrate_filt
 		 */
 		goto out_err_free;
 
-	/* We are guaranteed to never error here with cBPF to eBPF
-	 * transitions, since there's no issue with type compatibility
-	 * checks on program arrays.
-	 */
 	fp = bpf_prog_select_runtime(fp, &err);
+	if (err)
+		goto out_err_free;
 
 	kfree(old_prog);
 	return fp;
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -325,7 +325,13 @@ static struct ctl_table net_core_table[]
 		.data		= &bpf_jit_enable,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+#ifndef CONFIG_BPF_JIT_ALWAYS_ON
 		.proc_handler	= proc_dointvec
+#else
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= &one,
+		.extra2		= &one,
+#endif
 	},
 # ifdef CONFIG_HAVE_EBPF_JIT
 	{
--- a/net/socket.c
+++ b/net/socket.c
@@ -2642,6 +2642,15 @@ out_fs:
 
 core_initcall(sock_init);	/* early initcall */
 
+static int __init jit_init(void)
+{
+#ifdef CONFIG_BPF_JIT_ALWAYS_ON
+	bpf_jit_enable = 1;
+#endif
+	return 0;
+}
+pure_initcall(jit_init);
+
 #ifdef CONFIG_PROC_FS
 void socket_seq_show(struct seq_file *seq)
 {

  parent reply	other threads:[~2018-01-29 12:57 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-29 12:56 [PATCH 4.14 00/71] 4.14.16-stable review Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 01/71] orangefs: use list_for_each_entry_safe in purge_waiting_ops Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 02/71] orangefs: initialize op on loop restart in orangefs_devreq_read Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 03/71] mm, page_alloc: fix potential false positive in __zone_watermark_ok Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 04/71] netfilter: nfnetlink_cthelper: Add missing permission checks Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 05/71] netfilter: xt_osf: " Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 06/71] xfrm: Fix a race in the xdst pcpu cache Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 07/71] Revert "module: Add retpoline tag to VERMAGIC" Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 08/71] Input: xpad - add support for PDP Xbox One controllers Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 09/71] Input: trackpoint - force 3 buttons if 0 button is reported Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 10/71] Input: trackpoint - only expose supported controls for Elan, ALPS and NXP Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 11/71] Btrfs: fix stale entries in readdir Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 12/71] KVM: s390: add proper locking for CMMA migration bitmap Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 13/71] orangefs: fix deadlock; do not write i_size in read_iter Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 14/71] ARM: net: bpf: avoid bx instruction on non-Thumb capable CPUs Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 15/71] ARM: net: bpf: fix tail call jumps Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 16/71] ARM: net: bpf: fix stack alignment Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 17/71] ARM: net: bpf: move stack documentation Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 18/71] ARM: net: bpf: correct stack layout documentation Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 19/71] ARM: net: bpf: fix register saving Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 20/71] ARM: net: bpf: fix LDX instructions Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 21/71] ARM: net: bpf: clarify tail_call index Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 22/71] drm/vc4: Fix NULL pointer dereference in vc4_save_hang_state() Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 23/71] net: Allow neigh contructor functions ability to modify the primary_key Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 24/71] ipv4: Make neigh lookup keys for loopback/point-to-point devices be INADDR_ANY Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 25/71] dccp: dont restart ccid2_hc_tx_rto_expire() if sk in closed state Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 26/71] ipv6: Fix getsockopt() for sockets with default IPV6_AUTOFLOWLABEL Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 27/71] ipv6: fix udpv6 sendmsg crash caused by too small MTU Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 28/71] ipv6: ip6_make_skb() needs to clear cork.base.dst Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 29/71] lan78xx: Fix failure in USB Full Speed Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 30/71] net: igmp: fix source address check for IGMPv3 reports Greg Kroah-Hartman
2018-01-29 12:56 ` [PATCH 4.14 31/71] net: qdisc_pkt_len_init() should be more robust Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 32/71] net: tcp: close sock if net namespace is exiting Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 33/71] net/tls: Fix inverted error codes to avoid endless loop Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 34/71] net: vrf: Add support for sends to local broadcast address Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 35/71] pppoe: take ->needed_headroom of lower device into account on xmit Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 36/71] r8169: fix memory corruption on retrieval of hardware statistics Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 37/71] sctp: do not allow the v4 socket to bind a v4mapped v6 address Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 38/71] sctp: return error if the asoc has been peeled off in sctp_wait_for_sndbuf Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 39/71] tipc: fix a memory leak in tipc_nl_node_get_link() Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 40/71] {net,ib}/mlx5: Dont disable local loopback multicast traffic when needed Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 41/71] net/mlx5: Fix get vector affinity helper function Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 42/71] ppp: unlock all_ppp_mutex before registering device Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 43/71] be2net: restore properly promisc mode after queues reconfiguration Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 44/71] ip6_gre: init dev->mtu and dev->hard_header_len correctly Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 45/71] gso: validate gso_type in GSO handlers Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 46/71] mlxsw: spectrum_router: Dont log an error on missing neighbor Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 47/71] tun: fix a memory leak for tfile->tx_array Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 48/71] flow_dissector: properly cap thoff field Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 49/71] sctp: reinit stream if stream outcnt has been change by sinit in sendmsg Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 50/71] netlink: extack needs to be reset each time through loop Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 51/71] net/mlx5e: Fix fixpoint divide exception in mlx5e_am_stats_compare Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 52/71] nfp: use the correct index for link speed table Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 53/71] netlink: reset extack earlier in netlink_rcv_skb Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 54/71] net/tls: Only attach to sockets in ESTABLISHED state Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 55/71] tls: fix sw_ctx leak Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 56/71] tls: return -EBUSY if crypto_info is already set Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 57/71] tls: reset crypto_info when do_tls_setsockopt_tx fails Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 58/71] net: ipv4: Make "ip route get" match iif lo rules again Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 59/71] vmxnet3: repair memory leak Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 60/71] perf/x86/amd/power: Do not load AMD power module on !AMD platforms Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 61/71] x86/microcode/intel: Extend BDW late-loading further with LLC size check Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 62/71] x86/microcode: Fix again accessing initrd after having been freed Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 63/71] x86/mm/64: Fix vmapped stack syncing on very-large-memory 4-level systems Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 64/71] hrtimer: Reset hrtimer cpu base proper on CPU hotplug Greg Kroah-Hartman
2018-01-29 12:57 ` Greg Kroah-Hartman [this message]
2018-01-29 12:57 ` [PATCH 4.14 66/71] bpf: avoid false sharing of map refcount with max_entries Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 67/71] bpf: fix divides by zero Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 68/71] bpf: fix 32-bit divide " Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 69/71] bpf: reject stores into ctx via st and xadd Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 70/71] bpf, arm64: fix stack_depth tracking in combination with tail calls Greg Kroah-Hartman
2018-01-29 12:57 ` [PATCH 4.14 71/71] cpufreq: governor: Ensure sufficiently large sampling intervals Greg Kroah-Hartman
2018-01-29 23:59 ` [PATCH 4.14 00/71] 4.14.16-stable review Shuah Khan
2018-01-30 10:06 ` Naresh Kamboju
2018-01-30 12:53   ` Greg Kroah-Hartman
2018-01-30 14:21 ` Guenter Roeck
2018-01-30 14:52   ` Greg Kroah-Hartman

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=20180129123831.965109266@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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.