Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/src/main/java/org/tron/core/config/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public class NetConstants {
public static final int MAX_BLOCK_FETCH_PER_PEER = 100;
public static final int MAX_TRX_FETCH_PER_PEER = 1000;
public static final int MAX_SYNC_CHAIN_IDS = 30;
// AdvService stops queuing items to spread once more than this many are waiting.
public static final int MAX_SPREAD_SIZE = 1_000;
}

public class DatabaseConstants {
Expand Down
24 changes: 24 additions & 0 deletions framework/src/main/java/org/tron/core/net/message/TronMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package org.tron.core.net.message;

import com.google.protobuf.CodedInputStream;
import java.io.IOException;
import org.tron.common.overlay.message.Message;
import org.tron.core.exception.P2pException;
import org.tron.core.exception.P2pException.TypeEnum;

public abstract class TronMessage extends Message {

Expand All @@ -32,4 +36,24 @@ public TronMessage(byte[] rawData) {
public TronMessage(byte type, byte[] rawData) {
super(type, rawData);
}

/**
* Throws BAD_MESSAGE with {@code reason} if {@code data} has more than {@code maxCount}
* top-level fields with {@code fieldTag}. The fields are counted without being decoded.
*/
protected static void checkRepeatedCount(byte[] data, int fieldTag, int maxCount,
String reason) throws IOException, P2pException {
CodedInputStream input = CodedInputStream.newInstance(data);
int count = 0;
int tag;
while ((tag = input.readTag()) != 0) {
if (tag == fieldTag && ++count > maxCount) {
throw new P2pException(TypeEnum.BAD_MESSAGE, reason);
}
if (!input.skipField(tag)) {
break;
}
}
input.checkLastTagWas(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.tron.core.Constant.MIN_TRANSACTION_SIZE;
import static org.tron.core.config.Parameter.ChainConstant.BLOCK_SIZE;

import com.google.protobuf.CodedInputStream;
import com.google.protobuf.WireFormat;
import java.io.IOException;
import org.tron.common.overlay.message.Message;
Expand Down Expand Up @@ -45,20 +44,8 @@ private void precheck(byte[] data) throws IOException, P2pException {
if (data.length > MAX_BLOCK_SIZE) {
throw new P2pException(TypeEnum.BAD_MESSAGE, "block size over limit");
}
CodedInputStream input = CodedInputStream.newInstance(data);
int transactionCount = 0;
int tag;
while ((tag = input.readTag()) != 0) {
if (tag == TRANSACTIONS_TAG && ++transactionCount > MAX_TRANSACTION_COUNT) {
throw new P2pException(TypeEnum.BAD_MESSAGE, "block transaction count over limit");
}
// Skip each field without constructing transactions, bytes or unknown-field objects.
if (!input.skipField(tag)) {
break;
}
}
// A top-level block must end at EOF, not at an unmatched END_GROUP tag.
input.checkLastTagWas(0);
checkRepeatedCount(data, TRANSACTIONS_TAG, MAX_TRANSACTION_COUNT,
"block transaction count over limit");
}

public BlockMessage(BlockCapsule block) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package org.tron.core.net.message.adv;

import static org.tron.core.config.Parameter.NetConstants.MAX_BLOCK_FETCH_PER_PEER;
import static org.tron.core.config.Parameter.NetConstants.MAX_SPREAD_SIZE;
import static org.tron.core.config.Parameter.NetConstants.MAX_TRX_FETCH_PER_PEER;

import com.google.protobuf.WireFormat;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.tron.common.math.StrictMathWrapper;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TronMessage;
Expand All @@ -15,10 +21,20 @@

public class InventoryMessage extends TronMessage {

// An inventory carries at most about MAX_SPREAD_SIZE ids and a fetch at most
// MAX_TRX_FETCH_PER_PEER or MAX_BLOCK_FETCH_PER_PEER. The margin covers broadcasts that pass
// AdvService's unlocked queue-size check at the same time and each add one item.
private static final int SENDER_LIMIT_MARGIN = 10;
public static final int MAX_IDS_COUNT = SENDER_LIMIT_MARGIN * StrictMathWrapper.max(
MAX_SPREAD_SIZE, StrictMathWrapper.max(MAX_TRX_FETCH_PER_PEER, MAX_BLOCK_FETCH_PER_PEER));
private static final int IDS_TAG = (Inventory.IDS_FIELD_NUMBER << 3)
| WireFormat.WIRETYPE_LENGTH_DELIMITED;

protected Inventory inv;

public InventoryMessage(byte[] data) throws Exception {
super(data);
checkRepeatedCount(data, IDS_TAG, MAX_IDS_COUNT, "inventory ids count over limit");
this.type = MessageTypes.INVENTORY.asByte();
this.inv = ProtoUtil.parseFrom(Protocol.Inventory.parser(), data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import static org.tron.core.config.Parameter.NetConstants.MAX_TRX_FETCH_PER_PEER;

import com.google.protobuf.CodedInputStream;
import com.google.protobuf.WireFormat;
import java.io.IOException;
import java.util.List;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.exception.P2pException;
import org.tron.core.exception.P2pException.TypeEnum;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TronMessage;
import org.tron.protos.Protocol;
Expand Down Expand Up @@ -41,20 +39,9 @@ public TransactionsMessage(byte[] data) throws Exception {
}

private void precheck(byte[] data) throws IOException, P2pException {
CodedInputStream input = CodedInputStream.newInstance(data);
int transactionCount = 0;
int tag;
while ((tag = input.readTag()) != 0) {
// Our TRX fetch requests contain at most this many entries; replies may split the batch.
if (tag == TRANSACTIONS_TAG && ++transactionCount > MAX_TRX_FETCH_PER_PEER) {
throw new P2pException(TypeEnum.BAD_MESSAGE, "transactions count over limit");
}
// Count only top-level entries without parsing transaction contents.
if (!input.skipField(tag)) {
break;
}
}
input.checkLastTagWas(0);
// Our TRX fetch requests contain at most this many entries; replies may split the batch.
checkRepeatedCount(data, TRANSACTIONS_TAG, MAX_TRX_FETCH_PER_PEER,
"transactions count over limit");
}

public Protocol.Transactions getTransactions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.tron.core.net.message.sync;

import com.google.protobuf.WireFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.tron.core.capsule.BlockCapsule.BlockId;
import org.tron.core.config.Parameter.NetConstants;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TronMessage;
import org.tron.p2p.utils.ProtoUtil;
Expand All @@ -12,10 +14,18 @@

public class BlockInventoryMessage extends TronMessage {

// Peers send this message as SYNC_BLOCK_CHAIN, whose handler accepts at most
// MAX_SYNC_CHAIN_IDS block ids. A chain summary halves the remaining range at each step, so
// its size grows with the logarithm of the chain length.
public static final int MAX_IDS_COUNT = NetConstants.MAX_SYNC_CHAIN_IDS;
private static final int IDS_TAG = (BlockInventory.IDS_FIELD_NUMBER << 3)
| WireFormat.WIRETYPE_LENGTH_DELIMITED;

protected BlockInventory blockInventory;

public BlockInventoryMessage(byte[] data) throws Exception {
super(data);
checkRepeatedCount(data, IDS_TAG, MAX_IDS_COUNT, "block inventory ids count over limit");
this.type = MessageTypes.BLOCK_INVENTORY.asByte();
this.blockInventory = ProtoUtil.parseFrom(Protocol.BlockInventory.parser(), data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.tron.core.net.message.sync;

import com.google.protobuf.WireFormat;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.tron.core.capsule.BlockCapsule.BlockId;
import org.tron.core.config.Parameter.NetConstants;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TronMessage;
import org.tron.p2p.utils.ProtoUtil;
Expand All @@ -14,10 +16,17 @@

public class ChainInventoryMessage extends TronMessage {

// A reply lists the common block and at most SYNC_FETCH_BATCH_NUM blocks after it, and
// ChainInventoryMsgHandler rejects longer lists.
public static final int MAX_IDS_COUNT = (int) NetConstants.SYNC_FETCH_BATCH_NUM + 1;
private static final int IDS_TAG = (ChainInventory.IDS_FIELD_NUMBER << 3)
| WireFormat.WIRETYPE_LENGTH_DELIMITED;

protected ChainInventory chainInventory;

public ChainInventoryMessage(byte[] data) throws Exception {
super(data);
checkRepeatedCount(data, IDS_TAG, MAX_IDS_COUNT, "chain inventory ids count over limit");
this.type = MessageTypes.BLOCK_CHAIN_INVENTORY.asByte();
chainInventory = ProtoUtil.parseFrom(Protocol.ChainInventory.parser(), data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void check(PeerConnection peer, ChainInventoryMessage msg) throws P2pExc
"remainNum is negative: " + msg.getRemainNum());
}

if (blockIds.size() > NetConstants.SYNC_FETCH_BATCH_NUM + 1) {
if (blockIds.size() > ChainInventoryMessage.MAX_IDS_COUNT) {
throw new P2pException(TypeEnum.BAD_MESSAGE, "big blockIds size: " + blockIds.size());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tron.core.net.service.adv;

import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
import static org.tron.core.config.Parameter.NetConstants.MAX_SPREAD_SIZE;
import static org.tron.core.config.Parameter.NetConstants.MAX_TRX_FETCH_PER_PEER;
import static org.tron.core.config.Parameter.NetConstants.MSG_CACHE_DURATION_IN_BLOCKS;

Expand Down Expand Up @@ -47,7 +48,6 @@ public class AdvService {
private final int MAX_INV_TO_FETCH_CACHE_SIZE = 100_000;
private final int MAX_TRX_CACHE_SIZE = 50_000;
private final int MAX_BLOCK_CACHE_SIZE = 10;
private final int MAX_SPREAD_SIZE = 1_000;
private final long TIMEOUT = MSG_CACHE_DURATION_IN_BLOCKS * BLOCK_PRODUCED_INTERVAL;

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,34 @@ public void testCheckInvRateLimitBlockBoundary() throws Exception {
peer.getPeerStatistics().messageStatistics.tronInBlockInventoryElement.getCount(10));
}

@Test
public void testInventoryOverIdLimitDisconnects() throws Exception {
CommonParameter parameter = CommonParameter.getInstance();
parameter.setMaxTps(10);
parameter.setMaxBlockInvPerSecond(10);

PeerStatistics peerStatistics = new PeerStatistics();
PeerConnection peer = mock(PeerConnection.class);
Mockito.when(peer.getPeerStatistics()).thenReturn(peerStatistics);

P2pEventHandlerImpl handler = new P2pEventHandlerImpl();
Method method = handler.getClass()
.getDeclaredMethod("processMessage", PeerConnection.class, byte[].class);
method.setAccessible(true);

List<Sha256Hash> list = new ArrayList<>();
for (int i = 0; i <= InventoryMessage.MAX_IDS_COUNT; i++) {
list.add(new Sha256Hash(i, new byte[32]));
}
InventoryMessage msg = new InventoryMessage(list, InventoryType.TRX);
method.invoke(handler, peer, msg.getSendBytes());

// The id count is checked before decoding: BAD_MESSAGE -> disconnect(BAD_PROTOCOL).
verify(peer).disconnect(Protocol.ReasonCode.BAD_PROTOCOL);
Assert.assertEquals(0,
peerStatistics.messageStatistics.tronInTrxInventoryElement.getTotalCount());
}

@Test
public void testCheckInvRateLimitUnknownTypeRejected() throws Exception {
CommonParameter parameter = CommonParameter.getInstance();
Expand Down
Loading
Loading