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,
	syzbot+7d427828b2ea6e592804@syzkaller.appspotmail.com,
	Daniel Borkmann <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@kernel.org>
Subject: [PATCH 4.17 081/101] bpf: dont leave partial mangled prog in jit_subprogs error path
Date: Fri, 20 Jul 2018 14:14:27 +0200	[thread overview]
Message-ID: <20180720121426.701350320@linuxfoundation.org> (raw)
In-Reply-To: <20180720121422.837870592@linuxfoundation.org>

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

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

From: Daniel Borkmann <daniel@iogearbox.net>

commit c7a897843224a92209f306c984975b704969b89d upstream.

syzkaller managed to trigger the following bug through fault injection:

  [...]
  [  141.043668] verifier bug. No program starts at insn 3
  [  141.044648] WARNING: CPU: 3 PID: 4072 at kernel/bpf/verifier.c:1613
                 get_callee_stack_depth kernel/bpf/verifier.c:1612 [inline]
  [  141.044648] WARNING: CPU: 3 PID: 4072 at kernel/bpf/verifier.c:1613
                 fixup_call_args kernel/bpf/verifier.c:5587 [inline]
  [  141.044648] WARNING: CPU: 3 PID: 4072 at kernel/bpf/verifier.c:1613
                 bpf_check+0x525e/0x5e60 kernel/bpf/verifier.c:5952
  [  141.047355] CPU: 3 PID: 4072 Comm: a.out Not tainted 4.18.0-rc4+ #51
  [  141.048446] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),BIOS 1.10.2-1 04/01/2014
  [  141.049877] Call Trace:
  [  141.050324]  __dump_stack lib/dump_stack.c:77 [inline]
  [  141.050324]  dump_stack+0x1c9/0x2b4 lib/dump_stack.c:113
  [  141.050950]  ? dump_stack_print_info.cold.2+0x52/0x52 lib/dump_stack.c:60
  [  141.051837]  panic+0x238/0x4e7 kernel/panic.c:184
  [  141.052386]  ? add_taint.cold.5+0x16/0x16 kernel/panic.c:385
  [  141.053101]  ? __warn.cold.8+0x148/0x1ba kernel/panic.c:537
  [  141.053814]  ? __warn.cold.8+0x117/0x1ba kernel/panic.c:530
  [  141.054506]  ? get_callee_stack_depth kernel/bpf/verifier.c:1612 [inline]
  [  141.054506]  ? fixup_call_args kernel/bpf/verifier.c:5587 [inline]
  [  141.054506]  ? bpf_check+0x525e/0x5e60 kernel/bpf/verifier.c:5952
  [  141.055163]  __warn.cold.8+0x163/0x1ba kernel/panic.c:538
  [  141.055820]  ? get_callee_stack_depth kernel/bpf/verifier.c:1612 [inline]
  [  141.055820]  ? fixup_call_args kernel/bpf/verifier.c:5587 [inline]
  [  141.055820]  ? bpf_check+0x525e/0x5e60 kernel/bpf/verifier.c:5952
  [...]

What happens in jit_subprogs() is that kcalloc() for the subprog func
buffer is failing with NULL where we then bail out. Latter is a plain
return -ENOMEM, and this is definitely not okay since earlier in the
loop we are walking all subprogs and temporarily rewrite insn->off to
remember the subprog id as well as insn->imm to temporarily point the
call to __bpf_call_base + 1 for the initial JIT pass. Thus, bailing
out in such state and handing this over to the interpreter is troublesome
since later/subsequent e.g. find_subprog() lookups are based on wrong
insn->imm.

Therefore, once we hit this point, we need to jump to out_free path
where we undo all changes from earlier loop, so that interpreter can
work on unmodified insn->{off,imm}.

Another point is that should find_subprog() fail in jit_subprogs() due
to a verifier bug, then we also should not simply defer the program to
the interpreter since also here we did partial modifications. Instead
we should just bail out entirely and return an error to the user who is
trying to load the program.

Fixes: 1c2a088a6626 ("bpf: x64: add JIT support for multi-function programs")
Reported-by: syzbot+7d427828b2ea6e592804@syzkaller.appspotmail.com
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 kernel/bpf/verifier.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5349,6 +5349,10 @@ static int jit_subprogs(struct bpf_verif
 		if (insn->code != (BPF_JMP | BPF_CALL) ||
 		    insn->src_reg != BPF_PSEUDO_CALL)
 			continue;
+		/* Upon error here we cannot fall back to interpreter but
+		 * need a hard reject of the program. Thus -EFAULT is
+		 * propagated in any case.
+		 */
 		subprog = find_subprog(env, i + insn->imm + 1);
 		if (subprog < 0) {
 			WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
@@ -5369,7 +5373,7 @@ static int jit_subprogs(struct bpf_verif
 
 	func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL);
 	if (!func)
-		return -ENOMEM;
+		goto out_undo_insn;
 
 	for (i = 0; i <= env->subprog_cnt; i++) {
 		subprog_start = subprog_end;
@@ -5424,7 +5428,7 @@ static int jit_subprogs(struct bpf_verif
 		tmp = bpf_int_jit_compile(func[i]);
 		if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
 			verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
-			err = -EFAULT;
+			err = -ENOTSUPP;
 			goto out_free;
 		}
 		cond_resched();
@@ -5466,6 +5470,7 @@ out_free:
 		if (func[i])
 			bpf_jit_free(func[i]);
 	kfree(func);
+out_undo_insn:
 	/* cleanup main prog to be interpreted */
 	prog->jit_requested = 0;
 	for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
@@ -5492,6 +5497,8 @@ static int fixup_call_args(struct bpf_ve
 		err = jit_subprogs(env);
 		if (err == 0)
 			return 0;
+		if (err == -EFAULT)
+			return err;
 	}
 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
 	for (i = 0; i < prog->len; i++, insn++) {



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

Thread overview: 110+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-20 12:13 [PATCH 4.17 000/101] 4.17.9-stable review Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 001/101] compiler-gcc.h: Add __attribute__((gnu_inline)) to all inline declarations Greg Kroah-Hartman
2018-07-20 12:13   ` Greg Kroah-Hartman
2018-07-20 12:13 ` Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 002/101] x86/asm: Add _ASM_ARG* constants for argument registers to <asm/asm.h> Greg Kroah-Hartman
2018-07-20 12:13   ` Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 003/101] x86/paravirt: Make native_save_fl() extern inline Greg Kroah-Hartman
2018-07-20 12:13 ` Greg Kroah-Hartman
2018-07-20 12:13   ` Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 005/101] pinctrl: mt7622: fix error path on failing at groups building Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 006/101] pinctrl: mt7622: stop using the deprecated pinctrl_add_gpio_range Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 007/101] pinctrl: mt7622: fix a kernel panic when gpio-hog is being applied Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 008/101] alx: take rtnl before calling __alx_open from resume Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 010/101] atm: zatm: Fix potential Spectre v1 Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 011/101] hv_netvsc: split sub-channel setup into async and sync Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 012/101] ipv6: sr: fix passing wrong flags to crypto_alloc_shash() Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 013/101] ipvlan: fix IFLA_MTU ignored on NEWLINK Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 014/101] ixgbe: split XDP_TX tail and XDP_REDIRECT map flushing Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 015/101] net: dccp: avoid crash in ccid3_hc_rx_send_feedback() Greg Kroah-Hartman
2018-07-20 12:13   ` Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 016/101] net: dccp: switch rx_tstamp_last_feedback to monotonic clock Greg Kroah-Hartman
2018-07-20 12:13   ` Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 017/101] net: fix use-after-free in GRO with ESP Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 018/101] net: macb: Fix ptp time adjustment for large negative delta Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 019/101] net/mlx5e: Avoid dealing with vport representors if not being e-switch manager Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 020/101] net/mlx5e: Dont attempt to dereference the ppriv struct if not being eswitch manager Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 021/101] net/mlx5: E-Switch, Avoid setup attempt if not being e-switch manager Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 022/101] net/mlx5: Fix command interface race in polling mode Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 023/101] net/mlx5: Fix incorrect raw command length parsing Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 024/101] net/mlx5: Fix required capability for manipulating MPFS Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 025/101] net/mlx5: Fix wrong size allocation for QoS ETC TC regitster Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 026/101] net: mvneta: fix the Rx desc DMA address in the Rx path Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 027/101] net/packet: fix use-after-free Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 028/101] net/sched: act_ife: fix recursive lock and idr leak Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 029/101] net/sched: act_ife: preserve the action control in case of error Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 030/101] net_sched: blackhole: tell upper qdisc about dropped packets Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 031/101] net: sungem: fix rx checksum support Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 032/101] net/tcp: Fix socket lookups with SO_BINDTODEVICE Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 033/101] qede: Adverstise software timestamp caps when PHC is not available Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 034/101] qed: Fix setting of incorrect eswitch mode Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 035/101] qed: Fix use of incorrect size in memcpy call Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 036/101] qed: Limit msix vectors in kdump kernel to the minimum required count Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 038/101] r8152: napi hangup fix after disconnect Greg Kroah-Hartman
2018-07-20 12:13   ` [4.17,038/101] " Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 039/101] s390/qeth: dont clobber buffer on async TX completion Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 040/101] stmmac: fix DMA channel hang in half-duplex mode Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 041/101] strparser: Remove early eaten to fix full tcp receive buffer stall Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 042/101] tcp: fix Fast Open key endianness Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 044/101] vhost_net: validate sock before trying to put its fd Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 045/101] VSOCK: fix loopback on big-endian systems Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 046/101] hinic: reset irq affinity before freeing irq Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 047/101] nfp: flower: fix mpls ether type detection Greg Kroah-Hartman
2018-07-20 12:13   ` Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 048/101] net: macb: initialize bp->queues[0].bp for at91rm9200 Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 049/101] net: use dev_change_tx_queue_len() for SIOCSIFTXQLEN Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 050/101] nfp: reject binding to shared blocks Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 051/101] xen-netfront: Fix mismatched rtnl_unlock Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 052/101] xen-netfront: Update features after registering netdev Greg Kroah-Hartman
2018-07-20 12:13 ` [PATCH 4.17 053/101] enic: do not overwrite error code Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 055/101] IB/mlx5: Avoid dealing with vport representors if not being e-switch manager Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 056/101] Revert "s390/qeth: use Read device to query hypervisor for MAC" Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 057/101] s390/qeth: avoid using is_multicast_ether_addr_64bits on (u8 *)[6] Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 058/101] s390/qeth: fix race when setting MAC address Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 059/101] sfc: correctly initialise filter rwsem for farch Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 060/101] virtio_net: split XDP_TX kick and XDP_REDIRECT map flushing Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 061/101] x86/kvm/Kconfig: Ensure CRYPTO_DEV_CCP_DD state at minimum matches KVM_AMD Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 062/101] net: cxgb3_main: fix potential Spectre v1 Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 063/101] rtlwifi: Fix kernel Oops "Fw download fail!!" Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 064/101] rtlwifi: rtl8821ae: fix firmware is not ready to run Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 065/101] net: lan78xx: Fix race in tx pending skb size calculation Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 066/101] crypto: af_alg - Initialize sg_num_bytes in error code path Greg Kroah-Hartman
2018-07-20 13:08   ` KMSAN: uninit-value in af_alg_free_areq_sgls syzbot
2018-07-20 12:14 ` [PATCH 4.17 067/101] PCI: hv: Disable/enable IRQs rather than BH in hv_compose_msi_msg() Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 068/101] netfilter: ebtables: reject non-bridge targets Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 069/101] reiserfs: fix buffer overflow with long warning messages Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 070/101] KEYS: DNS: fix parsing multiple options Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 071/101] tls: Stricter error checking in zerocopy sendmsg path Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 072/101] autofs: fix slab out of bounds read in getname_kernel() Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 073/101] nsh: set mac len based on inner packet Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 074/101] netfilter: ipv6: nf_defrag: drop skb dst before queueing Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 075/101] bdi: Fix another oops in wb_workfn() Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 076/101] bpf: reject any prog that failed read-only lock Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 077/101] rds: avoid unenecessary cong_update in loop transport Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 078/101] block: dont use blocking queue entered for recursive bio submits Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 079/101] bpf: sockmap, fix crash when ipv6 sock is added Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 080/101] bpf: sockmap, consume_skb in close path Greg Kroah-Hartman
2018-07-20 12:14 ` Greg Kroah-Hartman [this message]
2018-07-20 12:14 ` [PATCH 4.17 082/101] net/nfc: Avoid stalls when nfc_alloc_send_skb() returned NULL Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 083/101] ipvs: initialize tbl->entries after allocation Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 084/101] ipvs: initialize tbl->entries in ip_vs_lblc_init_svc() Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 085/101] arm/arm64: smccc: Add SMCCC-specific return codes Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 086/101] arm64: Call ARCH_WORKAROUND_2 on transitions between EL0 and EL1 Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 087/101] arm64: Add per-cpu infrastructure to call ARCH_WORKAROUND_2 Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 088/101] arm64: Add ARCH_WORKAROUND_2 probing Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 089/101] arm64: Add ssbd command-line option Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 090/101] arm64: ssbd: Add global mitigation state accessor Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 091/101] arm64: ssbd: Skip apply_ssbd if not using dynamic mitigation Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 092/101] arm64: ssbd: Restore mitigation status on CPU resume Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 093/101] arm64: ssbd: Introduce thread flag to control userspace mitigation Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 094/101] arm64: ssbd: Add prctl interface for per-thread mitigation Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 095/101] arm64: KVM: Add HYP per-cpu accessors Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 096/101] arm64: KVM: Add ARCH_WORKAROUND_2 support for guests Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 097/101] arm64: KVM: Handle guests ARCH_WORKAROUND_2 requests Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 098/101] arm64: KVM: Add ARCH_WORKAROUND_2 discovery through ARCH_FEATURES_FUNC_ID Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 099/101] bpf: enforce correct alignment for instructions Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 100/101] bpf, arm32: fix to use bpf_jit_binary_lock_ro api Greg Kroah-Hartman
2018-07-20 12:14 ` [PATCH 4.17 101/101] bpf: undo prog rejection on read-only lock failure Greg Kroah-Hartman
2018-07-21  7:54 ` [PATCH 4.17 000/101] 4.17.9-stable review Naresh Kamboju
2018-07-22 11:42   ` Greg Kroah-Hartman
2018-07-21 13:42 ` Guenter Roeck

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=20180720121426.701350320@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 \
    --cc=syzbot+7d427828b2ea6e592804@syzkaller.appspotmail.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.