Router: Validate tunnel ID in requests

Fix max ID
This commit is contained in:
zzz
2018-03-07 18:06:46 +00:00
parent ceac733b66
commit 7433eeb5c0
5 changed files with 16 additions and 6 deletions

View File

@ -27,7 +27,7 @@ import java.io.OutputStream;
public class TunnelId extends DataStructureImpl {
private long _tunnelId;
public static final long MAX_ID_VALUE = (1L << 32) - 2L;
public static final long MAX_ID_VALUE = 0xffffffffL;
public TunnelId() {
_tunnelId = -1;

View File

@ -396,7 +396,7 @@ public class TunnelDispatcher implements Service {
long rv;
TunnelId tid;
do {
rv = 1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE - 1);
rv = 1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE);
tid = new TunnelId(rv);
} while (_outboundGateways.containsKey(tid));
return rv;
@ -413,7 +413,7 @@ public class TunnelDispatcher implements Service {
long rv;
TunnelId tid;
do {
rv = 1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE - 1);
rv = 1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE);
tid = new TunnelId(rv);
} while (_participants.containsKey(tid));
return rv;
@ -430,7 +430,7 @@ public class TunnelDispatcher implements Service {
long rv;
TunnelId tid;
do {
rv = 1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE - 1);
rv = 1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE);
tid = new TunnelId(rv);
} while (_inboundGateways.containsKey(tid));
return rv;

View File

@ -668,6 +668,16 @@ class BuildHandler implements Runnable {
return;
}
if (ourId <= 0 || ourId > TunnelId.MAX_ID_VALUE ||
nextId <= 0 || nextId > TunnelId.MAX_ID_VALUE) {
_context.statManager().addRateData("tunnel.rejectHostile", 1);
if (_log.shouldWarn())
_log.warn("Dropping build request, bad tunnel ID: " + req);
if (from != null)
_context.commSystem().mayDisconnect(from);
return;
}
// Loop checks
if ((!isOutEnd) && _context.routerHash().equals(nextPeer)) {
_context.statManager().addRateData("tunnel.rejectHostile", 1);

View File

@ -95,7 +95,7 @@ abstract class BuildRequestor {
else if (isIB && i == len - 1)
id = ctx.tunnelDispatcher().getNewIBEPID();
else
id = 1 + ctx.random().nextLong(TunnelId.MAX_ID_VALUE - 1);
id = 1 + ctx.random().nextLong(TunnelId.MAX_ID_VALUE);
cfg.getConfig(i).setReceiveTunnelId(DataHelper.toLong(4, id));
}

View File

@ -161,7 +161,7 @@ public class FragmentTest {
_context.random().nextBytes(toRouter.getData());
}
if (includeTunnel)
toTunnel = new TunnelId(_context.random().nextLong(TunnelId.MAX_ID_VALUE));
toTunnel = new TunnelId(1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE));
return new PendingGatewayMessage(m, toRouter, toTunnel);
}