Skip to content
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

Check socket address size #152

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Fix panic in `parseSockaddr` for malformed socket address. [#152](https://github.com/elastic/go-libaudit/pull/152)

### Removed

### Deprecated
Expand Down
18 changes: 18 additions & 0 deletions auparse/sockaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
package auparse

import (
"errors"
"strconv"
)

var (

Check failure on line 25 in auparse/sockaddr.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
// errInvalidSockAddrSize means socket address size is invalid.
errInvalidSockAddrSize = errors.New("invalid socket address size")
)

func parseSockaddr(s string) (map[string]string, error) {
if len(s) < 4 {
return nil, errInvalidSockAddrSize
}

addressFamily, err := hexToDec(s[2:4] + s[0:2]) // host-order
if err != nil {
return nil, err
Expand All @@ -38,6 +48,10 @@
out["family"] = "unix"
out["path"] = socket
case 2: // AF_INET
if len(s) < 16 {
return nil, errInvalidSockAddrSize
}

port, err := hexToDec(s[4:8])
if err != nil {
return nil, err
Expand All @@ -52,6 +66,10 @@
out["addr"] = ip
out["port"] = strconv.Itoa(int(port))
case 10: // AF_INET6
if len(s) < 48 {
return nil, errInvalidSockAddrSize
}

port, err := hexToDec(s[4:8])
if err != nil {
return nil, err
Expand Down
17 changes: 17 additions & 0 deletions auparse/sockaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,20 @@ func TestParseSockaddr(t *testing.T) {
assert.Equal(t, tc.data, data)
}
}

func FuzzParseSockaddr(f *testing.F) {
corpus := []string{
"02000050080808080000000000000000",
"0A000050000000002607F8B0400C0C06000000000000006700000000",
"01007075626C69632F7069636B75700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"0A00084300000000000000000000000000000000000000000000000000000000281E7423FD7F0000C05034088F7F000007000000000000001E2D440000000000000000000000000060D758078F7F00000300000000000000C00F020000000000000000000000000005202302000000000200000000000000FFFFFFFFFFFFFFFF",
}

for _, seed := range corpus {
f.Add(seed)
}

f.Fuzz(func(t *testing.T, saddr string) {
_, _ = parseSockaddr(saddr) // Fuzz for panics
})
}
Loading