Skip to content

Commit 92041b7

Browse files
authored
internal/driver: guard BuildID slice in locateBinaries against short values (#998)
The LLVM debug-file lookup sliced m.BuildID[:2] and m.BuildID[2:] to construct a filesystem path of the form <path>/<first2>/<rest>.debug. The existing guard only checked m.BuildID != "", so a BuildID with fewer than two characters (e.g. a single byte) caused a panic: runtime error: slice bounds out of range [:2] with length 1 The profile.proto format imposes no minimum length on BuildID, and the profile.CheckValid() function does not validate it either. A crafted profile with a one-character BuildID therefore reliably crashes any process that calls locateBinaries, including tools or servers that accept and analyze user-supplied profiles. Fix: wrap the LLVM path construction in a len(m.BuildID) >= 2 guard, matching the documented precondition of the LLVM build-id protocol ('the first two characters are used as directory'). Add a test case with BuildID="X" to TestSymbolizationPath to prevent regression.
1 parent 545e8a4 commit 92041b7

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

internal/driver/fetch.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ mapping:
430430
// Llvm buildid protocol: the first two characters of the build id
431431
// are used as directory, and the remaining part is in the filename.
432432
// e.g. `/ab/cdef0123456.debug`
433-
fileNames = append(fileNames, filepath.Join(path, m.BuildID[:2], m.BuildID[2:]+".debug"))
433+
if len(m.BuildID) >= 2 {
434+
fileNames = append(fileNames, filepath.Join(path, m.BuildID[:2], m.BuildID[2:]+".debug"))
435+
}
434436
}
435437
if m.File != "" {
436438
// Try both the basename and the full path, to support the same directory

internal/driver/fetch_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func TestSymbolizationPath(t *testing.T) {
7777
{"", "", "fghij10001", filepath.Join(tempdir, "pprof/binaries/fg/hij10001.debug"), 0},
7878
{"/nowhere:/alternate/architecture", "/usr/bin/binary", "fedcb10000", "/usr/bin/binary", 1},
7979
{"/nowhere:/alternate/architecture", "/usr/bin/binary", "abcde10002", "/usr/bin/binary", 1},
80+
// A single-character BuildID must not panic when the LLVM debug-file
81+
// lookup slices BuildID[:2] / BuildID[2:]. Prior to the fix, this caused
82+
// a "slice bounds out of range" panic.
83+
{"", "/usr/bin/binary", "X", "/usr/bin/binary", 0},
8084
} {
8185
os.Setenv("PPROF_BINARY_PATH", tc.env)
8286
p := &profile.Profile{

0 commit comments

Comments
 (0)