-
Notifications
You must be signed in to change notification settings - Fork 977
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
firewall reject packets: cleanup error cases
We found a few types of packets that could cause a panic here, live if a non-TCP packet has TCP header options set. These change should shore up any edge cases and ensure we reserve the maximum amount of buffer space we need to create the reject packet.
- Loading branch information
Showing
4 changed files
with
138 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package iputil | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/net/ipv4" | ||
) | ||
|
||
func Test_CreateRejectPacket(t *testing.T) { | ||
h := ipv4.Header{ | ||
Len: 20, | ||
Src: net.IPv4(10, 0, 0, 1), | ||
Dst: net.IPv4(10, 0, 0, 2), | ||
Protocol: 1, // ICMP | ||
} | ||
|
||
b, err := h.Marshal() | ||
if err != nil { | ||
t.Fatalf("h.Marhshal: %v", err) | ||
} | ||
b = append(b, []byte{0, 3, 0, 4}...) | ||
|
||
expectedLen := ipv4.HeaderLen + 8 + h.Len + 4 | ||
out := make([]byte, expectedLen) | ||
rejectPacket := CreateRejectPacket(b, out) | ||
assert.NotNil(t, rejectPacket) | ||
assert.Len(t, rejectPacket, expectedLen) | ||
|
||
// ICMP with max header len | ||
h = ipv4.Header{ | ||
Len: 60, | ||
Src: net.IPv4(10, 0, 0, 1), | ||
Dst: net.IPv4(10, 0, 0, 2), | ||
Protocol: 1, // ICMP | ||
Options: make([]byte, 40), | ||
} | ||
|
||
b, err = h.Marshal() | ||
if err != nil { | ||
t.Fatalf("h.Marhshal: %v", err) | ||
} | ||
b = append(b, []byte{0, 3, 0, 4, 0, 0, 0, 0}...) | ||
|
||
expectedLen = MaxRejectPacketSize | ||
out = make([]byte, MaxRejectPacketSize) | ||
rejectPacket = CreateRejectPacket(b, out) | ||
assert.NotNil(t, rejectPacket) | ||
assert.Len(t, rejectPacket, expectedLen) | ||
|
||
// TCP with max header len | ||
h = ipv4.Header{ | ||
Len: 60, | ||
Src: net.IPv4(10, 0, 0, 1), | ||
Dst: net.IPv4(10, 0, 0, 2), | ||
Protocol: 6, // TCP | ||
Options: make([]byte, 40), | ||
} | ||
|
||
b, err = h.Marshal() | ||
if err != nil { | ||
t.Fatalf("h.Marhshal: %v", err) | ||
} | ||
b = append(b, []byte{0, 3, 0, 4}...) | ||
b = append(b, make([]byte, 16)...) | ||
|
||
expectedLen = ipv4.HeaderLen + 20 | ||
out = make([]byte, expectedLen) | ||
rejectPacket = CreateRejectPacket(b, out) | ||
assert.NotNil(t, rejectPacket) | ||
assert.Len(t, rejectPacket, expectedLen) | ||
} |