Skip to content
Open
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
28 changes: 21 additions & 7 deletions Sources/Containerization/UnixSocketRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ extension UnixSocketRelay {

private func setupHostVsockDial() async throws {
let hostConn = configuration.destination

let socketType = try UnixType(
path: hostConn.path,
unlinkExisting: true
)
let hostSocket = try Socket(type: socketType)
try hostSocket.listen()
let hostSocket = try Self.makeHostListener(configuration)

log?.info(
"listening on host UDS",
Expand Down Expand Up @@ -124,6 +118,26 @@ extension UnixSocketRelay {
}
}

package static func makeHostListener(
_ configuration: UnixSocketConfiguration
) throws -> Socket {
let socketType = try UnixType(
path: configuration.destination.path,
perms: configuration.permissions.map {
mode_t($0.rawValue)
},
unlinkExisting: true
)
let socket = try Socket(type: socketType)
do {
try socket.listen()
return socket
} catch {
try? socket.close()
throw error
}
}

private func setupHostVsockListener() throws {
let hostPath = configuration.source

Expand Down
52 changes: 52 additions & 0 deletions Tests/ContainerizationTests/UnixSocketRelayTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the Containerization project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import Foundation
import Testing

@testable import Containerization

@Suite
struct UnixSocketRelayTests {
@Test
func outOfRelayAppliesRequestedHostSocketPermissions() throws {
let root = URL(fileURLWithPath: "/tmp").appendingPathComponent(
"containerization-relay-\(UUID())",
isDirectory: true
)
try FileManager.default.createDirectory(
at: root,
withIntermediateDirectories: true
)
defer { try? FileManager.default.removeItem(at: root) }
let socketPath = root.appendingPathComponent("service.sock")
let configuration = UnixSocketConfiguration(
source: URL(fileURLWithPath: "/run/service.sock"),
destination: socketPath,
permissions: .init(rawValue: 0o600),
direction: .outOf
)

let listener = try UnixSocketRelay.makeHostListener(configuration)
defer { try? listener.close() }
let permissions = try #require(
FileManager.default.attributesOfItem(atPath: socketPath.path)[
.posixPermissions
] as? NSNumber
)
#expect(permissions.uint16Value == 0o600)
}
}