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.11 16/41] bpf: Tighten speculative pointer arithmetic mask
Date: Mon, 26 Apr 2021 09:30:03 +0200 [thread overview]
Message-ID: <20210426072820.238631048@linuxfoundation.org> (raw)
In-Reply-To: <20210426072819.666570770@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 a543d929c348..d3a2f0cef76d 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5729,7 +5729,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))
@@ -5738,26 +5738,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;
@@ -5812,10 +5804,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);
@@ -5834,18 +5828,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
@@ -5988,6 +5997,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;
@@ -6054,12 +6064,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
*/
@@ -6110,10 +6123,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",
@@ -6196,6 +6205,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
next prev parent reply other threads:[~2021-04-26 7:45 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-26 7:29 [PATCH 5.11 00/41] 5.11.17-rc1 review Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 01/41] vhost-vdpa: protect concurrent access to vhost device iotlb Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 02/41] ovl: fix reference counting in ovl_mmap error path Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 03/41] coda: fix reference counting in coda_file_mmap " Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 04/41] amd/display: allow non-linear multi-planar formats Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 05/41] drm/amdgpu: reserve fence slot to update page table Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 06/41] drm/amdgpu: fix GCR_GENERAL_CNTL offset for dimgrey_cavefish Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 07/41] gpio: omap: Save and restore sysconfig Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 08/41] KEYS: trusted: Fix TPM reservation for seal/unseal Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 09/41] vdpa/mlx5: Set err = -ENOMEM in case dma_map_sg_attrs fails Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 10/41] pinctrl: lewisburg: Update number of pins in community Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 11/41] block: return -EBUSY when there are open partitions in blkdev_reread_part Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 5.11 12/41] pinctrl: core: Show pin numbers for the controllers with base = 0 Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 13/41] arm64: dts: allwinner: Revert SD card CD GPIO for Pine64-LTS Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 14/41] bpf: Allow variable-offset stack access Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 15/41] bpf: Refactor and streamline bounds check into helper Greg Kroah-Hartman
2021-04-26 7:30 ` Greg Kroah-Hartman [this message]
2021-04-26 7:30 ` [PATCH 5.11 17/41] locking/qrwlock: Fix ordering in queued_write_lock_slowpath() Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 18/41] perf/x86/intel/uncore: Remove uncore extra PCI dev HSWEP_PCI_PCU_3 Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 19/41] perf/x86/kvm: Fix Broadwell Xeon stepping in isolation_ucodes[] Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 20/41] perf auxtrace: Fix potential NULL pointer dereference Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 21/41] perf map: Fix error return code in maps__clone() Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 22/41] HID: google: add don USB id Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 23/41] HID: asus: Add support for 2021 ASUS N-Key keyboard Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 24/41] HID: alps: fix error return code in alps_input_configured() Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 25/41] HID cp2112: fix support for multiple gpiochips Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 26/41] HID: wacom: Assign boolean values to a bool variable Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 27/41] soc: qcom: geni: shield geni_icc_get() for ACPI boot Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 28/41] dmaengine: xilinx: dpdma: Fix descriptor issuing on video group Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 29/41] dmaengine: xilinx: dpdma: Fix race condition in done IRQ Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 30/41] ARM: dts: Fix swapped mmc order for omap3 Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 31/41] m68k: fix flatmem memory model setup Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 32/41] net: geneve: check skb is large enough for IPv4/IPv6 header Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 33/41] dmaengine: tegra20: Fix runtime PM imbalance on error Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 34/41] s390/entry: save the caller of psw_idle Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 35/41] arm64: kprobes: Restore local irqflag if kprobes is cancelled Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 36/41] xen-netback: Check for hotplug-status existence before watching Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 37/41] cavium/liquidio: Fix duplicate argument Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 38/41] csky: change a Kconfig symbol name to fix e1000 build error Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 39/41] ia64: fix discontig.c section mismatches Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 40/41] ia64: tools: remove duplicate definition of ia64_mf() on ia64 Greg Kroah-Hartman
2021-04-26 7:30 ` [PATCH 5.11 41/41] x86/crash: Fix crash_setup_memmap_entries() out-of-bounds access Greg Kroah-Hartman
2021-04-26 11:57 ` [PATCH 5.11 00/41] 5.11.17-rc1 review Fox Chen
2021-04-26 13:04 ` Jon Hunter
2021-04-26 17:37 ` Naresh Kamboju
2021-04-26 18:35 ` Guenter Roeck
2021-04-26 23:46 ` Shuah Khan
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=20210426072820.238631048@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 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).