Skip to content

Commit 947d35e

Browse files
committed
(fix): gosec high severity issues
Signed-off-by: Sachin Sampras M <[email protected]>
1 parent 3ef1052 commit 947d35e

File tree

9 files changed

+164
-14
lines changed

9 files changed

+164
-14
lines changed

pkg/api/api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ func NewAPI(treeID int64) (*API, error) {
150150
if !ok {
151151
return nil, fmt.Errorf("no root found for inactive shard %d", r.TreeID)
152152
}
153-
cp, err := util.CreateAndSignCheckpoint(ctx, viper.GetString("rekor_server.hostname"), r.TreeID, uint64(r.TreeLength), root.RootHash, r.Signer)
153+
treeLength, err := util.SafeInt64ToUint64(r.TreeLength)
154+
if err != nil {
155+
return nil, err
156+
}
157+
cp, err := util.CreateAndSignCheckpoint(ctx, viper.GetString("rekor_server.hostname"), r.TreeID, treeLength, root.RootHash, r.Signer)
154158
if err != nil {
155159
return nil, fmt.Errorf("error signing checkpoint for inactive shard %d: %w", r.TreeID, err)
156160
}

pkg/api/entries.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ func logEntryFromLeaf(ctx context.Context, leaf *trillian.LogLeaf, signedLogRoot
126126
sc = string(scBytes)
127127
}
128128

129+
treeSize, err := util.SafeUint64ToInt64(root.TreeSize)
130+
if err != nil {
131+
return nil, err
132+
}
133+
129134
inclusionProof := models.InclusionProof{
130-
TreeSize: conv.Pointer(int64(root.TreeSize)),
135+
TreeSize: conv.Pointer(treeSize),
131136
RootHash: conv.Pointer(hex.EncodeToString(root.RootHash)),
132137
LogIndex: conv.Pointer(proof.GetLeafIndex()),
133138
Hashes: hashes,
@@ -445,8 +450,13 @@ func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middl
445450
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, sthGenerateError)
446451
}
447452

453+
treeSize, err := util.SafeUint64ToInt64(root.TreeSize)
454+
if err != nil {
455+
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, validationError)
456+
}
457+
448458
inclusionProof := models.InclusionProof{
449-
TreeSize: conv.Pointer(int64(root.TreeSize)),
459+
TreeSize: conv.Pointer(treeSize),
450460
RootHash: conv.Pointer(hex.EncodeToString(root.RootHash)),
451461
LogIndex: conv.Pointer(queuedLeaf.LeafIndex),
452462
Hashes: hashes,

pkg/api/tlog.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ func GetLogInfoHandler(params tlog.GetLogInfoParams) middleware.Responder {
6565
}
6666

6767
hashString := hex.EncodeToString(root.RootHash)
68-
treeSize := int64(root.TreeSize)
68+
treeSize, err := util.SafeUint64ToInt64(root.TreeSize)
69+
if err != nil {
70+
return handleRekorAPIError(params, http.StatusInternalServerError, err, validationError)
71+
}
6972

7073
scBytes, err := util.CreateAndSignCheckpoint(ctx,
7174
viper.GetString("rekor_server.hostname"), api.logRanges.GetActive().TreeID, root.TreeSize, root.RootHash, api.logRanges.GetActive().Signer)
@@ -164,7 +167,10 @@ func inactiveShardLogInfo(ctx context.Context, tid int64, cachedCheckpoints map[
164167
}
165168

166169
hashString := hex.EncodeToString(root.RootHash)
167-
treeSize := int64(root.TreeSize)
170+
treeSize, err := util.SafeUint64ToInt64(root.TreeSize)
171+
if err != nil {
172+
return nil, err
173+
}
168174

169175
m := models.InactiveShardLogInfo{
170176
RootHash: &hashString,

pkg/sharding/ranges.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/sigstore/rekor/pkg/log"
3232
"github.com/sigstore/rekor/pkg/signer"
3333
"github.com/sigstore/rekor/pkg/trillianclient"
34+
"github.com/sigstore/rekor/pkg/util"
3435
"github.com/sigstore/sigstore/pkg/cryptoutils"
3536
"github.com/sigstore/sigstore/pkg/signature"
3637
"github.com/sigstore/sigstore/pkg/signature/options"
@@ -116,7 +117,11 @@ func (l *LogRanges) CompleteInitialization(ctx context.Context, tcm *trilliancli
116117
if err := root.UnmarshalBinary(resp.GetLatestResult.SignedLogRoot.LogRoot); err != nil {
117118
return nil, err
118119
}
119-
l.inactive[i].TreeLength = int64(root.TreeSize)
120+
treeSize, err := util.SafeUint64ToInt64(root.TreeSize)
121+
if err != nil {
122+
return nil, err
123+
}
124+
l.inactive[i].TreeLength = treeSize
120125
sthMap[r.TreeID] = root
121126
}
122127
return sthMap, nil

pkg/trillianclient/trillian_client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ func (t *TrillianClient) GetLeafAndProofByIndex(ctx context.Context, index int64
253253
})
254254

255255
if resp != nil && resp.Proof != nil {
256-
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, uint64(index), root.TreeSize, resp.GetLeaf().MerkleLeafHash, resp.Proof.Hashes, root.RootHash); err != nil {
256+
u_index, err := util.SafeInt64ToUint64(index)
257+
if err != nil {
258+
return &Response{
259+
Status: codes.OutOfRange,
260+
Err: status.Error(codes.OutOfRange, err.Error()),
261+
}
262+
263+
}
264+
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, u_index, root.TreeSize, resp.GetLeaf().MerkleLeafHash, resp.Proof.Hashes, root.RootHash); err != nil {
257265
return &Response{
258266
Status: status.Code(err),
259267
Err: err,

pkg/util/safe_convertors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ func SafeUint64ToInt64(u uint64) (int64, error) {
2626
}
2727
return int64(u), nil
2828
}
29+
30+
31+
func SafeInt64ToUint64(i int64) (uint64, error) {
32+
if i < 0 {
33+
return 0, fmt.Errorf("value %d is negative and cannot be converted to uint64", i)
34+
}
35+
return uint64(i), nil
36+
}

pkg/util/safe_convertors_test.go

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
package util
1616

1717
import (
18+
"fmt"
1819
"math"
1920
"testing"
2021
)
2122

2223
func TestSafeUint64ToInt64(t *testing.T) {
2324
tests := []struct {
24-
name string
25-
input uint64
26-
want int64
27-
wantErr bool
25+
name string
26+
input uint64
27+
want int64
28+
wantErr bool
2829
}{
2930
{
3031
name: "small positive number",
@@ -68,3 +69,62 @@ func TestSafeUint64ToInt64(t *testing.T) {
6869
})
6970
}
7071
}
72+
73+
func TestSafeInt64ToUint64(t *testing.T) {
74+
tests := []struct {
75+
name string
76+
input int64
77+
want uint64
78+
wantErr bool
79+
}{
80+
{
81+
name: "zero",
82+
input: 0,
83+
want: 0,
84+
wantErr: false,
85+
},
86+
{
87+
name: "positive value",
88+
input: 42,
89+
want: 42,
90+
wantErr: false,
91+
},
92+
{
93+
name: "max int64",
94+
input: math.MaxInt64,
95+
want: uint64(math.MaxInt64),
96+
wantErr: false,
97+
},
98+
{
99+
name: "negative value",
100+
input: -1,
101+
wantErr: true,
102+
},
103+
{
104+
name: "large negative",
105+
input: math.MinInt64,
106+
wantErr: true,
107+
},
108+
}
109+
110+
for _, tt := range tests {
111+
t.Run(tt.name, func(t *testing.T) {
112+
got, err := SafeInt64ToUint64(tt.input)
113+
114+
if (err != nil) != tt.wantErr {
115+
t.Fatalf("SafeInt64ToUint64(%d) error = %v, wantErr %t", tt.input, err, tt.wantErr)
116+
}
117+
118+
if !tt.wantErr && got != tt.want {
119+
t.Fatalf("SafeInt64ToUint64(%d) = %d, want %d", tt.input, got, tt.want)
120+
}
121+
122+
if tt.wantErr && err != nil {
123+
expectedMsg := fmt.Sprintf("value %d is negative and cannot be converted to uint64", tt.input)
124+
if err.Error() != expectedMsg {
125+
t.Fatalf("error message = %q, want %q", err.Error(), expectedMsg)
126+
}
127+
}
128+
})
129+
}
130+
}

pkg/verify/verify.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func ProveConsistency(ctx context.Context, rClient *client.Rekor,
6363
case oldTreeSize < newTreeSize:
6464
consistencyParams := tlog.NewGetLogProofParamsWithContext(ctx)
6565
consistencyParams.FirstSize = &oldTreeSize // Root size at the old, or trusted state.
66-
consistencyParams.LastSize = newTreeSize // Root size at the new state to verify against.
66+
consistencyParams.LastSize = newTreeSize // Root size at the new state to verify against.
6767
consistencyParams.TreeID = &treeID
6868
consistencyProof, err := rClient.Tlog.GetLogProof(consistencyParams)
6969
if err != nil {
@@ -174,8 +174,17 @@ func VerifyInclusion(ctx context.Context, e *models.LogEntryAnon) error {
174174
}
175175
leafHash := rfc6962.DefaultHasher.HashLeaf(entryBytes)
176176

177-
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, uint64(*e.Verification.InclusionProof.LogIndex),
178-
uint64(*e.Verification.InclusionProof.TreeSize), leafHash, hashes, rootHash); err != nil {
177+
logIndex, err := util.SafeInt64ToUint64(*e.Verification.InclusionProof.LogIndex)
178+
if err != nil {
179+
return err
180+
}
181+
treeSize, err := util.SafeInt64ToUint64(*e.Verification.InclusionProof.TreeSize)
182+
if err != nil {
183+
return err
184+
}
185+
186+
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, logIndex,
187+
treeSize, leafHash, hashes, rootHash); err != nil {
179188
return err
180189
}
181190

pkg/verify/verify_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,46 @@ func TestInclusion(t *testing.T) {
258258
},
259259
wantErr: true,
260260
},
261+
{
262+
name: "invalid inclusion - negative log index",
263+
e: models.LogEntryAnon{
264+
Body: "eyJhcGlWZXJzaW9uIjoiMC4wLjEiLCJraW5kIjoicmVrb3JkIiwic3BlYyI6eyJkYXRhIjp7Imhhc2giOnsiYWxnb3JpdGhtIjoic2hhMjU2IiwidmFsdWUiOiJlY2RjNTUzNmY3M2JkYWU4ODE2ZjBlYTQwNzI2ZWY1ZTliODEwZDkxNDQ5MzA3NTkwM2JiOTA2MjNkOTdiMWQ4In19LCJzaWduYXR1cmUiOnsiY29udGVudCI6Ik1FWUNJUUQvUGRQUW1LV0MxKzBCTkVkNWdLdlFHcjF4eGwzaWVVZmZ2M2prMXp6Skt3SWhBTEJqM3hmQXlXeGx6NGpwb0lFSVYxVWZLOXZua1VVT1NvZVp4QlpQSEtQQyIsImZvcm1hdCI6Ing1MDkiLCJwdWJsaWNLZXkiOnsiY29udGVudCI6IkxTMHRMUzFDUlVkSlRpQlFWVUpNU1VNZ1MwVlpMUzB0TFMwS1RVWnJkMFYzV1VoTGIxcEplbW93UTBGUldVbExiMXBKZW1vd1JFRlJZMFJSWjBGRlRVOWpWR1pTUWxNNWFtbFlUVGd4UmxvNFoyMHZNU3R2YldWTmR3cHRiaTh6TkRjdk5UVTJaeTlzY21sVE56SjFUV2haT1V4alZDczFWVW8yWmtkQ1oyeHlOVm80VERCS1RsTjFZWE41WldRNVQzUmhVblozUFQwS0xTMHRMUzFGVGtRZ1VGVkNURWxESUV0RldTMHRMUzB0Q2c9PSJ9fX19",
265+
IntegratedTime: &time,
266+
LogID: &logID,
267+
LogIndex: conv.Pointer(int64(-1)), // <- invalid
268+
Verification: &models.LogEntryAnonVerification{
269+
InclusionProof: &models.InclusionProof{
270+
TreeSize: conv.Pointer(int64(2)),
271+
RootHash: conv.Pointer("5be1758dd2228acfaf2546b4b6ce8aa40c82a3748f3dcb550e0d67ba34f02a45"),
272+
LogIndex: conv.Pointer(int64(-1)), // <- invalid
273+
Hashes: []string{
274+
"59a575f157274702c38de3ab1e1784226f391fb79500ebf9f02b4439fb77574c",
275+
},
276+
},
277+
},
278+
},
279+
wantErr: true,
280+
},
281+
{
282+
name: "invalid inclusion - negative tree size",
283+
e: models.LogEntryAnon{
284+
Body: "eyJhcGlWZXJzaW9uIjoiMC4wLjEiLCJraW5kIjoicmVrb3JkIiwic3BlYyI6eyJkYXRhIjp7Imhhc2giOnsiYWxnb3JpdGhtIjoic2hhMjU2IiwidmFsdWUiOiJlY2RjNTUzNmY3M2JkYWU4ODE2ZjBlYTQwNzI2ZWY1ZTliODEwZDkxNDQ5MzA3NTkwM2JiOTA2MjNkOTdiMWQ4In19LCJzaWduYXR1cmUiOnsiY29udGVudCI6Ik1FWUNJUUQvUGRQUW1LV0MxKzBCTkVkNWdLdlFHcjF4eGwzaWVVZmZ2M2prMXp6Skt3SWhBTEJqM3hmQXlXeGx6NGpwb0lFSVYxVWZLOXZua1VVT1NvZVp4QlpQSEtQQyIsImZvcm1hdCI6Ing1MDkiLCJwdWJsaWNLZXkiOnsiY29udGVudCI6IkxTMHRMUzFDUlVkSlRpQlFWVUpNU1VNZ1MwVlpMUzB0TFMwS1RVWnJkMFYzV1VoTGIxcEplbW93UTBGUldVbExiMXBKZW1vd1JFRlJZMFJSWjBGRlRVOWpWR1pTUWxNNWFtbFlUVGd4UmxvNFoyMHZNU3R2YldWTmR3cHRiaTh6TkRjdk5UVTJaeTlzY21sVE56SjFUV2haT1V4alZDczFWVW8yWmtkQ1oyeHlOVm80VERCS1RsTjFZWE41WldRNVQzUmhVblozUFQwS0xTMHRMUzFGVGtRZ1VGVkNURWxESUV0RldTMHRMUzB0Q2c9PSJ9fX19",
285+
IntegratedTime: &time,
286+
LogID: &logID,
287+
LogIndex: conv.Pointer(int64(1)),
288+
Verification: &models.LogEntryAnonVerification{
289+
InclusionProof: &models.InclusionProof{
290+
TreeSize: conv.Pointer(int64(-10)), // <- invalid
291+
RootHash: conv.Pointer("5be1758dd2228acfaf2546b4b6ce8aa40c82a3748f3dcb550e0d67ba34f02a45"),
292+
LogIndex: conv.Pointer(int64(1)),
293+
Hashes: []string{
294+
"59a575f157274702c38de3ab1e1784226f391fb79500ebf9f02b4439fb77574c",
295+
},
296+
},
297+
},
298+
},
299+
wantErr: true,
300+
},
261301
} {
262302
t.Run(string(test.name), func(t *testing.T) {
263303
ctx := context.Background()

0 commit comments

Comments
 (0)