Centos-kernel-stream-9/kernel/bpf
Rado Vrbovsky 98d1d44d43 Merge: CVE-2024-49888: bpf: Fix a sdiv overflow issue
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5789

JIRA: https://issues.redhat.com/browse/RHEL-64597  
CVE: CVE-2024-49888

```
bpf: Fix a sdiv overflow issue

Zac Ecob reported a problem where a bpf program may cause kernel crash due
to the following error:
  Oops: divide error: 0000 [#1] PREEMPT SMP KASAN PTI

The failure is due to the below signed divide:
  LLONG_MIN/-1 where LLONG_MIN equals to -9,223,372,036,854,775,808.
LLONG_MIN/-1 is supposed to give a positive number 9,223,372,036,854,775,808,
but it is impossible since for 64-bit system, the maximum positive
number is 9,223,372,036,854,775,807. On x86_64, LLONG_MIN/-1 will
cause a kernel exception. On arm64, the result for LLONG_MIN/-1 is
LLONG_MIN.

Further investigation found all the following sdiv/smod cases may trigger
an exception when bpf program is running on x86_64 platform:
  - LLONG_MIN/-1 for 64bit operation
  - INT_MIN/-1 for 32bit operation
  - LLONG_MIN%-1 for 64bit operation
  - INT_MIN%-1 for 32bit operation
where -1 can be an immediate or in a register.

On arm64, there are no exceptions:
  - LLONG_MIN/-1 = LLONG_MIN
  - INT_MIN/-1 = INT_MIN
  - LLONG_MIN%-1 = 0
  - INT_MIN%-1 = 0
where -1 can be an immediate or in a register.

Insn patching is needed to handle the above cases and the patched codes
produced results aligned with above arm64 result. The below are pseudo
codes to handle sdiv/smod exceptions including both divisor -1 and divisor 0
and the divisor is stored in a register.

sdiv:
      tmp = rX
      tmp += 1 /* [-1, 0] -> [0, 1]
      if tmp >(unsigned) 1 goto L2
      if tmp == 0 goto L1
      rY = 0
  L1:
      rY = -rY;
      goto L3
  L2:
      rY /= rX
  L3:

smod:
      tmp = rX
      tmp += 1 /* [-1, 0] -> [0, 1]
      if tmp >(unsigned) 1 goto L1
      if tmp == 1 (is64 ? goto L2 : goto L3)
      rY = 0;
      goto L2
  L1:
      rY %= rX
  L2:
      goto L4  // only when !is64
  L3:
      wY = wY  // only when !is64
  L4:

  [1] https://lore.kernel.org/bpf/tPJLTEh7S_DxFEqAI2Ji5MBSoZVg7_G-Py2iaZpAaWtM961fFTWtsnlzwvTbzBzaUzwQAoNATXKUlt0LZOFgnDcIyKCswAnAGdUF3LBrhGQ=@protonmail.com/

Reported-by: Zac Ecob <zacecob@protonmail.com>
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240913150326.1187788-1-yonghong.song@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
(cherry picked from commit 7dd34d7b7dcf9309fc6224caf4dd5b35bedddcb7)
```

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>

---

<small>Created 2024-11-15 10:34 UTC by backporter - [KWF FAQ](https://red.ht/kernel_workflow_doc) - [Slack #team-kernel-workflow](https://redhat-internal.slack.com/archives/C04LRUPMJQ5) - [Source](https://gitlab.com/cki-project/kernel-workflow/-/blob/main/webhook/utils/backporter.py) - [Documentation](https://gitlab.com/cki-project/kernel-workflow/-/blob/main/docs/README.backporter.md) - [Report an issue](https://gitlab.com/cki-project/kernel-workflow/-/issues/new?issue%5Btitle%5D=backporter%20webhook%20issue)</small>

Approved-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>
Approved-by: Derek Barbosa <debarbos@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-11-22 09:28:08 +00:00
..
preload
Kconfig
Makefile bpf: Introduce bpf_arena. 2024-10-15 10:49:15 +02:00
arena.c bpf: Remove redundant page mask of vmf->address 2024-11-13 09:39:12 +01:00
arraymap.c bpf: Do not walk twice the map on free 2024-11-11 07:44:55 +01:00
bloom_filter.c bpf: Check bloom filter map value size 2024-10-15 10:49:17 +02:00
bpf_cgrp_storage.c bpf: Enable bpf_cgrp_storage for cgroup1 non-attach case 2024-06-25 10:52:17 +02:00
bpf_inode_storage.c
bpf_iter.c bpf: move sleepable flag from bpf_prog_aux to bpf_prog 2024-10-15 10:49:16 +02:00
bpf_local_storage.c bpf: fix order of args in call to bpf_map_kvcalloc 2024-11-19 07:40:50 +01:00
bpf_lru_list.c
bpf_lru_list.h
bpf_lsm.c Merge: bpf, lsm: Add check for BPF LSM return value 2024-11-22 09:15:56 +00:00
bpf_struct_ops.c bpf: Check return from set_memory_rox() 2024-11-07 13:58:29 +01:00
bpf_task_storage.c
btf.c Merge: BPF 6.10 rebase 2024-11-22 09:24:52 +00:00
cgroup.c bpf: Allow helper bpf_get_[ns_]current_pid_tgid() for all prog types 2024-11-07 13:58:30 +01:00
cgroup_iter.c
core.c bpf: Switch to krealloc_array() 2024-11-11 07:44:54 +01:00
cpumap.c bpf: report RCU QS in cpumap kthread 2024-10-15 10:49:17 +02:00
cpumask.c bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs 2024-11-07 13:58:46 +01:00
devmap.c Merge: BPF 6.10 rebase 2024-11-22 09:24:52 +00:00
disasm.c bpf: add special internal-only MOV instruction to resolve per-CPU addrs 2024-11-07 13:58:42 +01:00
disasm.h
dispatcher.c bpf: Use arch_bpf_trampoline_size 2024-06-25 10:52:14 +02:00
hashtab.c bpf: Do not walk twice the hash map on free 2024-11-11 07:44:55 +01:00
helpers.c bpf: Defer work in bpf_timer_cancel_and_free 2024-11-19 07:40:49 +01:00
inode.c Merge: fs: backport mnt_idmap type 2024-11-11 08:26:30 +00:00
link_iter.c
local_storage.c
log.c bpf: Replace deprecated strncpy with strscpy 2024-11-07 13:58:42 +01:00
lpm_trie.c Merge: BPF 6.10 rebase 2024-11-22 09:24:52 +00:00
map_in_map.c bpf: save extended inner map info for percpu array maps as well 2024-11-13 09:39:18 +01:00
map_in_map.h bpf: Add map and need_defer parameters to .map_fd_put_ptr() 2024-06-25 10:52:04 +02:00
map_iter.c bpf: treewide: Annotate BPF kfuncs in BTF 2024-10-15 10:49:07 +02:00
memalloc.c bpf: Remove unnecessary cpu == 0 check in memalloc 2024-06-25 11:07:39 +02:00
mmap_unlock_work.h
mprog.c
net_namespace.c
offload.c
percpu_freelist.c
percpu_freelist.h
prog_iter.c
queue_stack_maps.c
reuseport_array.c
ringbuf.c bpf: Use raw_spinlock_t in ringbuf 2024-11-05 12:06:33 -03:00
stackmap.c bpf: Fix stackmap overflow check on 32-bit arches 2024-10-15 10:49:14 +02:00
syscall.c bpf: Add BPF_PROG_TYPE_CGROUP_SKB attach type enforcement in BPF_LINK_CREATE 2024-11-13 09:38:45 +01:00
sysfs_btf.c btf: Avoid weak external references 2024-11-11 07:44:47 +01:00
task_iter.c bpf: Fix an issue due to uninitialized bpf_iter_task 2024-06-25 11:07:44 +02:00
tcx.c
tnum.c bpf: simplify tnum output if a fully known constant 2024-06-25 10:52:03 +02:00
token.c bpf,token: Use BIT_ULL() to convert the bit mask 2024-10-15 10:49:05 +02:00
trampoline.c bpf: Choose RCU Tasks based on TASKS_RCU rather than PREEMPTION 2024-11-07 14:37:17 +01:00
verifier.c Merge: CVE-2024-49888: bpf: Fix a sdiv overflow issue 2024-11-22 09:28:08 +00:00