Skip to content

Commit ea3ce38

Browse files
authored
fix(net): discard unknown fields from transient messages (#7048)
* fix(net): discard unknown fields from transient messages Transient P2P and backup control messages retained attacker-supplied protobuf unknown fields after parsing, increasing their in-memory footprint without affecting message semantics. Parse non-persistent control messages with DiscardUnknownFieldsParser while preserving the exact serialized bytes of persisted and consensus-critical messages. * test(net): strengthen unknown-field discard coverage Assert that parsed control-message protobufs contain no unknown fields and equal clean messages, so the checks detect ordinary parseFrom calls. Cover block inventory, sync chain, and both disconnect byte parsers without exposing additional production getters. * fix(net): discard retained unknown fields from hello payloads Replace the inbound Hello data buffer with the cleaned protobuf bytes in both parsing constructors. Prevent connections from retaining the original unknown-field padding and ensure outgoing bytes use the same cleaned payload. * refactor(net): remove unused message constructors Remove explicit-type constructors with no production callers. Keep message types fixed in the byte-array constructors and preserve Hello payload normalization. * refactor(net): reuse protobuf parsing utility for control messages Centralize unknown-field discarding through ProtoUtil.parseFrom at the six transient-message parsing sites. Preserve HELLO payload normalization after parsing. * fix(p2p): discard retained unknown fields from hello payloads Rebuild the incoming HELLO payload from the parsed protobuf so channels no longer retain unknown-field padding in the original bytes. Preserve the known handshake fields and serialize the cleaned payload when sending.
1 parent 47b5529 commit ea3ce38

11 files changed

Lines changed: 211 additions & 21 deletions

File tree

‎framework/src/main/java/org/tron/common/backup/message/KeepAliveMessage.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.tron.common.backup.message.UdpMessageTypeEnum.BACKUP_KEEP_ALIVE;
44

55
import org.tron.p2p.discover.Node;
6+
import org.tron.p2p.utils.ProtoUtil;
67
import org.tron.protos.Discover;
78

89
public class KeepAliveMessage extends Message {
@@ -11,7 +12,7 @@ public class KeepAliveMessage extends Message {
1112

1213
public KeepAliveMessage(byte[] data) throws Exception {
1314
super(BACKUP_KEEP_ALIVE, data);
14-
backupMessage = Discover.BackupMessage.parseFrom(data);
15+
backupMessage = ProtoUtil.parseFrom(Discover.BackupMessage.parser(), data);
1516
}
1617

1718
public KeepAliveMessage(boolean flag, int priority) {

‎framework/src/main/java/org/tron/core/net/message/adv/InventoryMessage.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.tron.common.utils.Sha256Hash;
88
import org.tron.core.net.message.MessageTypes;
99
import org.tron.core.net.message.TronMessage;
10+
import org.tron.p2p.utils.ProtoUtil;
1011
import org.tron.protos.Protocol;
1112
import org.tron.protos.Protocol.Inventory;
1213
import org.tron.protos.Protocol.Inventory.InventoryType;
@@ -19,7 +20,7 @@ public class InventoryMessage extends TronMessage {
1920
public InventoryMessage(byte[] data) throws Exception {
2021
super(data);
2122
this.type = MessageTypes.INVENTORY.asByte();
22-
this.inv = Protocol.Inventory.parseFrom(data);
23+
this.inv = ProtoUtil.parseFrom(Protocol.Inventory.parser(), data);
2324
}
2425

2526
public InventoryMessage(Inventory inv) {

‎framework/src/main/java/org/tron/core/net/message/base/DisconnectMessage.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
import org.tron.core.net.message.MessageTypes;
44
import org.tron.core.net.message.TronMessage;
5+
import org.tron.p2p.utils.ProtoUtil;
56
import org.tron.protos.Protocol;
67
import org.tron.protos.Protocol.ReasonCode;
78

89
public class DisconnectMessage extends TronMessage {
910

1011
private Protocol.DisconnectMessage disconnectMessage;
1112

12-
public DisconnectMessage(byte type, byte[] rawData) throws Exception {
13-
super(type, rawData);
14-
this.disconnectMessage = Protocol.DisconnectMessage.parseFrom(this.data);
15-
}
16-
1713
public DisconnectMessage(byte[] data) throws Exception {
1814
super(MessageTypes.P2P_DISCONNECT.asByte(), data);
19-
this.disconnectMessage = Protocol.DisconnectMessage.parseFrom(data);
15+
this.disconnectMessage = ProtoUtil.parseFrom(Protocol.DisconnectMessage.parser(), data);
2016
}
2117

2218
public DisconnectMessage(ReasonCode reasonCode) {
@@ -46,4 +42,4 @@ public String toString() {
4642
public Class<?> getAnswerMessage() {
4743
return null;
4844
}
49-
}
45+
}

‎framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tron.core.net.message.TronMessage;
1515
import org.tron.p2p.discover.Node;
1616
import org.tron.p2p.utils.NetUtil;
17+
import org.tron.p2p.utils.ProtoUtil;
1718
import org.tron.program.Version;
1819
import org.tron.protos.Discover.Endpoint;
1920
import org.tron.protos.Protocol;
@@ -24,14 +25,10 @@ public class HelloMessage extends TronMessage {
2425
@Getter
2526
private Protocol.HelloMessage helloMessage;
2627

27-
public HelloMessage(byte type, byte[] rawData) throws Exception {
28-
super(type, rawData);
29-
this.helloMessage = Protocol.HelloMessage.parseFrom(rawData);
30-
}
31-
3228
public HelloMessage(byte[] data) throws Exception {
3329
super(MessageTypes.P2P_HELLO.asByte(), data);
34-
this.helloMessage = Protocol.HelloMessage.parseFrom(data);
30+
this.helloMessage = ProtoUtil.parseFrom(Protocol.HelloMessage.parser(), data);
31+
this.data = this.helloMessage.toByteArray();
3532
if (!valid()) {
3633
throw new P2pException(P2pException.TypeEnum.BAD_MESSAGE, "invalid hello message");
3734
}

‎framework/src/main/java/org/tron/core/net/message/sync/BlockInventoryMessage.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.tron.core.capsule.BlockCapsule.BlockId;
77
import org.tron.core.net.message.MessageTypes;
88
import org.tron.core.net.message.TronMessage;
9+
import org.tron.p2p.utils.ProtoUtil;
910
import org.tron.protos.Protocol;
1011
import org.tron.protos.Protocol.BlockInventory;
1112

@@ -16,7 +17,7 @@ public class BlockInventoryMessage extends TronMessage {
1617
public BlockInventoryMessage(byte[] data) throws Exception {
1718
super(data);
1819
this.type = MessageTypes.BLOCK_INVENTORY.asByte();
19-
this.blockInventory = Protocol.BlockInventory.parseFrom(data);
20+
this.blockInventory = ProtoUtil.parseFrom(Protocol.BlockInventory.parser(), data);
2021
}
2122

2223
public BlockInventoryMessage(List<BlockId> blockIds, BlockInventory.Type type) {

‎framework/src/main/java/org/tron/core/net/message/sync/ChainInventoryMessage.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.tron.core.capsule.BlockCapsule.BlockId;
99
import org.tron.core.net.message.MessageTypes;
1010
import org.tron.core.net.message.TronMessage;
11+
import org.tron.p2p.utils.ProtoUtil;
1112
import org.tron.protos.Protocol;
1213
import org.tron.protos.Protocol.ChainInventory;
1314

@@ -18,7 +19,7 @@ public class ChainInventoryMessage extends TronMessage {
1819
public ChainInventoryMessage(byte[] data) throws Exception {
1920
super(data);
2021
this.type = MessageTypes.BLOCK_CHAIN_INVENTORY.asByte();
21-
chainInventory = Protocol.ChainInventory.parseFrom(data);
22+
chainInventory = ProtoUtil.parseFrom(Protocol.ChainInventory.parser(), data);
2223
}
2324

2425
public ChainInventoryMessage(List<BlockId> blockIds, Long remainNum) {

‎framework/src/test/java/org/tron/common/backup/KeepAliveMessageTest.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,41 @@
22

33
import static org.tron.common.backup.message.UdpMessageTypeEnum.BACKUP_KEEP_ALIVE;
44

5+
import com.google.protobuf.ByteString;
6+
import com.google.protobuf.UnknownFieldSet;
57
import org.junit.Assert;
68
import org.junit.Test;
9+
import org.springframework.test.util.ReflectionTestUtils;
710
import org.tron.common.backup.message.KeepAliveMessage;
811
import org.tron.protos.Discover;
912

1013
public class KeepAliveMessageTest {
1114

15+
// A field number not used by BackupMessage, so it always parses as an unknown field.
16+
private static final int UNKNOWN_FIELD_NUMBER = 1000;
17+
18+
@Test
19+
public void discardsUnknownFields() throws Exception {
20+
Discover.BackupMessage backupMessage = Discover.BackupMessage.newBuilder()
21+
.setFlag(true).setPriority(10).build();
22+
UnknownFieldSet unknown = UnknownFieldSet.newBuilder()
23+
.addField(UNKNOWN_FIELD_NUMBER, UnknownFieldSet.Field.newBuilder()
24+
.addLengthDelimited(ByteString.copyFrom(new byte[8192]))
25+
.build())
26+
.build();
27+
byte[] padded = backupMessage.toBuilder().setUnknownFields(unknown).build().toByteArray();
28+
Assert.assertTrue(padded.length > backupMessage.toByteArray().length + 8000);
29+
30+
KeepAliveMessage m = new KeepAliveMessage(padded);
31+
Discover.BackupMessage parsed =
32+
(Discover.BackupMessage) ReflectionTestUtils.getField(m, "backupMessage");
33+
Assert.assertNotNull(parsed);
34+
Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty());
35+
Assert.assertEquals(backupMessage, parsed);
36+
Assert.assertTrue(m.getFlag());
37+
Assert.assertEquals(10, m.getPriority());
38+
}
39+
1240
@Test
1341
public void test() throws Exception {
1442
KeepAliveMessage m1 = new KeepAliveMessage(true, 10);

‎framework/src/test/java/org/tron/core/net/MessageTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public class MessageTest {
2222
public void test1() {
2323
DisconnectMessageTest disconnectMessageTest = new DisconnectMessageTest();
2424
try {
25-
disconnectMessage = new DisconnectMessage(MessageTypes.P2P_DISCONNECT.asByte(),
26-
disconnectMessageTest.toByteArray());
25+
disconnectMessage = new DisconnectMessage(disconnectMessageTest.toByteArray());
2726
} catch (Exception e) {
2827
Assert.assertTrue(e instanceof P2pException);
2928
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.tron.core.net;
2+
3+
import com.google.protobuf.ByteString;
4+
import com.google.protobuf.Message;
5+
import com.google.protobuf.UnknownFieldSet;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.springframework.test.util.ReflectionTestUtils;
9+
import org.tron.core.net.message.MessageTypes;
10+
import org.tron.core.net.message.adv.FetchInvDataMessage;
11+
import org.tron.core.net.message.adv.InventoryMessage;
12+
import org.tron.core.net.message.base.DisconnectMessage;
13+
import org.tron.core.net.message.handshake.HelloMessage;
14+
import org.tron.core.net.message.sync.BlockInventoryMessage;
15+
import org.tron.core.net.message.sync.ChainInventoryMessage;
16+
import org.tron.core.net.message.sync.SyncBlockChainMessage;
17+
import org.tron.protos.Protocol;
18+
import org.tron.protos.Protocol.Inventory.InventoryType;
19+
import org.tron.protos.Protocol.ReasonCode;
20+
21+
/**
22+
* Verifies that transient control messages (not persisted to the database) drop attacker-appended
23+
* unknown fields at parse time via {@code DiscardUnknownFieldsParser}, so padding cannot be
24+
* retained in memory. trx/trxs/block/pbft messages are intentionally excluded because they are
25+
* stored and rely on their exact serialized bytes for hashing/signature verification.
26+
*/
27+
public class MessageUnknownFieldTest {
28+
29+
// A field number not used by any message under test, so it always parses as an unknown field.
30+
private static final int UNKNOWN_FIELD_NUMBER = 1000;
31+
private static final int PADDING_SIZE = 8192;
32+
33+
private static byte[] withUnknown(Message msg) {
34+
UnknownFieldSet unknown = UnknownFieldSet.newBuilder()
35+
.addField(UNKNOWN_FIELD_NUMBER, UnknownFieldSet.Field.newBuilder()
36+
.addLengthDelimited(ByteString.copyFrom(new byte[PADDING_SIZE]))
37+
.build())
38+
.build();
39+
byte[] padded = msg.toBuilder().setUnknownFields(unknown).build().toByteArray();
40+
// Sanity: the padded bytes really are larger than the clean message.
41+
Assert.assertTrue(padded.length > msg.toByteArray().length + PADDING_SIZE - 16);
42+
return padded;
43+
}
44+
45+
@Test
46+
public void inventoryDiscardsUnknownFields() throws Exception {
47+
Protocol.Inventory inv = Protocol.Inventory.newBuilder()
48+
.setType(InventoryType.TRX)
49+
.addIds(ByteString.copyFrom(new byte[32]))
50+
.build();
51+
InventoryMessage msg = new InventoryMessage(withUnknown(inv));
52+
Assert.assertTrue(msg.getInventory().getUnknownFields().asMap().isEmpty());
53+
Assert.assertEquals(inv, msg.getInventory());
54+
Assert.assertEquals(1, msg.getHashList().size());
55+
}
56+
57+
@Test
58+
public void fetchInvDataDiscardsUnknownFields() throws Exception {
59+
Protocol.Inventory inv = Protocol.Inventory.newBuilder()
60+
.setType(InventoryType.BLOCK)
61+
.addIds(ByteString.copyFrom(new byte[32]))
62+
.build();
63+
FetchInvDataMessage msg = new FetchInvDataMessage(withUnknown(inv));
64+
Assert.assertTrue(msg.getInventory().getUnknownFields().asMap().isEmpty());
65+
Assert.assertEquals(inv, msg.getInventory());
66+
}
67+
68+
@Test
69+
public void helloMessageDiscardsUnknownFields() throws Exception {
70+
Protocol.HelloMessage.BlockId blockId = Protocol.HelloMessage.BlockId.newBuilder()
71+
.setHash(ByteString.copyFrom(new byte[32]))
72+
.build();
73+
Protocol.HelloMessage hello = Protocol.HelloMessage.newBuilder()
74+
.setVersion(1)
75+
.setTimestamp(123456789L)
76+
.setGenesisBlockId(blockId)
77+
.setSolidBlockId(blockId)
78+
.setHeadBlockId(blockId)
79+
.build();
80+
HelloMessage msg = new HelloMessage(withUnknown(hello));
81+
Assert.assertTrue(msg.getHelloMessage().getUnknownFields().asMap().isEmpty());
82+
Assert.assertEquals(1, msg.getVersion());
83+
Assert.assertEquals(123456789L, msg.getTimestamp());
84+
assertCleanHelloBytes(hello, msg);
85+
}
86+
87+
private static void assertCleanHelloBytes(Protocol.HelloMessage hello, HelloMessage msg) {
88+
Assert.assertEquals(hello, msg.getHelloMessage());
89+
byte[] clean = hello.toByteArray();
90+
Assert.assertArrayEquals(clean, msg.getData());
91+
byte[] expectedSendBytes = new byte[clean.length + 1];
92+
expectedSendBytes[0] = MessageTypes.P2P_HELLO.asByte();
93+
System.arraycopy(clean, 0, expectedSendBytes, 1, clean.length);
94+
Assert.assertArrayEquals(expectedSendBytes, msg.getSendBytes());
95+
}
96+
97+
@Test
98+
public void chainInventoryDiscardsUnknownFields() throws Exception {
99+
Protocol.ChainInventory chainInv = Protocol.ChainInventory.newBuilder()
100+
.addIds(Protocol.ChainInventory.BlockId.newBuilder()
101+
.setHash(ByteString.copyFrom(new byte[32])).setNumber(1).build())
102+
.setRemainNum(0)
103+
.build();
104+
ChainInventoryMessage msg = new ChainInventoryMessage(withUnknown(chainInv));
105+
Protocol.ChainInventory parsed =
106+
(Protocol.ChainInventory) ReflectionTestUtils.getField(msg, "chainInventory");
107+
Assert.assertNotNull(parsed);
108+
Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty());
109+
Assert.assertEquals(chainInv, parsed);
110+
Assert.assertEquals(1, msg.getBlockIds().size());
111+
Assert.assertEquals(Long.valueOf(0L), msg.getRemainNum());
112+
}
113+
114+
@Test
115+
public void disconnectMessageDiscardsUnknownFields() throws Exception {
116+
Protocol.DisconnectMessage disconnect = Protocol.DisconnectMessage.newBuilder()
117+
.setReason(ReasonCode.TOO_MANY_PEERS)
118+
.build();
119+
DisconnectMessage msg = new DisconnectMessage(withUnknown(disconnect));
120+
Protocol.DisconnectMessage parsed =
121+
(Protocol.DisconnectMessage) ReflectionTestUtils.getField(msg, "disconnectMessage");
122+
Assert.assertNotNull(parsed);
123+
Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty());
124+
Assert.assertEquals(disconnect, parsed);
125+
Assert.assertEquals(ReasonCode.TOO_MANY_PEERS, msg.getReason());
126+
}
127+
128+
@Test
129+
public void blockInventoryDiscardsUnknownFields() throws Exception {
130+
Protocol.BlockInventory inventory = Protocol.BlockInventory.newBuilder()
131+
.addIds(Protocol.BlockInventory.BlockId.newBuilder()
132+
.setHash(ByteString.copyFrom(new byte[32])).setNumber(1).build())
133+
.setType(Protocol.BlockInventory.Type.ADVTISE)
134+
.build();
135+
BlockInventoryMessage msg = new BlockInventoryMessage(withUnknown(inventory));
136+
Protocol.BlockInventory parsed =
137+
(Protocol.BlockInventory) ReflectionTestUtils.getField(msg, "blockInventory");
138+
Assert.assertNotNull(parsed);
139+
Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty());
140+
Assert.assertEquals(inventory, parsed);
141+
Assert.assertEquals(1, msg.getBlockIds().size());
142+
}
143+
144+
@Test
145+
public void syncBlockChainDiscardsUnknownFields() throws Exception {
146+
Protocol.BlockInventory inventory = Protocol.BlockInventory.newBuilder()
147+
.addIds(Protocol.BlockInventory.BlockId.newBuilder()
148+
.setHash(ByteString.copyFrom(new byte[32])).setNumber(1).build())
149+
.setType(Protocol.BlockInventory.Type.SYNC)
150+
.build();
151+
SyncBlockChainMessage msg = new SyncBlockChainMessage(withUnknown(inventory));
152+
Protocol.BlockInventory parsed =
153+
(Protocol.BlockInventory) ReflectionTestUtils.getField(msg, "blockInventory");
154+
Assert.assertNotNull(parsed);
155+
Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty());
156+
Assert.assertEquals(inventory, parsed);
157+
Assert.assertEquals(1, msg.getBlockIds().size());
158+
}
159+
}

‎p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class HelloMessage extends Message {
1717
public HelloMessage(byte[] data) throws Exception {
1818
super(MessageType.HANDSHAKE_HELLO, data);
1919
this.helloMessage = ProtoUtil.parseFrom(Connect.HelloMessage.parser(), data);
20+
this.data = this.helloMessage.toByteArray();
2021
}
2122

2223
public HelloMessage(DisconnectCode code, long time) {

0 commit comments

Comments
 (0)