Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Dynamic Stack Buffer Overflow #109

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions FlyingSocks/Sources/SocketAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,20 @@ public extension SocketAddress {
}

func makeStorage() -> sockaddr_storage {
var storage = sockaddr_storage()
var addr = self
return withUnsafePointer(to: &addr) {
$0.withMemoryRebound(to: sockaddr_storage.self, capacity: 1) {
$0.pointee
let addrSize = MemoryLayout<Self>.size
let storageSize = MemoryLayout<sockaddr_storage>.size

withUnsafePointer(to: &addr) { addrPtr in
let addrRawPtr = UnsafeRawPointer(addrPtr)
withUnsafeMutablePointer(to: &storage) { storagePtr in
let storageRawPtr = UnsafeMutableRawPointer(storagePtr)
let copySize = min(addrSize, storageSize)
storageRawPtr.copyMemory(from: addrRawPtr, byteCount: copySize)
}
}
return storage
}
}

Expand Down
117 changes: 117 additions & 0 deletions FlyingSocks/Tests/SocketAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,121 @@ final class SocketAddressTests: XCTestCase {
XCTAssertEqual($0, .unsupportedAddress)
}
}

func testSockaddrInToStorageConversion() throws {
Copy link
Owner

@swhitty swhitty Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the existing tests within SocketAddressTests already cover these same things?

  • testAddress_DecodesIP4()
  • testINET_ThrowsInvalidAddress_WhenFamilyIncorrect()
  • testAddress_DecodesIP6()
  • testINET6_ThrowsInvalidAddress_WhenFamilyIncorrect()
  • testUnix_IsCorrectlyDecodedFromStorage()
  • testUnix_ThrowsInvalidAddress_WhenFamilyIncorrect()

The existing makeStorage() is tested within the "decode" tests above. They are more of an encode/decode — they start with an address, call makeStorage() then convert back to the original address to assert the values are the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since they didn't run red, they should definitely be modified, if you think I'd better merge the tests. Since they run quite quickly, I thought it was no problem to validate the storage specific ones separately.

Tastes and slaps, I don't like overlapping test cases, however as our example shows, they can mask underlying-unit errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we go along with the current solution, I would like to note that I have worded the tests differently, if you like, I would be happy to reword them to the existing style (with underscores).

var addrIn = sockaddr_in()
addrIn.sin_family = sa_family_t(AF_INET)
addrIn.sin_port = UInt16(8080).bigEndian
addrIn.sin_addr = try Socket.makeInAddr(fromIP4: "192.168.1.1")

let storage = addrIn.makeStorage()

XCTAssertEqual(storage.ss_family, sa_family_t(AF_INET))
withUnsafeBytes(of: addrIn) { addrInPtr in
let storagePtr = addrInPtr.bindMemory(to: sockaddr_storage.self)
XCTAssertEqual(storagePtr.baseAddress!.pointee.ss_family, sa_family_t(AF_INET))
let sockaddrInPtr = addrInPtr.bindMemory(to: sockaddr_in.self)
XCTAssertEqual(sockaddrInPtr.baseAddress!.pointee.sin_port, addrIn.sin_port)
XCTAssertEqual(sockaddrInPtr.baseAddress!.pointee.sin_addr.s_addr, addrIn.sin_addr.s_addr)
}
}

func testSockaddrIn6ToStorageConversion() throws {
var addrIn6 = sockaddr_in6()
addrIn6.sin6_family = sa_family_t(AF_INET6)
addrIn6.sin6_port = UInt16(9090).bigEndian
addrIn6.sin6_addr = try Socket.makeInAddr(fromIP6: "fe80::1")

let storage = addrIn6.makeStorage()

XCTAssertEqual(storage.ss_family, sa_family_t(AF_INET6))
XCTAssertEqual(storage.ss_family, addrIn6.sin6_family)

withUnsafeBytes(of: addrIn6) { addrIn6Ptr in
let storagePtr = addrIn6Ptr.bindMemory(to: sockaddr_storage.self)
XCTAssertEqual(storagePtr.baseAddress!.pointee.ss_family, sa_family_t(AF_INET6))
let sockaddrIn6Ptr = addrIn6Ptr.bindMemory(to: sockaddr_in6.self)
XCTAssertEqual(sockaddrIn6Ptr.baseAddress!.pointee.sin6_port, addrIn6.sin6_port)
let addrArray = withUnsafeBytes(of: sockaddrIn6Ptr.baseAddress!.pointee.sin6_addr) {
Array($0.bindMemory(to: UInt8.self))
}
let expectedArray = withUnsafeBytes(of: addrIn6.sin6_addr) {
Array($0.bindMemory(to: UInt8.self))
}
XCTAssertEqual(addrArray, expectedArray)
}
}

func testSockaddrUnToStorageConversion() {
var addrUn = sockaddr_un()
addrUn.sun_family = sa_family_t(AF_UNIX)
let path = "/tmp/socket"
_ = path.withCString { pathPtr in
memcpy(&addrUn.sun_path, pathPtr, path.count + 1)
}

let storage = addrUn.makeStorage()

XCTAssertEqual(storage.ss_family, sa_family_t(AF_UNIX))
let addrUnBytes = withUnsafeBytes(of: addrUn) { Data($0) }
let storageBytes = withUnsafeBytes(of: storage) { Data($0) }
let storageSockaddrUnBytes = storageBytes[0..<MemoryLayout<sockaddr_un>.size]
XCTAssertEqual(addrUnBytes, storageSockaddrUnBytes)
}

func testInvalidAddressFamily() {
var storage = sockaddr_storage()
storage.ss_family = sa_family_t(AF_APPLETALK) // Invalid for our purpose

XCTAssertThrowsError(try sockaddr_in.make(from: storage)) { error in
XCTAssertEqual(error as? SocketError, SocketError.unsupportedAddress)
}

XCTAssertThrowsError(try sockaddr_in6.make(from: storage)) { error in
XCTAssertEqual(error as? SocketError, SocketError.unsupportedAddress)
}

XCTAssertThrowsError(try sockaddr_un.make(from: storage)) { error in
XCTAssertEqual(error as? SocketError, SocketError.unsupportedAddress)
}
}

func testMaximumPathLengthForUnixDomainSocket() {
var addrUn = sockaddr_un()
addrUn.sun_family = sa_family_t(AF_UNIX)
let maxPathLength = MemoryLayout<sockaddr_un>.size - MemoryLayout<sa_family_t>.size - 1
let maxPath = String(repeating: "a", count: maxPathLength)
_ = maxPath.withCString { pathPtr in
memcpy(&addrUn.sun_path, pathPtr, maxPath.count + 1)
}

let storage = addrUn.makeStorage()

XCTAssertEqual(storage.ss_family, sa_family_t(AF_UNIX))
let addrUnBytes = withUnsafeBytes(of: addrUn) { Data($0) }
let storageBytes = withUnsafeBytes(of: storage) { Data($0) }
let storageAddrUnBytes = storageBytes.prefix(MemoryLayout<sockaddr_un>.size)
XCTAssertEqual(addrUnBytes, storageAddrUnBytes)
}

func testMemoryBoundsInMakeStorage() {
var addrIn = sockaddr_in()
addrIn.sin_family = sa_family_t(AF_INET)

let storage = addrIn.makeStorage()

let addrSize = MemoryLayout<sockaddr_in>.size
let storageSize = MemoryLayout<sockaddr_storage>.size
XCTAssertLessThanOrEqual(addrSize, storageSize)

withUnsafeBytes(of: addrIn) { addrInPtr in
let addrInBytes = addrInPtr.bindMemory(to: UInt8.self).baseAddress!
withUnsafeBytes(of: storage) { storagePtr in
let storageBytes = storagePtr.bindMemory(to: UInt8.self).baseAddress!
for i in 0..<addrSize {
XCTAssertEqual(addrInBytes[i], storageBytes[i], "Mismatch at byte \(i)")
}
}
}
}
}