lib: replace stream_get_ipv4() with STREAM_GET() in link state parsing#22275
lib: replace stream_get_ipv4() with STREAM_GET() in link state parsing#22275guoguojia2021 wants to merge 1 commit into
Conversation
The stream_get_ipv4() function calls assert(0) when the stream does not
have enough data remaining, which causes the entire process to abort.
This is problematic for link state parsing functions (ls_parse_node and
ls_parse_attributes) because a malformed or truncated message from an
external source should not crash the daemon.
Replace all six stream_get_ipv4() call sites with the STREAM_GET() macro,
which uses longjmp to jump to the stream_failure label on underflow.
Both ls_parse_node() and ls_parse_attributes() already have proper
stream_failure cleanup handlers that log an error, free allocated memory,
and return NULL, so this change leverages the existing error recovery
paths.
The six replaced call sites are:
- ls_parse_node(): node->router_id
- ls_parse_attributes(): attr->standard.local, attr->standard.remote,
attr->standard.remote_addr, and two adj_sid neighbor.addr fields
(ADJ_PRI_IPV4 and ADJ_BCK_IPV4)
This is consistent with how IPv6 addresses are already read using
STREAM_GET() in the same functions, and follows the safe stream reading
pattern used throughout the rest of the link state parsing code.
Signed-off-by: guozhongfeng <guozhongfeng.gzf@alibaba-inc.com>
Greptile SummaryThis PR replaces six
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Receive link-state message] --> B[ls_parse_node / ls_parse_attributes]
B --> C{stream underflow?}
subgraph OLD["Before PR (stream_get_ipv4)"]
C1{underflow?} --> |yes| D1[STREAM_BOUND_WARN\nassert-0 → process abort]
C1 --> |no| E1[return parsed struct]
end
subgraph NEW["After PR (STREAM_GET)"]
C2{underflow?} --> |yes| D2[goto stream_failure\nlog error + free memory]
C2 --> |no| E2[return parsed struct]
D2 --> F2[return NULL]
end
C --> C1
C --> C2
style D1 fill:#ff6b6b,color:#fff
style D2 fill:#51cf66,color:#fff
style F2 fill:#51cf66,color:#fff
|
|
Yeah straight up NAK. If we are not properly sending data or not properly checking lengths of data then fix those not this. |
Thanks for the review. I understand your point — fixing the root cause is better than papering over it at the receiver.
|
|
Tick the box to add this pull request to the merge queue (same as
|
The stream_get_ipv4() function calls assert(0) when the stream does not have enough data remaining, which causes the entire process to abort. This is problematic for link state parsing functions (ls_parse_node and ls_parse_attributes) because a malformed or truncated message from an external source should not crash the daemon.
Replace all six stream_get_ipv4() call sites with the STREAM_GET() macro, which uses longjmp to jump to the stream_failure label on underflow. Both ls_parse_node() and ls_parse_attributes() already have proper stream_failure cleanup handlers that log an error, free allocated memory, and return NULL, so this change leverages the existing error recovery paths.
The six replaced call sites are:
This is consistent with how IPv6 addresses are already read using STREAM_GET() in the same functions, and follows the safe stream reading pattern used throughout the rest of the link state parsing code.