Skip to content

Commit f158e1a

Browse files
committed
Fix 4 bugs: embed entitlements, air vz-shim copy, install.sh codesign entitlements, launchd e2fsprogs PATH
- Bug 1: Embed vz.entitlements as a Go resource and write to temp file at runtime for codesigning, instead of looking for it next to the executable where it would never be found. - Bug 2: Add 'mkdir -p && cp' step in .air.darwin.toml to copy the built vz-shim binary to lib/hypervisor/vz/vz-shim/vz-shim (the go:embed directory) before building the main binary. - Bug 3: Generate entitlements plist inline in install.sh download path and pass --entitlements to codesign, so downloaded binaries get the required com.apple.security.virtualization entitlement. - Bug 4: Prepend /opt/homebrew/opt/e2fsprogs/sbin to the launchd plist PATH so mkfs.ext4 (keg-only on Homebrew) is found at runtime.
1 parent d8e95da commit f158e1a

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

.air.darwin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tmp_dir = "tmp"
77
bin = "./tmp/main"
88
# Build for macOS with vz support, then sign with entitlements
99
# Also builds and signs vz-shim (subprocess that hosts vz VMs)
10-
cmd = "make build-embedded && go build -o ./tmp/vz-shim ./cmd/vz-shim && codesign --sign - --entitlements vz.entitlements --force ./tmp/vz-shim && go build -tags containers_image_openpgp -o ./tmp/main ./cmd/api && codesign --sign - --entitlements vz.entitlements --force ./tmp/main"
10+
cmd = "make build-embedded && go build -o ./tmp/vz-shim ./cmd/vz-shim && codesign --sign - --entitlements vz.entitlements --force ./tmp/vz-shim && mkdir -p lib/hypervisor/vz/vz-shim && cp ./tmp/vz-shim lib/hypervisor/vz/vz-shim/vz-shim && go build -tags containers_image_openpgp -o ./tmp/main ./cmd/api && codesign --sign - --entitlements vz.entitlements --force ./tmp/main"
1111
delay = 1000
1212
exclude_dir = ["assets", "tmp", "vendor", "testdata", "bin", "scripts", "data", "kernel"]
1313
exclude_file = []

lib/hypervisor/vz/starter.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,25 @@ func extractShim() (string, error) {
5757
return
5858
}
5959

60+
// Write embedded entitlements to a temp file for codesigning
61+
entFile, err := os.CreateTemp("", "vz-entitlements-*.plist")
62+
if err != nil {
63+
os.Remove(f.Name())
64+
shimErr = fmt.Errorf("create entitlements temp file: %w", err)
65+
return
66+
}
67+
defer os.Remove(entFile.Name())
68+
defer entFile.Close()
69+
70+
if _, err := entFile.Write(vzEntitlements); err != nil {
71+
os.Remove(f.Name())
72+
shimErr = fmt.Errorf("write entitlements file: %w", err)
73+
return
74+
}
75+
entFile.Close()
76+
6077
// Codesign with entitlements for Virtualization.framework
61-
cmd := exec.Command("codesign", "--sign", "-", "--entitlements", entitlementsPath(), "--force", f.Name())
78+
cmd := exec.Command("codesign", "--sign", "-", "--entitlements", entFile.Name(), "--force", f.Name())
6279
if out, err := cmd.CombinedOutput(); err != nil {
6380
os.Remove(f.Name())
6481
shimErr = fmt.Errorf("codesign vz-shim: %s: %w", string(out), err)
@@ -70,15 +87,6 @@ func extractShim() (string, error) {
7087
return shimPath, shimErr
7188
}
7289

73-
// entitlementsPath returns the path to the vz.entitlements file.
74-
func entitlementsPath() string {
75-
exe, err := os.Executable()
76-
if err != nil {
77-
return "vz.entitlements"
78-
}
79-
return filepath.Join(filepath.Dir(exe), "vz.entitlements")
80-
}
81-
8290
// Starter implements hypervisor.VMStarter for Virtualization.framework.
8391
type Starter struct{}
8492

lib/hypervisor/vz/vz.entitlements

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<!-- Required for Virtualization.framework -->
6+
<key>com.apple.security.virtualization</key>
7+
<true/>
8+
<!-- Required for network operations (NAT, ingress) -->
9+
<key>com.apple.security.network.server</key>
10+
<true/>
11+
<key>com.apple.security.network.client</key>
12+
<true/>
13+
</dict>
14+
</plist>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build darwin
2+
3+
package vz
4+
5+
import _ "embed"
6+
7+
// vzEntitlements contains the embedded vz.entitlements plist file.
8+
// This is used at runtime to codesign the extracted vz-shim binary.
9+
//
10+
//go:embed vz.entitlements
11+
var vzEntitlements []byte

scripts/install.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,27 @@ else
348348
info "Extracting..."
349349
tar -xzf "${TMP_DIR}/${ARCHIVE_NAME}" -C "$TMP_DIR"
350350

351-
# On macOS, codesign after extraction
351+
# On macOS, codesign after extraction with virtualization entitlements
352352
if [ "$OS" = "darwin" ]; then
353353
info "Signing binaries..."
354-
codesign --force --sign - "${TMP_DIR}/${BINARY_NAME}" 2>/dev/null || true
355-
[ -f "${TMP_DIR}/vz-shim" ] && codesign --force --sign - "${TMP_DIR}/vz-shim" 2>/dev/null || true
354+
ENTITLEMENTS_TMP="${TMP_DIR}/vz.entitlements"
355+
cat > "$ENTITLEMENTS_TMP" << 'ENTITLEMENTS'
356+
<?xml version="1.0" encoding="UTF-8"?>
357+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
358+
<plist version="1.0">
359+
<dict>
360+
<key>com.apple.security.virtualization</key>
361+
<true/>
362+
<key>com.apple.security.network.server</key>
363+
<true/>
364+
<key>com.apple.security.network.client</key>
365+
<true/>
366+
</dict>
367+
</plist>
368+
ENTITLEMENTS
369+
codesign --force --sign - --entitlements "$ENTITLEMENTS_TMP" "${TMP_DIR}/${BINARY_NAME}" 2>/dev/null || true
370+
[ -f "${TMP_DIR}/vz-shim" ] && codesign --force --sign - --entitlements "$ENTITLEMENTS_TMP" "${TMP_DIR}/vz-shim" 2>/dev/null || true
371+
rm -f "$ENTITLEMENTS_TMP"
356372
fi
357373
fi
358374

@@ -543,7 +559,7 @@ if [ "$OS" = "darwin" ]; then
543559
<key>EnvironmentVariables</key>
544560
<dict>
545561
<key>PATH</key>
546-
<string>/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>${ENV_DICT}
562+
<string>/opt/homebrew/opt/e2fsprogs/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>${ENV_DICT}
547563
</dict>
548564
<key>KeepAlive</key>
549565
<true/>

0 commit comments

Comments
 (0)