Merge "Don\'t crash if we get a DHCP packet with the wrong port." into nyc-dev

am: ba27e40d4c

* commit 'ba27e40d4c9c1462b6adccc62c47700a9a0dea69':
  Don't crash if we get a DHCP packet with the wrong port.
This commit is contained in:
Lorenzo Colitti
2016-03-09 07:57:57 +00:00
committed by android-build-merger
2 changed files with 38 additions and 1 deletions

View File

@@ -819,7 +819,11 @@ abstract class DhcpPacket {
// server-to-server packets, e.g. for relays.
if (!isPacketToOrFromClient(udpSrcPort, udpDstPort) &&
!isPacketServerToServer(udpSrcPort, udpDstPort)) {
return null;
// This should almost never happen because we use SO_ATTACH_FILTER on the packet
// socket to drop packets that don't have the right source ports. However, it's
// possible that a packet arrives between when the socket is bound and when the
// filter is set. http://b/26696823 .
throw new ParseException("Unexpected UDP ports %d->%d", udpSrcPort, udpDstPort);
}
}

View File

@@ -557,6 +557,39 @@ public class DhcpPacketTest extends TestCase {
"wvm.edu", "10.1.105.252", null, 86400, false, dhcpResults);
}
@SmallTest
public void testUdpInvalidDstPort() throws Exception {
final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
// Ethernet header.
"9cd917000000001c2e0000000800" +
// IP header.
"45a00148000040003d115087d18194fb0a0f7af2" +
// UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
// NOTE: The destination port is a non-DHCP port.
"0043aaaa01341268" +
// BOOTP header.
"02010600d628ba8200000000000000000a0f7af2000000000a0fc818" +
// MAC address.
"9cd91700000000000000000000000000" +
// Server name.
"0000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000" +
// File.
"0000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000" +
// Options.
"6382536335010236040a0169fc3304000151800104ffff000003040a0fc817060cd1818003d1819403" +
"d18180060f0777766d2e6564751c040a0fffffff000000"
).toCharArray(), false));
try {
DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
fail("Packet with invalid dst port did not throw ParseException");
} catch (ParseException expected) {}
}
@SmallTest
public void testMultipleRouters() throws Exception {
final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((