Compare commits

..

No commits in common. "fb8abf63db8367efce11db6f2ccf4b7775553e0f" and "891b3cbde75c071b7339716d6fae727e26f19c0a" have entirely different histories.

4 changed files with 96 additions and 146 deletions

View File

@ -5,7 +5,6 @@ import (
"flag" "flag"
"os" "os"
"path" "path"
"strings"
"git.gensokyo.uk/security/fortify/fst" "git.gensokyo.uk/security/fortify/fst"
"git.gensokyo.uk/security/fortify/internal/fmsg" "git.gensokyo.uk/security/fortify/internal/fmsg"
@ -124,37 +123,98 @@ func actionInstall(args []string) {
Setup steps for files owned by the target user. Setup steps for files owned by the target user.
*/ */
withCacheDir("install", []string{ installConfig := &fst.Config{
// export inner bundle path in the environment ID: bundle.ID,
"export BUNDLE=" + fst.Tmp + "/bundle", Command: []string{shell, "-lc", "export BUNDLE=" + fst.Tmp + "/bundle && " + // export inner bundle path in the environment
// replace inner /etc "mkdir -p etc && chmod -R +w etc && rm -rf etc && cp -dRf $BUNDLE/etc etc && " + // replace inner /etc
"mkdir -p etc", "mkdir -p nix && chmod -R +w nix && rm -rf nix && cp -dRf /nix nix && " + // replace inner /nix
"chmod -R +w etc", "nix copy --offline --no-check-sigs --all --from file://$BUNDLE/res --to $PWD && " + // copy from binary cache
"rm -rf etc", "chmod 0755 .", // make cache directory world-readable for autoetc
"cp -dRf $BUNDLE/etc etc", },
// replace inner /nix Confinement: fst.ConfinementConfig{
"mkdir -p nix", AppID: bundle.AppID,
"chmod -R +w nix", Username: "nixos",
"rm -rf nix", Inner: path.Join("/data/data", bundle.ID, "cache"),
"cp -dRf /nix nix", Outer: pathSet.cacheDir, // this also ensures cacheDir via fshim
// copy from binary cache Sandbox: &fst.SandboxConfig{
"nix copy --offline --no-check-sigs --all --from file://$BUNDLE/res --to $PWD", Hostname: formatHostname(bundle.Name) + "-install",
// make cache directory world-readable for autoetc NoNewSession: dropShellInstall, // nix copy should not need job control
"chmod 0755 .", Filesystem: []*fst.FilesystemConfig{
}, workDir, bundle, pathSet, dropShellInstall, cleanup) {Src: path.Join(workDir, "nix"), Dst: "/nix", Must: true},
{Src: workDir, Dst: path.Join(fst.Tmp, "bundle"), Must: true},
},
Link: [][2]string{
{bundle.CurrentSystem, "/run/current-system"},
{"/run/current-system/sw/bin", "/bin"},
{"/run/current-system/sw/bin", "/usr/bin"},
},
Etc: path.Join(workDir, "etc"),
AutoEtc: true,
},
ExtraPerms: []*fst.ExtraPermConfig{
{Path: dataHome, Execute: true},
{Ensure: true, Path: pathSet.baseDir, Read: true, Write: true, Execute: true},
{Path: workDir, Execute: true},
},
},
}
if dropShellInstall {
installConfig.Command = []string{shell, "-l"}
fortifyApp(installConfig, cleanup)
cleanup()
fmsg.Exit(0)
}
fortifyApp(installConfig, cleanup)
/* /*
Activate home-manager generation. Activate home-manager generation.
*/ */
withNixDaemon("activate", []string{ activateConfig := &fst.Config{
// clean up broken links ID: bundle.ID,
"mkdir -p .local/state/{nix,home-manager}", Command: []string{shell, "-lc", "mkdir -p .local/state/{nix,home-manager} && chmod -R +w .local/state/{nix,home-manager} && rm -rf .local/state/{nix,home-manager} && " + // clean up broken links
"chmod -R +w .local/state/{nix,home-manager}", "nix-daemon --store / & " + // start nix-daemon
"rm -rf .local/state/{nix,home-manager}", "(while [ ! -S /nix/var/nix/daemon-socket/socket ]; do sleep 0.01; done) && " + // wait for socket to appear
// run activation script bundle.ActivationPackage + "/activate && " + // run activation script
bundle.ActivationPackage + "/activate", "pkill nix-daemon", // terminate nix-daemon
}, false, workDir, bundle, pathSet, dropShellActivate, cleanup) },
Confinement: fst.ConfinementConfig{
AppID: bundle.AppID,
Groups: bundle.Groups,
Username: "fortify",
Inner: path.Join("/data/data", bundle.ID),
Outer: pathSet.homeDir,
Sandbox: &fst.SandboxConfig{
Hostname: formatHostname(bundle.Name) + "-activate",
UserNS: true, // nix sandbox requires userns
NoNewSession: dropShellActivate, // home-manager activation should not need job control
Filesystem: []*fst.FilesystemConfig{
{Src: pathSet.nixPath, Dst: "/nix", Write: true, Must: true},
},
Link: [][2]string{
{bundle.CurrentSystem, "/run/current-system"},
{"/run/current-system/sw/bin", "/bin"},
{"/run/current-system/sw/bin", "/usr/bin"},
},
Etc: path.Join(pathSet.cacheDir, "etc"),
AutoEtc: true,
},
ExtraPerms: []*fst.ExtraPermConfig{
{Path: dataHome, Execute: true},
{Ensure: true, Path: pathSet.baseDir, Read: true, Write: true, Execute: true},
{Path: workDir, Execute: true},
},
},
}
if dropShellActivate {
activateConfig.Command = []string{shell, "-l"}
fortifyApp(activateConfig, cleanup)
cleanup()
fmsg.Exit(0)
}
fortifyApp(activateConfig, cleanup)
/* /*
Installation complete. Write metadata to block re-installs or downgrades. Installation complete. Write metadata to block re-installs or downgrades.
@ -182,89 +242,3 @@ func actionInstall(args []string) {
cleanup() cleanup()
} }
func withNixDaemon(action string, command []string, net bool, workDir string, bundle *bundleInfo, pathSet *appPathSet, dropShell bool, beforeFail func()) {
fortifyAppDropShell(&fst.Config{
ID: bundle.ID,
Command: []string{shell, "-lc", "rm -f /nix/var/nix/daemon-socket/socket && " +
// start nix-daemon
"nix-daemon --store / & " +
// wait for socket to appear
"(while [ ! -S /nix/var/nix/daemon-socket/socket ]; do sleep 0.01; done) && " +
strings.Join(command, " && ") +
// terminate nix-daemon
" && pkill nix-daemon",
},
Confinement: fst.ConfinementConfig{
AppID: bundle.AppID,
Groups: bundle.Groups,
Username: "fortify",
Inner: path.Join("/data/data", bundle.ID),
Outer: pathSet.homeDir,
Sandbox: &fst.SandboxConfig{
Hostname: formatHostname(bundle.Name) + "-" + action,
UserNS: true, // nix sandbox requires userns
Net: net,
NoNewSession: dropShell,
Filesystem: []*fst.FilesystemConfig{
{Src: pathSet.nixPath, Dst: "/nix", Write: true, Must: true},
},
Link: [][2]string{
{bundle.CurrentSystem, "/run/current-system"},
{"/run/current-system/sw/bin", "/bin"},
{"/run/current-system/sw/bin", "/usr/bin"},
},
Etc: path.Join(pathSet.cacheDir, "etc"),
AutoEtc: true,
},
ExtraPerms: []*fst.ExtraPermConfig{
{Path: dataHome, Execute: true},
{Ensure: true, Path: pathSet.baseDir, Read: true, Write: true, Execute: true},
{Path: workDir, Execute: true},
},
},
}, dropShell, beforeFail)
}
func withCacheDir(action string, command []string, workDir string, bundle *bundleInfo, pathSet *appPathSet, dropShell bool, beforeFail func()) {
fortifyAppDropShell(&fst.Config{
ID: bundle.ID,
Command: []string{shell, "-lc", strings.Join(command, " && ")},
Confinement: fst.ConfinementConfig{
AppID: bundle.AppID,
Username: "nixos",
Inner: path.Join("/data/data", bundle.ID, "cache"),
Outer: pathSet.cacheDir, // this also ensures cacheDir via fshim
Sandbox: &fst.SandboxConfig{
Hostname: formatHostname(bundle.Name) + "-" + action,
NoNewSession: dropShell,
Filesystem: []*fst.FilesystemConfig{
{Src: path.Join(workDir, "nix"), Dst: "/nix", Must: true},
{Src: workDir, Dst: path.Join(fst.Tmp, "bundle"), Must: true},
},
Link: [][2]string{
{bundle.CurrentSystem, "/run/current-system"},
{"/run/current-system/sw/bin", "/bin"},
{"/run/current-system/sw/bin", "/usr/bin"},
},
Etc: path.Join(workDir, "etc"),
AutoEtc: true,
},
ExtraPerms: []*fst.ExtraPermConfig{
{Path: dataHome, Execute: true},
{Ensure: true, Path: pathSet.baseDir, Read: true, Write: true, Execute: true},
{Path: workDir, Execute: true},
},
},
}, dropShell, beforeFail)
}
func fortifyAppDropShell(config *fst.Config, dropShell bool, beforeFail func()) {
if dropShell {
config.Command = []string{shell, "-l"}
fortifyApp(config, beforeFail)
beforeFail()
fmsg.Exit(0)
}
fortifyApp(config, beforeFail)
}

View File

@ -84,31 +84,7 @@ func actionStart(args []string) {
if app.GPU { if app.GPU {
config.Confinement.Sandbox.Filesystem = append(config.Confinement.Sandbox.Filesystem, config.Confinement.Sandbox.Filesystem = append(config.Confinement.Sandbox.Filesystem,
// flatpak commit 763a686d874dd668f0236f911de00b80766ffe79 &fst.FilesystemConfig{Src: "/dev/dri", Device: true})
&fst.FilesystemConfig{Src: "/dev/dri", Device: true},
// mali
&fst.FilesystemConfig{Src: "/dev/mali", Device: true},
&fst.FilesystemConfig{Src: "/dev/mali0", Device: true},
&fst.FilesystemConfig{Src: "/dev/umplock", Device: true},
// nvidia
&fst.FilesystemConfig{Src: "/dev/nvidiactl", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia-modeset", Device: true},
// nvidia OpenCL/CUDA
&fst.FilesystemConfig{Src: "/dev/nvidia-uvm", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia-uvm-tools", Device: true},
// flatpak commit d2dff2875bb3b7e2cd92d8204088d743fd07f3ff
&fst.FilesystemConfig{Src: "/dev/nvidia0", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia1", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia2", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia3", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia4", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia5", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia6", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia7", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia8", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia9", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia10", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia11", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia12", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia13", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia14", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia15", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia16", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia17", Device: true},
&fst.FilesystemConfig{Src: "/dev/nvidia18", Device: true}, &fst.FilesystemConfig{Src: "/dev/nvidia19", Device: true},
)
} }
fortifyApp(config, func() {}) fortifyApp(config, func() {})

12
flake.lock generated
View File

@ -7,11 +7,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1735344290, "lastModified": 1733951536,
"narHash": "sha256-oJDtWPH1oJT34RJK1FSWjwX4qcGOBRkcNQPD0EbSfNM=", "narHash": "sha256-Zb5ZCa7Xj+0gy5XVXINTSr71fCfAv+IKtmIXNrykT54=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "613691f285dad87694c2ba1c9e6298d04736292d", "rev": "1318c3f3b068cdcea922fa7c1a0a1f0c96c22f5f",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -23,11 +23,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1735326919, "lastModified": 1734298236,
"narHash": "sha256-BZlgs4l9CXAauo78giGCZdazMMk5VZNro7o5SHFUuyE=", "narHash": "sha256-aWhhqY44xBjMoO9r5fyPp5u8tqUNWRZ/m/P+abMSs5c=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "8f0aa155aa29f7d2b471aa2ffd322745bf2b2036", "rev": "eb919d9300b6a18f8583f58aef16db458fbd7bec",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -198,8 +198,8 @@ nixosTest {
machine.wait_for_file("/run/user/1000/wayland-1") machine.wait_for_file("/run/user/1000/wayland-1")
machine.wait_for_file("/tmp/sway-ipc.sock") machine.wait_for_file("/tmp/sway-ipc.sock")
# Create fortify uid 0 state directory: # Create fortify aid 0 home directory:
machine.succeed("install -dm 0755 -o u0_a0 -g users /var/lib/fortify/u0") machine.succeed("install -dm 0700 -o 1000000 -g 1000000 /var/lib/fortify/u0/a0")
# Start fortify outside Wayland session: # Start fortify outside Wayland session:
print(machine.succeed("sudo -u alice -i fortify -v run -a 0 touch /tmp/success-bare")) print(machine.succeed("sudo -u alice -i fortify -v run -a 0 touch /tmp/success-bare"))