Compare commits
19 Commits
9d9a165379
...
2e7e160683
Author | SHA1 | Date | |
---|---|---|---|
2e7e160683 | |||
79957f8ea7 | |||
7e52463445 | |||
89970f5197 | |||
35037705a9 | |||
647c6ea21b | |||
416d93e880 | |||
312753924b | |||
54308f79d2 | |||
dfa3217037 | |||
8000a2febb | |||
7bd48d3489 | |||
b5eaeac11a | |||
a9986aab6a | |||
ff30a5ab5d | |||
eb0c16dd8c | |||
4fa1e97026 | |||
64b6dc41ba | |||
c64b8163e7 |
@ -12,16 +12,17 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run fortify tests
|
||||||
run: |
|
run: nix build --out-link "result-fortify" --print-out-paths --print-build-logs .#checks.x86_64-linux.fortify
|
||||||
nix --print-build-logs --experimental-features 'nix-command flakes' flake check
|
|
||||||
nix build --out-link "result" --print-out-paths --print-build-logs .#checks.x86_64-linux.nixos-tests
|
- name: Run flake checks
|
||||||
|
run: nix --print-build-logs --experimental-features 'nix-command flakes' flake check
|
||||||
|
|
||||||
- name: Upload test output
|
- name: Upload test output
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v3
|
||||||
with:
|
with:
|
||||||
name: "nixos-vm-output"
|
name: "fortify-vm-output"
|
||||||
path: result/*
|
path: result-fortify/*
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
|
|
||||||
dist:
|
dist:
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
{
|
{
|
||||||
lib,
|
lib,
|
||||||
writeScript,
|
writeScript,
|
||||||
|
writeScriptBin,
|
||||||
runtimeShell,
|
runtimeShell,
|
||||||
writeText,
|
writeText,
|
||||||
symlinkJoin,
|
symlinkJoin,
|
||||||
@ -177,7 +178,7 @@ let
|
|||||||
};
|
};
|
||||||
in
|
in
|
||||||
|
|
||||||
writeScript "fortify-${pname}-bundle-prelude" ''
|
writeScriptBin "build-fpkg-${pname}" ''
|
||||||
#!${runtimeShell} -el
|
#!${runtimeShell} -el
|
||||||
OUT="$(mktemp -d)"
|
OUT="$(mktemp -d)"
|
||||||
TAR="$(mktemp -u)"
|
TAR="$(mktemp -u)"
|
@ -9,10 +9,10 @@ import (
|
|||||||
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
||||||
)
|
)
|
||||||
|
|
||||||
const shell = "/run/current-system/sw/bin/bash"
|
const shellPath = "/run/current-system/sw/bin/bash"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if err := os.Setenv("SHELL", shell); err != nil {
|
if err := os.Setenv("SHELL", shellPath); err != nil {
|
||||||
log.Fatalf("cannot set $SHELL: %v", err)
|
log.Fatalf("cannot set $SHELL: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ func actionStart(args []string) {
|
|||||||
if !dropShell {
|
if !dropShell {
|
||||||
command[0] = app.Launcher
|
command[0] = app.Launcher
|
||||||
} else {
|
} else {
|
||||||
command[0] = shell
|
command[0] = shellPath
|
||||||
}
|
}
|
||||||
command = append(command, args[1:]...)
|
command = append(command, args[1:]...)
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ func withNixDaemon(
|
|||||||
) {
|
) {
|
||||||
fortifyAppDropShell(updateConfig(&fst.Config{
|
fortifyAppDropShell(updateConfig(&fst.Config{
|
||||||
ID: app.ID,
|
ID: app.ID,
|
||||||
Command: []string{shell, "-lc", "rm -f /nix/var/nix/daemon-socket/socket && " +
|
Command: []string{shellPath, "-lc", "rm -f /nix/var/nix/daemon-socket/socket && " +
|
||||||
// start nix-daemon
|
// start nix-daemon
|
||||||
"nix-daemon --store / & " +
|
"nix-daemon --store / & " +
|
||||||
// wait for socket to appear
|
// wait for socket to appear
|
||||||
@ -59,7 +59,7 @@ func withNixDaemon(
|
|||||||
func withCacheDir(action string, command []string, workDir string, app *bundleInfo, pathSet *appPathSet, dropShell bool, beforeFail func()) {
|
func withCacheDir(action string, command []string, workDir string, app *bundleInfo, pathSet *appPathSet, dropShell bool, beforeFail func()) {
|
||||||
fortifyAppDropShell(&fst.Config{
|
fortifyAppDropShell(&fst.Config{
|
||||||
ID: app.ID,
|
ID: app.ID,
|
||||||
Command: []string{shell, "-lc", strings.Join(command, " && ")},
|
Command: []string{shellPath, "-lc", strings.Join(command, " && ")},
|
||||||
Confinement: fst.ConfinementConfig{
|
Confinement: fst.ConfinementConfig{
|
||||||
AppID: app.AppID,
|
AppID: app.AppID,
|
||||||
Username: "nixos",
|
Username: "nixos",
|
||||||
@ -92,7 +92,7 @@ func withCacheDir(action string, command []string, workDir string, app *bundleIn
|
|||||||
|
|
||||||
func fortifyAppDropShell(config *fst.Config, dropShell bool, beforeFail func()) {
|
func fortifyAppDropShell(config *fst.Config, dropShell bool, beforeFail func()) {
|
||||||
if dropShell {
|
if dropShell {
|
||||||
config.Command = []string{shell, "-l"}
|
config.Command = []string{shellPath, "-l"}
|
||||||
fortifyApp(config, beforeFail)
|
fortifyApp(config, beforeFail)
|
||||||
beforeFail()
|
beforeFail()
|
||||||
internal.Exit(0)
|
internal.Exit(0)
|
||||||
|
65
command/builder.go
Normal file
65
command/builder.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New initialises a root Node.
|
||||||
|
func New(output io.Writer, logf LogFunc, name string, early HandlerFunc) Command {
|
||||||
|
c := rootNode{newNode(output, logf, name, "")}
|
||||||
|
c.f = early
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNode(output io.Writer, logf LogFunc, name, usage string) *node {
|
||||||
|
n := &node{
|
||||||
|
name: name, usage: usage,
|
||||||
|
out: output, logf: logf,
|
||||||
|
set: flag.NewFlagSet(name, flag.ContinueOnError),
|
||||||
|
}
|
||||||
|
n.set.SetOutput(output)
|
||||||
|
n.set.Usage = func() {
|
||||||
|
_ = n.writeHelp()
|
||||||
|
if n.suffix.Len() > 0 {
|
||||||
|
_, _ = fmt.Fprintln(output, "Flags:")
|
||||||
|
n.set.PrintDefaults()
|
||||||
|
_, _ = fmt.Fprintln(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) Command(name, usage string, f HandlerFunc) Node {
|
||||||
|
n.NewCommand(name, usage, f)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) NewCommand(name, usage string, f HandlerFunc) Flag[Node] {
|
||||||
|
if f == nil {
|
||||||
|
panic("invalid handler")
|
||||||
|
}
|
||||||
|
if name == "" || usage == "" {
|
||||||
|
panic("invalid subcommand")
|
||||||
|
}
|
||||||
|
|
||||||
|
s := newNode(n.out, n.logf, name, usage)
|
||||||
|
s.f = f
|
||||||
|
if !n.adopt(s) {
|
||||||
|
panic("attempted to initialise subcommand with non-unique name")
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) New(name, usage string) Node {
|
||||||
|
if name == "" || usage == "" {
|
||||||
|
panic("invalid subcommand tree")
|
||||||
|
}
|
||||||
|
s := newNode(n.out, n.logf, name, usage)
|
||||||
|
if !n.adopt(s) {
|
||||||
|
panic("attempted to initialise subcommand tree with non-unique name")
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
56
command/builder_test.go
Normal file
56
command/builder_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package command_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.gensokyo.uk/security/fortify/command"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuild(t *testing.T) {
|
||||||
|
c := command.New(nil, nil, "test", nil)
|
||||||
|
stubHandler := func([]string) error { panic("unreachable") }
|
||||||
|
|
||||||
|
t.Run("nil direct handler", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Command", "invalid handler")
|
||||||
|
c.Command("name", "usage", nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("direct zero length", func(t *testing.T) {
|
||||||
|
wantPanic := "invalid subcommand"
|
||||||
|
t.Run("zero length name", func(t *testing.T) { defer checkRecover(t, "Command", wantPanic); c.Command("", "usage", stubHandler) })
|
||||||
|
t.Run("zero length usage", func(t *testing.T) { defer checkRecover(t, "Command", wantPanic); c.Command("name", "", stubHandler) })
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("direct adopt unique names", func(t *testing.T) {
|
||||||
|
c.Command("d0", "usage", stubHandler)
|
||||||
|
c.Command("d1", "usage", stubHandler)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("direct adopt non-unique name", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Command", "attempted to initialise subcommand with non-unique name")
|
||||||
|
c.Command("d0", "usage", stubHandler)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("zero length", func(t *testing.T) {
|
||||||
|
wantPanic := "invalid subcommand tree"
|
||||||
|
t.Run("zero length name", func(t *testing.T) { defer checkRecover(t, "New", wantPanic); c.New("", "usage") })
|
||||||
|
t.Run("zero length usage", func(t *testing.T) { defer checkRecover(t, "New", wantPanic); c.New("name", "") })
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("direct adopt unique names", func(t *testing.T) {
|
||||||
|
c.New("t0", "usage")
|
||||||
|
c.New("t1", "usage")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("direct adopt non-unique name", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Command", "attempted to initialise subcommand tree with non-unique name")
|
||||||
|
c.New("t0", "usage")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkRecover(t *testing.T, name, wantPanic string) {
|
||||||
|
if r := recover(); r != wantPanic {
|
||||||
|
t.Errorf("%s: panic = %v; wantPanic %v",
|
||||||
|
name, r, wantPanic)
|
||||||
|
}
|
||||||
|
}
|
50
command/command.go
Normal file
50
command/command.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// Package command implements generic nested command parsing.
|
||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UsageInternal causes the command to be hidden from help text when set as the usage string.
|
||||||
|
const UsageInternal = "internal"
|
||||||
|
|
||||||
|
type (
|
||||||
|
// HandlerFunc is called when matching a directly handled subcommand tree.
|
||||||
|
HandlerFunc = func(args []string) error
|
||||||
|
|
||||||
|
// LogFunc is the function signature of a printf function.
|
||||||
|
LogFunc = func(format string, a ...any)
|
||||||
|
|
||||||
|
// FlagDefiner is a deferred flag definer value, usually encapsulating the default value.
|
||||||
|
FlagDefiner interface {
|
||||||
|
// Define defines the flag in set.
|
||||||
|
Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string)
|
||||||
|
}
|
||||||
|
|
||||||
|
Flag[T any] interface {
|
||||||
|
// Flag defines a generic flag type in Node's flag set.
|
||||||
|
Flag(p any, name string, value FlagDefiner, usage string) T
|
||||||
|
}
|
||||||
|
|
||||||
|
Command interface {
|
||||||
|
Parse(arguments []string) error
|
||||||
|
baseNode[Command]
|
||||||
|
}
|
||||||
|
Node baseNode[Node]
|
||||||
|
|
||||||
|
baseNode[T any] interface {
|
||||||
|
// Command appends a subcommand with direct command handling.
|
||||||
|
Command(name, usage string, f HandlerFunc) T
|
||||||
|
|
||||||
|
// New returns a new subcommand tree.
|
||||||
|
New(name, usage string) (sub Node)
|
||||||
|
// NewCommand returns a new subcommand with direct command handling.
|
||||||
|
NewCommand(name, usage string, f HandlerFunc) (sub Flag[Node])
|
||||||
|
|
||||||
|
// PrintHelp prints a help message to the configured writer.
|
||||||
|
PrintHelp()
|
||||||
|
|
||||||
|
Flag[T]
|
||||||
|
}
|
||||||
|
)
|
77
command/flag.go
Normal file
77
command/flag.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FlagError wraps errors returned by [flag].
|
||||||
|
type FlagError struct{ error }
|
||||||
|
|
||||||
|
func (e FlagError) Success() bool { return errors.Is(e.error, flag.ErrHelp) }
|
||||||
|
func (e FlagError) Is(target error) bool {
|
||||||
|
return (e.error == nil && target == nil) ||
|
||||||
|
((e.error != nil && target != nil) && e.error.Error() == target.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) Flag(p any, name string, value FlagDefiner, usage string) Node {
|
||||||
|
value.Define(&n.suffix, n.set, p, name, usage)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringFlag is the default value of a string flag.
|
||||||
|
type StringFlag string
|
||||||
|
|
||||||
|
func (v StringFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string) {
|
||||||
|
set.StringVar(p.(*string), name, string(v), usage)
|
||||||
|
b.WriteString(" [" + prettyFlag(name) + " <value>]")
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntFlag is the default value of an int flag.
|
||||||
|
type IntFlag int
|
||||||
|
|
||||||
|
func (v IntFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string) {
|
||||||
|
set.IntVar(p.(*int), name, int(v), usage)
|
||||||
|
b.WriteString(" [" + prettyFlag(name) + " <int>]")
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolFlag is the default value of a bool flag.
|
||||||
|
type BoolFlag bool
|
||||||
|
|
||||||
|
func (v BoolFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string) {
|
||||||
|
set.BoolVar(p.(*bool), name, bool(v), usage)
|
||||||
|
b.WriteString(" [" + prettyFlag(name) + "]")
|
||||||
|
}
|
||||||
|
|
||||||
|
// RepeatableFlag implements an ordered, repeatable string flag.
|
||||||
|
type RepeatableFlag []string
|
||||||
|
|
||||||
|
func (r *RepeatableFlag) String() string {
|
||||||
|
if r == nil {
|
||||||
|
return "<nil>"
|
||||||
|
}
|
||||||
|
return strings.Join(*r, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RepeatableFlag) Set(v string) error {
|
||||||
|
*r = append(*r, v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RepeatableFlag) Define(b *strings.Builder, set *flag.FlagSet, _ any, name, usage string) {
|
||||||
|
set.Var(r, name, usage)
|
||||||
|
b.WriteString(" [" + prettyFlag(name) + " <value>]")
|
||||||
|
}
|
||||||
|
|
||||||
|
// this has no effect on parse outcome
|
||||||
|
func prettyFlag(name string) string {
|
||||||
|
switch len(name) {
|
||||||
|
case 0:
|
||||||
|
panic("zero length flag name")
|
||||||
|
case 1:
|
||||||
|
return "-" + name
|
||||||
|
default:
|
||||||
|
return "--" + name
|
||||||
|
}
|
||||||
|
}
|
53
command/help.go
Normal file
53
command/help.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrHelp = errors.New("help requested")
|
||||||
|
|
||||||
|
func (n *node) PrintHelp() { _ = n.writeHelp() }
|
||||||
|
|
||||||
|
func (n *node) writeHelp() error {
|
||||||
|
if _, err := fmt.Fprintf(n.out,
|
||||||
|
"\nUsage:\t%s [-h | --help]%s COMMAND [OPTIONS]\n",
|
||||||
|
strings.Join(append(n.prefix, n.name), " "), &n.suffix,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n.child != nil {
|
||||||
|
if _, err := fmt.Fprint(n.out, "\nCommands:\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tw := tabwriter.NewWriter(n.out, 0, 1, 4, ' ', 0)
|
||||||
|
if err := n.child.writeCommands(tw); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tw.Flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := n.out.Write([]byte{'\n'})
|
||||||
|
if err == nil {
|
||||||
|
err = ErrHelp
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) writeCommands(w io.Writer) error {
|
||||||
|
if n == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if n.usage != UsageInternal {
|
||||||
|
if _, err := fmt.Fprintf(w, "\t%s\t%s\n", n.name, n.usage); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n.next.writeCommands(w)
|
||||||
|
}
|
40
command/node.go
Normal file
40
command/node.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type node struct {
|
||||||
|
child, next *node
|
||||||
|
name, usage string
|
||||||
|
|
||||||
|
out io.Writer
|
||||||
|
logf LogFunc
|
||||||
|
|
||||||
|
prefix []string
|
||||||
|
suffix strings.Builder
|
||||||
|
|
||||||
|
f HandlerFunc
|
||||||
|
set *flag.FlagSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) adopt(v *node) bool {
|
||||||
|
if n.child != nil {
|
||||||
|
return n.child.append(v)
|
||||||
|
}
|
||||||
|
n.child = v
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) append(v *node) bool {
|
||||||
|
if n.name == v.name {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if n.next != nil {
|
||||||
|
return n.next.append(v)
|
||||||
|
}
|
||||||
|
n.next = v
|
||||||
|
return true
|
||||||
|
}
|
80
command/parse.go
Normal file
80
command/parse.go
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrEmptyTree = errors.New("subcommand tree has no nodes")
|
||||||
|
ErrNoMatch = errors.New("did not match any subcommand")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (n *node) Parse(arguments []string) error {
|
||||||
|
if n.usage == "" { // root node has zero length usage
|
||||||
|
if n.next != nil {
|
||||||
|
panic("invalid toplevel state")
|
||||||
|
}
|
||||||
|
goto match
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(arguments) == 0 {
|
||||||
|
// unreachable: zero length args cause upper level to return with a help message
|
||||||
|
panic("attempted to parse with zero length args")
|
||||||
|
}
|
||||||
|
if arguments[0] != n.name {
|
||||||
|
if n.next == nil {
|
||||||
|
n.printf("%q is not a valid command", arguments[0])
|
||||||
|
return ErrNoMatch
|
||||||
|
}
|
||||||
|
n.next.prefix = n.prefix
|
||||||
|
return n.next.Parse(arguments)
|
||||||
|
}
|
||||||
|
arguments = arguments[1:]
|
||||||
|
|
||||||
|
match:
|
||||||
|
if n.child != nil {
|
||||||
|
// propagate help prefix early: flag set usage dereferences help
|
||||||
|
n.child.prefix = append(n.prefix, n.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.set.Parsed() {
|
||||||
|
panic("invalid set state")
|
||||||
|
}
|
||||||
|
if err := n.set.Parse(arguments); err != nil {
|
||||||
|
return FlagError{err}
|
||||||
|
}
|
||||||
|
args := n.set.Args()
|
||||||
|
|
||||||
|
if n.child != nil {
|
||||||
|
if n.f != nil {
|
||||||
|
if n.usage != "" { // root node early special case
|
||||||
|
panic("invalid subcommand tree state")
|
||||||
|
}
|
||||||
|
|
||||||
|
// special case: root node calls HandlerFunc for initialisation
|
||||||
|
if err := n.f(nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
return n.writeHelp()
|
||||||
|
}
|
||||||
|
return n.child.Parse(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.f == nil {
|
||||||
|
n.printf("%q has no subcommands", n.name)
|
||||||
|
return ErrEmptyTree
|
||||||
|
}
|
||||||
|
return n.f(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) printf(format string, a ...any) {
|
||||||
|
if n.logf == nil {
|
||||||
|
log.Printf(format, a...)
|
||||||
|
} else {
|
||||||
|
n.logf(format, a...)
|
||||||
|
}
|
||||||
|
}
|
344
command/parse_test.go
Normal file
344
command/parse_test.go
Normal file
@ -0,0 +1,344 @@
|
|||||||
|
package command_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.gensokyo.uk/security/fortify/command"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParse(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
buildTree func(wout, wlog io.Writer) command.Command
|
||||||
|
args []string
|
||||||
|
want string
|
||||||
|
wantLog string
|
||||||
|
wantErr error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"d=0 empty sub",
|
||||||
|
func(wout, wlog io.Writer) command.Command { return command.New(wout, newLogFunc(wlog), "root", nil) },
|
||||||
|
[]string{""},
|
||||||
|
"", "test: \"root\" has no subcommands\n", command.ErrEmptyTree,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 empty sub garbage",
|
||||||
|
func(wout, wlog io.Writer) command.Command { return command.New(wout, newLogFunc(wlog), "root", nil) },
|
||||||
|
[]string{"a", "b", "c", "d"},
|
||||||
|
"", "test: \"root\" has no subcommands\n", command.ErrEmptyTree,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 no match",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"nonexistent"},
|
||||||
|
"", "test: \"nonexistent\" is not a valid command\n", command.ErrNoMatch,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 direct error",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"error"},
|
||||||
|
"", "", errSuccess,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 direct error garbage",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"error", "0", "1", "2"},
|
||||||
|
"", "", errSuccess,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 direct success out of order",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"succeed"},
|
||||||
|
"", "", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 direct success output",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"print", "0", "1", "2"},
|
||||||
|
"012", "", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 out of order string flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"string", "--string", "64d3b4b7b21788585845060e2199a78f"},
|
||||||
|
"flag provided but not defined: -string\n\nUsage:\ttest string [-h | --help] COMMAND [OPTIONS]\n\n", "",
|
||||||
|
errors.New("flag provided but not defined: -string"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 string flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"--string", "64d3b4b7b21788585845060e2199a78f", "string"},
|
||||||
|
"64d3b4b7b21788585845060e2199a78f", "", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 int flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"--int", "2147483647", "int"},
|
||||||
|
"2147483647", "", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 repeat flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"--repeat", "0", "--repeat", "1", "--repeat", "2", "--repeat", "3", "--repeat", "4", "repeat"},
|
||||||
|
"[0 1 2 3 4]", "", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 bool flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"-v", "succeed"},
|
||||||
|
"", "test: verbose\n", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 bool flag early error",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"--fail", "succeed"},
|
||||||
|
"", "", errSuccess,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"d=1 empty sub",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"empty"},
|
||||||
|
"", "test: \"empty\" has no subcommands\n", command.ErrEmptyTree,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=1 empty sub garbage",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"empty", "a", "b", "c", "d"},
|
||||||
|
"", "test: \"empty\" has no subcommands\n", command.ErrEmptyTree,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=1 empty sub help",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"empty", "-h"},
|
||||||
|
"\nUsage:\ttest empty [-h | --help] COMMAND [OPTIONS]\n\n", "", flag.ErrHelp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=1 no match",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"join", "23aa3bb0", "34986782", "d8859355", "cd9ac317", ", "},
|
||||||
|
"", "test: \"23aa3bb0\" is not a valid command\n", command.ErrNoMatch,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=1 direct success out",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"join", "out", "23aa3bb0", "34986782", "d8859355", "cd9ac317", ", "},
|
||||||
|
"23aa3bb0, 34986782, d8859355, cd9ac317", "", nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=1 direct success log",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"join", "log", "23aa3bb0", "34986782", "d8859355", "cd9ac317", ", "},
|
||||||
|
"", "test: 23aa3bb0, 34986782, d8859355, cd9ac317\n", nil,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"d=4 empty sub",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"deep", "d=2", "d=3", "d=4"},
|
||||||
|
"", "test: \"d=4\" has no subcommands\n", command.ErrEmptyTree},
|
||||||
|
|
||||||
|
{
|
||||||
|
"d=0 help",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{},
|
||||||
|
`
|
||||||
|
Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] [--repeat <value>] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
error return an error
|
||||||
|
print wraps Fprint
|
||||||
|
string print string passed by flag
|
||||||
|
int print int passed by flag
|
||||||
|
repeat print repeated values passed by flag
|
||||||
|
empty empty subcommand
|
||||||
|
join wraps strings.Join
|
||||||
|
succeed this command succeeds
|
||||||
|
deep top level of command tree with various levels
|
||||||
|
|
||||||
|
`, "", command.ErrHelp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=0 help flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"-h"},
|
||||||
|
`
|
||||||
|
Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] [--repeat <value>] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
error return an error
|
||||||
|
print wraps Fprint
|
||||||
|
string print string passed by flag
|
||||||
|
int print int passed by flag
|
||||||
|
repeat print repeated values passed by flag
|
||||||
|
empty empty subcommand
|
||||||
|
join wraps strings.Join
|
||||||
|
succeed this command succeeds
|
||||||
|
deep top level of command tree with various levels
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
-fail
|
||||||
|
fail early
|
||||||
|
-int int
|
||||||
|
store value for the "int" command (default -1)
|
||||||
|
-repeat value
|
||||||
|
store value for the "repeat" command
|
||||||
|
-string string
|
||||||
|
store value for the "string" command (default "default")
|
||||||
|
-v verbose output
|
||||||
|
|
||||||
|
`, "", flag.ErrHelp,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"d=1 help",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"join"},
|
||||||
|
`
|
||||||
|
Usage: test join [-h | --help] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
out write result to wout
|
||||||
|
log log result to wlog
|
||||||
|
|
||||||
|
`, "", command.ErrHelp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=1 help flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"join", "-h"},
|
||||||
|
`
|
||||||
|
Usage: test join [-h | --help] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
out write result to wout
|
||||||
|
log log result to wlog
|
||||||
|
|
||||||
|
`, "", flag.ErrHelp,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"d=2 help",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"deep", "d=2"},
|
||||||
|
`
|
||||||
|
Usage: test deep d=2 [-h | --help] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
d=3 relative third level
|
||||||
|
|
||||||
|
`, "", command.ErrHelp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"d=2 help flag",
|
||||||
|
buildTestCommand,
|
||||||
|
[]string{"deep", "d=2", "-h"},
|
||||||
|
`
|
||||||
|
Usage: test deep d=2 [-h | --help] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
d=3 relative third level
|
||||||
|
|
||||||
|
`, "", flag.ErrHelp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
wout, wlog := new(bytes.Buffer), new(bytes.Buffer)
|
||||||
|
c := tc.buildTree(wout, wlog)
|
||||||
|
|
||||||
|
if err := c.Parse(tc.args); !errors.Is(err, tc.wantErr) {
|
||||||
|
t.Errorf("Parse: error = %v; wantErr %v", err, tc.wantErr)
|
||||||
|
}
|
||||||
|
if got := wout.String(); got != tc.want {
|
||||||
|
t.Errorf("Parse: %s want %s", got, tc.want)
|
||||||
|
}
|
||||||
|
if gotLog := wlog.String(); gotLog != tc.wantLog {
|
||||||
|
t.Errorf("Parse: log = %s wantLog %s", gotLog, tc.wantLog)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
errJoinLen = errors.New("not enough arguments to join")
|
||||||
|
errSuccess = errors.New("success")
|
||||||
|
)
|
||||||
|
|
||||||
|
func buildTestCommand(wout, wlog io.Writer) (c command.Command) {
|
||||||
|
var (
|
||||||
|
flagVerbose bool
|
||||||
|
flagFail bool
|
||||||
|
|
||||||
|
flagString string
|
||||||
|
flagInt int
|
||||||
|
flagRepeat command.RepeatableFlag
|
||||||
|
)
|
||||||
|
|
||||||
|
logf := newLogFunc(wlog)
|
||||||
|
c = command.New(wout, logf, "test", func([]string) error {
|
||||||
|
if flagVerbose {
|
||||||
|
logf("verbose")
|
||||||
|
}
|
||||||
|
if flagFail {
|
||||||
|
return errSuccess
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}).
|
||||||
|
Flag(&flagVerbose, "v", command.BoolFlag(false), "verbose output").
|
||||||
|
Flag(&flagFail, "fail", command.BoolFlag(false), "fail early").
|
||||||
|
Command("error", "return an error", func([]string) error {
|
||||||
|
return errSuccess
|
||||||
|
}).
|
||||||
|
Command("print", "wraps Fprint", func(args []string) error {
|
||||||
|
a := make([]any, len(args))
|
||||||
|
for i, v := range args {
|
||||||
|
a[i] = v
|
||||||
|
}
|
||||||
|
_, err := fmt.Fprint(wout, a...)
|
||||||
|
return err
|
||||||
|
}).
|
||||||
|
Flag(&flagString, "string", command.StringFlag("default"), "store value for the \"string\" command").
|
||||||
|
Command("string", "print string passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagString); return err }).
|
||||||
|
Flag(&flagInt, "int", command.IntFlag(-1), "store value for the \"int\" command").
|
||||||
|
Command("int", "print int passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagInt); return err }).
|
||||||
|
Flag(nil, "repeat", &flagRepeat, "store value for the \"repeat\" command").
|
||||||
|
Command("repeat", "print repeated values passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagRepeat); return err })
|
||||||
|
|
||||||
|
c.New("empty", "empty subcommand")
|
||||||
|
c.New("hidden", command.UsageInternal)
|
||||||
|
|
||||||
|
c.New("join", "wraps strings.Join").
|
||||||
|
Command("out", "write result to wout", func(args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return errJoinLen
|
||||||
|
}
|
||||||
|
_, err := fmt.Fprint(wout, strings.Join(args[:len(args)-1], args[len(args)-1]))
|
||||||
|
return err
|
||||||
|
}).
|
||||||
|
Command("log", "log result to wlog", func(args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return errJoinLen
|
||||||
|
}
|
||||||
|
logf("%s", strings.Join(args[:len(args)-1], args[len(args)-1]))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Command("succeed", "this command succeeds", func([]string) error { return nil })
|
||||||
|
|
||||||
|
c.New("deep", "top level of command tree with various levels").
|
||||||
|
New("d=2", "relative second level").
|
||||||
|
New("d=3", "relative third level").
|
||||||
|
New("d=4", "relative fourth level")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLogFunc(w io.Writer) command.LogFunc { return log.New(w, "test: ", 0).Printf }
|
54
command/unreachable_test.go
Normal file
54
command/unreachable_test.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseUnreachable(t *testing.T) {
|
||||||
|
// top level bypasses name matching and recursive calls to Parse
|
||||||
|
// returns when encountering zero-length args
|
||||||
|
t.Run("zero-length args", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Parse", "attempted to parse with zero length args")
|
||||||
|
_ = newNode(panicWriter{}, nil, " ", " ").Parse(nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
// top level must not have siblings
|
||||||
|
t.Run("toplevel siblings", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Parse", "invalid toplevel state")
|
||||||
|
n := newNode(panicWriter{}, nil, " ", "")
|
||||||
|
n.append(newNode(panicWriter{}, nil, " ", " "))
|
||||||
|
_ = n.Parse(nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
// a node with descendents must not have a direct handler
|
||||||
|
t.Run("sub handle conflict", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Parse", "invalid subcommand tree state")
|
||||||
|
n := newNode(panicWriter{}, nil, " ", " ")
|
||||||
|
n.adopt(newNode(panicWriter{}, nil, " ", " "))
|
||||||
|
n.f = func([]string) error { panic("unreachable") }
|
||||||
|
_ = n.Parse([]string{" "})
|
||||||
|
})
|
||||||
|
|
||||||
|
// this would only happen if a node was matched twice
|
||||||
|
t.Run("parsed flag set", func(t *testing.T) {
|
||||||
|
defer checkRecover(t, "Parse", "invalid set state")
|
||||||
|
n := newNode(panicWriter{}, nil, " ", "")
|
||||||
|
set := flag.NewFlagSet("parsed", flag.ContinueOnError)
|
||||||
|
set.SetOutput(panicWriter{})
|
||||||
|
_ = set.Parse(nil)
|
||||||
|
n.set = set
|
||||||
|
_ = n.Parse(nil)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type panicWriter struct{}
|
||||||
|
|
||||||
|
func (p panicWriter) Write([]byte) (int, error) { panic("unreachable") }
|
||||||
|
|
||||||
|
func checkRecover(t *testing.T, name, wantPanic string) {
|
||||||
|
if r := recover(); r != wantPanic {
|
||||||
|
t.Errorf("%s: panic = %v; wantPanic %v",
|
||||||
|
name, r, wantPanic)
|
||||||
|
}
|
||||||
|
}
|
14
command/wrap.go
Normal file
14
command/wrap.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
// the top level node wants [Command] returned for its builder methods
|
||||||
|
type rootNode struct{ *node }
|
||||||
|
|
||||||
|
func (r rootNode) Command(name, usage string, f HandlerFunc) Command {
|
||||||
|
r.node.Command(name, usage, f)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r rootNode) Flag(p any, name string, value FlagDefiner, usage string) Command {
|
||||||
|
r.node.Flag(p, name, value, usage)
|
||||||
|
return r
|
||||||
|
}
|
23
flake.nix
23
flake.nix
@ -32,7 +32,7 @@
|
|||||||
buildPackage = forAllSystems (
|
buildPackage = forAllSystems (
|
||||||
system:
|
system:
|
||||||
nixpkgsFor.${system}.callPackage (
|
nixpkgsFor.${system}.callPackage (
|
||||||
import ./bundle.nix {
|
import ./cmd/fpkg/build.nix {
|
||||||
inherit
|
inherit
|
||||||
nixpkgsFor
|
nixpkgsFor
|
||||||
system
|
system
|
||||||
@ -57,18 +57,16 @@
|
|||||||
;
|
;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
check-formatting =
|
formatting = runCommandLocal "check-formatting" { nativeBuildInputs = [ nixfmt-rfc-style ]; } ''
|
||||||
runCommandLocal "check-formatting" { nativeBuildInputs = [ nixfmt-rfc-style ]; }
|
cd ${./.}
|
||||||
''
|
|
||||||
cd ${./.}
|
|
||||||
|
|
||||||
echo "running nixfmt..."
|
echo "running nixfmt..."
|
||||||
nixfmt --check .
|
nixfmt --check .
|
||||||
|
|
||||||
touch $out
|
touch $out
|
||||||
'';
|
'';
|
||||||
|
|
||||||
check-lint =
|
lint =
|
||||||
runCommandLocal "check-lint"
|
runCommandLocal "check-lint"
|
||||||
{
|
{
|
||||||
nativeBuildInputs = [
|
nativeBuildInputs = [
|
||||||
@ -88,10 +86,7 @@
|
|||||||
touch $out
|
touch $out
|
||||||
'';
|
'';
|
||||||
|
|
||||||
nixos-tests = callPackage ./test.nix {
|
fortify = callPackage ./tests/fortify { inherit system self; };
|
||||||
inherit system self home-manager;
|
|
||||||
inherit (self.packages.${system}) fortify;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
17
fst/app.go
17
fst/app.go
@ -1,3 +1,4 @@
|
|||||||
|
// Package fst exports shared fortify types.
|
||||||
package fst
|
package fst
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -6,16 +7,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type App interface {
|
type App interface {
|
||||||
// ID returns a copy of App's unique ID.
|
// ID returns a copy of [fst.ID] held by App.
|
||||||
ID() ID
|
ID() ID
|
||||||
// Run sets up the system and runs the App.
|
|
||||||
Run(ctx context.Context, rs *RunState) error
|
|
||||||
|
|
||||||
Seal(config *Config) error
|
// Seal determines the outcome of config as a [SealedApp].
|
||||||
|
// The value of config might be overwritten and must not be used again.
|
||||||
|
Seal(config *Config) (SealedApp, error)
|
||||||
|
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunState stores the outcome of a call to [App.Run].
|
type SealedApp interface {
|
||||||
|
// Run commits sealed system setup and starts the app process.
|
||||||
|
Run(ctx context.Context, rs *RunState) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunState stores the outcome of a call to [SealedApp.Run].
|
||||||
type RunState struct {
|
type RunState struct {
|
||||||
// Time is the exact point in time where the process was created.
|
// Time is the exact point in time where the process was created.
|
||||||
// Location must be set to UTC.
|
// Location must be set to UTC.
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
// Package fst exports shared fortify types.
|
|
||||||
package fst
|
|
@ -2,6 +2,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.gensokyo.uk/security/fortify/fst"
|
"git.gensokyo.uk/security/fortify/fst"
|
||||||
@ -20,15 +21,23 @@ func New(os sys.State) (fst.App, error) {
|
|||||||
return a, err
|
return a, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MustNew(os sys.State) fst.App {
|
||||||
|
a, err := New(os)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("cannot create app: %v", err)
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
type app struct {
|
type app struct {
|
||||||
id *stringPair[fst.ID]
|
id *stringPair[fst.ID]
|
||||||
sys sys.State
|
sys sys.State
|
||||||
|
|
||||||
*appSeal
|
*outcome
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) ID() fst.ID { return a.id.unwrap() }
|
func (a *app) ID() fst.ID { a.mu.RLock(); defer a.mu.RUnlock(); return a.id.unwrap() }
|
||||||
|
|
||||||
func (a *app) String() string {
|
func (a *app) String() string {
|
||||||
if a == nil {
|
if a == nil {
|
||||||
@ -38,32 +47,33 @@ func (a *app) String() string {
|
|||||||
a.mu.RLock()
|
a.mu.RLock()
|
||||||
defer a.mu.RUnlock()
|
defer a.mu.RUnlock()
|
||||||
|
|
||||||
if a.appSeal != nil {
|
if a.outcome != nil {
|
||||||
if a.appSeal.user.uid == nil {
|
if a.outcome.user.uid == nil {
|
||||||
return fmt.Sprintf("(sealed app %s with invalid uid)", a.id)
|
return fmt.Sprintf("(sealed app %s with invalid uid)", a.id)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("(sealed app %s as uid %s)", a.id, a.appSeal.user.uid)
|
return fmt.Sprintf("(sealed app %s as uid %s)", a.id, a.outcome.user.uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("(unsealed app %s)", a.id)
|
return fmt.Sprintf("(unsealed app %s)", a.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) Seal(config *fst.Config) (err error) {
|
func (a *app) Seal(config *fst.Config) (fst.SealedApp, error) {
|
||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
defer a.mu.Unlock()
|
defer a.mu.Unlock()
|
||||||
|
|
||||||
if a.appSeal != nil {
|
if a.outcome != nil {
|
||||||
panic("app sealed twice")
|
panic("app sealed twice")
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
return fmsg.WrapError(ErrConfig,
|
return nil, fmsg.WrapError(ErrConfig,
|
||||||
"attempted to seal app with nil config")
|
"attempted to seal app with nil config")
|
||||||
}
|
}
|
||||||
|
|
||||||
seal := new(appSeal)
|
seal := new(outcome)
|
||||||
err = seal.finalise(a.sys, config, a.id.String())
|
seal.id = a.id
|
||||||
|
err := seal.finalise(a.sys, config)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
a.appSeal = seal
|
a.outcome = seal
|
||||||
}
|
}
|
||||||
return
|
return seal, err
|
||||||
}
|
}
|
||||||
|
@ -29,17 +29,21 @@ func TestApp(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
a := app.NewWithID(tc.id, tc.os)
|
a := app.NewWithID(tc.id, tc.os)
|
||||||
|
var (
|
||||||
|
gotSys *system.I
|
||||||
|
gotBwrap *bwrap.Config
|
||||||
|
)
|
||||||
if !t.Run("seal", func(t *testing.T) {
|
if !t.Run("seal", func(t *testing.T) {
|
||||||
if err := a.Seal(tc.config); err != nil {
|
if sa, err := a.Seal(tc.config); err != nil {
|
||||||
t.Errorf("Seal: error = %v", err)
|
t.Errorf("Seal: error = %v", err)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
gotSys, gotBwrap = app.AppSystemBwrap(a, sa)
|
||||||
}
|
}
|
||||||
}) {
|
}) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gotSys, gotBwrap := app.AppSystemBwrap(a)
|
|
||||||
|
|
||||||
t.Run("compare sys", func(t *testing.T) {
|
t.Run("compare sys", func(t *testing.T) {
|
||||||
if !gotSys.Equal(tc.wantSys) {
|
if !gotSys.Equal(tc.wantSys) {
|
||||||
t.Errorf("Seal: sys = %#v, want %#v",
|
t.Errorf("Seal: sys = %#v, want %#v",
|
||||||
|
@ -14,7 +14,11 @@ func NewWithID(id fst.ID, os sys.State) fst.App {
|
|||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
func AppSystemBwrap(a fst.App) (*system.I, *bwrap.Config) {
|
func AppSystemBwrap(a fst.App, sa fst.SealedApp) (*system.I, *bwrap.Config) {
|
||||||
v := a.(*app)
|
v := a.(*app)
|
||||||
return v.appSeal.sys, v.appSeal.container
|
seal := sa.(*outcome)
|
||||||
|
if v.outcome != seal || v.id != seal.id {
|
||||||
|
panic("broken app/outcome link")
|
||||||
|
}
|
||||||
|
return seal.sys, seal.container
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,16 @@ import (
|
|||||||
|
|
||||||
const shimSetupTimeout = 5 * time.Second
|
const shimSetupTimeout = 5 * time.Second
|
||||||
|
|
||||||
func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
func (seal *outcome) Run(ctx context.Context, rs *fst.RunState) error {
|
||||||
a.mu.Lock()
|
if !seal.f.CompareAndSwap(false, true) {
|
||||||
defer a.mu.Unlock()
|
// run does much more than just starting a process; calling it twice, even if the first call fails, will result
|
||||||
|
// in inconsistent state that is impossible to clean up; return here to limit damage and hopefully give the
|
||||||
|
// other Run a chance to return
|
||||||
|
panic("attempted to run twice")
|
||||||
|
}
|
||||||
|
|
||||||
if rs == nil {
|
if rs == nil {
|
||||||
panic("attempted to pass nil state to run")
|
panic("invalid state")
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -33,8 +37,8 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
shimExec := [2]string{helper.BubblewrapName}
|
shimExec := [2]string{helper.BubblewrapName}
|
||||||
if len(a.appSeal.command) > 0 {
|
if len(seal.command) > 0 {
|
||||||
shimExec[1] = a.appSeal.command[0]
|
shimExec[1] = seal.command[0]
|
||||||
}
|
}
|
||||||
for i, n := range shimExec {
|
for i, n := range shimExec {
|
||||||
if len(n) == 0 {
|
if len(n) == 0 {
|
||||||
@ -54,15 +58,15 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
prepare/revert os state
|
prepare/revert os state
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if err := a.appSeal.sys.Commit(ctx); err != nil {
|
if err := seal.sys.Commit(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
store := state.NewMulti(a.sys.Paths().RunDirPath)
|
store := state.NewMulti(seal.runDirPath)
|
||||||
deferredStoreFunc := func(c state.Cursor) error { return nil }
|
deferredStoreFunc := func(c state.Cursor) error { return nil }
|
||||||
defer func() {
|
defer func() {
|
||||||
var revertErr error
|
var revertErr error
|
||||||
storeErr := new(StateStoreError)
|
storeErr := new(StateStoreError)
|
||||||
storeErr.Inner, storeErr.DoErr = store.Do(a.appSeal.user.aid.unwrap(), func(c state.Cursor) {
|
storeErr.Inner, storeErr.DoErr = store.Do(seal.user.aid.unwrap(), func(c state.Cursor) {
|
||||||
revertErr = func() error {
|
revertErr = func() error {
|
||||||
storeErr.InnerErr = deferredStoreFunc(c)
|
storeErr.InnerErr = deferredStoreFunc(c)
|
||||||
|
|
||||||
@ -75,7 +79,7 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
ec.Set(system.Process)
|
ec.Set(system.Process)
|
||||||
if states, err := c.Load(); err != nil {
|
if states, err := c.Load(); err != nil {
|
||||||
// revert per-process state here to limit damage
|
// revert per-process state here to limit damage
|
||||||
return errors.Join(err, a.appSeal.sys.Revert(ec))
|
return errors.Join(err, seal.sys.Revert(ec))
|
||||||
} else {
|
} else {
|
||||||
if l := len(states); l == 0 {
|
if l := len(states); l == 0 {
|
||||||
fmsg.Verbose("no other launchers active, will clean up globals")
|
fmsg.Verbose("no other launchers active, will clean up globals")
|
||||||
@ -111,7 +115,7 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := a.appSeal.sys.Revert(ec)
|
err := seal.sys.Revert(ec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = err.(RevertCompoundError)
|
err = err.(RevertCompoundError)
|
||||||
}
|
}
|
||||||
@ -129,9 +133,9 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
waitErr := make(chan error, 1)
|
waitErr := make(chan error, 1)
|
||||||
cmd := new(shim.Shim)
|
cmd := new(shim.Shim)
|
||||||
if startTime, err := cmd.Start(
|
if startTime, err := cmd.Start(
|
||||||
a.appSeal.user.aid.String(),
|
seal.user.aid.String(),
|
||||||
a.appSeal.user.supp,
|
seal.user.supp,
|
||||||
a.appSeal.bwrapSync,
|
seal.bwrapSync,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
@ -139,20 +143,20 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
rs.Time = startTime
|
rs.Time = startTime
|
||||||
}
|
}
|
||||||
|
|
||||||
shimSetupCtx, shimSetupCancel := context.WithDeadline(ctx, time.Now().Add(shimSetupTimeout))
|
c, cancel := context.WithTimeout(ctx, shimSetupTimeout)
|
||||||
defer shimSetupCancel()
|
defer cancel()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
waitErr <- cmd.Unwrap().Wait()
|
waitErr <- cmd.Unwrap().Wait()
|
||||||
// cancel shim setup in case shim died before receiving payload
|
// cancel shim setup in case shim died before receiving payload
|
||||||
shimSetupCancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := cmd.Serve(shimSetupCtx, &shim.Payload{
|
if err := cmd.Serve(c, &shim.Payload{
|
||||||
Argv: a.appSeal.command,
|
Argv: seal.command,
|
||||||
Exec: shimExec,
|
Exec: shimExec,
|
||||||
Bwrap: a.appSeal.container,
|
Bwrap: seal.container,
|
||||||
Home: a.appSeal.user.data,
|
Home: seal.user.data,
|
||||||
|
|
||||||
Verbose: fmsg.Load(),
|
Verbose: fmsg.Load(),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
@ -161,14 +165,14 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
|
|
||||||
// shim accepted setup payload, create process state
|
// shim accepted setup payload, create process state
|
||||||
sd := state.State{
|
sd := state.State{
|
||||||
ID: a.id.unwrap(),
|
ID: seal.id.unwrap(),
|
||||||
PID: cmd.Unwrap().Process.Pid,
|
PID: cmd.Unwrap().Process.Pid,
|
||||||
Time: *rs.Time,
|
Time: *rs.Time,
|
||||||
}
|
}
|
||||||
var earlyStoreErr = new(StateStoreError) // returned after blocking on waitErr
|
var earlyStoreErr = new(StateStoreError) // returned after blocking on waitErr
|
||||||
earlyStoreErr.Inner, earlyStoreErr.DoErr = store.Do(a.appSeal.user.aid.unwrap(), func(c state.Cursor) { earlyStoreErr.InnerErr = c.Save(&sd, a.appSeal.ct) })
|
earlyStoreErr.Inner, earlyStoreErr.DoErr = store.Do(seal.user.aid.unwrap(), func(c state.Cursor) { earlyStoreErr.InnerErr = c.Save(&sd, seal.ct) })
|
||||||
// destroy defunct state entry
|
// destroy defunct state entry
|
||||||
deferredStoreFunc = func(c state.Cursor) error { return c.Destroy(a.id.unwrap()) }
|
deferredStoreFunc = func(c state.Cursor) error { return c.Destroy(seal.id.unwrap()) }
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case err := <-waitErr: // block until fsu/shim returns
|
case err := <-waitErr: // block until fsu/shim returns
|
||||||
@ -201,9 +205,9 @@ func (a *app) Run(ctx context.Context, rs *fst.RunState) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmsg.Resume()
|
fmsg.Resume()
|
||||||
if a.appSeal.dbusMsg != nil {
|
if seal.dbusMsg != nil {
|
||||||
// dump dbus message buffer
|
// dump dbus message buffer
|
||||||
a.appSeal.dbusMsg()
|
seal.dbusMsg()
|
||||||
}
|
}
|
||||||
|
|
||||||
return earlyStoreErr.equiv("cannot save process state:")
|
return earlyStoreErr.equiv("cannot save process state:")
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"git.gensokyo.uk/security/fortify/acl"
|
"git.gensokyo.uk/security/fortify/acl"
|
||||||
"git.gensokyo.uk/security/fortify/dbus"
|
"git.gensokyo.uk/security/fortify/dbus"
|
||||||
@ -57,8 +58,13 @@ var (
|
|||||||
|
|
||||||
var posixUsername = regexp.MustCompilePOSIX("^[a-z_]([A-Za-z0-9_-]{0,31}|[A-Za-z0-9_-]{0,30}\\$)$")
|
var posixUsername = regexp.MustCompilePOSIX("^[a-z_]([A-Za-z0-9_-]{0,31}|[A-Za-z0-9_-]{0,30}\\$)$")
|
||||||
|
|
||||||
// appSeal stores copies of various parts of [fst.Config]
|
// outcome stores copies of various parts of [fst.Config]
|
||||||
type appSeal struct {
|
type outcome struct {
|
||||||
|
// copied from initialising [app]
|
||||||
|
id *stringPair[fst.ID]
|
||||||
|
// copied from [sys.State] response
|
||||||
|
runDirPath string
|
||||||
|
|
||||||
// passed through from [fst.Config]
|
// passed through from [fst.Config]
|
||||||
command []string
|
command []string
|
||||||
|
|
||||||
@ -68,16 +74,16 @@ type appSeal struct {
|
|||||||
// dump dbus proxy message buffer
|
// dump dbus proxy message buffer
|
||||||
dbusMsg func()
|
dbusMsg func()
|
||||||
|
|
||||||
user appUser
|
user fsuUser
|
||||||
sys *system.I
|
sys *system.I
|
||||||
container *bwrap.Config
|
container *bwrap.Config
|
||||||
bwrapSync *os.File
|
bwrapSync *os.File
|
||||||
|
|
||||||
// protected by upstream mutex
|
f atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// appUser stores post-fsu credentials and metadata
|
// fsuUser stores post-fsu credentials and metadata
|
||||||
type appUser struct {
|
type fsuUser struct {
|
||||||
// application id
|
// application id
|
||||||
aid *stringPair[int]
|
aid *stringPair[int]
|
||||||
// target uid resolved by fid:aid
|
// target uid resolved by fid:aid
|
||||||
@ -94,7 +100,7 @@ type appUser struct {
|
|||||||
username string
|
username string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (seal *appSeal) finalise(sys sys.State, config *fst.Config, id string) error {
|
func (seal *outcome) finalise(sys sys.State, config *fst.Config) error {
|
||||||
{
|
{
|
||||||
// encode initial configuration for state tracking
|
// encode initial configuration for state tracking
|
||||||
ct := new(bytes.Buffer)
|
ct := new(bytes.Buffer)
|
||||||
@ -118,7 +124,7 @@ func (seal *appSeal) finalise(sys sys.State, config *fst.Config, id string) erro
|
|||||||
Resolve post-fsu user state
|
Resolve post-fsu user state
|
||||||
*/
|
*/
|
||||||
|
|
||||||
seal.user = appUser{
|
seal.user = fsuUser{
|
||||||
aid: newInt(config.Confinement.AppID),
|
aid: newInt(config.Confinement.AppID),
|
||||||
data: config.Confinement.Outer,
|
data: config.Confinement.Outer,
|
||||||
home: config.Confinement.Inner,
|
home: config.Confinement.Inner,
|
||||||
@ -223,6 +229,7 @@ func (seal *appSeal) finalise(sys sys.State, config *fst.Config, id string) erro
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
sc := sys.Paths()
|
sc := sys.Paths()
|
||||||
|
seal.runDirPath = sc.RunDirPath
|
||||||
seal.sys = system.New(seal.user.uid.unwrap())
|
seal.sys = system.New(seal.user.uid.unwrap())
|
||||||
seal.sys.IsVerbose = fmsg.Load
|
seal.sys.IsVerbose = fmsg.Load
|
||||||
seal.sys.Verbose = fmsg.Verbose
|
seal.sys.Verbose = fmsg.Verbose
|
||||||
@ -243,10 +250,10 @@ func (seal *appSeal) finalise(sys sys.State, config *fst.Config, id string) erro
|
|||||||
seal.sys.UpdatePermType(system.User, sc.RuntimePath, acl.Execute)
|
seal.sys.UpdatePermType(system.User, sc.RuntimePath, acl.Execute)
|
||||||
|
|
||||||
// outer process-specific share directory
|
// outer process-specific share directory
|
||||||
sharePath := path.Join(sc.SharePath, id)
|
sharePath := path.Join(sc.SharePath, seal.id.String())
|
||||||
seal.sys.Ephemeral(system.Process, sharePath, 0711)
|
seal.sys.Ephemeral(system.Process, sharePath, 0711)
|
||||||
// similar to share but within XDG_RUNTIME_DIR
|
// similar to share but within XDG_RUNTIME_DIR
|
||||||
sharePathLocal := path.Join(sc.RunDirPath, id)
|
sharePathLocal := path.Join(sc.RunDirPath, seal.id.String())
|
||||||
seal.sys.Ephemeral(system.Process, sharePathLocal, 0700)
|
seal.sys.Ephemeral(system.Process, sharePathLocal, 0700)
|
||||||
seal.sys.UpdatePerm(sharePathLocal, acl.Execute)
|
seal.sys.UpdatePerm(sharePathLocal, acl.Execute)
|
||||||
|
|
||||||
@ -327,14 +334,14 @@ func (seal *appSeal) finalise(sys sys.State, config *fst.Config, id string) erro
|
|||||||
|
|
||||||
if !config.Confinement.Sandbox.DirectWayland { // set up security-context-v1
|
if !config.Confinement.Sandbox.DirectWayland { // set up security-context-v1
|
||||||
socketDir := path.Join(sc.SharePath, "wayland")
|
socketDir := path.Join(sc.SharePath, "wayland")
|
||||||
outerPath := path.Join(socketDir, id)
|
outerPath := path.Join(socketDir, seal.id.String())
|
||||||
seal.sys.Ensure(socketDir, 0711)
|
seal.sys.Ensure(socketDir, 0711)
|
||||||
appID := config.ID
|
appID := config.ID
|
||||||
if appID == "" {
|
if appID == "" {
|
||||||
// use instance ID in case app id is not set
|
// use instance ID in case app id is not set
|
||||||
appID = "uk.gensokyo.fortify." + id
|
appID = "uk.gensokyo.fortify." + seal.id.String()
|
||||||
}
|
}
|
||||||
seal.sys.Wayland(&seal.bwrapSync, outerPath, socketPath, appID, id)
|
seal.sys.Wayland(&seal.bwrapSync, outerPath, socketPath, appID, seal.id.String())
|
||||||
seal.container.Bind(outerPath, innerPath)
|
seal.container.Bind(outerPath, innerPath)
|
||||||
} else { // bind mount wayland socket (insecure)
|
} else { // bind mount wayland socket (insecure)
|
||||||
fmsg.Verbose("direct wayland access, PROCEED WITH CAUTION")
|
fmsg.Verbose("direct wayland access, PROCEED WITH CAUTION")
|
||||||
|
433
main.go
433
main.go
@ -3,19 +3,19 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"flag"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"text/tabwriter"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.gensokyo.uk/security/fortify/command"
|
||||||
"git.gensokyo.uk/security/fortify/dbus"
|
"git.gensokyo.uk/security/fortify/dbus"
|
||||||
"git.gensokyo.uk/security/fortify/fst"
|
"git.gensokyo.uk/security/fortify/fst"
|
||||||
"git.gensokyo.uk/security/fortify/helper/seccomp"
|
"git.gensokyo.uk/security/fortify/helper/seccomp"
|
||||||
@ -30,36 +30,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flagVerbose bool
|
errSuccess = errors.New("success")
|
||||||
flagJSON bool
|
|
||||||
|
|
||||||
//go:embed LICENSE
|
//go:embed LICENSE
|
||||||
license string
|
license string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() { fmsg.Prepare("fortify") }
|
||||||
fmsg.Prepare("fortify")
|
|
||||||
|
|
||||||
flag.BoolVar(&flagVerbose, "v", false, "Verbose output")
|
|
||||||
flag.BoolVar(&flagJSON, "json", false, "Format output in JSON when applicable")
|
|
||||||
}
|
|
||||||
|
|
||||||
var std sys.State = new(sys.Std)
|
var std sys.State = new(sys.Std)
|
||||||
|
|
||||||
type gl []string
|
|
||||||
|
|
||||||
func (g *gl) String() string {
|
|
||||||
if g == nil {
|
|
||||||
return "<nil>"
|
|
||||||
}
|
|
||||||
return strings.Join(*g, " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *gl) Set(v string) error {
|
|
||||||
*g = append(*g, v)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// early init argv0 check, skips root check and duplicate PR_SET_DUMPABLE
|
// early init argv0 check, skips root check and duplicate PR_SET_DUMPABLE
|
||||||
init0.TryArgv0()
|
init0.TryArgv0()
|
||||||
@ -73,112 +53,56 @@ func main() {
|
|||||||
log.Fatal("this program must not run as root")
|
log.Fatal("this program must not run as root")
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.CommandLine.Usage = func() {
|
err := buildCommand(os.Stderr).Parse(os.Args[1:])
|
||||||
fmt.Println()
|
if errors.Is(err, errSuccess) || errors.Is(err, command.ErrHelp) {
|
||||||
fmt.Println("Usage:\tfortify [-v] [--json] COMMAND [OPTIONS]")
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println("Commands:")
|
|
||||||
w := tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0)
|
|
||||||
commands := [][2]string{
|
|
||||||
{"app", "Launch app defined by the specified config file"},
|
|
||||||
{"run", "Configure and start a permissive default sandbox"},
|
|
||||||
{"show", "Show the contents of an app configuration"},
|
|
||||||
{"ps", "List active apps and their state"},
|
|
||||||
{"version", "Show fortify version"},
|
|
||||||
{"license", "Show full license text"},
|
|
||||||
{"template", "Produce a config template"},
|
|
||||||
{"help", "Show this help message"},
|
|
||||||
}
|
|
||||||
for _, c := range commands {
|
|
||||||
_, _ = fmt.Fprintf(w, "\t%s\t%s\n", c[0], c[1])
|
|
||||||
}
|
|
||||||
if err := w.Flush(); err != nil {
|
|
||||||
fmt.Printf("fortify: cannot write command list: %v\n", err)
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
flag.Parse()
|
|
||||||
fmsg.Store(flagVerbose)
|
|
||||||
|
|
||||||
args := flag.Args()
|
|
||||||
if len(args) == 0 {
|
|
||||||
flag.CommandLine.Usage()
|
|
||||||
internal.Exit(0)
|
internal.Exit(0)
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
if errors.Is(err, command.ErrNoMatch) || errors.Is(err, command.ErrEmptyTree) {
|
||||||
|
internal.Exit(1)
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
log.Fatal("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch args[0] {
|
var flagError command.FlagError
|
||||||
case "version": // print version string
|
if !errors.As(err, &flagError) {
|
||||||
if v, ok := internal.Check(internal.Version); ok {
|
log.Printf("command: %v", err)
|
||||||
fmt.Println(v)
|
internal.Exit(1)
|
||||||
} else {
|
panic("unreachable")
|
||||||
fmt.Println("impure")
|
}
|
||||||
}
|
fmsg.Verbose(flagError.Error())
|
||||||
|
if flagError.Success() {
|
||||||
internal.Exit(0)
|
internal.Exit(0)
|
||||||
|
}
|
||||||
|
internal.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
case "license": // print embedded license
|
func buildCommand(out io.Writer) command.Command {
|
||||||
fmt.Println(license)
|
var (
|
||||||
internal.Exit(0)
|
flagVerbose bool
|
||||||
|
flagJSON bool
|
||||||
|
)
|
||||||
|
c := command.New(out, log.Printf, "fortify", func([]string) error { fmsg.Store(flagVerbose); return nil }).
|
||||||
|
Flag(&flagVerbose, "v", command.BoolFlag(false), "Print debug messages to the console").
|
||||||
|
Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output as JSON when applicable")
|
||||||
|
|
||||||
case "template": // print full template configuration
|
c.Command("app", "Launch app defined by the specified config file", func(args []string) error {
|
||||||
printJSON(os.Stdout, false, fst.Template())
|
if len(args) < 1 {
|
||||||
internal.Exit(0)
|
|
||||||
|
|
||||||
case "help": // print help message
|
|
||||||
flag.CommandLine.Usage()
|
|
||||||
internal.Exit(0)
|
|
||||||
|
|
||||||
case "ps": // print all state info
|
|
||||||
set := flag.NewFlagSet("ps", flag.ExitOnError)
|
|
||||||
var short bool
|
|
||||||
set.BoolVar(&short, "short", false, "Print instance id")
|
|
||||||
|
|
||||||
// Ignore errors; set is set for ExitOnError.
|
|
||||||
_ = set.Parse(args[1:])
|
|
||||||
|
|
||||||
printPs(os.Stdout, time.Now().UTC(), state.NewMulti(std.Paths().RunDirPath), short)
|
|
||||||
internal.Exit(0)
|
|
||||||
|
|
||||||
case "show": // pretty-print app info
|
|
||||||
set := flag.NewFlagSet("show", flag.ExitOnError)
|
|
||||||
var short bool
|
|
||||||
set.BoolVar(&short, "short", false, "Omit filesystem information")
|
|
||||||
|
|
||||||
// Ignore errors; set is set for ExitOnError.
|
|
||||||
_ = set.Parse(args[1:])
|
|
||||||
|
|
||||||
switch len(set.Args()) {
|
|
||||||
case 0: // system
|
|
||||||
printShowSystem(os.Stdout, short)
|
|
||||||
|
|
||||||
case 1: // instance
|
|
||||||
name := set.Args()[0]
|
|
||||||
config, instance := tryShort(name)
|
|
||||||
if config == nil {
|
|
||||||
config = tryPath(name)
|
|
||||||
}
|
|
||||||
printShowInstance(os.Stdout, time.Now().UTC(), instance, config, short)
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Fatal("show requires 1 argument")
|
|
||||||
}
|
|
||||||
internal.Exit(0)
|
|
||||||
|
|
||||||
case "app": // launch app from configuration file
|
|
||||||
if len(args) < 2 {
|
|
||||||
log.Fatal("app requires at least 1 argument")
|
log.Fatal("app requires at least 1 argument")
|
||||||
}
|
}
|
||||||
|
|
||||||
// config extraArgs...
|
// config extraArgs...
|
||||||
config := tryPath(args[1])
|
config := tryPath(args[0])
|
||||||
config.Command = append(config.Command, args[2:]...)
|
config.Command = append(config.Command, args[1:]...)
|
||||||
|
|
||||||
// invoke app
|
// invoke app
|
||||||
runApp(config)
|
runApp(app.MustNew(std), config)
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
|
})
|
||||||
|
|
||||||
case "run": // run app in permissive defaults usage pattern
|
{
|
||||||
set := flag.NewFlagSet("run", flag.ExitOnError)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dbusConfigSession string
|
dbusConfigSession string
|
||||||
dbusConfigSystem string
|
dbusConfigSystem string
|
||||||
@ -187,138 +111,191 @@ func main() {
|
|||||||
|
|
||||||
fid string
|
fid string
|
||||||
aid int
|
aid int
|
||||||
groups gl
|
groups command.RepeatableFlag
|
||||||
homeDir string
|
homeDir string
|
||||||
userName string
|
userName string
|
||||||
enablements [system.ELen]bool
|
enablements [system.ELen]bool
|
||||||
)
|
)
|
||||||
|
|
||||||
set.StringVar(&dbusConfigSession, "dbus-config", "builtin", "Path to D-Bus proxy config file, or \"builtin\" for defaults")
|
c.NewCommand("run", "Configure and start a permissive default sandbox", func(args []string) error {
|
||||||
set.StringVar(&dbusConfigSystem, "dbus-system", "nil", "Path to system D-Bus proxy config file, or \"nil\" to disable")
|
// initialise config from flags
|
||||||
set.BoolVar(&mpris, "mpris", false, "Allow owning MPRIS D-Bus path, has no effect if custom config is available")
|
config := &fst.Config{
|
||||||
set.BoolVar(&dbusVerbose, "dbus-log", false, "Force logging in the D-Bus proxy")
|
ID: fid,
|
||||||
|
Command: args,
|
||||||
|
}
|
||||||
|
|
||||||
set.StringVar(&fid, "id", "", "App ID, leave empty to disable security context app_id")
|
if aid < 0 || aid > 9999 {
|
||||||
set.IntVar(&aid, "a", 0, "Fortify application ID")
|
log.Fatalf("aid %d out of range", aid)
|
||||||
set.Var(&groups, "g", "Groups inherited by the app process")
|
}
|
||||||
set.StringVar(&homeDir, "d", "os", "Application home directory")
|
|
||||||
set.StringVar(&userName, "u", "chronos", "Passwd name within sandbox")
|
|
||||||
set.BoolVar(&enablements[system.EWayland], "wayland", false, "Allow Wayland connections")
|
|
||||||
set.BoolVar(&enablements[system.EX11], "X", false, "Share X11 socket and allow connection")
|
|
||||||
set.BoolVar(&enablements[system.EDBus], "dbus", false, "Proxy D-Bus connection")
|
|
||||||
set.BoolVar(&enablements[system.EPulse], "pulse", false, "Share PulseAudio socket and cookie")
|
|
||||||
|
|
||||||
// Ignore errors; set is set for ExitOnError.
|
// resolve home/username from os when flag is unset
|
||||||
_ = set.Parse(args[1:])
|
var (
|
||||||
|
passwd *user.User
|
||||||
// initialise config from flags
|
passwdOnce sync.Once
|
||||||
config := &fst.Config{
|
passwdFunc = func() {
|
||||||
ID: fid,
|
var us string
|
||||||
Command: set.Args(),
|
if uid, err := std.Uid(aid); err != nil {
|
||||||
}
|
fmsg.PrintBaseError(err, "cannot obtain uid from fsu:")
|
||||||
|
os.Exit(1)
|
||||||
if aid < 0 || aid > 9999 {
|
} else {
|
||||||
log.Fatalf("aid %d out of range", aid)
|
us = strconv.Itoa(uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolve home/username from os when flag is unset
|
if u, err := user.LookupId(us); err != nil {
|
||||||
var (
|
fmsg.Verbosef("cannot look up uid %s", us)
|
||||||
passwd *user.User
|
passwd = &user.User{
|
||||||
passwdOnce sync.Once
|
Uid: us,
|
||||||
passwdFunc = func() {
|
Gid: us,
|
||||||
var us string
|
Username: "chronos",
|
||||||
if uid, err := std.Uid(aid); err != nil {
|
Name: "Fortify",
|
||||||
fmsg.PrintBaseError(err, "cannot obtain uid from fsu:")
|
HomeDir: "/var/empty",
|
||||||
os.Exit(1)
|
}
|
||||||
} else {
|
} else {
|
||||||
us = strconv.Itoa(uid)
|
passwd = u
|
||||||
}
|
|
||||||
|
|
||||||
if u, err := user.LookupId(us); err != nil {
|
|
||||||
fmsg.Verbosef("cannot look up uid %s", us)
|
|
||||||
passwd = &user.User{
|
|
||||||
Uid: us,
|
|
||||||
Gid: us,
|
|
||||||
Username: "chronos",
|
|
||||||
Name: "Fortify",
|
|
||||||
HomeDir: "/var/empty",
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
passwd = u
|
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if homeDir == "os" {
|
||||||
|
passwdOnce.Do(passwdFunc)
|
||||||
|
homeDir = passwd.HomeDir
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
if homeDir == "os" {
|
if userName == "chronos" {
|
||||||
passwdOnce.Do(passwdFunc)
|
passwdOnce.Do(passwdFunc)
|
||||||
homeDir = passwd.HomeDir
|
userName = passwd.Username
|
||||||
}
|
|
||||||
|
|
||||||
if userName == "chronos" {
|
|
||||||
passwdOnce.Do(passwdFunc)
|
|
||||||
userName = passwd.Username
|
|
||||||
}
|
|
||||||
|
|
||||||
config.Confinement.AppID = aid
|
|
||||||
config.Confinement.Groups = groups
|
|
||||||
config.Confinement.Outer = homeDir
|
|
||||||
config.Confinement.Username = userName
|
|
||||||
|
|
||||||
// enablements from flags
|
|
||||||
for i := system.Enablement(0); i < system.Enablement(system.ELen); i++ {
|
|
||||||
if enablements[i] {
|
|
||||||
config.Confinement.Enablements.Set(i)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// parse D-Bus config file from flags if applicable
|
config.Confinement.AppID = aid
|
||||||
if enablements[system.EDBus] {
|
config.Confinement.Groups = groups
|
||||||
if dbusConfigSession == "builtin" {
|
config.Confinement.Outer = homeDir
|
||||||
config.Confinement.SessionBus = dbus.NewConfig(fid, true, mpris)
|
config.Confinement.Username = userName
|
||||||
} else {
|
|
||||||
if c, err := dbus.NewConfigFromFile(dbusConfigSession); err != nil {
|
// enablements from flags
|
||||||
log.Fatalf("cannot load session bus proxy config from %q: %s", dbusConfigSession, err)
|
for i := system.Enablement(0); i < system.Enablement(system.ELen); i++ {
|
||||||
} else {
|
if enablements[i] {
|
||||||
config.Confinement.SessionBus = c
|
config.Confinement.Enablements.Set(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// system bus proxy is optional
|
// parse D-Bus config file from flags if applicable
|
||||||
if dbusConfigSystem != "nil" {
|
if enablements[system.EDBus] {
|
||||||
if c, err := dbus.NewConfigFromFile(dbusConfigSystem); err != nil {
|
if dbusConfigSession == "builtin" {
|
||||||
log.Fatalf("cannot load system bus proxy config from %q: %s", dbusConfigSystem, err)
|
config.Confinement.SessionBus = dbus.NewConfig(fid, true, mpris)
|
||||||
} else {
|
} else {
|
||||||
config.Confinement.SystemBus = c
|
if conf, err := dbus.NewConfigFromFile(dbusConfigSession); err != nil {
|
||||||
|
log.Fatalf("cannot load session bus proxy config from %q: %s", dbusConfigSession, err)
|
||||||
|
} else {
|
||||||
|
config.Confinement.SessionBus = conf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// system bus proxy is optional
|
||||||
|
if dbusConfigSystem != "nil" {
|
||||||
|
if conf, err := dbus.NewConfigFromFile(dbusConfigSystem); err != nil {
|
||||||
|
log.Fatalf("cannot load system bus proxy config from %q: %s", dbusConfigSystem, err)
|
||||||
|
} else {
|
||||||
|
config.Confinement.SystemBus = conf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// override log from configuration
|
||||||
|
if dbusVerbose {
|
||||||
|
config.Confinement.SessionBus.Log = true
|
||||||
|
config.Confinement.SystemBus.Log = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// override log from configuration
|
// invoke app
|
||||||
if dbusVerbose {
|
runApp(app.MustNew(std), config)
|
||||||
config.Confinement.SessionBus.Log = true
|
panic("unreachable")
|
||||||
config.Confinement.SystemBus.Log = true
|
}).
|
||||||
}
|
Flag(&dbusConfigSession, "dbus-config", command.StringFlag("builtin"),
|
||||||
}
|
"Path to D-Bus proxy config file, or \"builtin\" for defaults").
|
||||||
|
Flag(&dbusConfigSystem, "dbus-system", command.StringFlag("nil"),
|
||||||
// invoke app
|
"Path to system D-Bus proxy config file, or \"nil\" to disable").
|
||||||
runApp(config)
|
Flag(&mpris, "mpris", command.BoolFlag(false),
|
||||||
panic("unreachable")
|
"Allow owning MPRIS D-Bus path, has no effect if custom config is available").
|
||||||
|
Flag(&dbusVerbose, "dbus-log", command.BoolFlag(false),
|
||||||
// internal commands
|
"Force logging in the D-Bus proxy").
|
||||||
case "shim":
|
Flag(&fid, "id", command.StringFlag(""),
|
||||||
shim.Main()
|
"App ID, leave empty to disable security context app_id").
|
||||||
internal.Exit(0)
|
Flag(&aid, "a", command.IntFlag(0),
|
||||||
case "init":
|
"Fortify application ID").
|
||||||
init0.Main()
|
Flag(nil, "g", &groups,
|
||||||
internal.Exit(0)
|
"Groups inherited by the app process").
|
||||||
|
Flag(&homeDir, "d", command.StringFlag("os"),
|
||||||
default:
|
"Application home directory").
|
||||||
log.Fatalf("%q is not a valid command", args[0])
|
Flag(&userName, "u", command.StringFlag("chronos"),
|
||||||
|
"Passwd name within sandbox").
|
||||||
|
Flag(&enablements[system.EWayland], "wayland", command.BoolFlag(false),
|
||||||
|
"Allow Wayland connections").
|
||||||
|
Flag(&enablements[system.EX11], "X", command.BoolFlag(false),
|
||||||
|
"Share X11 socket and allow connection").
|
||||||
|
Flag(&enablements[system.EDBus], "dbus", command.BoolFlag(false),
|
||||||
|
"Proxy D-Bus connection").
|
||||||
|
Flag(&enablements[system.EPulse], "pulse", command.BoolFlag(false),
|
||||||
|
"Share PulseAudio socket and cookie")
|
||||||
}
|
}
|
||||||
|
|
||||||
panic("unreachable")
|
var showFlagShort bool
|
||||||
|
c.NewCommand("show", "Show the contents of an app configuration", func(args []string) error {
|
||||||
|
switch len(args) {
|
||||||
|
case 0: // system
|
||||||
|
printShowSystem(os.Stdout, showFlagShort, flagJSON)
|
||||||
|
|
||||||
|
case 1: // instance
|
||||||
|
name := args[0]
|
||||||
|
config, instance := tryShort(name)
|
||||||
|
if config == nil {
|
||||||
|
config = tryPath(name)
|
||||||
|
}
|
||||||
|
printShowInstance(os.Stdout, time.Now().UTC(), instance, config, showFlagShort, flagJSON)
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Fatal("show requires 1 argument")
|
||||||
|
}
|
||||||
|
return errSuccess
|
||||||
|
}).Flag(&showFlagShort, "short", command.BoolFlag(false), "Omit filesystem information")
|
||||||
|
|
||||||
|
var psFlagShort bool
|
||||||
|
c.NewCommand("ps", "List active apps and their state", func(args []string) error {
|
||||||
|
printPs(os.Stdout, time.Now().UTC(), state.NewMulti(std.Paths().RunDirPath), psFlagShort, flagJSON)
|
||||||
|
return errSuccess
|
||||||
|
}).Flag(&psFlagShort, "short", command.BoolFlag(false), "Print instance id")
|
||||||
|
|
||||||
|
c.Command("version", "Show fortify version", func(args []string) error {
|
||||||
|
if v, ok := internal.Check(internal.Version); ok {
|
||||||
|
fmt.Println(v)
|
||||||
|
} else {
|
||||||
|
fmt.Println("impure")
|
||||||
|
}
|
||||||
|
return errSuccess
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Command("license", "Show full license text", func(args []string) error {
|
||||||
|
fmt.Println(license)
|
||||||
|
return errSuccess
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Command("template", "Produce a config template", func(args []string) error {
|
||||||
|
printJSON(os.Stdout, false, fst.Template())
|
||||||
|
return errSuccess
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Command("help", "Show this help message", func([]string) error {
|
||||||
|
c.PrintHelp()
|
||||||
|
return errSuccess
|
||||||
|
})
|
||||||
|
|
||||||
|
// internal commands
|
||||||
|
c.Command("shim", command.UsageInternal, func([]string) error { shim.Main(); return errSuccess })
|
||||||
|
c.Command("init", command.UsageInternal, func([]string) error { init0.Main(); return errSuccess })
|
||||||
|
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func runApp(config *fst.Config) {
|
func runApp(a fst.App, config *fst.Config) {
|
||||||
rs := new(fst.RunState)
|
rs := new(fst.RunState)
|
||||||
ctx, stop := signal.NotifyContext(context.Background(),
|
ctx, stop := signal.NotifyContext(context.Background(),
|
||||||
syscall.SIGINT, syscall.SIGTERM)
|
syscall.SIGINT, syscall.SIGTERM)
|
||||||
@ -328,12 +305,10 @@ func runApp(config *fst.Config) {
|
|||||||
seccomp.CPrintln = log.Println
|
seccomp.CPrintln = log.Println
|
||||||
}
|
}
|
||||||
|
|
||||||
if a, err := app.New(std); err != nil {
|
if sa, err := a.Seal(config); err != nil {
|
||||||
log.Fatalf("cannot create app: %s", err)
|
|
||||||
} else if err = a.Seal(config); err != nil {
|
|
||||||
fmsg.PrintBaseError(err, "cannot seal app:")
|
fmsg.PrintBaseError(err, "cannot seal app:")
|
||||||
internal.Exit(1)
|
internal.Exit(1)
|
||||||
} else if err = a.Run(ctx, rs); err != nil {
|
} else if err = sa.Run(ctx, rs); err != nil {
|
||||||
if rs.Time == nil {
|
if rs.Time == nil {
|
||||||
fmsg.PrintBaseError(err, "cannot start app:")
|
fmsg.PrintBaseError(err, "cannot start app:")
|
||||||
} else {
|
} else {
|
||||||
|
81
main_test.go
Normal file
81
main_test.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.gensokyo.uk/security/fortify/command"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHelp(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"main", []string{}, `
|
||||||
|
Usage: fortify [-h | --help] [-v] [--json] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
app Launch app defined by the specified config file
|
||||||
|
run Configure and start a permissive default sandbox
|
||||||
|
show Show the contents of an app configuration
|
||||||
|
ps List active apps and their state
|
||||||
|
version Show fortify version
|
||||||
|
license Show full license text
|
||||||
|
template Produce a config template
|
||||||
|
help Show this help message
|
||||||
|
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"run", []string{"run", "-h"}, `
|
||||||
|
Usage: fortify run [-h | --help] [--dbus-config <value>] [--dbus-system <value>] [--mpris] [--dbus-log] [--id <value>] [-a <int>] [-g <value>] [-d <value>] [-u <value>] [--wayland] [-X] [--dbus] [--pulse] COMMAND [OPTIONS]
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
-X Share X11 socket and allow connection
|
||||||
|
-a int
|
||||||
|
Fortify application ID
|
||||||
|
-d string
|
||||||
|
Application home directory (default "os")
|
||||||
|
-dbus
|
||||||
|
Proxy D-Bus connection
|
||||||
|
-dbus-config string
|
||||||
|
Path to D-Bus proxy config file, or "builtin" for defaults (default "builtin")
|
||||||
|
-dbus-log
|
||||||
|
Force logging in the D-Bus proxy
|
||||||
|
-dbus-system string
|
||||||
|
Path to system D-Bus proxy config file, or "nil" to disable (default "nil")
|
||||||
|
-g value
|
||||||
|
Groups inherited by the app process
|
||||||
|
-id string
|
||||||
|
App ID, leave empty to disable security context app_id
|
||||||
|
-mpris
|
||||||
|
Allow owning MPRIS D-Bus path, has no effect if custom config is available
|
||||||
|
-pulse
|
||||||
|
Share PulseAudio socket and cookie
|
||||||
|
-u string
|
||||||
|
Passwd name within sandbox (default "chronos")
|
||||||
|
-wayland
|
||||||
|
Allow Wayland connections
|
||||||
|
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
out := new(bytes.Buffer)
|
||||||
|
c := buildCommand(out)
|
||||||
|
if err := c.Parse(tc.args); !errors.Is(err, command.ErrHelp) && !errors.Is(err, flag.ErrHelp) {
|
||||||
|
t.Errorf("Parse: error = %v; want %v",
|
||||||
|
err, command.ErrHelp)
|
||||||
|
}
|
||||||
|
if got := out.String(); got != tc.want {
|
||||||
|
t.Errorf("Parse: %s want %s", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,7 @@ package
|
|||||||
|
|
||||||
|
|
||||||
*Default:*
|
*Default:*
|
||||||
` <derivation fortify-0.2.16> `
|
` <derivation fortify-0.2.17> `
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
buildGoModule rec {
|
buildGoModule rec {
|
||||||
pname = "fortify";
|
pname = "fortify";
|
||||||
version = "0.2.16";
|
version = "0.2.17";
|
||||||
|
|
||||||
src = builtins.path {
|
src = builtins.path {
|
||||||
name = "fortify-src";
|
name = "fortify-src";
|
||||||
|
6
print.go
6
print.go
@ -18,7 +18,7 @@ import (
|
|||||||
"git.gensokyo.uk/security/fortify/internal/state"
|
"git.gensokyo.uk/security/fortify/internal/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printShowSystem(output io.Writer, short bool) {
|
func printShowSystem(output io.Writer, short, flagJSON bool) {
|
||||||
t := newPrinter(output)
|
t := newPrinter(output)
|
||||||
defer t.MustFlush()
|
defer t.MustFlush()
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ func printShowSystem(output io.Writer, short bool) {
|
|||||||
func printShowInstance(
|
func printShowInstance(
|
||||||
output io.Writer, now time.Time,
|
output io.Writer, now time.Time,
|
||||||
instance *state.State, config *fst.Config,
|
instance *state.State, config *fst.Config,
|
||||||
short bool) {
|
short, flagJSON bool) {
|
||||||
if flagJSON {
|
if flagJSON {
|
||||||
if instance != nil {
|
if instance != nil {
|
||||||
printJSON(output, short, instance)
|
printJSON(output, short, instance)
|
||||||
@ -190,7 +190,7 @@ func printShowInstance(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printPs(output io.Writer, now time.Time, s state.Store, short bool) {
|
func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON bool) {
|
||||||
var entries state.Entries
|
var entries state.Entries
|
||||||
if e, err := state.Join(s); err != nil {
|
if e, err := state.Join(s); err != nil {
|
||||||
log.Fatalf("cannot join store: %v", err)
|
log.Fatalf("cannot join store: %v", err)
|
||||||
|
@ -448,14 +448,8 @@ App
|
|||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
{
|
|
||||||
v := flagJSON
|
|
||||||
t.Cleanup(func() { flagJSON = v })
|
|
||||||
flagJSON = tc.json
|
|
||||||
}
|
|
||||||
|
|
||||||
output := new(strings.Builder)
|
output := new(strings.Builder)
|
||||||
printShowInstance(output, testTime, tc.instance, tc.config, tc.short)
|
printShowInstance(output, testTime, tc.instance, tc.config, tc.short, tc.json)
|
||||||
if got := output.String(); got != tc.want {
|
if got := output.String(); got != tc.want {
|
||||||
t.Errorf("printShowInstance: got\n%s\nwant\n%s",
|
t.Errorf("printShowInstance: got\n%s\nwant\n%s",
|
||||||
got, tc.want)
|
got, tc.want)
|
||||||
@ -645,14 +639,8 @@ func Test_printPs(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
{
|
|
||||||
v := flagJSON
|
|
||||||
t.Cleanup(func() { flagJSON = v })
|
|
||||||
flagJSON = tc.json
|
|
||||||
}
|
|
||||||
|
|
||||||
output := new(strings.Builder)
|
output := new(strings.Builder)
|
||||||
printPs(output, testTime, stubStore(tc.entries), tc.short)
|
printPs(output, testTime, stubStore(tc.entries), tc.short, tc.json)
|
||||||
if got := output.String(); got != tc.want {
|
if got := output.String(); got != tc.want {
|
||||||
t.Errorf("printPs: got\n%s\nwant\n%s",
|
t.Errorf("printPs: got\n%s\nwant\n%s",
|
||||||
got, tc.want)
|
got, tc.want)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// Package system provides tools for safely interacting with the operating system.
|
||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -14,6 +15,7 @@ const (
|
|||||||
Process = Enablement(ELen + 1)
|
Process = Enablement(ELen + 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Criteria specifies types of Op to revert.
|
||||||
type Criteria struct {
|
type Criteria struct {
|
||||||
*Enablements
|
*Enablements
|
||||||
}
|
}
|
||||||
@ -42,6 +44,7 @@ type Op interface {
|
|||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TypeString returns the string representation of a type stored as an [Enablement].
|
||||||
func TypeString(e Enablement) string {
|
func TypeString(e Enablement) string {
|
||||||
switch e {
|
switch e {
|
||||||
case User:
|
case User:
|
||||||
@ -64,6 +67,7 @@ func New(uid int) (sys *I) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// An I provides indirect bulk operating system interaction. I must not be copied.
|
||||||
type I struct {
|
type I struct {
|
||||||
uid int
|
uid int
|
||||||
ops []Op
|
ops []Op
|
||||||
@ -91,6 +95,7 @@ func (sys *I) wrapErrSuffix(err error, a ...any) error {
|
|||||||
return sys.wrapErr(err, append(a, err)...)
|
return sys.wrapErr(err, append(a, err)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Equal returns whether all [Op] instances held by v is identical to that of sys.
|
||||||
func (sys *I) Equal(v *I) bool {
|
func (sys *I) Equal(v *I) bool {
|
||||||
if v == nil || sys.uid != v.uid || len(sys.ops) != len(v.ops) {
|
if v == nil || sys.uid != v.uid || len(sys.ops) != len(v.ops) {
|
||||||
return false
|
return false
|
||||||
@ -105,6 +110,8 @@ func (sys *I) Equal(v *I) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Commit applies all [Op] held by [I] and reverts successful [Op] on first error encountered.
|
||||||
|
// Commit must not be called more than once.
|
||||||
func (sys *I) Commit(ctx context.Context) error {
|
func (sys *I) Commit(ctx context.Context) error {
|
||||||
sys.lock.Lock()
|
sys.lock.Lock()
|
||||||
defer sys.lock.Unlock()
|
defer sys.lock.Unlock()
|
||||||
@ -141,6 +148,7 @@ func (sys *I) Commit(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Revert reverts all [Op] meeting [Criteria] held by [I].
|
||||||
func (sys *I) Revert(ec *Criteria) error {
|
func (sys *I) Revert(ec *Criteria) error {
|
||||||
sys.lock.Lock()
|
sys.lock.Lock()
|
||||||
defer sys.lock.Unlock()
|
defer sys.lock.Unlock()
|
||||||
|
401
test.nix
401
test.nix
@ -1,401 +0,0 @@
|
|||||||
{
|
|
||||||
system,
|
|
||||||
self,
|
|
||||||
home-manager,
|
|
||||||
nixosTest,
|
|
||||||
fortify,
|
|
||||||
}:
|
|
||||||
|
|
||||||
nixosTest {
|
|
||||||
name = "fortify";
|
|
||||||
|
|
||||||
# adapted from nixos sway integration tests
|
|
||||||
|
|
||||||
# testScriptWithTypes:49: error: Cannot call function of unknown type
|
|
||||||
# (machine.succeed if succeed else machine.execute)(
|
|
||||||
# ^
|
|
||||||
# Found 1 error in 1 file (checked 1 source file)
|
|
||||||
skipTypeCheck = true;
|
|
||||||
|
|
||||||
nodes.machine =
|
|
||||||
{
|
|
||||||
lib,
|
|
||||||
pkgs,
|
|
||||||
config,
|
|
||||||
...
|
|
||||||
}:
|
|
||||||
{
|
|
||||||
users.users = {
|
|
||||||
alice = {
|
|
||||||
isNormalUser = true;
|
|
||||||
description = "Alice Foobar";
|
|
||||||
password = "foobar";
|
|
||||||
uid = 1000;
|
|
||||||
};
|
|
||||||
untrusted = {
|
|
||||||
isNormalUser = true;
|
|
||||||
description = "Untrusted user";
|
|
||||||
password = "foobar";
|
|
||||||
uid = 1001;
|
|
||||||
|
|
||||||
# For deny unmapped uid test:
|
|
||||||
packages = [ config.environment.fortify.package ];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
home-manager.users.alice.home.stateVersion = "24.11";
|
|
||||||
|
|
||||||
# Automatically login on tty1 as a normal user:
|
|
||||||
services.getty.autologinUser = "alice";
|
|
||||||
|
|
||||||
environment = {
|
|
||||||
systemPackages = with pkgs; [
|
|
||||||
# For glinfo and wayland-info:
|
|
||||||
mesa-demos
|
|
||||||
wayland-utils
|
|
||||||
|
|
||||||
# For D-Bus tests:
|
|
||||||
libnotify
|
|
||||||
mako
|
|
||||||
|
|
||||||
# For go tests:
|
|
||||||
self.packages.${system}.fhs
|
|
||||||
];
|
|
||||||
|
|
||||||
variables = {
|
|
||||||
SWAYSOCK = "/tmp/sway-ipc.sock";
|
|
||||||
WLR_RENDERER = "pixman";
|
|
||||||
};
|
|
||||||
|
|
||||||
# To help with OCR:
|
|
||||||
etc."xdg/foot/foot.ini".text = lib.generators.toINI { } {
|
|
||||||
main = {
|
|
||||||
font = "inconsolata:size=14";
|
|
||||||
};
|
|
||||||
colors = rec {
|
|
||||||
foreground = "000000";
|
|
||||||
background = "ffffff";
|
|
||||||
regular2 = foreground;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
fonts.packages = [ pkgs.inconsolata ];
|
|
||||||
|
|
||||||
# Automatically configure and start Sway when logging in on tty1:
|
|
||||||
programs.bash.loginShellInit = ''
|
|
||||||
if [ "$(tty)" = "/dev/tty1" ]; then
|
|
||||||
set -e
|
|
||||||
|
|
||||||
mkdir -p ~/.config/sway
|
|
||||||
(sed s/Mod4/Mod1/ /etc/sway/config &&
|
|
||||||
echo 'output * bg ${pkgs.nixos-artwork.wallpapers.simple-light-gray.gnomeFilePath} fill' &&
|
|
||||||
echo 'output Virtual-1 res 1680x1050') > ~/.config/sway/config
|
|
||||||
|
|
||||||
sway --validate
|
|
||||||
systemd-cat --identifier=sway sway && touch /tmp/sway-exit-ok
|
|
||||||
fi
|
|
||||||
'';
|
|
||||||
|
|
||||||
programs.sway.enable = true;
|
|
||||||
|
|
||||||
# For PulseAudio tests:
|
|
||||||
security.rtkit.enable = true;
|
|
||||||
services.pipewire = {
|
|
||||||
enable = true;
|
|
||||||
alsa.enable = true;
|
|
||||||
alsa.support32Bit = true;
|
|
||||||
pulse.enable = true;
|
|
||||||
jack.enable = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
virtualisation.qemu.options = [
|
|
||||||
# Need to switch to a different GPU driver than the default one (-vga std) so that Sway can launch:
|
|
||||||
"-vga none -device virtio-gpu-pci"
|
|
||||||
|
|
||||||
# Increase Go test compiler performance:
|
|
||||||
"-smp 8"
|
|
||||||
];
|
|
||||||
|
|
||||||
environment.fortify = {
|
|
||||||
enable = true;
|
|
||||||
package = fortify.overrideAttrs (previousAttrs: {
|
|
||||||
GOFLAGS = previousAttrs.GOFLAGS ++ [ "-race" ];
|
|
||||||
|
|
||||||
# fsu does not like cgo
|
|
||||||
disallowedReferences = previousAttrs.disallowedReferences ++ [ fortify ];
|
|
||||||
postInstall =
|
|
||||||
previousAttrs.postInstall
|
|
||||||
+ ''
|
|
||||||
cp -a "${fortify}/libexec/fsu" "$out/libexec/fsu"
|
|
||||||
sed -i 's:${fortify}:${placeholder "out"}:' "$out/libexec/fsu"
|
|
||||||
'';
|
|
||||||
});
|
|
||||||
stateDir = "/var/lib/fortify";
|
|
||||||
users.alice = 0;
|
|
||||||
|
|
||||||
home-manager = _: _: { home.stateVersion = "23.05"; };
|
|
||||||
|
|
||||||
apps = [
|
|
||||||
{
|
|
||||||
name = "ne-foot";
|
|
||||||
verbose = true;
|
|
||||||
share = pkgs.foot;
|
|
||||||
packages = [ pkgs.foot ];
|
|
||||||
command = "foot";
|
|
||||||
capability = {
|
|
||||||
dbus = false;
|
|
||||||
pulse = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "pa-foot";
|
|
||||||
verbose = true;
|
|
||||||
share = pkgs.foot;
|
|
||||||
packages = [ pkgs.foot ];
|
|
||||||
command = "foot";
|
|
||||||
capability.dbus = false;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "x11-alacritty";
|
|
||||||
verbose = true;
|
|
||||||
share = pkgs.alacritty;
|
|
||||||
packages = [ pkgs.alacritty ];
|
|
||||||
command = "alacritty";
|
|
||||||
capability = {
|
|
||||||
wayland = false;
|
|
||||||
x11 = true;
|
|
||||||
dbus = false;
|
|
||||||
pulse = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "da-foot";
|
|
||||||
verbose = true;
|
|
||||||
insecureWayland = true;
|
|
||||||
share = pkgs.foot;
|
|
||||||
packages = [ pkgs.foot ];
|
|
||||||
command = "foot";
|
|
||||||
capability = {
|
|
||||||
dbus = false;
|
|
||||||
pulse = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
name = "strace-failure";
|
|
||||||
verbose = true;
|
|
||||||
share = pkgs.strace;
|
|
||||||
command = "strace true";
|
|
||||||
capability = {
|
|
||||||
wayland = false;
|
|
||||||
x11 = false;
|
|
||||||
dbus = false;
|
|
||||||
pulse = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
imports = [
|
|
||||||
self.nixosModules.fortify
|
|
||||||
home-manager.nixosModules.home-manager
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
testScript = ''
|
|
||||||
import shlex
|
|
||||||
import json
|
|
||||||
|
|
||||||
q = shlex.quote
|
|
||||||
NODE_GROUPS = ["nodes", "floating_nodes"]
|
|
||||||
|
|
||||||
|
|
||||||
def swaymsg(command: str = "", succeed=True, type="command"):
|
|
||||||
assert command != "" or type != "command", "Must specify command or type"
|
|
||||||
shell = q(f"swaymsg -t {q(type)} -- {q(command)}")
|
|
||||||
with machine.nested(
|
|
||||||
f"sending swaymsg {shell!r}" + " (allowed to fail)" * (not succeed)
|
|
||||||
):
|
|
||||||
ret = (machine.succeed if succeed else machine.execute)(
|
|
||||||
f"su - alice -c {shell}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# execute also returns a status code, but disregard.
|
|
||||||
if not succeed:
|
|
||||||
_, ret = ret
|
|
||||||
|
|
||||||
if not succeed and not ret:
|
|
||||||
return None
|
|
||||||
|
|
||||||
parsed = json.loads(ret)
|
|
||||||
return parsed
|
|
||||||
|
|
||||||
|
|
||||||
def walk(tree):
|
|
||||||
yield tree
|
|
||||||
for group in NODE_GROUPS:
|
|
||||||
for node in tree.get(group, []):
|
|
||||||
yield from walk(node)
|
|
||||||
|
|
||||||
|
|
||||||
def wait_for_window(pattern):
|
|
||||||
def func(last_chance):
|
|
||||||
nodes = (node["name"] for node in walk(swaymsg(type="get_tree")))
|
|
||||||
|
|
||||||
if last_chance:
|
|
||||||
nodes = list(nodes)
|
|
||||||
machine.log(f"Last call! Current list of windows: {nodes}")
|
|
||||||
|
|
||||||
return any(pattern in name for name in nodes)
|
|
||||||
|
|
||||||
retry(func)
|
|
||||||
|
|
||||||
|
|
||||||
def collect_state_ui(name):
|
|
||||||
swaymsg(f"exec fortify ps > '/tmp/{name}.ps'")
|
|
||||||
machine.copy_from_vm(f"/tmp/{name}.ps", "")
|
|
||||||
swaymsg(f"exec fortify --json ps > '/tmp/{name}.json'")
|
|
||||||
machine.copy_from_vm(f"/tmp/{name}.json", "")
|
|
||||||
machine.screenshot(name)
|
|
||||||
|
|
||||||
|
|
||||||
def check_state(name, enablements):
|
|
||||||
instances = json.loads(machine.succeed("sudo -u alice -i XDG_RUNTIME_DIR=/run/user/1000 fortify --json ps"))
|
|
||||||
if len(instances) != 1:
|
|
||||||
raise Exception(f"unexpected state length {len(instances)}")
|
|
||||||
instance = next(iter(instances.values()))
|
|
||||||
|
|
||||||
config = instance['config']
|
|
||||||
|
|
||||||
if len(config['command']) != 1 or not(config['command'][0].startswith("/nix/store/")) or not(config['command'][0].endswith(f"{name}-start")):
|
|
||||||
raise Exception(f"unexpected command {instance['config']['command']}")
|
|
||||||
|
|
||||||
if config['confinement']['enablements'] != enablements:
|
|
||||||
raise Exception(f"unexpected enablements {instance['config']['confinement']['enablements']}")
|
|
||||||
|
|
||||||
|
|
||||||
def fortify(command):
|
|
||||||
swaymsg(f"exec fortify {command}")
|
|
||||||
|
|
||||||
start_all()
|
|
||||||
machine.wait_for_unit("multi-user.target")
|
|
||||||
|
|
||||||
# Run fortify Go tests outside of nix build in the background:
|
|
||||||
machine.succeed("rm -rf /tmp/src && cp -a '${self.packages.${system}.fortify.src}' /tmp/src")
|
|
||||||
machine.succeed("fortify-fhs -c '(cd /tmp/src && go generate ./... && go test ./... && touch /tmp/success-gotest)' &> /tmp/gotest &")
|
|
||||||
|
|
||||||
# To check fortify's version:
|
|
||||||
print(machine.succeed("sudo -u alice -i fortify version"))
|
|
||||||
|
|
||||||
# Wait for Sway to complete startup:
|
|
||||||
machine.wait_for_file("/run/user/1000/wayland-1")
|
|
||||||
machine.wait_for_file("/tmp/sway-ipc.sock")
|
|
||||||
|
|
||||||
# Deny unmapped uid:
|
|
||||||
denyOutput = machine.fail("sudo -u untrusted -i fortify run &>/dev/stdout")
|
|
||||||
print(denyOutput)
|
|
||||||
denyOutputVerbose = machine.fail("sudo -u untrusted -i fortify -v run &>/dev/stdout")
|
|
||||||
print(denyOutputVerbose)
|
|
||||||
|
|
||||||
# Verify PrintBaseError behaviour:
|
|
||||||
if denyOutput != "fsu: uid 1001 is not in the fsurc file\n":
|
|
||||||
raise Exception(f"unexpected deny output:\n{denyOutput}")
|
|
||||||
if denyOutputVerbose != "fsu: uid 1001 is not in the fsurc file\nfortify: *cannot obtain uid from fsu: permission denied\n":
|
|
||||||
raise Exception(f"unexpected deny verbose output:\n{denyOutputVerbose}")
|
|
||||||
|
|
||||||
# Start fortify permissive defaults outside Wayland session:
|
|
||||||
print(machine.succeed("sudo -u alice -i fortify -v run -a 0 touch /tmp/success-bare"))
|
|
||||||
machine.wait_for_file("/tmp/fortify.1000/tmpdir/0/success-bare")
|
|
||||||
|
|
||||||
# Verify silent output permissive defaults:
|
|
||||||
output = machine.succeed("sudo -u alice -i fortify run -a 0 true &>/dev/stdout")
|
|
||||||
if output != "":
|
|
||||||
raise Exception(f"unexpected output\n{output}")
|
|
||||||
|
|
||||||
# Verify graceful failure on bad Wayland display name:
|
|
||||||
print(machine.fail("sudo -u alice -i fortify -v run --wayland true"))
|
|
||||||
|
|
||||||
# Start fortify permissive defaults within Wayland session:
|
|
||||||
fortify('-v run --wayland --dbus notify-send -a "NixOS Tests" "Test notification" "Notification from within sandbox." && touch /tmp/dbus-done')
|
|
||||||
machine.wait_for_file("/tmp/dbus-done")
|
|
||||||
collect_state_ui("dbus_notify_exited")
|
|
||||||
machine.succeed("pkill -9 mako")
|
|
||||||
|
|
||||||
# Start app (foot) with Wayland enablement:
|
|
||||||
swaymsg("exec ne-foot")
|
|
||||||
wait_for_window("u0_a1@machine")
|
|
||||||
machine.send_chars("clear; wayland-info && touch /tmp/success-client\n")
|
|
||||||
machine.wait_for_file("/tmp/fortify.1000/tmpdir/1/success-client")
|
|
||||||
collect_state_ui("foot_wayland")
|
|
||||||
check_state("ne-foot", 1)
|
|
||||||
# Verify acl on XDG_RUNTIME_DIR:
|
|
||||||
print(machine.succeed("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000001"))
|
|
||||||
machine.send_chars("exit\n")
|
|
||||||
machine.wait_until_fails("pgrep foot")
|
|
||||||
# Verify acl cleanup on XDG_RUNTIME_DIR:
|
|
||||||
machine.wait_until_fails("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000001")
|
|
||||||
|
|
||||||
# Start app (foot) with Wayland enablement from a terminal:
|
|
||||||
swaymsg("exec foot $SHELL -c '(ne-foot) & sleep 1 && fortify show $(fortify ps --short) && touch /tmp/ps-show-ok && cat'")
|
|
||||||
wait_for_window("u0_a1@machine")
|
|
||||||
machine.send_chars("clear; wayland-info && touch /tmp/success-client-term\n")
|
|
||||||
machine.wait_for_file("/tmp/fortify.1000/tmpdir/1/success-client-term")
|
|
||||||
machine.wait_for_file("/tmp/ps-show-ok")
|
|
||||||
collect_state_ui("foot_wayland_term")
|
|
||||||
check_state("ne-foot", 1)
|
|
||||||
machine.send_chars("exit\n")
|
|
||||||
wait_for_window("foot")
|
|
||||||
machine.send_key("ctrl-c")
|
|
||||||
machine.wait_until_fails("pgrep foot")
|
|
||||||
|
|
||||||
# Test PulseAudio (fortify does not support PipeWire yet):
|
|
||||||
swaymsg("exec pa-foot")
|
|
||||||
wait_for_window("u0_a2@machine")
|
|
||||||
machine.send_chars("clear; pactl info && touch /tmp/success-pulse\n")
|
|
||||||
machine.wait_for_file("/tmp/fortify.1000/tmpdir/2/success-pulse")
|
|
||||||
collect_state_ui("pulse_wayland")
|
|
||||||
check_state("pa-foot", 9)
|
|
||||||
machine.send_chars("exit\n")
|
|
||||||
machine.wait_until_fails("pgrep foot")
|
|
||||||
|
|
||||||
# Test XWayland (foot does not support X):
|
|
||||||
swaymsg("exec x11-alacritty")
|
|
||||||
wait_for_window("u0_a3@machine")
|
|
||||||
machine.send_chars("clear; glinfo && touch /tmp/success-client-x11\n")
|
|
||||||
machine.wait_for_file("/tmp/fortify.1000/tmpdir/3/success-client-x11")
|
|
||||||
collect_state_ui("alacritty_x11")
|
|
||||||
check_state("x11-alacritty", 2)
|
|
||||||
machine.send_chars("exit\n")
|
|
||||||
machine.wait_until_fails("pgrep alacritty")
|
|
||||||
|
|
||||||
# Start app (foot) with direct Wayland access:
|
|
||||||
swaymsg("exec da-foot")
|
|
||||||
wait_for_window("u0_a4@machine")
|
|
||||||
machine.send_chars("clear; wayland-info && touch /tmp/success-direct\n")
|
|
||||||
machine.wait_for_file("/tmp/fortify.1000/tmpdir/4/success-direct")
|
|
||||||
collect_state_ui("foot_direct")
|
|
||||||
check_state("da-foot", 1)
|
|
||||||
# Verify acl on XDG_RUNTIME_DIR:
|
|
||||||
print(machine.succeed("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000004"))
|
|
||||||
machine.send_chars("exit\n")
|
|
||||||
machine.wait_until_fails("pgrep foot")
|
|
||||||
# Verify acl cleanup on XDG_RUNTIME_DIR:
|
|
||||||
machine.wait_until_fails("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000004")
|
|
||||||
|
|
||||||
# Test syscall filter:
|
|
||||||
print(machine.fail("sudo -u alice -i XDG_RUNTIME_DIR=/run/user/1000 strace-failure"))
|
|
||||||
|
|
||||||
# Exit Sway and verify process exit status 0:
|
|
||||||
swaymsg("exit", succeed=False)
|
|
||||||
machine.wait_until_fails("pgrep -x sway")
|
|
||||||
machine.wait_for_file("/tmp/sway-exit-ok")
|
|
||||||
|
|
||||||
# Print fortify runDir contents:
|
|
||||||
print(machine.succeed("find /run/user/1000/fortify"))
|
|
||||||
|
|
||||||
# Verify go test status:
|
|
||||||
machine.wait_for_file("/tmp/gotest")
|
|
||||||
print(machine.succeed("cat /tmp/gotest"))
|
|
||||||
machine.wait_for_file("/tmp/success-gotest")
|
|
||||||
'';
|
|
||||||
}
|
|
163
tests/fortify/configuration.nix
Normal file
163
tests/fortify/configuration.nix
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
{
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
config,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
{
|
||||||
|
users.users = {
|
||||||
|
alice = {
|
||||||
|
isNormalUser = true;
|
||||||
|
description = "Alice Foobar";
|
||||||
|
password = "foobar";
|
||||||
|
uid = 1000;
|
||||||
|
};
|
||||||
|
untrusted = {
|
||||||
|
isNormalUser = true;
|
||||||
|
description = "Untrusted user";
|
||||||
|
password = "foobar";
|
||||||
|
uid = 1001;
|
||||||
|
|
||||||
|
# For deny unmapped uid test:
|
||||||
|
packages = [ config.environment.fortify.package ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
home-manager.users.alice.home.stateVersion = "24.11";
|
||||||
|
|
||||||
|
# Automatically login on tty1 as a normal user:
|
||||||
|
services.getty.autologinUser = "alice";
|
||||||
|
|
||||||
|
environment = {
|
||||||
|
systemPackages = with pkgs; [
|
||||||
|
# For glinfo and wayland-info:
|
||||||
|
mesa-demos
|
||||||
|
wayland-utils
|
||||||
|
|
||||||
|
# For D-Bus tests:
|
||||||
|
libnotify
|
||||||
|
mako
|
||||||
|
];
|
||||||
|
|
||||||
|
variables = {
|
||||||
|
SWAYSOCK = "/tmp/sway-ipc.sock";
|
||||||
|
WLR_RENDERER = "pixman";
|
||||||
|
};
|
||||||
|
|
||||||
|
# To help with OCR:
|
||||||
|
etc."xdg/foot/foot.ini".text = lib.generators.toINI { } {
|
||||||
|
main = {
|
||||||
|
font = "inconsolata:size=14";
|
||||||
|
};
|
||||||
|
colors = rec {
|
||||||
|
foreground = "000000";
|
||||||
|
background = "ffffff";
|
||||||
|
regular2 = foreground;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
fonts.packages = [ pkgs.inconsolata ];
|
||||||
|
|
||||||
|
# Automatically configure and start Sway when logging in on tty1:
|
||||||
|
programs.bash.loginShellInit = ''
|
||||||
|
if [ "$(tty)" = "/dev/tty1" ]; then
|
||||||
|
set -e
|
||||||
|
|
||||||
|
mkdir -p ~/.config/sway
|
||||||
|
(sed s/Mod4/Mod1/ /etc/sway/config &&
|
||||||
|
echo 'output * bg ${pkgs.nixos-artwork.wallpapers.simple-light-gray.gnomeFilePath} fill' &&
|
||||||
|
echo 'output Virtual-1 res 1680x1050') > ~/.config/sway/config
|
||||||
|
|
||||||
|
sway --validate
|
||||||
|
systemd-cat --identifier=sway sway && touch /tmp/sway-exit-ok
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
|
||||||
|
programs.sway.enable = true;
|
||||||
|
|
||||||
|
# For PulseAudio tests:
|
||||||
|
security.rtkit.enable = true;
|
||||||
|
services.pipewire = {
|
||||||
|
enable = true;
|
||||||
|
alsa.enable = true;
|
||||||
|
alsa.support32Bit = true;
|
||||||
|
pulse.enable = true;
|
||||||
|
jack.enable = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
virtualisation.qemu.options = [
|
||||||
|
# Need to switch to a different GPU driver than the default one (-vga std) so that Sway can launch:
|
||||||
|
"-vga none -device virtio-gpu-pci"
|
||||||
|
|
||||||
|
# Increase Go test compiler performance:
|
||||||
|
"-smp 8"
|
||||||
|
];
|
||||||
|
|
||||||
|
environment.fortify = {
|
||||||
|
enable = true;
|
||||||
|
stateDir = "/var/lib/fortify";
|
||||||
|
users.alice = 0;
|
||||||
|
|
||||||
|
home-manager = _: _: { home.stateVersion = "23.05"; };
|
||||||
|
|
||||||
|
apps = [
|
||||||
|
{
|
||||||
|
name = "ne-foot";
|
||||||
|
verbose = true;
|
||||||
|
share = pkgs.foot;
|
||||||
|
packages = [ pkgs.foot ];
|
||||||
|
command = "foot";
|
||||||
|
capability = {
|
||||||
|
dbus = false;
|
||||||
|
pulse = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "pa-foot";
|
||||||
|
verbose = true;
|
||||||
|
share = pkgs.foot;
|
||||||
|
packages = [ pkgs.foot ];
|
||||||
|
command = "foot";
|
||||||
|
capability.dbus = false;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "x11-alacritty";
|
||||||
|
verbose = true;
|
||||||
|
share = pkgs.alacritty;
|
||||||
|
packages = [ pkgs.alacritty ];
|
||||||
|
command = "alacritty";
|
||||||
|
capability = {
|
||||||
|
wayland = false;
|
||||||
|
x11 = true;
|
||||||
|
dbus = false;
|
||||||
|
pulse = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "da-foot";
|
||||||
|
verbose = true;
|
||||||
|
insecureWayland = true;
|
||||||
|
share = pkgs.foot;
|
||||||
|
packages = [ pkgs.foot ];
|
||||||
|
command = "foot";
|
||||||
|
capability = {
|
||||||
|
dbus = false;
|
||||||
|
pulse = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "strace-failure";
|
||||||
|
verbose = true;
|
||||||
|
share = pkgs.strace;
|
||||||
|
command = "strace true";
|
||||||
|
capability = {
|
||||||
|
wayland = false;
|
||||||
|
x11 = false;
|
||||||
|
dbus = false;
|
||||||
|
pulse = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
51
tests/fortify/default.nix
Normal file
51
tests/fortify/default.nix
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
{
|
||||||
|
system,
|
||||||
|
self,
|
||||||
|
nixosTest,
|
||||||
|
writeShellScriptBin,
|
||||||
|
}:
|
||||||
|
|
||||||
|
nixosTest {
|
||||||
|
name = "fortify";
|
||||||
|
nodes.machine = {
|
||||||
|
environment.systemPackages = [
|
||||||
|
# For go tests:
|
||||||
|
self.packages.${system}.fhs
|
||||||
|
(writeShellScriptBin "fortify-src" "echo -n ${self.packages.${system}.fortify.src}")
|
||||||
|
];
|
||||||
|
|
||||||
|
# Run with Go race detector:
|
||||||
|
environment.fortify.package =
|
||||||
|
let
|
||||||
|
inherit (self.packages.${system}) fortify;
|
||||||
|
in
|
||||||
|
fortify.overrideAttrs (previousAttrs: {
|
||||||
|
GOFLAGS = previousAttrs.GOFLAGS ++ [ "-race" ];
|
||||||
|
|
||||||
|
# fsu does not like cgo
|
||||||
|
disallowedReferences = previousAttrs.disallowedReferences ++ [ fortify ];
|
||||||
|
postInstall =
|
||||||
|
previousAttrs.postInstall
|
||||||
|
+ ''
|
||||||
|
cp -a "${fortify}/libexec/fsu" "$out/libexec/fsu"
|
||||||
|
sed -i 's:${fortify}:${placeholder "out"}:' "$out/libexec/fsu"
|
||||||
|
'';
|
||||||
|
});
|
||||||
|
|
||||||
|
imports = [
|
||||||
|
./configuration.nix
|
||||||
|
|
||||||
|
self.nixosModules.fortify
|
||||||
|
self.inputs.home-manager.nixosModules.home-manager
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
# adapted from nixos sway integration tests
|
||||||
|
|
||||||
|
# testScriptWithTypes:49: error: Cannot call function of unknown type
|
||||||
|
# (machine.succeed if succeed else machine.execute)(
|
||||||
|
# ^
|
||||||
|
# Found 1 error in 1 file (checked 1 source file)
|
||||||
|
skipTypeCheck = true;
|
||||||
|
testScript = builtins.readFile ./test.py;
|
||||||
|
}
|
199
tests/fortify/test.py
Normal file
199
tests/fortify/test.py
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
import json
|
||||||
|
import shlex
|
||||||
|
|
||||||
|
q = shlex.quote
|
||||||
|
NODE_GROUPS = ["nodes", "floating_nodes"]
|
||||||
|
|
||||||
|
|
||||||
|
def swaymsg(command: str = "", succeed=True, type="command"):
|
||||||
|
assert command != "" or type != "command", "Must specify command or type"
|
||||||
|
shell = q(f"swaymsg -t {q(type)} -- {q(command)}")
|
||||||
|
with machine.nested(
|
||||||
|
f"sending swaymsg {shell!r}" + " (allowed to fail)" * (not succeed)
|
||||||
|
):
|
||||||
|
ret = (machine.succeed if succeed else machine.execute)(
|
||||||
|
f"su - alice -c {shell}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# execute also returns a status code, but disregard.
|
||||||
|
if not succeed:
|
||||||
|
_, ret = ret
|
||||||
|
|
||||||
|
if not succeed and not ret:
|
||||||
|
return None
|
||||||
|
|
||||||
|
parsed = json.loads(ret)
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
|
def walk(tree):
|
||||||
|
yield tree
|
||||||
|
for group in NODE_GROUPS:
|
||||||
|
for node in tree.get(group, []):
|
||||||
|
yield from walk(node)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_for_window(pattern):
|
||||||
|
def func(last_chance):
|
||||||
|
nodes = (node["name"] for node in walk(swaymsg(type="get_tree")))
|
||||||
|
|
||||||
|
if last_chance:
|
||||||
|
nodes = list(nodes)
|
||||||
|
machine.log(f"Last call! Current list of windows: {nodes}")
|
||||||
|
|
||||||
|
return any(pattern in name for name in nodes)
|
||||||
|
|
||||||
|
retry(func)
|
||||||
|
|
||||||
|
|
||||||
|
def collect_state_ui(name):
|
||||||
|
swaymsg(f"exec fortify ps > '/tmp/{name}.ps'")
|
||||||
|
machine.copy_from_vm(f"/tmp/{name}.ps", "")
|
||||||
|
swaymsg(f"exec fortify --json ps > '/tmp/{name}.json'")
|
||||||
|
machine.copy_from_vm(f"/tmp/{name}.json", "")
|
||||||
|
machine.screenshot(name)
|
||||||
|
|
||||||
|
|
||||||
|
def check_state(name, enablements):
|
||||||
|
instances = json.loads(machine.succeed("sudo -u alice -i XDG_RUNTIME_DIR=/run/user/1000 fortify --json ps"))
|
||||||
|
if len(instances) != 1:
|
||||||
|
raise Exception(f"unexpected state length {len(instances)}")
|
||||||
|
instance = next(iter(instances.values()))
|
||||||
|
|
||||||
|
config = instance['config']
|
||||||
|
|
||||||
|
if len(config['command']) != 1 or not (config['command'][0].startswith("/nix/store/")) or not (
|
||||||
|
config['command'][0].endswith(f"{name}-start")):
|
||||||
|
raise Exception(f"unexpected command {instance['config']['command']}")
|
||||||
|
|
||||||
|
if config['confinement']['enablements'] != enablements:
|
||||||
|
raise Exception(f"unexpected enablements {instance['config']['confinement']['enablements']}")
|
||||||
|
|
||||||
|
|
||||||
|
def fortify(command):
|
||||||
|
swaymsg(f"exec fortify {command}")
|
||||||
|
|
||||||
|
|
||||||
|
start_all()
|
||||||
|
machine.wait_for_unit("multi-user.target")
|
||||||
|
|
||||||
|
# Run fortify Go tests outside of nix build in the background:
|
||||||
|
machine.succeed("rm -rf /tmp/src && cp -a \"$(fortify-src)\" /tmp/src")
|
||||||
|
machine.succeed(
|
||||||
|
"fortify-fhs -c '(cd /tmp/src && go generate ./... && go test ./... && touch /tmp/success-gotest)' &> /tmp/gotest &")
|
||||||
|
|
||||||
|
# To check fortify's version:
|
||||||
|
print(machine.succeed("sudo -u alice -i fortify version"))
|
||||||
|
|
||||||
|
# Wait for Sway to complete startup:
|
||||||
|
machine.wait_for_file("/run/user/1000/wayland-1")
|
||||||
|
machine.wait_for_file("/tmp/sway-ipc.sock")
|
||||||
|
|
||||||
|
# Deny unmapped uid:
|
||||||
|
denyOutput = machine.fail("sudo -u untrusted -i fortify run &>/dev/stdout")
|
||||||
|
print(denyOutput)
|
||||||
|
denyOutputVerbose = machine.fail("sudo -u untrusted -i fortify -v run &>/dev/stdout")
|
||||||
|
print(denyOutputVerbose)
|
||||||
|
|
||||||
|
# Verify PrintBaseError behaviour:
|
||||||
|
if denyOutput != "fsu: uid 1001 is not in the fsurc file\n":
|
||||||
|
raise Exception(f"unexpected deny output:\n{denyOutput}")
|
||||||
|
if denyOutputVerbose != "fsu: uid 1001 is not in the fsurc file\nfortify: *cannot obtain uid from fsu: permission denied\n":
|
||||||
|
raise Exception(f"unexpected deny verbose output:\n{denyOutputVerbose}")
|
||||||
|
|
||||||
|
# Start fortify permissive defaults outside Wayland session:
|
||||||
|
print(machine.succeed("sudo -u alice -i fortify -v run -a 0 touch /tmp/success-bare"))
|
||||||
|
machine.wait_for_file("/tmp/fortify.1000/tmpdir/0/success-bare")
|
||||||
|
|
||||||
|
# Verify silent output permissive defaults:
|
||||||
|
output = machine.succeed("sudo -u alice -i fortify run -a 0 true &>/dev/stdout")
|
||||||
|
if output != "":
|
||||||
|
raise Exception(f"unexpected output\n{output}")
|
||||||
|
|
||||||
|
# Verify graceful failure on bad Wayland display name:
|
||||||
|
print(machine.fail("sudo -u alice -i fortify -v run --wayland true"))
|
||||||
|
|
||||||
|
# Start fortify permissive defaults within Wayland session:
|
||||||
|
fortify(
|
||||||
|
'-v run --wayland --dbus notify-send -a "NixOS Tests" "Test notification" "Notification from within sandbox." && touch /tmp/dbus-done')
|
||||||
|
machine.wait_for_file("/tmp/dbus-done")
|
||||||
|
collect_state_ui("dbus_notify_exited")
|
||||||
|
machine.succeed("pkill -9 mako")
|
||||||
|
|
||||||
|
# Start app (foot) with Wayland enablement:
|
||||||
|
swaymsg("exec ne-foot")
|
||||||
|
wait_for_window("u0_a1@machine")
|
||||||
|
machine.send_chars("clear; wayland-info && touch /tmp/success-client\n")
|
||||||
|
machine.wait_for_file("/tmp/fortify.1000/tmpdir/1/success-client")
|
||||||
|
collect_state_ui("foot_wayland")
|
||||||
|
check_state("ne-foot", 1)
|
||||||
|
# Verify acl on XDG_RUNTIME_DIR:
|
||||||
|
print(machine.succeed("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000001"))
|
||||||
|
machine.send_chars("exit\n")
|
||||||
|
machine.wait_until_fails("pgrep foot")
|
||||||
|
# Verify acl cleanup on XDG_RUNTIME_DIR:
|
||||||
|
machine.wait_until_fails("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000001")
|
||||||
|
|
||||||
|
# Start app (foot) with Wayland enablement from a terminal:
|
||||||
|
swaymsg(
|
||||||
|
"exec foot $SHELL -c '(ne-foot) & sleep 1 && fortify show $(fortify ps --short) && touch /tmp/ps-show-ok && cat'")
|
||||||
|
wait_for_window("u0_a1@machine")
|
||||||
|
machine.send_chars("clear; wayland-info && touch /tmp/success-client-term\n")
|
||||||
|
machine.wait_for_file("/tmp/fortify.1000/tmpdir/1/success-client-term")
|
||||||
|
machine.wait_for_file("/tmp/ps-show-ok")
|
||||||
|
collect_state_ui("foot_wayland_term")
|
||||||
|
check_state("ne-foot", 1)
|
||||||
|
machine.send_chars("exit\n")
|
||||||
|
wait_for_window("foot")
|
||||||
|
machine.send_key("ctrl-c")
|
||||||
|
machine.wait_until_fails("pgrep foot")
|
||||||
|
|
||||||
|
# Test PulseAudio (fortify does not support PipeWire yet):
|
||||||
|
swaymsg("exec pa-foot")
|
||||||
|
wait_for_window("u0_a2@machine")
|
||||||
|
machine.send_chars("clear; pactl info && touch /tmp/success-pulse\n")
|
||||||
|
machine.wait_for_file("/tmp/fortify.1000/tmpdir/2/success-pulse")
|
||||||
|
collect_state_ui("pulse_wayland")
|
||||||
|
check_state("pa-foot", 9)
|
||||||
|
machine.send_chars("exit\n")
|
||||||
|
machine.wait_until_fails("pgrep foot")
|
||||||
|
|
||||||
|
# Test XWayland (foot does not support X):
|
||||||
|
swaymsg("exec x11-alacritty")
|
||||||
|
wait_for_window("u0_a3@machine")
|
||||||
|
machine.send_chars("clear; glinfo && touch /tmp/success-client-x11\n")
|
||||||
|
machine.wait_for_file("/tmp/fortify.1000/tmpdir/3/success-client-x11")
|
||||||
|
collect_state_ui("alacritty_x11")
|
||||||
|
check_state("x11-alacritty", 2)
|
||||||
|
machine.send_chars("exit\n")
|
||||||
|
machine.wait_until_fails("pgrep alacritty")
|
||||||
|
|
||||||
|
# Start app (foot) with direct Wayland access:
|
||||||
|
swaymsg("exec da-foot")
|
||||||
|
wait_for_window("u0_a4@machine")
|
||||||
|
machine.send_chars("clear; wayland-info && touch /tmp/success-direct\n")
|
||||||
|
machine.wait_for_file("/tmp/fortify.1000/tmpdir/4/success-direct")
|
||||||
|
collect_state_ui("foot_direct")
|
||||||
|
check_state("da-foot", 1)
|
||||||
|
# Verify acl on XDG_RUNTIME_DIR:
|
||||||
|
print(machine.succeed("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000004"))
|
||||||
|
machine.send_chars("exit\n")
|
||||||
|
machine.wait_until_fails("pgrep foot")
|
||||||
|
# Verify acl cleanup on XDG_RUNTIME_DIR:
|
||||||
|
machine.wait_until_fails("getfacl --absolute-names --omit-header --numeric /run/user/1000 | grep 1000004")
|
||||||
|
|
||||||
|
# Test syscall filter:
|
||||||
|
print(machine.fail("sudo -u alice -i XDG_RUNTIME_DIR=/run/user/1000 strace-failure"))
|
||||||
|
|
||||||
|
# Exit Sway and verify process exit status 0:
|
||||||
|
swaymsg("exit", succeed=False)
|
||||||
|
machine.wait_until_fails("pgrep -x sway")
|
||||||
|
machine.wait_for_file("/tmp/sway-exit-ok")
|
||||||
|
|
||||||
|
# Print fortify runDir contents:
|
||||||
|
print(machine.succeed("find /run/user/1000/fortify"))
|
||||||
|
|
||||||
|
# Verify go test status:
|
||||||
|
machine.wait_for_file("/tmp/gotest")
|
||||||
|
print(machine.succeed("cat /tmp/gotest"))
|
||||||
|
machine.wait_for_file("/tmp/success-gotest")
|
Loading…
Reference in New Issue
Block a user