2024-09-22 00:29:36 +09:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-10-23 21:46:21 +09:00
|
|
|
"io/fs"
|
2024-09-22 00:29:36 +09:00
|
|
|
"os/user"
|
2024-10-11 04:18:15 +09:00
|
|
|
"path"
|
2024-09-22 00:29:36 +09:00
|
|
|
"strconv"
|
|
|
|
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/dbus"
|
|
|
|
"git.ophivana.moe/security/fortify/internal"
|
|
|
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
2024-10-20 22:54:47 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/shim"
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/state"
|
|
|
|
"git.ophivana.moe/security/fortify/internal/system"
|
2024-09-22 00:29:36 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LaunchMethodSudo uint8 = iota
|
|
|
|
LaunchMethodMachineCtl
|
|
|
|
)
|
|
|
|
|
2024-10-18 22:25:09 +09:00
|
|
|
var method = [...]string{
|
|
|
|
LaunchMethodSudo: "sudo",
|
|
|
|
LaunchMethodMachineCtl: "systemd",
|
|
|
|
}
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
var (
|
|
|
|
ErrConfig = errors.New("no configuration to seal")
|
|
|
|
ErrUser = errors.New("unknown user")
|
|
|
|
ErrLaunch = errors.New("invalid launch method")
|
|
|
|
|
|
|
|
ErrSudo = errors.New("sudo not available")
|
|
|
|
ErrSystemd = errors.New("systemd not available")
|
|
|
|
ErrMachineCtl = errors.New("machinectl not available")
|
|
|
|
)
|
|
|
|
|
2024-10-20 22:54:47 +09:00
|
|
|
// appSeal seals the application with child-related information
|
|
|
|
type appSeal struct {
|
|
|
|
// app unique ID string representation
|
|
|
|
id string
|
|
|
|
// wayland mediation, disabled if nil
|
|
|
|
wl *shim.Wayland
|
|
|
|
|
|
|
|
// freedesktop application ID
|
|
|
|
fid string
|
|
|
|
// argv to start process with in the final confined environment
|
|
|
|
command []string
|
|
|
|
// persistent process state store
|
|
|
|
store state.Store
|
|
|
|
|
|
|
|
// uint8 representation of launch method sealed from config
|
|
|
|
launchOption uint8
|
|
|
|
// process-specific share directory path
|
|
|
|
share string
|
|
|
|
// process-specific share directory path local to XDG_RUNTIME_DIR
|
|
|
|
shareLocal string
|
|
|
|
|
|
|
|
// path to launcher program
|
|
|
|
toolPath string
|
|
|
|
// pass-through enablement tracking from config
|
|
|
|
et system.Enablements
|
|
|
|
|
|
|
|
// prevents sharing from happening twice
|
|
|
|
shared bool
|
|
|
|
// seal system-level component
|
|
|
|
sys *appSealSys
|
|
|
|
|
2024-10-23 21:46:21 +09:00
|
|
|
internal.Paths
|
2024-10-20 22:54:47 +09:00
|
|
|
|
|
|
|
// protected by upstream mutex
|
|
|
|
}
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
// Seal seals the app launch context
|
|
|
|
func (a *app) Seal(config *Config) error {
|
|
|
|
a.lock.Lock()
|
|
|
|
defer a.lock.Unlock()
|
|
|
|
|
|
|
|
if a.seal != nil {
|
|
|
|
panic("app sealed twice")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrConfig,
|
|
|
|
"attempted to seal app with nil config")
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// create seal
|
|
|
|
seal := new(appSeal)
|
|
|
|
|
|
|
|
// fetch system constants
|
2024-10-23 21:46:21 +09:00
|
|
|
seal.Paths = a.os.Paths()
|
2024-09-22 00:29:36 +09:00
|
|
|
|
|
|
|
// pass through config values
|
2024-10-20 00:07:48 +09:00
|
|
|
seal.id = a.id.String()
|
2024-09-22 00:29:36 +09:00
|
|
|
seal.fid = config.ID
|
|
|
|
seal.command = config.Command
|
|
|
|
|
|
|
|
// parses launch method text and looks up tool path
|
|
|
|
switch config.Method {
|
2024-10-18 22:25:09 +09:00
|
|
|
case method[LaunchMethodSudo]:
|
2024-09-22 00:29:36 +09:00
|
|
|
seal.launchOption = LaunchMethodSudo
|
2024-10-23 21:46:21 +09:00
|
|
|
if sudoPath, err := a.os.LookPath("sudo"); err != nil {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrSudo,
|
|
|
|
"sudo not found")
|
2024-09-22 00:29:36 +09:00
|
|
|
} else {
|
|
|
|
seal.toolPath = sudoPath
|
|
|
|
}
|
2024-10-18 22:25:09 +09:00
|
|
|
case method[LaunchMethodMachineCtl]:
|
2024-09-22 00:29:36 +09:00
|
|
|
seal.launchOption = LaunchMethodMachineCtl
|
2024-10-27 12:08:17 +09:00
|
|
|
if !a.os.SdBooted() {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrSystemd,
|
|
|
|
"system has not been booted with systemd as init system")
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
2024-10-23 21:46:21 +09:00
|
|
|
if machineCtlPath, err := a.os.LookPath("machinectl"); err != nil {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrMachineCtl,
|
|
|
|
"machinectl not found")
|
2024-09-22 00:29:36 +09:00
|
|
|
} else {
|
|
|
|
seal.toolPath = machineCtlPath
|
|
|
|
}
|
|
|
|
default:
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrLaunch,
|
|
|
|
"invalid launch method")
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// create seal system component
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys = new(appSealSys)
|
2024-09-22 00:29:36 +09:00
|
|
|
|
|
|
|
// look up fortify executable path
|
2024-10-23 21:46:21 +09:00
|
|
|
if p, err := a.os.Executable(); err != nil {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapErrorSuffix(err, "cannot look up fortify executable path:")
|
2024-09-22 00:29:36 +09:00
|
|
|
} else {
|
|
|
|
seal.sys.executable = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up user from system
|
2024-10-23 21:46:21 +09:00
|
|
|
if u, err := a.os.Lookup(config.User); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
if errors.As(err, new(user.UnknownUserError)) {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrUser, "unknown user", config.User)
|
2024-09-22 00:29:36 +09:00
|
|
|
} else {
|
|
|
|
// unreachable
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.user = u
|
2024-10-11 04:18:15 +09:00
|
|
|
seal.sys.runtime = path.Join("/run/user", u.Uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// map sandbox config to bwrap
|
|
|
|
if config.Confinement.Sandbox == nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("sandbox configuration not supplied, PROCEED WITH CAUTION")
|
2024-10-11 04:18:15 +09:00
|
|
|
|
|
|
|
// permissive defaults
|
|
|
|
conf := &SandboxConfig{
|
|
|
|
UserNS: true,
|
|
|
|
Net: true,
|
|
|
|
NoNewSession: true,
|
|
|
|
}
|
|
|
|
// bind entries in /
|
2024-10-23 21:46:21 +09:00
|
|
|
if d, err := a.os.ReadDir("/"); err != nil {
|
2024-10-11 04:18:15 +09:00
|
|
|
return err
|
|
|
|
} else {
|
2024-10-12 22:33:04 +09:00
|
|
|
b := make([]*FilesystemConfig, 0, len(d))
|
2024-10-11 04:18:15 +09:00
|
|
|
for _, ent := range d {
|
2024-10-25 13:31:57 +09:00
|
|
|
p := "/" + ent.Name()
|
|
|
|
switch p {
|
|
|
|
case "/proc":
|
|
|
|
case "/dev":
|
|
|
|
case "/run":
|
|
|
|
case "/tmp":
|
|
|
|
case "/mnt":
|
|
|
|
|
|
|
|
case "/etc":
|
|
|
|
b = append(b, &FilesystemConfig{Src: p, Dst: "/dev/fortify/etc", Write: false, Must: true})
|
2024-10-11 04:18:15 +09:00
|
|
|
default:
|
2024-10-12 22:33:04 +09:00
|
|
|
b = append(b, &FilesystemConfig{Src: p, Write: true, Must: true})
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
}
|
2024-10-12 22:33:04 +09:00
|
|
|
conf.Filesystem = append(conf.Filesystem, b...)
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
// bind entries in /run
|
2024-10-23 21:46:21 +09:00
|
|
|
if d, err := a.os.ReadDir("/run"); err != nil {
|
2024-10-11 04:18:15 +09:00
|
|
|
return err
|
|
|
|
} else {
|
2024-10-12 22:33:04 +09:00
|
|
|
b := make([]*FilesystemConfig, 0, len(d))
|
2024-10-11 04:18:15 +09:00
|
|
|
for _, ent := range d {
|
|
|
|
name := ent.Name()
|
|
|
|
switch name {
|
|
|
|
case "user":
|
|
|
|
case "dbus":
|
|
|
|
default:
|
|
|
|
p := "/run/" + name
|
2024-10-12 22:33:04 +09:00
|
|
|
b = append(b, &FilesystemConfig{Src: p, Write: true, Must: true})
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
}
|
2024-10-12 22:33:04 +09:00
|
|
|
conf.Filesystem = append(conf.Filesystem, b...)
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
2024-10-13 02:43:00 +09:00
|
|
|
// hide nscd from sandbox if present
|
|
|
|
nscd := "/var/run/nscd"
|
2024-10-23 21:46:21 +09:00
|
|
|
if _, err := a.os.Stat(nscd); !errors.Is(err, fs.ErrNotExist) {
|
2024-10-16 01:38:59 +09:00
|
|
|
conf.Override = append(conf.Override, nscd)
|
2024-10-13 02:43:00 +09:00
|
|
|
}
|
2024-10-12 22:55:53 +09:00
|
|
|
// bind GPU stuff
|
2024-10-16 14:38:57 +09:00
|
|
|
if config.Confinement.Enablements.Has(system.EX11) || config.Confinement.Enablements.Has(system.EWayland) {
|
2024-10-12 22:55:53 +09:00
|
|
|
conf.Filesystem = append(conf.Filesystem, &FilesystemConfig{Src: "/dev/dri", Device: true})
|
|
|
|
}
|
2024-10-25 13:31:57 +09:00
|
|
|
// link host /etc to prevent passwd/group from being overwritten
|
|
|
|
if d, err := a.os.ReadDir("/etc"); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
b := make([][2]string, 0, len(d))
|
|
|
|
for _, ent := range d {
|
|
|
|
name := ent.Name()
|
|
|
|
switch name {
|
|
|
|
case "passwd":
|
|
|
|
case "group":
|
|
|
|
|
|
|
|
case "mtab":
|
|
|
|
b = append(b, [2]string{
|
|
|
|
"/proc/mounts",
|
|
|
|
"/etc/" + name,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
b = append(b, [2]string{
|
|
|
|
"/dev/fortify/etc/" + name,
|
|
|
|
"/etc/" + name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conf.Link = append(conf.Link, b...)
|
|
|
|
}
|
|
|
|
|
2024-10-11 04:18:15 +09:00
|
|
|
config.Confinement.Sandbox = conf
|
|
|
|
}
|
|
|
|
seal.sys.bwrap = config.Confinement.Sandbox.Bwrap()
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.override = config.Confinement.Sandbox.Override
|
2024-10-11 04:18:15 +09:00
|
|
|
if seal.sys.bwrap.SetEnv == nil {
|
|
|
|
seal.sys.bwrap.SetEnv = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2024-10-20 22:54:47 +09:00
|
|
|
// create wayland struct and client wait channel if mediated wayland is enabled
|
|
|
|
// this field being set enables mediated wayland setup later on
|
2024-10-11 04:18:15 +09:00
|
|
|
if config.Confinement.Sandbox.Wayland {
|
2024-10-20 22:54:47 +09:00
|
|
|
seal.wl = shim.NewWayland()
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// open process state store
|
|
|
|
// the simple store only starts holding an open file after first action
|
|
|
|
// store activity begins after Start is called and must end before Wait
|
2024-10-23 21:46:21 +09:00
|
|
|
seal.store = state.NewSimple(seal.RunDirPath, seal.sys.user.Uid)
|
2024-09-22 00:29:36 +09:00
|
|
|
|
|
|
|
// parse string UID
|
2024-10-16 01:38:59 +09:00
|
|
|
if u, err := strconv.Atoi(seal.sys.user.Uid); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
// unreachable unless kernel bug
|
|
|
|
panic("uid parse")
|
|
|
|
} else {
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.I = system.New(u)
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass through enablements
|
|
|
|
seal.et = config.Confinement.Enablements
|
|
|
|
|
|
|
|
// this method calls all share methods in sequence
|
2024-10-23 21:46:21 +09:00
|
|
|
if err := seal.shareAll([2]*dbus.Config{config.Confinement.SessionBus, config.Confinement.SystemBus}, a.os); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// verbose log seal information
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("created application seal as user",
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.user.Username, "("+seal.sys.user.Uid+"),",
|
2024-09-22 00:29:36 +09:00
|
|
|
"method:", config.Method+",",
|
|
|
|
"launcher:", seal.toolPath+",",
|
|
|
|
"command:", config.Command)
|
|
|
|
|
|
|
|
// seal app and release lock
|
|
|
|
a.seal = seal
|
|
|
|
return nil
|
|
|
|
}
|