The vulnerability is a race condition in the ImageMagick distributed pixel cache server. The core of the vulnerability lies in the DistributePixelCacheServer function, which handles incoming client connections. When a new connection is accepted, a new thread is created to handle it. The vulnerability arises because the socket descriptor for the new connection was passed to the thread as a pointer to a variable on the main thread's stack. If new connections arrived in rapid succession, this stack variable could be overwritten with a new socket descriptor before the previously created thread had a chance to read it. This could lead to a situation where two threads would be using the same socket descriptor, or a thread could be using a closed and reused file descriptor, resulting in a file descriptor hijacking.
The fix involves changing the way the socket descriptor is passed to the handler thread. Instead of passing a pointer to a stack variable, the patched code allocates memory on the heap for each new connection's socket descriptor. A pointer to this heap-allocated memory is then passed to the new thread. This ensures that each thread gets a unique and stable pointer to its socket descriptor, thus eliminating the race condition.
In addition to fixing the race condition, the patches also introduced more robust validation of data received from the client in functions like WriteDistributeCacheMetacontent, WriteDistributeCachePixels, ReadDistributeCacheMetacontent, and ReadDistributeCachePixels. These changes prevent potential buffer and heap overflows by validating the size of the incoming data before processing it. While these are separate from the race condition, they are part of the overall security hardening of the distributed pixel cache functionality.