Skip to content

Commit 2916d33

Browse files
authored
Merge pull request #5 from ZaparooProject/fix/iso9660-path-table-bounds
fix: prevent panic on malformed ISO9660 path table
2 parents 2b224dd + c5214f3 commit 2916d33

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

iso9660/iso9660.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,16 @@ func (iso *ISO9660) parsePathTable() error {
198198
iso.pathTable = nil
199199
i := 0
200200
for i < len(pathTableRaw) {
201-
if i >= len(pathTableRaw) {
202-
break
203-
}
204-
205201
dirNameLen := int(pathTableRaw[i])
206202
if dirNameLen == 0 {
207203
break
208204
}
209205

206+
// Validate we have enough data for this entry (8 byte header + name)
207+
if i+8+dirNameLen > len(pathTableRaw) {
208+
return fmt.Errorf("truncated path table entry at offset %d", i)
209+
}
210+
210211
// Extended attribute record length at i+1 (skip)
211212
dirLBA := binary.LittleEndian.Uint32(pathTableRaw[i+2 : i+6])
212213
dirParentIdx := int(binary.LittleEndian.Uint16(pathTableRaw[i+6:i+8])) - 1

iso9660/iso9660_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,40 @@ func TestGetDataPreparerID(t *testing.T) {
545545
// Data preparer ID is at a different offset - our minimal ISO doesn't set it
546546
_ = iso.GetDataPreparerID()
547547
}
548+
549+
// TestISO9660_TruncatedPathTable verifies bounds check for malformed path table entries.
550+
func TestISO9660_TruncatedPathTable(t *testing.T) {
551+
t.Parallel()
552+
553+
tmpDir := t.TempDir()
554+
555+
// Create a minimal ISO
556+
isoData := createMinimalISO("VOL", "SYS", "PUB")
557+
558+
// Corrupt the path table: set a directory name length that exceeds the path table size
559+
// Path table is at block 18 (offset 18 * 2048 = 36864)
560+
pathTableOffset := 18 * 2048
561+
562+
// Set directory name length to a large value (e.g., 100) that exceeds remaining data
563+
// The path table entry format is:
564+
// - byte 0: directory name length
565+
// - byte 1: extended attribute record length
566+
// - bytes 2-5: directory LBA (little-endian)
567+
// - bytes 6-7: parent directory number (little-endian)
568+
// - bytes 8+: directory name
569+
isoData[pathTableOffset] = 100 // Large name length that would overflow
570+
571+
isoPath := filepath.Join(tmpDir, "truncated.iso")
572+
if err := os.WriteFile(isoPath, isoData, 0o600); err != nil {
573+
t.Fatalf("Failed to write ISO: %v", err)
574+
}
575+
576+
// This should fail with a truncated path table error, not panic
577+
_, err := Open(isoPath)
578+
if err == nil {
579+
t.Error("Open() should error for truncated path table entry")
580+
}
581+
if !strings.Contains(err.Error(), "truncated path table entry") {
582+
t.Errorf("Open() error = %v, want error containing 'truncated path table entry'", err)
583+
}
584+
}

0 commit comments

Comments
 (0)