xserver/hw/xfree86
Aaron Plattner 72c5d153c9 xfree86: NUL-terminate strings in hwEnableIO
The Linux version of xf86EnableIO calls a helper function called hwEnableIO().
Except on Alpha, this function reads /proc/ioports looking for the 'keyboard'
and 'timer' ports, extracts the port ranges, and enables access to them. It does
this by reading 4 bytes from the string for the start port number and 4 bytes
for the last port number, passing those to atoi(). However, it doesn't add a
fifth byte for a NUL terminator, so some implementations of atoi() read past the
end of this string, triggering an AddressSanitizer error:

  ==1383==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff71fd5b74 at pc 0x7fe1be0de3e0 bp 0x7fff71fd5ae0 sp 0x7fff71fd5288
  READ of size 5 at 0x7fff71fd5b74 thread T0
      #0 0x7fe1be0de3df in __interceptor_atoi /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:520
      #1 0x564971adcc45 in hwEnableIO ../hw/xfree86/os-support/linux/lnx_video.c:138
      #2 0x564971adce87 in xf86EnableIO ../hw/xfree86/os-support/linux/lnx_video.c:174
      #3 0x5649719f6a30 in InitOutput ../hw/xfree86/common/xf86Init.c:439
      #4 0x564971585924 in dix_main ../dix/main.c:190
      #5 0x564971b6246e in main ../dix/stubmain.c:34
      #6 0x7fe1bdab6b24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
      #7 0x564971490e9d in _start (/home/aaron/git/x/xserver/build.asan/hw/xfree86/Xorg+0xb2e9d)

  Address 0x7fff71fd5b74 is located in stack of thread T0 at offset 100 in frame
      #0 0x564971adc96a in hwEnableIO ../hw/xfree86/os-support/linux/lnx_video.c:118

    This frame has 3 object(s):
      [32, 40) 'n' (line 120)
      [64, 72) 'buf' (line 122)
      [96, 100) 'target' (line 122) <== Memory access at offset 100 overflows this variable
  HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
        (longjmp and C++ exceptions *are* supported)
  SUMMARY: AddressSanitizer: stack-buffer-overflow /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:520 in __interceptor_atoi
  Shadow bytes around the buggy address:
    0x10006e3f2b10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10006e3f2b20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10006e3f2b30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10006e3f2b40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10006e3f2b50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  =>0x10006e3f2b60: 00 00 f1 f1 f1 f1 00 f2 f2 f2 00 f2 f2 f2[04]f3
    0x10006e3f2b70: f3 f3 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10006e3f2b80: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
    0x10006e3f2b90: f1 f1 f8 f2 00 f2 f2 f2 f8 f3 f3 f3 00 00 00 00
    0x10006e3f2ba0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1
    0x10006e3f2bb0: f1 f1 00 f3 f3 f3 00 00 00 00 00 00 00 00 00 00
  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
    Shadow gap:              cc
  ==1383==ABORTING

Fix this by NUL-terminating the string.

Fixes: https://gitlab.freedesktop.org/xorg/xserver/-/issues/1193#note_1053306
Signed-off-by: Aaron Plattner <aplattner@nvidia.com>
2021-09-10 11:20:47 -07:00
..
common xf86: Assign GPUs to screens according to configuration 2021-07-30 00:27:39 +00:00
ddc xfree86: fix gtf support detection for EDID 1.4 2021-04-06 11:25:42 +00:00
dixmods mi: List extensions in usage message 2021-01-29 12:52:09 +00:00
doc meson: Implement developer documentation build 2021-08-20 10:26:07 +00:00
dri Fix spelling/wording issues 2020-07-05 13:07:33 -07:00
dri2 dri2: Sync i965_pci_ids.h from mesa iris_pci_ids.h 2021-04-24 19:21:41 +00:00
drivers modesetting: Only use GAMMA_LUT if its size is 1024 2021-09-09 22:45:06 -07:00
exa meson: Install man pages 2018-03-27 10:28:33 -04:00
fbdevhw Fix spelling/wording issues 2020-07-05 13:07:33 -07:00
glamor_egl glamor: Make Xv extension initialize at depth 30. 2018-02-27 10:18:06 -05:00
i2c Fix spelling/wording issues 2020-07-05 13:07:33 -07:00
int10 int10: wrap entire V_ADDR_R* macros in parens for safer expansion 2020-11-17 18:43:39 -08:00
loader loader: strdup const string assigned to local variable name 2020-01-28 20:35:06 +00:00
man Update URL's in man pages 2020-05-10 17:46:33 -07:00
modes xfree86: Move xf86CVTMode() function 2021-08-06 11:29:29 +00:00
os-support xfree86: NUL-terminate strings in hwEnableIO 2021-09-10 11:20:47 -07:00
parser Fix spelling/wording issues 2020-07-05 13:07:33 -07:00
ramdac xfree86: Take second reference for SavedCursor in xf86CursorSetCursor 2020-08-31 12:10:43 +02:00
shadowfb meson: Fix linkage of loadable modules for PE/COFF 2017-10-12 15:22:04 -04:00
utils xfree86/cvt: Drop cvt utility 2021-08-06 11:29:29 +00:00
vgahw Fix spelling/wording issues 2020-07-05 13:07:33 -07:00
x86emu Fix spelling/wording issues 2020-07-05 13:07:33 -07:00
xkb XKB: Add debug key actions for grabs & window tree 2020-07-31 05:25:50 +00:00
.gitignore Xorg: Add Xorg.wrap to hw/xfree86/.gitignore 2014-04-18 11:39:16 +02:00
Makefile.am xfree86: Merge vbe into int10 2019-10-02 10:03:26 -04:00
Xorg.sh.in xfree86: rename Xorg.bin to Xorg 2015-01-05 09:53:58 +10:00
meson.build meson: Implement developer documentation build 2021-08-20 10:26:07 +00:00
sdksyms.sh Consolidate fourcc.h 2020-09-15 11:43:16 +02:00
xorg-wrapper.c Fix various spelling errors 2019-10-01 17:05:28 +00:00
xorgconf.cpp Fix spelling/wording issues 2020-07-05 13:07:33 -07:00