flawopen.com/Command Injection/Swift
Imagine a secretary who executes whatever is written on a memo. If someone writes 'Buy stamps AND delete all company files', command injection is doing both because the memo didn't separate instructions from items.
In Swift CLI tools, server-side Swift (Vapor), or macOS utilities, invoking system() or passing strings to /bin/sh -c via Process causes command injection.
macOS helper tools and XPC services running with root privileges have suffered local privilege escalation through unvalidated Process arguments.
Apple Security Advisories & CVE Database.// Calling /bin/sh with string interpolation
import Foundation
func ping(host: String) {
// Attacker input: "127.0.0.1; whoami"
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sh")
process.arguments = ["-c", "ping -c 1 \(host)"]
try? process.run()
}
// Calling binary directly with isolated arguments array
import Foundation
func ping(host: String) {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/sbin/ping")
// host is strictly an argument, never parsed as a shell command
process.arguments = ["-c", "1", host]
try? process.run()
}
Setting executableURL directly to the target binary and passing parameters in the arguments array calls the operating system's posix_spawn API directly without invoking a shell.
Importing Darwin or Glibc allows calling C's system() directly, which inherits all the vulnerabilities of C command injection.
The macOS App Sandbox restricts file and network access, but helper tools or server-side Swift applications running outside the sandbox have full system access.
grep -rn 'process.arguments = \["-c"' --include="*.swift" .
grep -rn "system(" --include="*.swift" .
executableURL directly to the binary patharguments arraysystem() function in SwiftNo. iOS apps run in an isolated sandbox where spawning arbitrary subprocesses is restricted by the operating system.