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 3 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
16 changes: 16 additions & 0 deletions auparse/sockaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
package auparse

import (
"errors"
"strconv"
)

// errInvalidSockAddrSize means socket address size is invalid.
var errInvalidSockAddrSize = errors.New("invalid socket address size")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the errors returned included the family and the len of the provided address.

type invalidSockAddrErr struct {
    family string
    size   int
}

func (e invalidSockAddrErr) Error() string {
    return fmt.Sprintf("invalid socket address size family=%s: %d, e.family, e.size)
}

populated appropriately below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What family to specify in case of len(sockAddr) < 4? Or it would be better to introduce another error type for malformed families? Like so:

type invalidSockFamilyError struct {
    family string
}

func (e invalidSockFamilyError) Error() string {
    return fmt.Sprintf("invalid socket address family=%s", e.family)
}

// ...

if len(s) < 4 {
    return nil, invalidSockFamilyError{
        family: s,
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest either calling the family "invalid", or conditionally format an error in the Error method when the size is less than 4 and don't use the family field in that case.

type invalidSockAddrErr struct {
    family string
    size   int
}

func (e invalidSockAddrErr) Error() string {
    if e.size < 4 {
        return fmt.Sprintf("invalid family: too short: %d", e.size)
    }
    return fmt.Sprintf("invalid socket address size family=%s: %d, e.family, e.size)
}

and in the preamble of the func,

func parseSockaddr(s string) (map[string]string, error) {
	if len(s) < 4 {
		return nil, invalidSockAddrErr{size: len(s)}
	}
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: b1c5994


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 +46,10 @@ func parseSockaddr(s string) (map[string]string, error) {
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 +64,10 @@ func parseSockaddr(s string) (map[string]string, error) {
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