-
Notifications
You must be signed in to change notification settings - Fork 0
🛡️ Sentinel: [HIGH] Buffer Overflow in Network Encoding due to missing bounds check #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ const GOSSIP_ENTRY_SIZE = 32 + 1 + 8 + 1 + 4 + 2 + 1 + 32 + 1 + 4 + 2 + 1; // 89 | |
|
|
||
| /// Encode a Ping message into a buffer. Returns bytes written. | ||
| pub fn encodePing(buf: []u8, ping: messages.Ping) !usize { | ||
| const required = 1 + 32 + 8 + 1 + ping.gossip.len * GOSSIP_ENTRY_SIZE; | ||
| if (buf.len < required) return error.BufferTooShort; | ||
|
|
||
| var pos: usize = 0; | ||
|
|
||
| // Type tag | ||
|
|
@@ -45,6 +48,9 @@ pub fn encodePing(buf: []u8, ping: messages.Ping) !usize { | |
|
|
||
| /// Encode an Ack message into a buffer. Returns bytes written. | ||
| pub fn encodeAck(buf: []u8, ack: messages.Ack) !usize { | ||
| const required = 1 + 32 + 8 + 1 + ack.gossip.len * GOSSIP_ENTRY_SIZE; | ||
|
||
| if (buf.len < required) return error.BufferTooShort; | ||
|
|
||
| var pos: usize = 0; | ||
|
|
||
| buf[pos] = @intFromEnum(messages.MessageType.ack); | ||
|
|
@@ -69,6 +75,9 @@ pub fn encodeAck(buf: []u8, ack: messages.Ack) !usize { | |
|
|
||
| /// Encode a PingReq message. | ||
| pub fn encodePingReq(buf: []u8, req: messages.PingReq) !usize { | ||
| const required = 1 + 32 + 32 + 8; | ||
| if (buf.len < required) return error.BufferTooShort; | ||
|
|
||
| var pos: usize = 0; | ||
|
|
||
| buf[pos] = @intFromEnum(messages.MessageType.ping_req); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
requiredsize calculation forencodePingusesping.gossip.len * GOSSIP_ENTRY_SIZE, but the actual encoding loop at line 38 only writes@min(ping.gossip.len, 255)entries (gossip_count). Ifping.gossip.len > 255, the bounds check will compute arequiredvalue that is much larger than what will actually be written, causing a spuriouserror.BufferTooShorteven when the buffer is sufficient to hold all 255 gossip entries (up to ~22,755 bytes). Inswim.zig, this error is silently swallowed withcatch return, meaning pings would be silently dropped whenping.gossip.len > 255.The
requiredcalculation should use@min(ping.gossip.len, 255) * GOSSIP_ENTRY_SIZEto correctly reflect how many bytes will actually be written.