Skip to content

iostat: Fix stack-use-after-return with options -N and -j - #463

Merged
sysstat merged 1 commit into
sysstat:masterfrom
sagaragas:fix/iostat-stack-use-after-return
Nov 14, 2026
Merged

sysstat merged 1 commit into
sysstat:masterfrom
sagaragas:fix/iostat-stack-use-after-return

Conversation

@sagaragas

Copy link
Copy Markdown
Contributor

get_dm_name_from_registered_name() (option -N) and get_pretty_name_from_persistent() (option -j) return basename() of a local array. So add_list_device() reads the device name from the stack frame of a function that has already returned.

A normal build still passes the simulation tests, but an ASan build fails tests 02644, 02650, 02660 and 02665 on master (663071d):

export CFLAGS="-g -O1 -fsanitize=address -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address"
./do_test conf && ./do_test comp
TZ=GMT ASAN_OPTIONS=detect_leaks=0 ./do_test sim ignore

Test 02644 (iostat -dN virtualhd sdr):

==75229==ERROR: AddressSanitizer: stack-use-after-return on address 0x70762d5010a3 at pc 0x70762f09c79c bp 0x7ffd4e13b340 sp 0x7ffd4e13ab00
READ of size 5 at 0x70762d5010a3 thread T0
    [...]
    #3 0x59537248f447 in add_list_device iostat.c:357
    #4 0x59537248e461 in main iostat.c:2395
    [...]

Address 0x70762d5010a3 is located in stack of thread T0 at offset 4259 in frame
    #0 0x59537248ec0f in get_dm_name_from_registered_name iostat.c:140

  This frame has 2 object(s):
    [32, 4128) 'filen' (line 142)
    [4256, 8352) 'target' (line 143) <== Memory access at offset 4259 is inside this variable

The three -j tests report the same error for target in get_pretty_name_from_persistent().

This change makes both arrays static, as get_devname_from_sysfs() already does for the same readlink()/basename() sequence. add_list_device() copies the name before either function is called again.

Tested on Debian 13 with gcc 14.2.0. With ASan, the four tests now pass, and test 01705 fails before and after because it uses LD_PRELOAD, which is unrelated. A normal build passes all 531 tests and make extratest before and after the change. Not tested with real device mapper or persistent names, only with the tests/root1 tree.

🤖 This change was written with Droid.

get_dm_name_from_registered_name() (option -N) and
get_pretty_name_from_persistent() (option -j) read a symlink into a
local array and return basename() of it, which is a pointer into that
array. add_list_device() then reads the device name from the stack
frame of a function that has already returned.

The simulation tests pass with a normal build, but with
-fsanitize=address tests 02644, 02650, 02660 and 02665 abort with
"stack-use-after-return".

Make both arrays static, as get_devname_from_sysfs() already does for
the same readlink()/basename() sequence. add_list_device() copies the
name into the device list before either function is called again.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@sysstat
sysstat merged commit edd9964 into sysstat:master Nov 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants