Fix test data and improve /proc/net/tcp header parsing

The header checking code was unsafe because:

1. It was accessing the byteslice at [2] without ensuring a length >= 3
2. It was assuming that the indentation of the 'sl' header is always 2 (which seems to be the case in recent kernels 8f18e4d03e/net/ipv4/tcp_ipv4.c (L2304) and 8f18e4d03e/net/ipv6/tcp_ipv6.c (L1831) ) but it's more robust to simply trim the byteslice.
This commit is contained in:
Alfonso Acosta
2017-01-04 00:12:47 +00:00
parent 99a7dc3b9a
commit 89a0ab6799
2 changed files with 10 additions and 7 deletions

View File

@@ -5,6 +5,9 @@ import (
"net"
)
// Used to check whether we are parsing a header line
var slHeader = []byte("sl")
// ProcNet is an iterator to parse /proc/net/tcp{,6} files.
type ProcNet struct {
b []byte
@@ -31,16 +34,16 @@ again:
}
b := p.b
if p.b[2] == 's' {
var (
sl, local, remote, inode []byte
)
sl, b = nextField(b) // 'sl' column
if bytes.Equal(sl, slHeader) {
// Skip header
p.b = nextLine(b)
goto again
}
var (
local, remote, inode []byte
)
_, b = nextField(b) // 'sl' column
local, b = nextField(b)
remote, b = nextField(b)
_, b = nextField(b) // 'st' column

View File

@@ -59,7 +59,7 @@ func TestProcNet(t *testing.T) {
func TestTransport6(t *testing.T) {
// Abridged copy of my /proc/net/tcp6
testString := ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
testString := ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
0: 00000000000000000000000000000000:19C8 00000000000000000000000000000000:0000 01 00000000:00000000 00:00000000 00000000 0 0 23661201 1 ffff880103fb4800 100 0 0 10 -1
8: 4500032000BE692B8AE31EBD919D9D10:D61C 5014002A080805400000000015100000:01BB 01 00000000:00000000 02:00000045 00000000 1000 0 36856710 2 ffff88010b796080 22 4 30 8 7
`