2024-09-22 00:29:36 +09:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-11-16 21:19:45 +09:00
|
|
|
"fmt"
|
2024-10-23 21:46:21 +09:00
|
|
|
"io/fs"
|
2024-10-11 04:18:15 +09:00
|
|
|
"path"
|
2024-11-19 21:01:41 +09:00
|
|
|
"regexp"
|
2024-09-22 00:29:36 +09:00
|
|
|
"strconv"
|
|
|
|
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/dbus"
|
2024-12-18 13:45:55 +09:00
|
|
|
"git.ophivana.moe/security/fortify/fipc"
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
2024-11-02 03:03:44 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/linux"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrConfig = errors.New("no configuration to seal")
|
2024-11-16 21:19:45 +09:00
|
|
|
ErrUser = errors.New("invalid aid")
|
|
|
|
ErrHome = errors.New("invalid home directory")
|
2024-11-19 21:01:41 +09:00
|
|
|
ErrName = errors.New("invalid username")
|
2024-09-22 00:29:36 +09:00
|
|
|
)
|
|
|
|
|
2024-11-19 21:01:41 +09:00
|
|
|
var posixUsername = regexp.MustCompilePOSIX("^[a-z_]([A-Za-z0-9_-]{0,31}|[A-Za-z0-9_-]{0,30}\\$)$")
|
|
|
|
|
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
|
2024-11-03 03:07:02 +09:00
|
|
|
// dbus proxy message buffer retriever
|
|
|
|
dbusMsg func(f func(msgbuf []string))
|
2024-10-20 22:54:47 +09:00
|
|
|
|
|
|
|
// freedesktop application ID
|
|
|
|
fid string
|
|
|
|
// argv to start process with in the final confined environment
|
|
|
|
command []string
|
|
|
|
// persistent process state store
|
|
|
|
store state.Store
|
|
|
|
|
|
|
|
// process-specific share directory path
|
|
|
|
share string
|
|
|
|
// process-specific share directory path local to XDG_RUNTIME_DIR
|
|
|
|
shareLocal string
|
|
|
|
|
|
|
|
// pass-through enablement tracking from config
|
|
|
|
et system.Enablements
|
2024-12-06 04:25:33 +09:00
|
|
|
// wayland socket direct access
|
|
|
|
directWayland bool
|
2024-10-20 22:54:47 +09:00
|
|
|
|
|
|
|
// prevents sharing from happening twice
|
|
|
|
shared bool
|
|
|
|
// seal system-level component
|
|
|
|
sys *appSealSys
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
linux.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
|
2024-12-18 13:45:55 +09:00
|
|
|
func (a *app) Seal(config *fipc.Config) error {
|
2024-09-22 00:29:36 +09:00
|
|
|
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
|
|
|
|
|
|
|
|
// create seal system component
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys = new(appSealSys)
|
2024-09-22 00:29:36 +09:00
|
|
|
|
2024-11-04 03:15:39 +09:00
|
|
|
// mapped uid
|
2024-11-09 11:57:36 +09:00
|
|
|
if config.Confinement.Sandbox != nil && config.Confinement.Sandbox.MapRealUID {
|
2024-11-04 03:15:39 +09:00
|
|
|
seal.sys.mappedID = a.os.Geteuid()
|
|
|
|
} else {
|
|
|
|
seal.sys.mappedID = 65534
|
|
|
|
}
|
|
|
|
seal.sys.mappedIDString = strconv.Itoa(seal.sys.mappedID)
|
|
|
|
seal.sys.runtime = path.Join("/run/user", seal.sys.mappedIDString)
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
// validate uid and set user info
|
|
|
|
if config.Confinement.AppID < 0 || config.Confinement.AppID > 9999 {
|
|
|
|
return fmsg.WrapError(ErrUser,
|
|
|
|
fmt.Sprintf("aid %d out of range", config.Confinement.AppID))
|
|
|
|
} else {
|
|
|
|
seal.sys.user = appUser{
|
|
|
|
aid: config.Confinement.AppID,
|
|
|
|
as: strconv.Itoa(config.Confinement.AppID),
|
2024-11-18 00:18:21 +09:00
|
|
|
data: config.Confinement.Outer,
|
|
|
|
home: config.Confinement.Inner,
|
2024-11-16 21:19:45 +09:00
|
|
|
username: config.Confinement.Username,
|
|
|
|
}
|
|
|
|
if seal.sys.user.username == "" {
|
|
|
|
seal.sys.user.username = "chronos"
|
2024-11-19 21:01:41 +09:00
|
|
|
} else if !posixUsername.MatchString(seal.sys.user.username) {
|
|
|
|
return fmsg.WrapError(ErrName,
|
|
|
|
fmt.Sprintf("invalid user name %q", seal.sys.user.username))
|
2024-11-16 21:19:45 +09:00
|
|
|
}
|
2024-11-18 00:18:21 +09:00
|
|
|
if seal.sys.user.data == "" || !path.IsAbs(seal.sys.user.data) {
|
2024-11-16 21:19:45 +09:00
|
|
|
return fmsg.WrapError(ErrHome,
|
2024-11-18 00:18:21 +09:00
|
|
|
fmt.Sprintf("invalid home directory %q", seal.sys.user.data))
|
|
|
|
}
|
|
|
|
if seal.sys.user.home == "" {
|
|
|
|
seal.sys.user.home = seal.sys.user.data
|
2024-11-16 21:19:45 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// invoke fsu for full uid
|
|
|
|
if u, err := a.os.Uid(seal.sys.user.aid); err != nil {
|
|
|
|
return fmsg.WrapErrorSuffix(err,
|
|
|
|
"cannot obtain uid from fsu:")
|
2024-09-22 00:29:36 +09:00
|
|
|
} else {
|
2024-11-16 21:19:45 +09:00
|
|
|
seal.sys.user.uid = u
|
|
|
|
seal.sys.user.us = strconv.Itoa(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve supplementary group ids from names
|
|
|
|
seal.sys.user.supp = make([]string, len(config.Confinement.Groups))
|
|
|
|
for i, name := range config.Confinement.Groups {
|
|
|
|
if g, err := a.os.LookupGroup(name); err != nil {
|
|
|
|
return fmsg.WrapError(err,
|
|
|
|
fmt.Sprintf("unknown group %q", name))
|
|
|
|
} else {
|
|
|
|
seal.sys.user.supp[i] = g.Gid
|
|
|
|
}
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-12-18 13:45:55 +09:00
|
|
|
conf := &fipc.SandboxConfig{
|
2024-10-11 04:18:15 +09:00
|
|
|
UserNS: true,
|
|
|
|
Net: true,
|
|
|
|
NoNewSession: true,
|
2024-11-04 22:18:05 +09:00
|
|
|
AutoEtc: true,
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
// 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-12-18 13:45:55 +09:00
|
|
|
b := make([]*fipc.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":
|
2024-11-04 22:18:05 +09:00
|
|
|
|
2024-10-11 04:18:15 +09:00
|
|
|
default:
|
2024-12-18 13:45:55 +09:00
|
|
|
b = append(b, &fipc.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-12-18 13:45:55 +09:00
|
|
|
b := make([]*fipc.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-12-18 13:45:55 +09:00
|
|
|
b = append(b, &fipc.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-12-18 13:45:55 +09:00
|
|
|
conf.Filesystem = append(conf.Filesystem, &fipc.FilesystemConfig{Src: "/dev/dri", Device: true})
|
2024-10-12 22:55:53 +09:00
|
|
|
}
|
2024-10-25 13:31:57 +09:00
|
|
|
|
2024-10-11 04:18:15 +09:00
|
|
|
config.Confinement.Sandbox = conf
|
|
|
|
}
|
2024-12-06 04:25:33 +09:00
|
|
|
seal.directWayland = config.Confinement.Sandbox.DirectWayland
|
2024-11-04 22:18:05 +09:00
|
|
|
if b, err := config.Confinement.Sandbox.Bwrap(a.os); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
seal.sys.bwrap = b
|
|
|
|
}
|
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-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-11-16 21:19:45 +09:00
|
|
|
seal.store = state.NewSimple(seal.RunDirPath, seal.sys.user.as)
|
2024-09-22 00:29:36 +09:00
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
// initialise system interface with full uid
|
|
|
|
seal.sys.I = system.New(seal.sys.user.uid)
|
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-11-16 21:19:45 +09:00
|
|
|
fmsg.VPrintf("created application seal for uid %s (%s) groups: %v, command: %s",
|
|
|
|
seal.sys.user.us, seal.sys.user.username, config.Confinement.Groups, config.Command)
|
2024-09-22 00:29:36 +09:00
|
|
|
|
|
|
|
// seal app and release lock
|
|
|
|
a.seal = seal
|
2024-12-18 13:45:55 +09:00
|
|
|
a.ct = newAppCt(config)
|
2024-09-22 00:29:36 +09:00
|
|
|
return nil
|
|
|
|
}
|