perf record: reject CPU indices equal to the mask size
Subsystems (2)
PATCH 1/1 perf record: reject CPU indices equal to the mask size
From: Paul Moses <[email protected]>
record__mmap_cpu_mask_init() rejects CPU indices greater than nbits but accepts nbits itself. Valid indices end at nbits - 1. When nbits is a multiple of BITS_PER_LONG, __set_bit() accesses the word immediately past the bitmap allocation.
User specified recording thread masks reach this write before they are intersected with the selected CPU mask. For example, an affinity mask containing CPU 64 overruns a bitmap allocated for 64 possible CPUs.
==40343==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7bbab4000018 at pc 0x6195de57af45 bp 0x7ffe25545d20 sp 0x7ffe25545d18 READ of size 8 at 0x7bbab4000018 thread T0
Reject indices greater than or equal to nbits before setting a bit.
--- tools/perf/builtin-record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-record.c +2 -2
@@ -3689,8 +3689,8 @@ static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpreturn 0 ;perf_cpu_map__for_each_cpu_skip_any (cpu, idx, cpus) { -/* Return ENODEV is input cpu is greater than max cpu */ -if ((unsigned long )cpu.cpu > mask->nbits) +/* Return ENODEV if the CPU index is outside the mask. */ +if ((unsigned long )cpu.cpu >= mask->nbits)return -ENODEV ;__set_bit (cpu.cpu, mask->bits); }
Signature · 1 line
2.56.GIT
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 11 lines
From: Paul Moses <[email protected]>
record__mmap_cpu_mask_init()rejects CPU indices greater than nbits but accepts nbits itself. Valid indices end at nbits - 1. When nbits is a multiple ofBITS_PER_LONG,__set_bit()accesses the word immediately past the bitmap allocation.User specified recording thread masks reach this write before they are intersected with the selected CPU mask. For example, an affinity mask containing CPU 64 overruns a bitmap allocated for 64 possible CPUs.
Quoted 8 lines
==40343==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x7bbab4000018at pc0x6195de57af45bp0x7ffe25545d20sp0x7ffe25545d18READ of size 8 at0x7bbab4000018thread T0Reject indices greater than or equal to nbits before setting a bit.
Fixes cbd7bfc7fd99 ("tools/perf: Fix out of bound access to cpu mask array")Signed-off-by Paul Moses <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerQuoted 10 lines · Paul
From: Paul Moses <[email protected]>
record__mmap_cpu_mask_init()rejects CPU indices greater than nbits but accepts nbits itself. Valid indices end at nbits - 1. When nbits is a multiple ofBITS_PER_LONG,__set_bit()accesses the word immediately past the bitmap allocation.User specified recording thread masks reach this write before they are intersected with the selected CPU mask. For example, an affinity mask containing CPU 64 overruns a bitmap allocated for 64 possible CPUs.
What's the exact command line?
Quoted 9 lines
==40343==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x7bbab4000018at pc0x6195de57af45bp0x7ffe25545d20sp0x7ffe25545d18READ of size 8 at0x7bbab4000018thread T0Reject indices greater than or equal to nbits before setting a bit.
Fixes cbd7bfc7fd99 ("tools/perf: Fix out of bound access to cpu mask array")Signed-off-by Paul Moses <[email protected]>
Arnaldo, do you want to take it for 7.4?
Thanks,
Namhyung
Quoted 23 lines
--- tools/perf/builtin-record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/perf/builtin-record.c +2
-2@@ -3689,8 +3689,8 @@ static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpreturn 0 ;perf_cpu_map__for_each_cpu_skip_any (cpu, idx, cpus) { -/* Return ENODEV is input cpu is greater than max cpu */ -if ((unsigned long )cpu.cpu > mask->nbits) +/* Return ENODEV if the CPU index is outside the mask. */ +if ((unsigned long )cpu.cpu >= mask->nbits)return -ENODEV ;__set_bit (cpu.cpu, mask->bits); }Signature · 1 line
2.56.GIT
Quoted 8 lines · Namhyung Kim
Quoted 7 lines · Paul
From: Paul Moses <[email protected]>
record__mmap_cpu_mask_init()rejects CPU indices greater than nbits but accepts nbits itself. Valid indices end at nbits - 1. When nbits is a multiple ofBITS_PER_LONG,__set_bit()accesses the word immediately past the bitmap allocation.
Quoted 6 lines
Quoted 3 lines
User specified recording thread masks reach this write before they are intersected with the selected CPU mask. For example, an affinity mask containing CPU 64 overruns a bitmap allocated for 64 possible CPUs.
What's the exact command line?
mkdir -p /tmp/perf-asan-build
make -C /usr/src/linux-7.x/tools/perf O=/tmp/perf-asan-build -j"$(nproc)" \
WERROR=0 NO_RUST=1 NO_SHELLCHECK=1 NO_LIBPYTHON=1 \
EXTRA_CFLAGS='-fsanitize=address -fno-omit-frame-pointer -g' \
LDFLAGS='-fsanitize=address' perf
qemu-system-x86_64 -enable-kvm -cpu host -smp 64 -m 4G \
-kernel /boot/vmlinuz-$(uname -r) \
-initrd /tmp/perf-guest-sym.cpio.zst \
-append 'console=ttyS0 rdinit=/init panic=-1 loglevel=4' \
-nographic -no-reboot
ASAN_SYMBOLIZER_PATH=/bin/llvm-symbolizer \
ASAN_OPTIONS=detect_leaks=0:abort_on_error=1:symbolize=1 \
/perf record -e cpu-clock -C 0 --threads=0/64 -o /tmp/perf.data -- sleep 0.05RUNNING: perf record -e cpu-clock -C 0 --threads=0/64
=================================================================
==540==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6c9cc78223b8 at pc 0x60af685bdb4d bp 0x7ffd1e954400 sp 0x7ffd1e9543f8 READ of size 8 at 0x6c9cc78223b8 thread T0
#0 0x60af685bdb4c in ___set_bit /usr/src/linux-7.x/tools/include/asm-generic/bitops/non-atomic.h:22:6
#1 0x60af685bdb4c in record__mmap_cpu_mask_init /usr/src/linux-7.x/tools/perf/builtin-record.c:3695:3
#2 0x60af685bde4d in record__mmap_cpu_mask_init_spec /usr/src/linux-7.x/tools/perf/builtin-record.c:3710:6
#3 0x60af685bde4d in record__init_thread_masks_spec /usr/src/linux-7.x/tools/perf/builtin-record.c:3819:9
#4 0x60af685b9bcc in record__init_thread_user_masks /usr/src/linux-7.x/tools/perf/builtin-record.c:4015:8
#5 0x60af685b9bcc in record__init_thread_masks /usr/src/linux-7.x/tools/perf/builtin-record.c:4075:9
#6 0x60af685b809a in cmd_record /usr/src/linux-7.x/tools/perf/builtin-record.c:4395:8
#7 0x60af686cc167 in run_builtin /usr/src/linux-7.x/tools/perf/perf.c:348:11
#8 0x60af686cb739 in main /usr/src/linux-7.x/tools/perf/perf.c
#9 0x707cd1a470b9 (/lib/ld-musl-x86_64.so.1+0x690b9) (BuildId: d1535db89359be00fb635e9145a3a49d0cbd8261)0x6c9cc78223b8 is located 0 bytes after 8-byte region [0x6c9cc78223b0,0x6c9cc78223b8) allocated by thread T0 here:
#0 0x60af685506d2 in calloc (/perf+0xc266d2) (BuildId: c5c8ceb2947501d8a08fbc5dd506840be170f336)
#1 0x60af685bdcd9 in bitmap_zalloc /usr/src/linux-7.x/tools/include/linux/bitmap.h:121:9
#2 0x60af685bdcd9 in record__mmap_cpu_mask_alloc /usr/src/linux-7.x/tools/perf/builtin-record.c:3093:15
#3 0x60af685bdcd9 in record__thread_mask_alloc /usr/src/linux-7.x/tools/perf/builtin-record.c:3117:8
#4 0x60af685bdcd9 in record__init_thread_masks_spec /usr/src/linux-7.x/tools/perf/builtin-record.c:3807:8
#5 0x60af685b9bcc in record__init_thread_user_masks /usr/src/linux-7.x/tools/perf/builtin-record.c:4015:8
#6 0x60af685b9bcc in record__init_thread_masks /usr/src/linux-7.x/tools/perf/builtin-record.c:4075:9
#7 0x60af685b809a in cmd_record /usr/src/linux-7.x/tools/perf/builtin-record.c:4395:8
#8 0x60af686cc167 in run_builtin /usr/src/linux-7.x/tools/perf/perf.c:348:11
#9 0x60af686cb739 in main /usr/src/linux-7.x/tools/perf/perf.c
#10 0x707cd1a470b9 (/lib/ld-musl-x86_64.so.1+0x690b9) (BuildId: d1535db89359be00fb635e9145a3a49d0cbd8261)SUMMARY: AddressSanitizer: heap-buffer-overflow /usr/src/linux-7.x/tools/include/asm-generic/bitops/non-atomic.h:22:6 in ___set_bit Shadow bytes around the buggy address:
0x6c9cc7822100: fa fa fd fa fa fa 00 fa fa fa 00 fa fa fa fd fa
0x6c9cc7822180: fa fa 01 fa fa fa 00 fa fa fa fd fa fa fa 00 fa
0x6c9cc7822200: fa fa 00 fa fa fa 00 fa fa fa 00 fa fa fa 00 fa
0x6c9cc7822280: fa fa 02 fa fa fa 00 fa fa fa 03 fa fa fa fd fa
0x6c9cc7822300: fa fa fd fa fa fa 00 fa fa fa 00 fa fa fa 00 fa
=>0x6c9cc7822380: fa fa 00 fa fa fa 00[fa]fa fa fd fd fa fa fd fa
0x6c9cc7822400: fa fa 00 02 fa fa 00 fa fa fa fa fa fa fa fa fa
0x6c9cc7822480: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x6c9cc7822500: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x6c9cc7822580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x6c9cc7822600: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb
==540==ABORTING
Aborted
PERF_RC=134