SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL_CloseAudioDevice heap-use-after-free crash for specific audio capture devices

Open oo8dev opened this issue 3 years ago • 5 comments

We have found that in our software, specific audio capture devices causes double-free (and use-after-free) crash in SDL_CloseAudioDevice function. The same thing can be reproduced in testaudio program (See video below)

(Video with audio)

https://github.com/libsdl-org/SDL/assets/29149078/a6f49adc-e354-43e8-aa23-6b7c54b22fa8

How to reproduce (Linux):

  1. Install EasyEffects
  2. Start audio capture using "Easy Effects Source" device in testaudio.c program
  3. Close audio stream

We have found out that this can also happen on MacOS on specific devices, according to sent crash reports on our side (SDL_CloseAudioDeviceREAL).

Output from this device (testaudioinfo):

INFO: Built-in audio drivers:
INFO:   0: pulseaudio
INFO:   1: alsa
INFO:   2: jack
INFO:   3: pipewire
INFO:   4: dsp
INFO:   5: disk
INFO:   6: dummy
INFO: Select a driver with the SDL_AUDIO_DRIVER environment variable.
INFO: Using audio driver: pulseaudio

INFO: Found 3 capture devices:
INFO:   0: Easy Effects Source  <----------------- Problematic device
INFO:      Sample Rate: 48000
INFO:      Channels: 2
INFO:      SDL_AudioFormat: 8120
INFO:      Buffer Size: 1024 frames
INFO:   1: NCX Analog Stereo
INFO:      Sample Rate: 48000
INFO:      Channels: 2
INFO:      SDL_AudioFormat: 8010
INFO:      Buffer Size: 1024 frames
INFO:   2: Loopback Analog Stereo
INFO:      Sample Rate: 48000
INFO:      Channels: 2
INFO:      SDL_AudioFormat: 8020
INFO:      Buffer Size: 1024 frames

Tested on main branch, newest commit (bf64fecf)

oo8dev avatar Oct 12 '23 10:10 oo8dev

Additional info from sanitizers (-fsanitize=address,undefined) shows up another use-after-free error: image

As you can see, DestroyPhysicalAudioDevice performs SDL_free(device), then accessing device->thread_alive a line below.

Tested on (95a9271d)

oo8dev avatar Oct 13 '23 19:10 oo8dev

The thread_alive issue you mentioned is now fixed, I assume the other crash is still present, though.

icculus avatar Oct 14 '23 17:10 icculus

Yes, can confirm.

oo8dev avatar Oct 15 '23 09:10 oo8dev

The latest fixes this bug for me (which went from 100% reproducible with EasyEffects to not reproducible at all), but it's a dramatic change, so please let me know if other problems pop up.

icculus avatar Oct 16 '23 14:10 icculus

Just tested it, works perfectly! Thanks!

oo8dev avatar Oct 16 '23 18:10 oo8dev