Skip to content

Commit

Permalink
fix seg fault on raced bytes array (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet authored Nov 18, 2024
1 parent dc487d4 commit 6f56537
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.23.0
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sync v0.6.0
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.18.0
golang.org/x/text v0.14.0 // indirect
Expand Down
15 changes: 0 additions & 15 deletions utils/sigs/sigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,6 @@ func EncodeUint64(val uint64) []byte {
return encodedVal
}

// Join() is faster than bytes.Join because it does what
// bytes.Join() does without appending (empty) separators
func Join(s [][]byte) []byte {
n := 0
for _, v := range s {
n += len(v)
}

b, i := make([]byte, n), 0
for _, v := range s {
i += copy(b[i:], v)
}
return b
}

func GetKeyName(clientCtx client.Context) (string, error) {
_, name, _, err := client.GetFromFields(clientCtx, clientCtx.Keyring, clientCtx.From)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion x/conflict/types/relay_finalization.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"encoding/json"
fmt "fmt"

Expand Down Expand Up @@ -55,7 +56,7 @@ func (rf RelayFinalization) DataToSign() []byte {
sdkAccAddress,
relaySessionHash,
}
return sigs.Join(msgParts)
return bytes.Join(msgParts, nil)
}

func (rfm RelayFinalization) HashRounds() int {
Expand Down
6 changes: 3 additions & 3 deletions x/pairing/types/relay_exchange.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"strings"

"github.com/lavanet/lava/v4/utils"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (re RelayExchange) DataToSign() []byte {
metadataBytes,
}

return sigs.Join(msgParts)
return bytes.Join(msgParts, nil)
}

func (re RelayExchange) HashRounds() int {
Expand All @@ -65,6 +66,5 @@ func (rp RelayPrivateData) GetContentHashData() []byte {
seenBlockBytes,
rp.Salt,
}
msgData := sigs.Join(msgParts)
return msgData
return bytes.Join(msgParts, nil)
}
113 changes: 113 additions & 0 deletions x/pairing/types/relay_exchange_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGetContentHashData_NilInputs(t *testing.T) {
// Test case with all nil/empty values
rp := RelayPrivateData{
Metadata: nil,
Extensions: nil,
Addon: "",
ApiInterface: "",
ConnectionType: "",
ApiUrl: "",
Data: nil,
Salt: nil,
RequestBlock: 0,
SeenBlock: 0,
}

// Function should not panic and return a valid byte slice
result := rp.GetContentHashData()
require.NotNil(t, result)

// Test with nil Extensions but non-nil empty slice
rp.Extensions = []string{}
result = rp.GetContentHashData()
require.NotNil(t, result)

// Test with nil Metadata but non-nil empty slice
rp.Metadata = []Metadata{}
result = rp.GetContentHashData()
require.NotNil(t, result)
}

func TestGetContentHashData_NilData(t *testing.T) {
// Test case with nil Data field specifically
rp := RelayPrivateData{
Metadata: []Metadata{},
Extensions: []string{},
Addon: "",
ApiInterface: "",
ConnectionType: "",
ApiUrl: "",
Data: nil, // Explicitly nil
Salt: make([]byte, 8),
RequestBlock: 0,
SeenBlock: 0,
}

// Function should not panic
result := rp.GetContentHashData()
require.NotNil(t, result)
}

func TestGetContentHashData_EdgeCases(t *testing.T) {
testCases := []struct {
name string
rp RelayPrivateData
}{
{
name: "nil metadata with data",
rp: RelayPrivateData{
Metadata: nil,
Data: []byte("test"),
},
},
{
name: "nil extensions with data",
rp: RelayPrivateData{
Extensions: nil,
Data: []byte("test"),
},
},
{
name: "nil everything",
rp: RelayPrivateData{
Metadata: nil,
Extensions: nil,
Addon: "",
ApiInterface: "",
ConnectionType: "",
ApiUrl: "",
Data: nil,
Salt: nil,
},
},
{
name: "empty strings with nil slices",
rp: RelayPrivateData{
Metadata: []Metadata{},
Extensions: []string{},
Addon: "",
ApiInterface: "",
ConnectionType: "",
ApiUrl: "",
Data: nil,
Salt: nil,
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Function should not panic
result := tc.rp.GetContentHashData()
require.NotNil(t, result)
})
}
}
4 changes: 3 additions & 1 deletion x/pairing/types/relay_session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"bytes"

"github.com/lavanet/lava/v4/utils/sigs"
)

Expand Down Expand Up @@ -28,5 +30,5 @@ func (rs RelaySession) CalculateHashForFinalization() []byte {
blockHeightBytes,
relayNumBytes,
}
return sigs.Join(msgParts)
return bytes.Join(msgParts, nil)
}

0 comments on commit 6f56537

Please sign in to comment.