Skip to content

Commit a23bcf0

Browse files
authored
Propagate host ssh socket permissions to relay socket in guest. (#811)
- Closes #580. - Depends on apple/containerization#362. ## Type of Change - [x] Bug fix - [ ] New feature - [ ] Breaking change - [ ] Documentation update ## Testing - [x] Tested locally - [ ] Added/updated tests - [ ] Added/updated docs
1 parent 9f8a0fb commit a23bcf0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/Services/ContainerSandboxService/SandboxService.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import NIO
2929
import NIOFoundationCompat
3030
import SocketForwarder
3131
import Synchronization
32+
import SystemPackage
3233

3334
import struct ContainerizationOCI.Mount
3435
import struct ContainerizationOCI.Process
@@ -51,7 +52,7 @@ public actor SandboxService {
5152
private static let sshAuthSocketGuestPath = "/run/host-services/ssh-auth.sock"
5253
private static let sshAuthSocketEnvVar = "SSH_AUTH_SOCK"
5354

54-
private static func hostSocketUrl(config: ContainerConfiguration) -> URL? {
55+
private static func sshAuthSocketHostUrl(config: ContainerConfiguration) -> URL? {
5556
if config.ssh, let sshSocket = Foundation.ProcessInfo.processInfo.environment[Self.sshAuthSocketEnvVar] {
5657
return URL(fileURLWithPath: sshSocket)
5758
}
@@ -793,11 +794,16 @@ public actor SandboxService {
793794
czConfig.sockets.append(socketConfig)
794795
}
795796

796-
if let socketUrl = Self.hostSocketUrl(config: config) {
797+
if let socketUrl = Self.sshAuthSocketHostUrl(config: config) {
798+
let socketPath = socketUrl.path(percentEncoded: false)
799+
let attrs = try? FileManager.default.attributesOfItem(atPath: socketPath)
800+
let permissions = (attrs?[.posixPermissions] as? NSNumber)
801+
.map { FilePermissions(rawValue: mode_t($0.intValue)) }
797802
let socketConfig = UnixSocketConfiguration(
798803
source: socketUrl,
799804
destination: URL(fileURLWithPath: Self.sshAuthSocketGuestPath),
800-
direction: .into
805+
permissions: permissions,
806+
direction: .into,
801807
)
802808
czConfig.sockets.append(socketConfig)
803809
}
@@ -835,7 +841,7 @@ public actor SandboxService {
835841
czConfig.process.arguments = [process.executable] + process.arguments
836842
czConfig.process.environmentVariables = process.environment
837843

838-
if Self.hostSocketUrl(config: config) != nil {
844+
if Self.sshAuthSocketHostUrl(config: config) != nil {
839845
if !czConfig.process.environmentVariables.contains(where: { $0.starts(with: "\(Self.sshAuthSocketEnvVar)=") }) {
840846
czConfig.process.environmentVariables.append("\(Self.sshAuthSocketEnvVar)=\(Self.sshAuthSocketGuestPath)")
841847
}
@@ -877,7 +883,7 @@ public actor SandboxService {
877883
proc.arguments = [config.executable] + config.arguments
878884
proc.environmentVariables = config.environment
879885

880-
if Self.hostSocketUrl(config: containerConfig) != nil {
886+
if Self.sshAuthSocketHostUrl(config: containerConfig) != nil {
881887
if !proc.environmentVariables.contains(where: { $0.starts(with: "\(Self.sshAuthSocketEnvVar)=") }) {
882888
proc.environmentVariables.append("\(Self.sshAuthSocketEnvVar)=\(Self.sshAuthSocketGuestPath)")
883889
}

0 commit comments

Comments
 (0)