|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 - 2022 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + */ |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import TSCBasic |
| 13 | + |
| 14 | +/// A sandbox profile representing the desired restrictions. The implementation can vary between platforms. Currently |
| 15 | +/// the only control a client has is in the path rules, but in the future there should also be options for controlling |
| 16 | +/// blocking of network access and process launching. |
| 17 | +public struct SandboxProfile: Equatable { |
| 18 | + /// An ordered list of path rules, where the last rule to cover a particular path "wins". These will be resolved |
| 19 | + /// to absolute paths at the time the profile is applied. They are applied after any of the implicit directories |
| 20 | + /// referenced by other sandbox profile settings. |
| 21 | + public var pathAccessRules: [PathAccessRule] |
| 22 | + |
| 23 | + /// Represents a rule for access to a path and everything under it. |
| 24 | + public enum PathAccessRule: Equatable { |
| 25 | + case noaccess(AbsolutePath) |
| 26 | + case readonly(AbsolutePath) |
| 27 | + case writable(AbsolutePath) |
| 28 | + } |
| 29 | + |
| 30 | + /// Configures a SandboxProfile for blocking network access and writing to the file system except to specifically |
| 31 | + /// permitted locations. |
| 32 | + public init(_ pathAccessRules: [PathAccessRule] = []) { |
| 33 | + self.pathAccessRules = pathAccessRules |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +extension SandboxProfile { |
| 38 | + /// Applies the sandbox profile to the given command line (if the platform supports it), and returns the modified |
| 39 | + /// command line. On platforms that don't support sandboxing, the unmodified command line is returned. |
| 40 | + public func apply(to command: [String]) throws -> [String] { |
| 41 | + #if os(macOS) |
| 42 | + return ["/usr/bin/sandbox-exec", "-p", try self.generateMacOSSandboxProfileString()] + command |
| 43 | + #else |
| 44 | + // rdar://40235432, rdar://75636874 tracks implementing sandboxes for other platforms. |
| 45 | + return command |
| 46 | + #endif |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// MARK: - macOS |
| 51 | + |
| 52 | +#if os(macOS) |
| 53 | +fileprivate extension SandboxProfile { |
| 54 | + /// Private function that generates a Darwin sandbox profile suitable for passing to `sandbox-exec(1)`. |
| 55 | + func generateMacOSSandboxProfileString() throws -> String { |
| 56 | + var contents = "(version 1)\n" |
| 57 | + |
| 58 | + // Deny everything by default. |
| 59 | + contents += "(deny default)\n" |
| 60 | + |
| 61 | + // Import the system sandbox profile. |
| 62 | + contents += "(import \"system.sb\")\n" |
| 63 | + |
| 64 | + // Allow operations on subprocesses. |
| 65 | + contents += "(allow process*)\n" |
| 66 | + |
| 67 | + // Allow reading any file that isn't protected by TCC or permissions (ideally we'd only allow a specific set |
| 68 | + // of readable locations, and can maybe tighten this in the future). |
| 69 | + contents += "(allow file-read*)\n" |
| 70 | + |
| 71 | + // Apply customized rules for specific file system locations. Everything is readonly by default, so we just |
| 72 | + // either allow or deny writing, in order. Later rules have precedence over earlier rules. |
| 73 | + for rule in pathAccessRules { |
| 74 | + switch rule { |
| 75 | + case .noaccess(let path): |
| 76 | + contents += "(deny file-* (subpath \(try resolveSymlinksAndQuotePath(path))))\n" |
| 77 | + case .readonly(let path): |
| 78 | + contents += "(deny file-write* (subpath \(try resolveSymlinksAndQuotePath(path))))\n" |
| 79 | + case .writable(let path): |
| 80 | + contents += "(allow file-write* (subpath \(try resolveSymlinksAndQuotePath(path))))\n" |
| 81 | + } |
| 82 | + } |
| 83 | + return contents |
| 84 | + } |
| 85 | + |
| 86 | + /// Private helper function that resolves an AbsolutePath and returns it as a string quoted for use as a subpath |
| 87 | + /// in a .sb sandbox profile. |
| 88 | + func resolveSymlinksAndQuotePath(_ path: AbsolutePath) throws -> String { |
| 89 | + return try "\"" + resolveSymlinks(path).pathString |
| 90 | + .replacingOccurrences(of: "\\", with: "\\\\") |
| 91 | + .replacingOccurrences(of: "\"", with: "\\\"") |
| 92 | + + "\"" |
| 93 | + } |
| 94 | +} |
| 95 | +#endif |
0 commit comments