Skip to content
Permalink
Browse files
[NTP] Fix NET-704: NTPUDPClient does not check response packet pairing.
  • Loading branch information
Ondřej Fibich committed Nov 8, 2021
1 parent 0846dae commit 1e32f35c5a1064fbf638e18032f62c8aae4a5b4a
Showing 1 changed file with 9 additions and 1 deletion.
@@ -70,7 +70,8 @@ public TimeInfo getTime(final InetAddress host) throws IOException
* @param host The address of the server.
* @param port The port of the service.
* @return The time value retrieved from the server.
* @throws IOException If an error occurs while retrieving the time.
* @throws IOException If an error occurs while retrieving the time or if
* received packet does not match the request.
*/
public TimeInfo getTime(final InetAddress host, final int port) throws IOException
{
@@ -106,6 +107,13 @@ public TimeInfo getTime(final InetAddress host, final int port) throws IOExcepti
_socket_.receive(receivePacket);

final long returnTimeMillis = System.currentTimeMillis();

// Prevent invalid time information if response does not match request
if (!now.equals(recMessage.getOriginateTimeStamp()))
{
throw new IOException("Originate time does not match the request");
}

// create TimeInfo message container but don't pre-compute the details yet
return new TimeInfo(recMessage, returnTimeMillis, false);
}

0 comments on commit 1e32f35

Please sign in to comment.