Skip to content

Commit

Permalink
Merge pull request #384 from tri-adam/overflow-fix
Browse files Browse the repository at this point in the history
fix: correct the range check for descriptor IDs
  • Loading branch information
tri-adam authored Aug 23, 2024
2 parents fd8a090 + 6f00aba commit 1ed3ce5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/sif/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (f *FileImage) writeDataObject(i int, di DescriptorInput, t time.Time) erro
}

// We derive the ID from i, so make sure the ID will not overflow.
if i >= math.MaxInt32 {
if int64(i) >= math.MaxUint32 {
return errObjectIDOverflow
}

Expand Down Expand Up @@ -233,8 +233,16 @@ func OptCreateWithCloseOnUnload(b bool) CreateOpt {
}
}

var errDescriptorCapacityNotSupported = errors.New("descriptor capacity not supported")

// createContainer creates a new SIF container file in rw, according to opts.
func createContainer(rw ReadWriter, co createOpts) (*FileImage, error) {
// The supported number of descriptors is limited by the unsigned 32-bit ID field in each
// rawDescriptor.
if co.descriptorCapacity >= math.MaxUint32 {
return nil, errDescriptorCapacityNotSupported
}

rds := make([]rawDescriptor, co.descriptorCapacity)
rdsSize := int64(binary.Size(rds))

Expand Down
7 changes: 7 additions & 0 deletions pkg/sif/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ func TestCreateContainerAtPath(t *testing.T) {
opts []CreateOpt
wantErr error
}{
{
name: "ErrDescriptorCapacityNotSupported",
opts: []CreateOpt{
OptCreateWithDescriptorCapacity(math.MaxUint32),
},
wantErr: errDescriptorCapacityNotSupported,
},
{
name: "ErrInsufficientCapacity",
opts: []CreateOpt{
Expand Down

0 comments on commit 1ed3ce5

Please sign in to comment.