2
1

0001-fix-buffer-overflow-in-bpf_object__init_prog.patch 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. From 806b4e0a9f658d831119cece11a082ba1578b800 Mon Sep 17 00:00:00 2001
  2. From: Viktor Malik <vmalik@redhat.com>
  3. Date: Tue, 15 Apr 2025 17:50:14 +0200
  4. Subject: [PATCH] libbpf: Fix buffer overflow in bpf_object__init_prog
  5. Upstream: https://github.com/libbpf/libbpf/commit/806b4e0a9f658d831119cece11a082ba1578b800
  6. CVE: CVE-2025-29481
  7. As shown in [1], it is possible to corrupt a BPF ELF file such that
  8. arbitrary BPF instructions are loaded by libbpf. This can be done by
  9. setting a symbol (BPF program) section offset to a large (unsigned)
  10. number such that <section start + symbol offset> overflows and points
  11. before the section data in the memory.
  12. Consider the situation below where:
  13. - prog_start = sec_start + symbol_offset <-- size_t overflow here
  14. - prog_end = prog_start + prog_size
  15. prog_start sec_start prog_end sec_end
  16. | | | |
  17. v v v v
  18. .....................|################################|............
  19. The report in [1] also provides a corrupted BPF ELF which can be used as
  20. a reproducer:
  21. $ readelf -S crash
  22. Section Headers:
  23. [Nr] Name Type Address Offset
  24. Size EntSize Flags Link Info Align
  25. ...
  26. [ 2] uretprobe.mu[...] PROGBITS 0000000000000000 00000040
  27. 0000000000000068 0000000000000000 AX 0 0 8
  28. $ readelf -s crash
  29. Symbol table '.symtab' contains 8 entries:
  30. Num: Value Size Type Bind Vis Ndx Name
  31. ...
  32. 6: ffffffffffffffb8 104 FUNC GLOBAL DEFAULT 2 handle_tp
  33. Here, the handle_tp prog has section offset ffffffffffffffb8, i.e. will
  34. point before the actual memory where section 2 is allocated.
  35. This is also reported by AddressSanitizer:
  36. =================================================================
  37. ==1232==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c7302fe0000 at pc 0x7fc3046e4b77 bp 0x7ffe64677cd0 sp 0x7ffe64677490
  38. READ of size 104 at 0x7c7302fe0000 thread T0
  39. #0 0x7fc3046e4b76 in memcpy (/lib64/libasan.so.8+0xe4b76)
  40. #1 0x00000040df3e in bpf_object__init_prog /src/libbpf/src/libbpf.c:856
  41. #2 0x00000040df3e in bpf_object__add_programs /src/libbpf/src/libbpf.c:928
  42. #3 0x00000040df3e in bpf_object__elf_collect /src/libbpf/src/libbpf.c:3930
  43. #4 0x00000040df3e in bpf_object_open /src/libbpf/src/libbpf.c:8067
  44. #5 0x00000040f176 in bpf_object__open_file /src/libbpf/src/libbpf.c:8090
  45. #6 0x000000400c16 in main /poc/poc.c:8
  46. #7 0x7fc3043d25b4 in __libc_start_call_main (/lib64/libc.so.6+0x35b4)
  47. #8 0x7fc3043d2667 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x3667)
  48. #9 0x000000400b34 in _start (/poc/poc+0x400b34)
  49. 0x7c7302fe0000 is located 64 bytes before 104-byte region [0x7c7302fe0040,0x7c7302fe00a8)
  50. allocated by thread T0 here:
  51. #0 0x7fc3046e716b in malloc (/lib64/libasan.so.8+0xe716b)
  52. #1 0x7fc3045ee600 in __libelf_set_rawdata_wrlock (/lib64/libelf.so.1+0xb600)
  53. #2 0x7fc3045ef018 in __elf_getdata_rdlock (/lib64/libelf.so.1+0xc018)
  54. #3 0x00000040642f in elf_sec_data /src/libbpf/src/libbpf.c:3740
  55. The problem here is that currently, libbpf only checks that the program
  56. end is within the section bounds. There used to be a check
  57. `while (sec_off < sec_sz)` in bpf_object__add_programs, however, it was
  58. removed by commit 6245947c1b3c ("libbpf: Allow gaps in BPF program
  59. sections to support overriden weak functions").
  60. Add a check for detecting the overflow of `sec_off + prog_sz` to
  61. bpf_object__init_prog to fix this issue.
  62. [1] https://github.com/lmarch2/poc/blob/main/libbpf/libbpf.md
  63. Fixes: 6245947c1b3c ("libbpf: Allow gaps in BPF program sections to support overriden weak functions")
  64. Reported-by: lmarch2 <2524158037@qq.com>
  65. Signed-off-by: Viktor Malik <vmalik@redhat.com>
  66. Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
  67. Reviewed-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
  68. Link: https://github.com/lmarch2/poc/blob/main/libbpf/libbpf.md
  69. Link: https://lore.kernel.org/bpf/20250415155014.397603-1-vmalik@redhat.com
  70. Signed-off-by: Titouan Christophe <titouan.christophe@mind.be>
  71. ---
  72. src/libbpf.c | 2 +-
  73. 1 file changed, 1 insertion(+), 1 deletion(-)
  74. diff --git a/src/libbpf.c b/src/libbpf.c
  75. index b2591f5ca..56250b5ac 100644
  76. --- a/src/libbpf.c
  77. +++ b/src/libbpf.c
  78. @@ -896,7 +896,7 @@ bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
  79. return -LIBBPF_ERRNO__FORMAT;
  80. }
  81. - if (sec_off + prog_sz > sec_sz) {
  82. + if (sec_off + prog_sz > sec_sz || sec_off + prog_sz < sec_off) {
  83. pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
  84. sec_name, sec_off);
  85. return -LIBBPF_ERRNO__FORMAT;