2024-09-22 00:29:36 +09:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-11-04 22:18:05 +09:00
|
|
|
"errors"
|
2024-10-15 02:15:55 +09:00
|
|
|
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/dbus"
|
|
|
|
"git.ophivana.moe/security/fortify/helper/bwrap"
|
2024-11-04 22:18:05 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/linux"
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/system"
|
2024-09-22 00:29:36 +09:00
|
|
|
)
|
|
|
|
|
2024-11-06 03:49:39 +09:00
|
|
|
const fTmp = "/fortify"
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
// Config is used to seal an *App
|
|
|
|
type Config struct {
|
|
|
|
// D-Bus application ID
|
|
|
|
ID string `json:"id"`
|
|
|
|
// value passed through to the child process as its argv
|
|
|
|
Command []string `json:"command"`
|
|
|
|
|
|
|
|
// child confinement configuration
|
|
|
|
Confinement ConfinementConfig `json:"confinement"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfinementConfig defines fortified child's confinement
|
|
|
|
type ConfinementConfig struct {
|
2024-11-16 21:19:45 +09:00
|
|
|
// numerical application id, determines uid in the init namespace
|
|
|
|
AppID int `json:"app_id"`
|
|
|
|
// list of supplementary groups to inherit
|
|
|
|
Groups []string `json:"groups"`
|
|
|
|
// passwd username in the sandbox, defaults to chronos
|
|
|
|
Username string `json:"username,omitempty"`
|
2024-11-18 00:18:21 +09:00
|
|
|
// home directory in sandbox, empty for outer
|
|
|
|
Inner string `json:"home_inner"`
|
|
|
|
// home directory in init namespace
|
|
|
|
Outer string `json:"home"`
|
2024-10-11 02:01:03 +09:00
|
|
|
// bwrap sandbox confinement configuration
|
2024-10-11 04:18:15 +09:00
|
|
|
Sandbox *SandboxConfig `json:"sandbox"`
|
2024-10-11 02:01:03 +09:00
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
// reference to a system D-Bus proxy configuration,
|
|
|
|
// nil value disables system bus proxy
|
|
|
|
SystemBus *dbus.Config `json:"system_bus,omitempty"`
|
|
|
|
// reference to a session D-Bus proxy configuration,
|
|
|
|
// nil value makes session bus proxy assume built-in defaults
|
|
|
|
SessionBus *dbus.Config `json:"session_bus,omitempty"`
|
|
|
|
|
|
|
|
// child capability enablements
|
2024-10-16 14:38:57 +09:00
|
|
|
Enablements system.Enablements `json:"enablements"`
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
2024-10-11 04:18:15 +09:00
|
|
|
|
|
|
|
// SandboxConfig describes resources made available to the sandbox.
|
|
|
|
type SandboxConfig struct {
|
|
|
|
// unix hostname within sandbox
|
|
|
|
Hostname string `json:"hostname,omitempty"`
|
|
|
|
// userns availability within sandbox
|
|
|
|
UserNS bool `json:"userns,omitempty"`
|
|
|
|
// share net namespace
|
|
|
|
Net bool `json:"net,omitempty"`
|
2024-11-06 03:49:39 +09:00
|
|
|
// share all devices
|
|
|
|
Dev bool `json:"dev,omitempty"`
|
2024-10-11 04:18:15 +09:00
|
|
|
// do not run in new session
|
|
|
|
NoNewSession bool `json:"no_new_session,omitempty"`
|
2024-11-04 03:15:39 +09:00
|
|
|
// map target user uid to privileged user uid in the user namespace
|
2024-11-09 11:57:36 +09:00
|
|
|
MapRealUID bool `json:"map_real_uid"`
|
2024-12-06 04:25:33 +09:00
|
|
|
// direct access to wayland socket
|
|
|
|
DirectWayland bool `json:"direct_wayland,omitempty"`
|
2024-10-11 04:18:15 +09:00
|
|
|
|
|
|
|
// final environment variables
|
|
|
|
Env map[string]string `json:"env"`
|
2024-10-12 22:33:04 +09:00
|
|
|
// sandbox host filesystem access
|
|
|
|
Filesystem []*FilesystemConfig `json:"filesystem"`
|
2024-10-25 13:29:01 +09:00
|
|
|
// symlinks created inside the sandbox
|
|
|
|
Link [][2]string `json:"symlink"`
|
2024-11-04 22:18:05 +09:00
|
|
|
// automatically set up /etc symlinks
|
|
|
|
AutoEtc bool `json:"auto_etc"`
|
2024-10-16 01:38:59 +09:00
|
|
|
// paths to override by mounting tmpfs over them
|
|
|
|
Override []string `json:"override"`
|
2024-10-12 22:33:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
type FilesystemConfig struct {
|
|
|
|
// mount point in sandbox, same as src if empty
|
|
|
|
Dst string `json:"dst,omitempty"`
|
|
|
|
// host filesystem path to make available to sandbox
|
|
|
|
Src string `json:"src"`
|
|
|
|
// write access
|
|
|
|
Write bool `json:"write,omitempty"`
|
|
|
|
// device access
|
|
|
|
Device bool `json:"dev,omitempty"`
|
|
|
|
// exit if unable to share
|
|
|
|
Must bool `json:"require,omitempty"`
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
|
2024-10-15 02:15:55 +09:00
|
|
|
// Bwrap returns the address of the corresponding bwrap.Config to s.
|
|
|
|
// Note that remaining tmpfs entries must be queued by the caller prior to launch.
|
2024-11-04 22:18:05 +09:00
|
|
|
func (s *SandboxConfig) Bwrap(os linux.System) (*bwrap.Config, error) {
|
2024-10-11 04:18:15 +09:00
|
|
|
if s == nil {
|
2024-11-04 22:18:05 +09:00
|
|
|
return nil, errors.New("nil sandbox config")
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
|
|
|
|
2024-11-04 22:18:05 +09:00
|
|
|
var uid int
|
2024-11-09 11:57:36 +09:00
|
|
|
if !s.MapRealUID {
|
2024-11-04 03:15:39 +09:00
|
|
|
uid = 65534
|
2024-11-04 22:18:05 +09:00
|
|
|
} else {
|
|
|
|
uid = os.Geteuid()
|
2024-11-04 03:15:39 +09:00
|
|
|
}
|
|
|
|
|
2024-10-15 02:15:55 +09:00
|
|
|
conf := (&bwrap.Config{
|
2024-10-11 04:18:15 +09:00
|
|
|
Net: s.Net,
|
|
|
|
UserNS: s.UserNS,
|
|
|
|
Hostname: s.Hostname,
|
|
|
|
Clearenv: true,
|
|
|
|
SetEnv: s.Env,
|
|
|
|
NewSession: !s.NoNewSession,
|
|
|
|
DieWithParent: true,
|
2024-10-14 02:27:02 +09:00
|
|
|
AsInit: true,
|
2024-10-15 02:15:55 +09:00
|
|
|
|
|
|
|
// initialise map
|
2024-11-04 22:18:05 +09:00
|
|
|
Chmod: make(bwrap.ChmodConfig),
|
2024-10-15 02:15:55 +09:00
|
|
|
}).
|
2024-11-04 03:15:39 +09:00
|
|
|
SetUID(uid).SetGID(uid).
|
2024-11-06 03:49:39 +09:00
|
|
|
Procfs("/proc").
|
|
|
|
Tmpfs(fTmp, 4*1024)
|
|
|
|
|
|
|
|
if !s.Dev {
|
|
|
|
conf.DevTmpfs("/dev").Mqueue("/dev/mqueue")
|
|
|
|
} else {
|
|
|
|
conf.Bind("/dev", "/dev", false, true, true)
|
|
|
|
}
|
2024-10-11 04:18:15 +09:00
|
|
|
|
2024-11-04 22:18:05 +09:00
|
|
|
if !s.AutoEtc {
|
|
|
|
conf.Dir("/etc")
|
|
|
|
}
|
|
|
|
|
2024-10-12 22:33:04 +09:00
|
|
|
for _, c := range s.Filesystem {
|
|
|
|
if c == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-10-15 02:15:55 +09:00
|
|
|
src := c.Src
|
|
|
|
dest := c.Dst
|
2024-10-12 22:33:04 +09:00
|
|
|
if c.Dst == "" {
|
2024-10-15 02:15:55 +09:00
|
|
|
dest = c.Src
|
2024-10-12 22:33:04 +09:00
|
|
|
}
|
2024-10-15 02:15:55 +09:00
|
|
|
conf.Bind(src, dest, !c.Must, c.Write, c.Device)
|
2024-10-13 02:39:27 +09:00
|
|
|
}
|
|
|
|
|
2024-10-25 13:29:01 +09:00
|
|
|
for _, l := range s.Link {
|
|
|
|
conf.Symlink(l[0], l[1])
|
|
|
|
}
|
|
|
|
|
2024-11-04 22:18:05 +09:00
|
|
|
if s.AutoEtc {
|
2024-11-06 03:49:39 +09:00
|
|
|
conf.Bind("/etc", fTmp+"/etc")
|
2024-11-04 22:18:05 +09:00
|
|
|
|
|
|
|
// link host /etc contents to prevent passwd/group from being overwritten
|
|
|
|
if d, err := os.ReadDir("/etc"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
for _, ent := range d {
|
|
|
|
name := ent.Name()
|
|
|
|
switch name {
|
|
|
|
case "passwd":
|
|
|
|
case "group":
|
|
|
|
|
|
|
|
case "mtab":
|
|
|
|
conf.Symlink("/proc/mounts", "/etc/"+name)
|
|
|
|
default:
|
2024-11-06 03:49:39 +09:00
|
|
|
conf.Symlink(fTmp+"/etc/"+name, "/etc/"+name)
|
2024-11-04 22:18:05 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
2024-10-11 04:18:15 +09:00
|
|
|
}
|
2024-10-12 19:46:07 +09:00
|
|
|
|
|
|
|
// Template returns a fully populated instance of Config.
|
|
|
|
func Template() *Config {
|
|
|
|
return &Config{
|
2024-11-16 21:19:45 +09:00
|
|
|
ID: "org.chromium.Chromium",
|
2024-10-12 19:46:07 +09:00
|
|
|
Command: []string{
|
|
|
|
"chromium",
|
|
|
|
"--ignore-gpu-blocklist",
|
|
|
|
"--disable-smooth-scrolling",
|
|
|
|
"--enable-features=UseOzonePlatform",
|
|
|
|
"--ozone-platform=wayland",
|
|
|
|
},
|
|
|
|
Confinement: ConfinementConfig{
|
2024-11-16 21:19:45 +09:00
|
|
|
AppID: 9,
|
|
|
|
Groups: []string{"video"},
|
|
|
|
Username: "chronos",
|
2024-11-18 00:18:21 +09:00
|
|
|
Outer: "/var/lib/persist/home/org.chromium.Chromium",
|
|
|
|
Inner: "/var/lib/fortify",
|
2024-10-12 19:46:07 +09:00
|
|
|
Sandbox: &SandboxConfig{
|
2024-12-06 04:25:33 +09:00
|
|
|
Hostname: "localhost",
|
|
|
|
UserNS: true,
|
|
|
|
Net: true,
|
|
|
|
NoNewSession: true,
|
|
|
|
MapRealUID: true,
|
|
|
|
Dev: true,
|
|
|
|
DirectWayland: false,
|
2024-10-12 19:46:07 +09:00
|
|
|
// example API credentials pulled from Google Chrome
|
|
|
|
// DO NOT USE THESE IN A REAL BROWSER
|
|
|
|
Env: map[string]string{
|
|
|
|
"GOOGLE_API_KEY": "AIzaSyBHDrl33hwRp4rMQY0ziRbj8K9LPA6vUCY",
|
|
|
|
"GOOGLE_DEFAULT_CLIENT_ID": "77185425430.apps.googleusercontent.com",
|
|
|
|
"GOOGLE_DEFAULT_CLIENT_SECRET": "OTJgUOQcT7lO7GsGZq2G4IlT",
|
|
|
|
},
|
2024-10-12 22:33:04 +09:00
|
|
|
Filesystem: []*FilesystemConfig{
|
2024-11-04 22:18:05 +09:00
|
|
|
{Src: "/nix/store"},
|
|
|
|
{Src: "/run/current-system"},
|
|
|
|
{Src: "/run/opengl-driver"},
|
|
|
|
{Src: "/var/db/nix-channels"},
|
|
|
|
{Src: "/home/chronos", Write: true, Must: true},
|
|
|
|
{Src: "/dev/dri", Device: true},
|
2024-10-12 22:33:04 +09:00
|
|
|
},
|
2024-11-04 22:18:05 +09:00
|
|
|
Link: [][2]string{{"/run/user/65534", "/run/user/150"}},
|
|
|
|
AutoEtc: true,
|
2024-10-16 01:38:59 +09:00
|
|
|
Override: []string{"/var/run/nscd"},
|
2024-10-12 19:46:07 +09:00
|
|
|
},
|
|
|
|
SystemBus: &dbus.Config{
|
|
|
|
See: nil,
|
|
|
|
Talk: []string{"org.bluez", "org.freedesktop.Avahi", "org.freedesktop.UPower"},
|
|
|
|
Own: nil,
|
|
|
|
Call: nil,
|
|
|
|
Broadcast: nil,
|
|
|
|
Log: false,
|
|
|
|
Filter: true,
|
|
|
|
},
|
|
|
|
SessionBus: &dbus.Config{
|
|
|
|
See: nil,
|
|
|
|
Talk: []string{"org.freedesktop.Notifications", "org.freedesktop.FileManager1", "org.freedesktop.ScreenSaver",
|
|
|
|
"org.freedesktop.secrets", "org.kde.kwalletd5", "org.kde.kwalletd6", "org.gnome.SessionManager"},
|
|
|
|
Own: []string{"org.chromium.Chromium.*", "org.mpris.MediaPlayer2.org.chromium.Chromium.*",
|
|
|
|
"org.mpris.MediaPlayer2.chromium.*"},
|
|
|
|
Call: map[string]string{"org.freedesktop.portal.*": "*"},
|
|
|
|
Broadcast: map[string]string{"org.freedesktop.portal.*": "@/org/freedesktop/portal/*"},
|
|
|
|
Log: false,
|
|
|
|
Filter: true,
|
|
|
|
},
|
2024-10-16 14:38:57 +09:00
|
|
|
Enablements: system.EWayland.Mask() | system.EDBus.Mask() | system.EPulse.Mask(),
|
2024-10-12 19:46:07 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|