Fix panic in raw socket fragmentation when payload buffer exceeds packet size #1077
+4
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a panic that occurs when fragmenting raw socket packets. The panic happens when the packet that is being sent is larger than the MTU, and is not exactly the same size as the fragmentation buffer, causing
copy_from_slice
to fail with mismatched slice lengths.Bug Details
When a raw socket packet requires fragmentation (packet size > MTU), the following sequence occurs:
InterfaceInner::dispatch_ip()
, the fragmentation path is entered whentotal_ip_len > self.caps.ip_mtu()
smoltcp/src/iface/interface/mod.rs
Lines 1212 to 1281 in a54589c
emit_ip(&ip_repr, &mut frag.buffer)
to emit the packet into the fragmentation buffersmoltcp/src/iface/interface/mod.rs
Line 1251 in a54589c
emit_ip
, after emitting the IP header, it passes the remaining fragmentation buffer toemit_payload
:smoltcp/src/iface/interface/mod.rs
Lines 1200 to 1205 in a54589c
emit_payload
attempts to copy the entire raw packet into the fragmentation buffer "payload":smoltcp/src/iface/packet.rs
Lines 132 to 133 in a54589c
The issue is that
raw_packet
can be much smaller thanpayload
, since the payload is the fragmentation buffer . For example:This mismatch causes
copy_from_slice
to panic with:Fix
The fix ensures we only write to the portion of the destination buffer that matches the source size:
This prevents the panic while maintaining the same behavior - the raw packet data is copied to the beginning of the payload buffer, and any remaining buffer space is left untouched.
Testing
This fix has been tested with raw socket packets that require fragmentation and no longer causes panics when the fragmentation buffer is larger than the packet payload.