Skip to content

Instantly share code, notes, and snippets.

@dedobbin
Last active November 27, 2023 23:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedobbin/29adb1dae10932b4a88721bb00a1fc45 to your computer and use it in GitHub Desktop.
Save dedobbin/29adb1dae10932b4a88721bb00a1fc45 to your computer and use it in GitHub Desktop.
breaking alsa
// Problem solved: ok very silly. I used snd_pcm_hw_params_alloca, which allocates on stack.
// Use snd_pcm_hw_params_malloc instead
// oops
// Reference: https://gist.github.com/ghedo/963382/815c98d1ba0eda1b486eb9d80d9a91a81d995283
// This function used to corrupts the memory
void init_params (snd_pcm_t **pcm_handle, snd_pcm_hw_params_t **params)
{
// https://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m___h_w___params.html#ga06b83cb9a788f99b7b09b570b4355cee
//snd_pcm_hw_params_alloca(params);
snd_pcm_hw_params_malloc(params);
// https://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m___h_w___params.html#ga6e2dd8efbb7a4084bd05e6cc458d84f7
snd_pcm_hw_params_any(*pcm_handle, *params);
}
// Function to display the corruption
void corruption_test()
{
unsigned rc;
// Obtain handle to PCM device
snd_pcm_t *pcm_handle;
if (rc = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0){
printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(rc));
exit(1);
}
#ifndef BROKEN_FUNC
// Initialize params, works fine like this
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(&params);
snd_pcm_hw_params_any(pcm_handle, params);
#else
// HERE LIES THE PROBLEM
// Initialize params, when done like this, snd_pcm_hw_params_set_access
// causes *** stack smashing detected ***: <unknown> terminated
snd_pcm_hw_params_t *params;
init_params(&pcm_handle, &params);
#endif
if (rc = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(rc));
exit(1);
}
snd_pcm_access_t acc;
snd_pcm_hw_params_get_access(params, &acc);
printf("access: %s\n", acc == SND_PCM_ACCESS_RW_INTERLEAVED ? "interleaved" : "other");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment