The vulnerability lies in the tempfile.TemporaryDirectory class's cleanup mechanism, specifically when handling permission errors. The cleanup() method initiates this process by calling the _rmtree() method. The _rmtree() method, in its onerror handler (used during recursive deletion), would attempt to reset permissions on files or directories it couldn't initially delete. In the vulnerable versions, the calls to os.chmod and os.chflags within this error handler (originally in a nested resetperms function) would follow symlinks by default. This meant that if a symlink within the temporary directory pointed to an external file/directory, the permission changes would apply to the target of the symlink, not the symlink itself. The patches address this by ensuring that os.chmod and os.chflags are called with follow_symlinks=False (where supported) or by checking if the path is a symlink before attempting to change permissions. Therefore, tempfile.TemporaryDirectory.cleanup is the public entry point, and tempfile.TemporaryDirectory._rmtree is the method containing the flawed error handling logic that dereferenced symlinks.