Skip to content

Commit

Permalink
Protect Hunks from invalid lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
Grayson Hansard committed Jan 30, 2020
1 parent a372299 commit 1e01fb2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/ipspatcher/Hunk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ extension Hunk {

let length = 5 + UInt16(slice[slice.startIndex + 3]) << 16 + UInt16(slice[slice.startIndex + 4])
let payloadLength = length > 0 ? length : 3;
let bytes = Array<UInt8>(slice[ slice.startIndex ..< slice.startIndex + Int(payloadLength) ])
let predictedPaylodOffset = slice.startIndex + Int(payloadLength)

guard predictedPaylodOffset < slice.endIndex else {
return nil
}

let bytes = Array<UInt8>(slice[ slice.startIndex ..< predictedPaylodOffset ])
return Hunk.from(bytes: bytes)
}
}
13 changes: 13 additions & 0 deletions Tests/ipspatcherTests/HunkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ final class HunkTests: XCTestCase {
XCTAssertEqual(0x0002, hunk.RLELength)
XCTAssertEqual(0xFF, hunk.RLEPayload)
}

func testHunkWithIncorrectLength() {
let bytes: [UInt8] = [
0x01, 0x02, 0x03, /* Offset */
0x00, 0x02, /* Length */
0xFF /* Payload */
]

bytes.withUnsafeBufferPointer {
let hunk = Hunk.from(slice: $0[ $0.startIndex ..< $0.endIndex ])
XCTAssertNil(hunk)
}
}
}

0 comments on commit 1e01fb2

Please sign in to comment.