fix: enhance token server request handling and add max frame length validation to prevent memory issues - #3572
fix: enhance token server request handling and add max frame length validation to prevent memory issues#3572yungyu16 wants to merge 1 commit into
Conversation
|
这个问题,有可能被恶意利用,影响微服务进程可用性。 |
c7758bf to
8a14d79
Compare
|
我刚刚提交也是这样的,和你一样的提示。 |
|
Thanks for working on this. Validating network-provided lengths before allocating memory is the correct direction. However, the current implementation still needs the following changes:
The Nmap service probe happens to match the current Sentinel protocol layout. A protocol magic header could prevent this kind of accidental protocol misidentification, but that should be treated as a separate protocol-evolution topic and does not replace validation of length and count fields. The PR history also contains a merge commit used to synchronize with the upstream branch. Please rebase/squash it before merging and link the corresponding issue as required by the project contribution guidelines. |
|
@LearningGp This PR addresses malformed frame length/count validation in the cluster token server, includes regression tests, links issue #3636, and is based on the latest |
|
Thanks for the update. The current commit correctly addresses the original memory-allocation issue: Ping now compares the declared length with the actual remaining bytes before allocating the byte array, so the Nmap frame no longer causes a 16 MiB allocation. The ParamFlow validation for parameter counts, string lengths, negative values, and trailing bytes has also been completed. However, complete outer-frame handling is still incomplete.
For the current Nmap frame, the first length check closes the connection, so the original large allocation is prevented. However, 21 bytes remain unread when the exception is thrown. When closing the connection triggers I verified these behaviors with
Please add At minimum, the tests should cover a request header shorter than 5 bytes, an unknown request type, a Ping with the length field completely missing, a truncated Flow request, a Flow request with trailing bytes, the current Nmap frame, and a malformed frame followed by a valid Ping. The current tests mainly invoke leaf decoders directly or manually fire These issues do not change the conclusion that the original 16 MiB allocation problem has been fixed, but the current implementation does not yet fully satisfy the requirement that malformed frames leave no residual data that can affect subsequent requests. |



中文版本
请描述这个PR的作用以及为什么需要它
本PR增强了token服务器的请求处理能力,并增加了最大帧长度验证,以防止潜在的安全风险和性能问题。
当公司安全团队使用nmap进行端口扫描时,可能会向token服务器发送畸形数据包。
这些数据包可能包含异常大的长度字段,导致服务器创建极大的字节数组,从而引发过多的内存消耗和Full GC问题。
现象
在maxFrameLength最大为1024时,解码ping报文时,会创建16M的临时数组,带来内存压力。
复现
com.alibaba.csp.sentinel.demo.cluster.ClusterServerDemobrew install nmapnmap -oX - 127.0.0.1 -p 11111 -T4 -sT -sV -Pn -n --host-timeout 300000ms --max-retries 1 --min-parallelism 16 --max-scan-delay 5scom.alibaba.csp.sentinel.cluster.server.codec.data.PingRequestDataDecoder.decode断点,可以看到解码出超大的length原理
根据namp端口特征 规则库可以看到,DNSVersionBindReqTCP 类型的探测报文会被token server误解码为ping包。
https://raw.githubusercontent.com/nmap/nmap/refs/heads/master/nmap-service-probes
这个PR是否修复了某个问题?
Fixes #3636
修复畸形报文中的长度或数量字段导致过度内存分配、越界读取,以及残留字节影响后续解码的问题。
请描述您是如何解决的
exceptionCaught并关闭连接。请描述如何验证这个PR
新增并执行 43 个相关测试,覆盖:
特别说明(给评审人员)
此修复解决了畸形数据包可能导致过度内存分配的潜在安全和性能问题。该解决方案引入了适当的数据包大小验证和限制,以防止拒绝服务场景的发生。
English Version
Describe what this PR does / why we need it
This PR enhances the token server's request handling and adds max frame length validation to prevent potential security risks and performance issues. When the company's security team performs port scanning using nmap, malformed packets may be sent to the token server. These packets may contain abnormally large length fields, which could cause the server to create extremely large byte arrays, leading to excessive memory consumption and Full GC issues.
Phenomenon
When maxFrameLength is set to a maximum of 1024, decoding ping packets creates a 16M temporary array, causing memory pressure.
Reproduction Steps
com.alibaba.csp.sentinel.demo.cluster.ClusterServerDemobrew install nmapnmap -oX - 127.0.0.1 -p 11111 -T4 -sT -sV -Pn -n --host-timeout 300000ms --max-retries 1 --min-parallelism 16 --max-scan-delay 5scom.alibaba.csp.sentinel.cluster.server.codec.data.PingRequestDataDecoder.decodeto see the decoded oversizedlengthPrinciple
According to the nmap port characteristic rule base, DNSVersionBindReqTCP type probe packets are misdecoded by the token server as ping packets.
https://raw.githubusercontent.com/nmap/nmap/refs/heads/master/nmap-service-probes
Does this pull request fix one issue?
Fixes #3636
This fixes excessive allocation, out-of-bounds reads, and residual bytes affecting later decoding when malformed frames contain invalid length or count fields.
Describe how you did it
exceptionCaught, which closes the connection.Describe how to verify it
Added and executed 43 related tests covering:
Special notes for reviews
This fix addresses a potential security and performance issue where malformed packets could cause excessive memory allocation. The solution introduces proper validation and limits on packet sizes to prevent denial-of-service scenarios.