Skip to content

Commit 05c76ee

Browse files
neildgopherbot
authored andcommitted
ipv4, ipv6: avoid slice bounds panics on empty batch or BPF filter
Return 0, nil from ReadBatch and WriteBatch when ms is empty on non-Linux platforms (matching the Linux behavior), and return an error from SetBPF when the filter slice is empty or exceeds MaxUint16 instructions. Change-Id: I8863524004aad5a6bc5f967f02e529866a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/852829 Auto-Submit: Damien Neil <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent dad306f commit 05c76ee

8 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎ipv4/batch.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
7575
if !c.ok() {
7676
return 0, errInvalidConn
7777
}
78+
if len(ms) == 0 {
79+
return 0, nil
80+
}
7881
switch runtime.GOOS {
7982
case "linux":
8083
n, err := c.RecvMsgs([]socket.Message(ms), flags)
@@ -109,6 +112,9 @@ func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
109112
if !c.ok() {
110113
return 0, errInvalidConn
111114
}
115+
if len(ms) == 0 {
116+
return 0, nil
117+
}
112118
switch runtime.GOOS {
113119
case "linux":
114120
n, err := c.SendMsgs([]socket.Message(ms), flags)
@@ -141,6 +147,9 @@ func (c *packetHandler) ReadBatch(ms []Message, flags int) (int, error) {
141147
if !c.ok() {
142148
return 0, errInvalidConn
143149
}
150+
if len(ms) == 0 {
151+
return 0, nil
152+
}
144153
switch runtime.GOOS {
145154
case "linux":
146155
n, err := c.RecvMsgs([]socket.Message(ms), flags)
@@ -175,6 +184,9 @@ func (c *packetHandler) WriteBatch(ms []Message, flags int) (int, error) {
175184
if !c.ok() {
176185
return 0, errInvalidConn
177186
}
187+
if len(ms) == 0 {
188+
return 0, nil
189+
}
178190
switch runtime.GOOS {
179191
case "linux":
180192
n, err := c.SendMsgs([]socket.Message(ms), flags)

‎ipv4/bpf_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func TestBPF(t *testing.T) {
2727

2828
p := ipv4.NewPacketConn(l)
2929

30+
if err := p.SetBPF(nil); err == nil {
31+
t.Fatal("got nil; want non-nil error")
32+
}
33+
3034
// This filter accepts UDP packets whose first payload byte is
3135
// even.
3236
prog, err := bpf.Assemble([]bpf.Instruction{

‎ipv4/readwrite_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
351351
}
352352
defer c.Close()
353353
p := ipv4.NewPacketConn(c)
354+
if n, err := p.ReadBatch(nil, 0); n != 0 || err != nil {
355+
t.Fatalf("got %d, %v; want 0, nil", n, err)
356+
}
357+
if n, err := p.WriteBatch(nil, 0); n != 0 || err != nil {
358+
t.Fatalf("got %d, %v; want 0, nil", n, err)
359+
}
354360
t.Run("ToFrom", func(t *testing.T) {
355361
testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), false)
356362
})

‎ipv4/sys_bpf.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package ipv4
88

99
import (
10+
"errors"
11+
"math"
1012
"unsafe"
1113

1214
"golang.org/x/net/bpf"
@@ -15,6 +17,9 @@ import (
1517
)
1618

1719
func (so *sockOpt) setAttachFilter(c *socket.Conn, f []bpf.RawInstruction) error {
20+
if len(f) == 0 || len(f) > math.MaxUint16 {
21+
return errors.New("invalid filter")
22+
}
1823
prog := unix.SockFprog{
1924
Len: uint16(len(f)),
2025
Filter: (*unix.SockFilter)(unsafe.Pointer(&f[0])),

‎ipv6/batch.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
6666
if !c.ok() {
6767
return 0, errInvalidConn
6868
}
69+
if len(ms) == 0 {
70+
return 0, nil
71+
}
6972
switch runtime.GOOS {
7073
case "linux":
7174
n, err := c.RecvMsgs([]socket.Message(ms), flags)
@@ -97,6 +100,9 @@ func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
97100
if !c.ok() {
98101
return 0, errInvalidConn
99102
}
103+
if len(ms) == 0 {
104+
return 0, nil
105+
}
100106
switch runtime.GOOS {
101107
case "linux":
102108
n, err := c.SendMsgs([]socket.Message(ms), flags)

‎ipv6/bpf_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func TestBPF(t *testing.T) {
3131

3232
p := ipv6.NewPacketConn(l)
3333

34+
if err := p.SetBPF(nil); err == nil {
35+
t.Fatal("got nil; want non-nil error")
36+
}
37+
3438
// This filter accepts UDP packets whose first payload byte is
3539
// even.
3640
prog, err := bpf.Assemble([]bpf.Instruction{

‎ipv6/readwrite_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
349349
}
350350
defer c.Close()
351351
p := ipv6.NewPacketConn(c)
352+
if n, err := p.ReadBatch(nil, 0); n != 0 || err != nil {
353+
t.Fatalf("got %d, %v; want 0, nil", n, err)
354+
}
355+
if n, err := p.WriteBatch(nil, 0); n != 0 || err != nil {
356+
t.Fatalf("got %d, %v; want 0, nil", n, err)
357+
}
352358
t.Run("ToFrom", func(t *testing.T) {
353359
testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), false)
354360
})

‎ipv6/sys_bpf.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package ipv6
88

99
import (
10+
"errors"
11+
"math"
1012
"unsafe"
1113

1214
"golang.org/x/net/bpf"
@@ -15,6 +17,9 @@ import (
1517
)
1618

1719
func (so *sockOpt) setAttachFilter(c *socket.Conn, f []bpf.RawInstruction) error {
20+
if len(f) == 0 || len(f) > math.MaxUint16 {
21+
return errors.New("invalid filter")
22+
}
1823
prog := unix.SockFprog{
1924
Len: uint16(len(f)),
2025
Filter: (*unix.SockFilter)(unsafe.Pointer(&f[0])),

0 commit comments

Comments
 (0)