The vulnerability is a critical authentication bypass in the SSH server logic. It stems from a flaw in how the user's identity was managed during the public key authentication handshake. The process involves two key functions: PublicKeyHandler and AuthenticationMiddleware.
-
SSHServer.PublicKeyHandler: This function is called for each public key an SSH client offers. The vulnerability was introduced here because the code would immediately look up the user associated with the offered public key and store that user's identity in the session context. This happened before the client had to prove ownership of the key. An attacker could abuse this by offering the public key of a privileged user (the victim), thereby "poisoning" the session context with the victim's identity.
-
AuthenticationMiddleware: This function runs after a key has been successfully verified. The flaw here was that it did not re-verify and set the user identity based on the key that actually passed authentication. Instead, it trusted the (potentially poisoned) user identity already in the context. So, after the attacker's initial attempt with the victim's key failed (as expected), they would proceed to authenticate with their own valid key. The AuthenticationMiddleware would see a successful authentication but would fail to update the user context, leaving the attacker authenticated as the victim.
The patch rectifies this by removing the premature user-setting logic from PublicKeyHandler and adding explicit user-setting logic to AuthenticationMiddleware. This ensures the user identity in the context always matches the public key that was successfully used to authenticate, thereby closing the bypass vulnerability.