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, Piotr Krysiuk <piotras@gmail.com>,
	Benedict Schlueter <benedict.schlueter@rub.de>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.10 12/36] bpf: Tighten speculative pointer arithmetic mask
Date: Mon, 26 Apr 2021 09:29:54 +0200	[thread overview]
Message-ID: <20210426072819.211919290@linuxfoundation.org> (raw)
In-Reply-To: <20210426072818.777662399@linuxfoundation.org>

From: Daniel Borkmann <daniel@iogearbox.net>

[ Upstream commit 7fedb63a8307dda0ec3b8969a3b233a1dd7ea8e0 ]

This work tightens the offset mask we use for unprivileged pointer arithmetic
in order to mitigate a corner case reported by Piotr and Benedict where in
the speculative domain it is possible to advance, for example, the map value
pointer by up to value_size-1 out-of-bounds in order to leak kernel memory
via side-channel to user space.

Before this change, the computed ptr_limit for retrieve_ptr_limit() helper
represents largest valid distance when moving pointer to the right or left
which is then fed as aux->alu_limit to generate masking instructions against
the offset register. After the change, the derived aux->alu_limit represents
the largest potential value of the offset register which we mask against which
is just a narrower subset of the former limit.

For minimal complexity, we call sanitize_ptr_alu() from 2 observation points
in adjust_ptr_min_max_vals(), that is, before and after the simulated alu
operation. In the first step, we retieve the alu_state and alu_limit before
the operation as well as we branch-off a verifier path and push it to the
verification stack as we did before which checks the dst_reg under truncation,
in other words, when the speculative domain would attempt to move the pointer
out-of-bounds.

In the second step, we retrieve the new alu_limit and calculate the absolute
distance between both. Moreover, we commit the alu_state and final alu_limit
via update_alu_sanitation_state() to the env's instruction aux data, and bail
out from there if there is a mismatch due to coming from different verification
paths with different states.

Reported-by: Piotr Krysiuk <piotras@gmail.com>
Reported-by: Benedict Schlueter <benedict.schlueter@rub.de>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: Benedict Schlueter <benedict.schlueter@rub.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 kernel/bpf/verifier.c | 73 ++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 29 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 1b97fd364ce2..b9180509917e 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5674,7 +5674,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
 	bool off_is_neg = off_reg->smin_value < 0;
 	bool mask_to_left = (opcode == BPF_ADD &&  off_is_neg) ||
 			    (opcode == BPF_SUB && !off_is_neg);
-	u32 off, max = 0, ptr_limit = 0;
+	u32 max = 0, ptr_limit = 0;
 
 	if (!tnum_is_const(off_reg->var_off) &&
 	    (off_reg->smin_value < 0) != (off_reg->smax_value < 0))
@@ -5683,26 +5683,18 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
 	switch (ptr_reg->type) {
 	case PTR_TO_STACK:
 		/* Offset 0 is out-of-bounds, but acceptable start for the
-		 * left direction, see BPF_REG_FP.
+		 * left direction, see BPF_REG_FP. Also, unknown scalar
+		 * offset where we would need to deal with min/max bounds is
+		 * currently prohibited for unprivileged.
 		 */
 		max = MAX_BPF_STACK + mask_to_left;
-		/* Indirect variable offset stack access is prohibited in
-		 * unprivileged mode so it's not handled here.
-		 */
-		off = ptr_reg->off + ptr_reg->var_off.value;
-		if (mask_to_left)
-			ptr_limit = MAX_BPF_STACK + off;
-		else
-			ptr_limit = -off - 1;
+		ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off);
 		break;
 	case PTR_TO_MAP_VALUE:
 		max = ptr_reg->map_ptr->value_size;
-		if (mask_to_left) {
-			ptr_limit = ptr_reg->umax_value + ptr_reg->off;
-		} else {
-			off = ptr_reg->smin_value + ptr_reg->off;
-			ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
-		}
+		ptr_limit = (mask_to_left ?
+			     ptr_reg->smin_value :
+			     ptr_reg->umax_value) + ptr_reg->off;
 		break;
 	default:
 		return REASON_TYPE;
@@ -5757,10 +5749,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
 			    struct bpf_insn *insn,
 			    const struct bpf_reg_state *ptr_reg,
 			    const struct bpf_reg_state *off_reg,
-			    struct bpf_reg_state *dst_reg)
+			    struct bpf_reg_state *dst_reg,
+			    struct bpf_insn_aux_data *tmp_aux,
+			    const bool commit_window)
 {
+	struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : tmp_aux;
 	struct bpf_verifier_state *vstate = env->cur_state;
-	struct bpf_insn_aux_data *aux = cur_aux(env);
 	bool off_is_neg = off_reg->smin_value < 0;
 	bool ptr_is_dst_reg = ptr_reg == dst_reg;
 	u8 opcode = BPF_OP(insn->code);
@@ -5779,18 +5773,33 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
 	if (vstate->speculative)
 		goto do_sim;
 
-	alu_state  = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
-	alu_state |= ptr_is_dst_reg ?
-		     BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
-
 	err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
 	if (err < 0)
 		return err;
 
+	if (commit_window) {
+		/* In commit phase we narrow the masking window based on
+		 * the observed pointer move after the simulated operation.
+		 */
+		alu_state = tmp_aux->alu_state;
+		alu_limit = abs(tmp_aux->alu_limit - alu_limit);
+	} else {
+		alu_state  = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
+		alu_state |= ptr_is_dst_reg ?
+			     BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
+	}
+
 	err = update_alu_sanitation_state(aux, alu_state, alu_limit);
 	if (err < 0)
 		return err;
 do_sim:
+	/* If we're in commit phase, we're done here given we already
+	 * pushed the truncated dst_reg into the speculative verification
+	 * stack.
+	 */
+	if (commit_window)
+		return 0;
+
 	/* Simulate and find potential out-of-bounds access under
 	 * speculative execution from truncation as a result of
 	 * masking when off was not within expected range. If off
@@ -5933,6 +5942,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 	    smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
 	u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
 	    umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
+	struct bpf_insn_aux_data tmp_aux = {};
 	u8 opcode = BPF_OP(insn->code);
 	u32 dst = insn->dst_reg;
 	int ret;
@@ -5999,12 +6009,15 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 	/* pointer types do not carry 32-bit bounds at the moment. */
 	__mark_reg32_unbounded(dst_reg);
 
-	switch (opcode) {
-	case BPF_ADD:
-		ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg);
+	if (sanitize_needed(opcode)) {
+		ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg,
+				       &tmp_aux, false);
 		if (ret < 0)
 			return sanitize_err(env, insn, ret, off_reg, dst_reg);
+	}
 
+	switch (opcode) {
+	case BPF_ADD:
 		/* We can take a fixed offset as long as it doesn't overflow
 		 * the s32 'off' field
 		 */
@@ -6055,10 +6068,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 		}
 		break;
 	case BPF_SUB:
-		ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg);
-		if (ret < 0)
-			return sanitize_err(env, insn, ret, off_reg, dst_reg);
-
 		if (dst_reg == off_reg) {
 			/* scalar -= pointer.  Creates an unknown scalar */
 			verbose(env, "R%d tried to subtract pointer from scalar\n",
@@ -6141,6 +6150,12 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 
 	if (sanitize_check_bounds(env, insn, dst_reg) < 0)
 		return -EACCES;
+	if (sanitize_needed(opcode)) {
+		ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg,
+				       &tmp_aux, true);
+		if (ret < 0)
+			return sanitize_err(env, insn, ret, off_reg, dst_reg);
+	}
 
 	return 0;
 }
-- 
2.30.2




  parent reply	other threads:[~2021-04-26  7:49 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-26  7:29 [PATCH 5.10 00/36] 5.10.33-rc1 review Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 01/36] vhost-vdpa: protect concurrent access to vhost device iotlb Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 02/36] gpio: omap: Save and restore sysconfig Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 03/36] KEYS: trusted: Fix TPM reservation for seal/unseal Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 04/36] vdpa/mlx5: Set err = -ENOMEM in case dma_map_sg_attrs fails Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 05/36] pinctrl: lewisburg: Update number of pins in community Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 06/36] block: return -EBUSY when there are open partitions in blkdev_reread_part Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 07/36] pinctrl: core: Show pin numbers for the controllers with base = 0 Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 08/36] arm64: dts: allwinner: Revert SD card CD GPIO for Pine64-LTS Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 09/36] bpf: Permits pointers on stack for helper calls Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 10/36] bpf: Allow variable-offset stack access Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 11/36] bpf: Refactor and streamline bounds check into helper Greg Kroah-Hartman
2021-04-26  7:29 ` Greg Kroah-Hartman [this message]
2021-04-26  7:29 ` [PATCH 5.10 13/36] locking/qrwlock: Fix ordering in queued_write_lock_slowpath() Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 14/36] perf/x86/intel/uncore: Remove uncore extra PCI dev HSWEP_PCI_PCU_3 Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 15/36] perf/x86/kvm: Fix Broadwell Xeon stepping in isolation_ucodes[] Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 16/36] perf auxtrace: Fix potential NULL pointer dereference Greg Kroah-Hartman
2021-04-26  7:29 ` [PATCH 5.10 17/36] perf map: Fix error return code in maps__clone() Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 18/36] HID: google: add don USB id Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 19/36] HID: alps: fix error return code in alps_input_configured() Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 20/36] HID cp2112: fix support for multiple gpiochips Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 21/36] HID: wacom: Assign boolean values to a bool variable Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 22/36] soc: qcom: geni: shield geni_icc_get() for ACPI boot Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 23/36] dmaengine: xilinx: dpdma: Fix descriptor issuing on video group Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 24/36] dmaengine: xilinx: dpdma: Fix race condition in done IRQ Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 25/36] ARM: dts: Fix swapped mmc order for omap3 Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 26/36] net: geneve: check skb is large enough for IPv4/IPv6 header Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 27/36] dmaengine: tegra20: Fix runtime PM imbalance on error Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 28/36] s390/entry: save the caller of psw_idle Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 29/36] arm64: kprobes: Restore local irqflag if kprobes is cancelled Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 30/36] xen-netback: Check for hotplug-status existence before watching Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 31/36] cavium/liquidio: Fix duplicate argument Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 32/36] kasan: fix hwasan build for gcc Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 33/36] csky: change a Kconfig symbol name to fix e1000 build error Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 34/36] ia64: fix discontig.c section mismatches Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 35/36] ia64: tools: remove duplicate definition of ia64_mf() on ia64 Greg Kroah-Hartman
2021-04-26  7:30 ` [PATCH 5.10 36/36] x86/crash: Fix crash_setup_memmap_entries() out-of-bounds access Greg Kroah-Hartman
2021-04-26 13:04 ` [PATCH 5.10 00/36] 5.10.33-rc1 review Jon Hunter
2021-04-26 14:24 ` Fox Chen
2021-04-26 15:50 ` Patrick Mccormick
2021-04-26 16:54 ` Florian Fainelli
2021-04-26 18:34 ` Guenter Roeck
2021-04-26 20:33 ` Sudip Mukherjee
2021-04-26 23:46 ` Shuah Khan
2021-04-27  2:13 ` Samuel Zou
2021-04-27  6:14 ` Naresh Kamboju
2021-04-27  7:40 ` Pavel Machek
2021-04-27 15:34 ` Andrei Rabusov

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=20210426072819.211919290@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ast@kernel.org \
    --cc=benedict.schlueter@rub.de \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=piotras@gmail.com \
    --cc=sashal@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.