Skip to contentSkip to conversation

CVE-2023-52920: bpf: support non-r10 register spill/fill to/from stack in precision tracking

DISCUSSION Greg Kroah-Hartman 4 participants 5 messages Nov 5, 2024 – Nov 12, 2024

lore
Share & tools
lore
#0 lore raw

Description
===========

In the Linux kernel, the following vulnerability has been resolved:

bpf: support non-r10 register spill/fill to/from stack in precision tracking

Use instruction (jump) history to record instructions that performed register spill/fill to/from stack, regardless if this was done through read-only r10 register, or any other register after copying r10 into it *and* potentially adjusting offset.

To make this work reliably, we push extra per-instruction flags into instruction history, encoding stack slot index (spi) and stack frame number in extra 10 bit flags we take away from prev_idx in instruction history. We don't touch idx field for maximum performance, as it's checked most frequently during backtracking.

This change removes basically the last remaining practical limitation of precision backtracking logic in BPF verifier. It fixes known deficiencies, but also opens up new opportunities to reduce number of verified states, explored in the subsequent patches.

There are only three differences in selftests' BPF object files according to veristat, all in the positive direction (less states).

File                                    Program        Insns (A)  Insns (B)  Insns  (DIFF)  States (A)  States (B)  States (DIFF)
--------------------------------------  -------------  ---------  ---------  -------------  ----------  ----------  -------------
test_cls_redirect_dynptr.bpf.linked3.o  cls_redirect        2987       2864  -123 (-4.12%)         240         231    -9 (-3.75%)
xdp_synproxy_kern.bpf.linked3.o         syncookie_tc       82848      82661  -187 (-0.23%)        5107        5073   -34 (-0.67%)
xdp_synproxy_kern.bpf.linked3.o         syncookie_xdp      85116      84964  -152 (-0.18%)        5162        5130   -32 (-0.62%)

Note, I avoided renaming jmp_history to more generic insn_hist to minimize number of lines changed and potential merge conflicts between bpf and bpf-next trees.

Notice also cur_hist_entry pointer reset to NULL at the beginning of instruction verification loop. This pointer avoids the problem of relying on last jump history entry's insn_idx to determine whether we already have entry for current instruction or not. It can happen that we added jump history entry because current instruction is_jmp_point(), but also we need to add instruction flags for stack access. In this case, we don't want to entries, so we need to reuse last added entry, if it is present.

Relying on insn_idx comparison has the same ambiguity problem as the one that was fixed recently in [0], so we avoid that.

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

The Linux kernel CVE team has assigned CVE-2023-52920 to this issue.

Affected and fixed versions
===========================

	Fixed in 6.8 with commit 41f6f64e6999

Please see https://www.kernel.org for a full list of currently supported kernel versions by the kernel community.

Unaffected versions might change over time as fixes are backported to older supported kernel versions. The official CVE entry at

	https://cve.org/CVERecord/?id=CVE-2023-52920

will be updated if fixes are backported, please check that for the most up to date information about this issue.

Affected files
==============

The file(s) affected by this issue are:
include/linux/bpf_verifier.h
kernel/bpf/verifier.c tools/testing/selftests/bpf/progs/verifier_subprog_precision.c tools/testing/selftests/bpf/verifier/precise.c

Mitigation
==========

The Linux kernel CVE team recommends that you update to the latest stable kernel version for this, and many other bugfixes. Individual changes are never tested alone, but rather are part of a larger kernel release. Cherry-picking individual commits is not recommended or supported by the Linux kernel community at all. If however, updating to the latest release is impossible, the individual changes to resolve this issue can be found at these commits:

	https://git.kernel.org/stable/c/41f6f64e6999a837048b1bd13a2f8742964eca6b
#1 lore raw

Hi Andrii and Eduard,

I'm trying to determine the security implication of CVE-2023-52920, or more specifically, what does commit 41f6f64e6999 ("bpf: support non-r10 register spill/fill to/from stack in precision tracking") fix. Superficially this looks more like an improvement to the verifier.

Quoted 6 lines · Greg Kroah-Hartman

Description
===========

In the Linux kernel, the following vulnerability has been resolved:

bpf: support non-r10 register spill/fill to/from stack in precision tracking

...

Before this commit, precise tracking of stack spills/fills was only supported if the frame pointer register r10 was directly used (and any offset to the frame pointer had to be presented through insn->off).

In the case where r10 is indirectly used (e.g. using r6 where r6 = r10 - 16), backtrack_insn() would simply return 0, implying that precision backtracking has completed; which technically isn't correct. However, since any register that is stored to the stack is already marked as precise in check_stack_write_fixed_off(), the verifier would not miss marking registers as precise - it would just be much more conservative than needed by marking more registers as precise than necessary.

In other words, even without commit 41f6f64e6999, it shouldn't be possible to craft a BPF program that tricks the verifier into incorrectly omitting precision marks, as the verifier would err on the side of being more strict.

Without this commit, the most significant impact would appear to be that a BPF program could trigger warnings as reported by Tao Lyu[1].

Does that assessment sound correct?

Thanks,
Shung-Hsi Yu

1: https://lore.kernel.org/all/[email protected]/

#2 lore raw
Quoted 1 line · Shung-Hsi Yu

Hi Andrii and Eduard,

Hi Shung-Hsi,

Quoted 4 lines

I'm trying to determine the security implication of CVE-2023-52920, or more specifically, what does commit 41f6f64e6999 ("bpf: support non-r10 register spill/fill to/from stack in precision tracking") fix. Superficially this looks more like an improvement to the verifier.

It is my understanding as well, that this commit is an optimization to avoid some precision marks. I do not see any security implications.

Thanks,
Eduard

[...]

#3 lore raw
Quoted 9 lines
Quoted 1 line

Hi Andrii and Eduard,

Hi Shung-Hsi,

Quoted 4 lines

I'm trying to determine the security implication of CVE-2023-52920, or more specifically, what does commit 41f6f64e6999 ("bpf: support non-r10 register spill/fill to/from stack in precision tracking") fix. Superficially this looks more like an improvement to the verifier.

Quoted 7 lines

It is my understanding as well, that this commit is an optimization to avoid some precision marks. I do not see any security implications.

Thanks,
Eduard

[...]

Hi Andrii, Eduard, and Shung-Hsi,

The previous discussion is here: https://lore.kernel.org/bpf/[email protected]/

In short, without this patch, the verifier will miss checking an execution path that can have out-of-bound access, which eventually leads to privilege escalation, like obtaining the root privilege.

Best,
Tao

#4 lore raw

On Fri, Nov 08, 2024 at 07:59:22AM GMT, Tao Lyu wrote:
...

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I'm trying to determine the security implication of CVE-2023-52920, or more specifically, what does commit 41f6f64e6999 ("bpf: support non-r10 register spill/fill to/from stack in precision tracking") fix. Superficially this looks more like an improvement to the verifier.

It is my understanding as well, that this commit is an optimization to avoid some precision marks. I do not see any security implications.

...

Quoted 6 lines

Hi Andrii, Eduard, and Shung-Hsi,

The previous discussion is here: https://lore.kernel.org/bpf/[email protected]/

In short, without this patch, the verifier will miss checking an execution path that can have out-of-bound access, which eventually leads to privilege escalation, like obtaining the root privilege.

Thank you both for looking into this, and Tao's pointer to previous discussion. I'll try to have this backported to at least stable 6.6.y.

Best,
Shung-Hsi