Skip to content

Commit

Permalink
Add a check to ensure that indexes into DirNames are in range. (#51)
Browse files Browse the repository at this point in the history
The check above ensures that the lengths of these arrays are constrained, but
does not ensure that the indexes themselves are within bounds. This leads to
a panic when consuming malformed data.

A similar check can be found at:
https://github.com/rpm-software-management/rpm/blob/master/lib/tagexts.c#L99.
  • Loading branch information
geremy-condra committed Dec 6, 2023
1 parent 4c52bf7 commit a8af76a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,11 @@ func (p *PackageInfo) InstalledFileNames() ([]string, error) {

var filePaths []string
for i, baseName := range p.BaseNames {
dir := p.DirNames[p.DirIndexes[i]]
idx := p.DirIndexes[i]
if len(p.DirNames) <= int(idx) {
return nil, xerrors.Errorf("invalid rpm %s", p.Name)
}
dir := p.DirNames[idx]
filePaths = append(filePaths, path.Join(dir, baseName)) // should be slash-separated
}
return filePaths, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/rpmdb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpmdb

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -813,3 +814,13 @@ func TestRpmDB_Package(t *testing.T) {
})
}
}

func TestNevra(t *testing.T) {
blob, err := os.ReadFile("testdata/blob.bin")
indexEntries, err := headerImport(blob)
require.NoError(t, err)
pkg, err := getNEVRA(indexEntries)
require.NoError(t, err)
_, err = pkg.InstalledFiles()
require.Error(t, err)
}
Binary file added pkg/testdata/blob.bin
Binary file not shown.

0 comments on commit a8af76a

Please sign in to comment.