├── .gitattributes ├── LICENSE ├── README.md ├── confined.sb ├── download.sb ├── exampleBuilds.md ├── nomach.sb └── ping.sb /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Brian Swift 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MacOS Sandbox Build 2 | 3 | ## What is this? 4 | 5 | MacOS sandbox profiles and instructions for command line (Terminal) building of software in a sandboxed environment. 6 | 7 | ## Audience 8 | 9 | People who build software from the command line on macOS and have security related concerns about what could be occurring in a complex build process. 10 | 11 | ## How do I use it? 12 | 13 | After some setup, software build commands are executed in a restricted environment defined by the `confined.sb` profile using macOS command `sandbox-exec`. Three parameters passed to `confined.sb` specify directories accessible to the build command that are additions to a limited set of standard system directories. 14 | * `_RX1` : contents are readable and executable, and metadata can be read from its path-ancestors 15 | * `_RW1`: contents are readable and writable 16 | * `_TMPDIR`: contents are readable and writable 17 | 18 | The following example steps through building `cmake`. 19 | 20 | ### Download Profiles 21 | 22 | ``` 23 | mkdir -p "$HOME/Development/github" 24 | cd "$HOME/Development/github" 25 | git clone https://github.com/BrianSwift/macOSSandboxBuild.git 26 | ``` 27 | 28 | ### Setup `TMPDIR` 29 | 30 | ``` 31 | export TMPDIR="$HOME/Dev Space/sandtmp" 32 | mkdir -p "$TMPDIR" 33 | ``` 34 | 35 | ### Load `xcrun_db` cache 36 | Identify tool names in `/usr/bin` that might bounce to an XCode tool (this is intersection of tools in Xcode.app and tools in `/usr/bin`.) 37 | This annoying setup process only needs to be done once. 38 | ``` 39 | (ls /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin ; ls /Applications/Xcode.app/Contents/Developer/usr/bin ) | sort -u >/tmp/tool_list_xcode.txt 40 | ls /usr/bin | sort >/tmp/tool_list_usr_bin.txt 41 | comm -12 /tmp/tool_list_usr_bin.txt /tmp/tool_list_xcode.txt >/tmp/tool_list_to_cache.txt 42 | ``` 43 | Execute `xcrun -find` for each tool name to load `xcrun_db`. Execution in sandbox using `nomach.sb` profile causes `xcrun` to fallback to using `$TMPDIR` for location of `xcrun_db`, rather than `_CS_DARWIN_USER_TEMP_DIR` returned by `confstr(3)`. `nomach.sb` only denies `mach-lookup` operations. It is not more restrictive because only Apple provided code is executed. 44 | 45 | First pass takes a few seconds per tool, and may get error messages when looking up `DeRez` and `swift`. Second command produces lots of errors, but that is expected because some apps don't recognize `--version`. Final command should only take a fraction of second per tool, and produce no errors. 46 | ``` 47 | /tmp/sblog-cmake-01.txt 92 | ``` 93 | bootstrap `cmake` within confined sandbox 94 | ``` 95 | cd "$HOME/Dev Space/Net/cmake/Build" 96 | PATH="`pwd`:$PATH" /usr/bin/time sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="$PWD" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" "$HOME/Dev Space/Net/cmake/Src/cmake-3.21.2/bootstrap" --prefix="$HOME/Dev Space/Net/cmake/Inst" 97 | : Expected final output 98 | : CMake has bootstrapped. Now run make. 99 | : 463.29 real 376.10 user 79.49 sys 100 | ``` 101 | build and install `cmake` within confined sandbox 102 | ``` 103 | PATH="`pwd`:$PATH" /usr/bin/time sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="`dirname $PWD`" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" make -j 4 install 104 | : Expected final output 105 | : -- Installing: /Users/sand/Dev Space/Net/cmake/Inst/share/bash-completion/completions/ctest 106 | : 501.18 real 1820.55 user 124.39 sys 107 | ``` 108 | Stop logging 109 | 110 | Stop (^C) the `log stream...` running in another window. 111 | 112 | A tally of sandbox allow/deny log messages can be produced with this command. 113 | ``` 114 | tr '0123456789' '##########' s8v4.pbrt <<'EOFF' 346 | Film "rgb" 347 | "integer yresolution" [ 300 ] 348 | "integer xresolution" [ 300 ] 349 | "string filename" [ "s8.png" ] 350 | 351 | Sampler "halton" 352 | "integer pixelsamples" [ 128 ] 353 | Integrator "bdpt" 354 | LookAt 796800000 0 0 355 | 800000000 0 0 356 | 0 0 1 357 | Camera "perspective" 358 | "float fov" [ 4 ] 359 | WorldBegin 360 | LightSource "distant" 361 | "float scale" [1.5] 362 | "blackbody L" [ 6500 ] 363 | "point3 to" [ 1 0 0 ] 364 | AttributeBegin 365 | Material "diffuse" 366 | "rgb reflectance" [ 0.9 0.9 0.9 ] 367 | Translate 800000000 0 0 368 | Shape "sphere" 369 | "float radius" [ 80000 ] 370 | AttributeEnd 371 | EOFF 372 | 373 | /usr/bin/time sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="$PWD" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" "$HOME/Dev Space/Net/pbrt-v4/Inst/bin/pbrt" s8v4.pbrt 374 | pbrt version 4 (built Sep 13 2021 at 20:35:33) 375 | Copyright (c)1998-2021 Matt Pharr, Wenzel Jakob, and Greg Humphreys. 376 | The source code to pbrt (but *not* the book contents) is covered by the Apache 2.0 License. 377 | See the file LICENSE.txt for the conditions of the license. 378 | Rendering: [+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] (10.6s) 379 | 10.82 real 78.99 user 0.17 sys 380 | ``` 381 | ### llvm clang 382 | ``` 383 | mkdir -p "$HOME/Dev Space/Net/llvm/"{Src,Build,Dist,Inst} 384 | cd "$HOME/Dev Space/Net/llvm/Src" 385 | git clone --depth=1 https://github.com/llvm/llvm-project.git 386 | 387 | ;: patch llvm bug that causes abort durring clang link of build directory path contains space character 388 | cd "$HOME/Dev Space/Net/llvm/Src/llvm-project/clang/tools/driver" 389 | patch <<'EOFF' 390 | --- CMakeLists.txt 2021-09-14 13:10:43.000000000 -0700 391 | +++ CMakeLists-fixed.txt 2021-09-14 13:13:16.000000000 -0700 392 | @@ -82,7 +82,7 @@ 393 | set(TOOL_INFO_PLIST_OUT "${CMAKE_CURRENT_BINARY_DIR}/${TOOL_INFO_PLIST}") 394 | target_link_libraries(clang 395 | PRIVATE 396 | - "-Wl,-sectcreate,__TEXT,__info_plist,${TOOL_INFO_PLIST_OUT}") 397 | + "-Wl,-sectcreate,__TEXT,__info_plist,\"${TOOL_INFO_PLIST_OUT}\"") 398 | configure_file("${TOOL_INFO_PLIST}.in" "${TOOL_INFO_PLIST_OUT}" @ONLY) 399 | 400 | set(TOOL_INFO_UTI) 401 | EOFF 402 | 403 | 404 | cd "$HOME/Dev Space/Net/llvm/Build" 405 | unset CMAKE_PREFIX_PATH 406 | /usr/bin/time sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="$PWD" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" cmake -S "$HOME/Dev Space/Net/llvm/Src/llvm-project/llvm" -B . --install-prefix "$HOME/Dev Space/Net/llvm/Inst" -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" 407 | ... 408 | -- Performing Test HAVE_STEADY_CLOCK -- success 409 | -- Configuring done 410 | -- Generating done 411 | -- Build files have been written to: /Users/sand/Dev Space/Net/llvm/Build 412 | 65.92 real 27.86 user 34.20 sys 413 | 414 | /usr/bin/time sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="`dirname $PWD`" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" make -j 4 install ; date 415 | ... 416 | -- Installing: /Users/sand/Dev Space/Net/llvm/Inst/lib/cmake/llvm/./TableGen.cmake 417 | 5701.77 real 20778.79 user 1164.39 sys 418 | Tue Sep 14 15:07:26 PDT 2021 419 | ``` 420 | Test trivial compile with built clang 421 | ``` 422 | mkdir -p "$HOME/Dev Space/Net/tests" 423 | cd "$HOME/Dev Space/Net/tests" 424 | cat >main.c <<'EOFF' 425 | #include 426 | int main(){ 427 | printf("hello you\n"); 428 | return 0; 429 | } 430 | EOFF 431 | 432 | ;: Note: needed -isysroot to find stdio.h 433 | ;: Maybe there is better way to build clang to specify default system search 434 | sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="$PWD" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" "$HOME/Dev Space/Net/llvm/Inst"/bin/clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk main.c 435 | sandbox-exec -D_RX1="$HOME/Dev Space/Net" -D_RW1="$PWD" -D_TMPDIR="$TMPDIR" -f "$HOME/Development/github/macOSSandboxBuild/confined.sb" "`pwd`/a.out" 436 | ``` 437 | -------------------------------------------------------------------------------- /nomach.sb: -------------------------------------------------------------------------------- 1 | (version 1) 2 | ;; Profile that just blocks mach-lookup 3 | ;; Only used while populating xcrun_db 4 | (allow default) 5 | (deny mach-lookup) 6 | -------------------------------------------------------------------------------- /ping.sb: -------------------------------------------------------------------------------- 1 | (version 1) 2 | 3 | ;; sandbox profile for ping ping6 4 | 5 | ;; For explanation and usage see https://github.com/BrianSwift/macOSSandboxBuild 6 | 7 | (deny default) 8 | 9 | (allow file-read* process-exec 10 | (literal "/sbin/ping") 11 | (literal "/sbin/ping6") 12 | ) 13 | 14 | (allow network-outbound 15 | (literal "/private/var/run/mDNSResponder") 16 | (remote udp) ; ping 17 | ) 18 | 19 | (allow network-inbound (local udp "*:*")) ; ping 20 | 21 | (allow file-read-metadata 22 | (literal "/var") ; needed for DNS resolution 23 | ) 24 | --------------------------------------------------------------------------------