2024-10-16 01:31:23 +09:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2024-11-03 03:07:02 +09:00
|
|
|
"bytes"
|
2024-10-16 01:31:23 +09:00
|
|
|
"errors"
|
|
|
|
"os"
|
2024-11-03 03:07:02 +09:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2024-10-16 01:31:23 +09:00
|
|
|
|
2024-12-20 00:20:02 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/dbus"
|
|
|
|
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
2024-10-16 01:31:23 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrDBusConfig = errors.New("dbus config not supplied")
|
|
|
|
)
|
|
|
|
|
2024-10-27 11:56:20 +09:00
|
|
|
func (sys *I) MustProxyDBus(sessionPath string, session *dbus.Config, systemPath string, system *dbus.Config) *I {
|
2024-11-03 03:07:02 +09:00
|
|
|
if _, err := sys.ProxyDBus(session, system, sessionPath, systemPath); err != nil {
|
2024-10-27 11:56:20 +09:00
|
|
|
panic(err.Error())
|
|
|
|
} else {
|
|
|
|
return sys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 03:07:02 +09:00
|
|
|
func (sys *I) ProxyDBus(session, system *dbus.Config, sessionPath, systemPath string) (func(f func(msgbuf []string)), error) {
|
2024-10-16 01:31:23 +09:00
|
|
|
d := new(DBus)
|
|
|
|
|
|
|
|
// used by waiting goroutine to notify process exit
|
|
|
|
d.done = make(chan struct{})
|
|
|
|
|
|
|
|
// session bus is mandatory
|
|
|
|
if session == nil {
|
2024-11-03 03:07:02 +09:00
|
|
|
return nil, fmsg.WrapError(ErrDBusConfig,
|
2024-10-16 01:31:23 +09:00
|
|
|
"attempted to seal message bus proxy without session bus config")
|
|
|
|
}
|
|
|
|
|
|
|
|
// system bus is optional
|
|
|
|
d.system = system == nil
|
|
|
|
|
|
|
|
// upstream address, downstream socket path
|
|
|
|
var sessionBus, systemBus [2]string
|
|
|
|
|
|
|
|
// resolve upstream bus addresses
|
|
|
|
sessionBus[0], systemBus[0] = dbus.Address()
|
|
|
|
|
|
|
|
// set paths from caller
|
|
|
|
sessionBus[1], systemBus[1] = sessionPath, systemPath
|
|
|
|
|
|
|
|
// create proxy instance
|
|
|
|
d.proxy = dbus.New(sessionBus, systemBus)
|
|
|
|
|
|
|
|
defer func() {
|
2024-10-21 20:47:02 +09:00
|
|
|
if fmsg.Verbose() && d.proxy.Sealed() {
|
|
|
|
fmsg.VPrintln("sealed session proxy", session.Args(sessionBus))
|
2024-10-16 01:31:23 +09:00
|
|
|
if system != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("sealed system proxy", system.Args(systemBus))
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("message bus proxy final args:", d.proxy)
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// queue operation
|
|
|
|
sys.ops = append(sys.ops, d)
|
|
|
|
|
|
|
|
// seal dbus proxy
|
2024-11-03 03:07:02 +09:00
|
|
|
d.out = &scanToFmsg{msg: new(strings.Builder)}
|
|
|
|
return d.out.F, fmsg.WrapErrorSuffix(d.proxy.Seal(session, system),
|
2024-10-16 01:31:23 +09:00
|
|
|
"cannot seal message bus proxy:")
|
|
|
|
}
|
|
|
|
|
|
|
|
type DBus struct {
|
|
|
|
proxy *dbus.Proxy
|
|
|
|
|
2024-11-03 03:07:02 +09:00
|
|
|
out *scanToFmsg
|
2024-10-16 01:31:23 +09:00
|
|
|
// whether system bus proxy is enabled
|
|
|
|
system bool
|
|
|
|
// notification from goroutine waiting for dbus.Proxy
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
2024-10-16 14:38:57 +09:00
|
|
|
func (d *DBus) Type() Enablement {
|
2024-10-16 01:31:23 +09:00
|
|
|
return Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBus) apply(_ *I) error {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintf("session bus proxy on %q for upstream %q", d.proxy.Session()[1], d.proxy.Session()[0])
|
2024-10-16 01:31:23 +09:00
|
|
|
if d.system {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintf("system bus proxy on %q for upstream %q", d.proxy.System()[1], d.proxy.System()[0])
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// ready channel passed to dbus package
|
|
|
|
ready := make(chan error, 1)
|
|
|
|
|
|
|
|
// background dbus proxy start
|
2024-11-03 03:07:02 +09:00
|
|
|
if err := d.proxy.Start(ready, d.out, true); err != nil {
|
2024-10-16 01:31:23 +09:00
|
|
|
return fmsg.WrapErrorSuffix(err,
|
|
|
|
"cannot start message bus proxy:")
|
|
|
|
}
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("starting message bus proxy:", d.proxy)
|
|
|
|
if fmsg.Verbose() { // save the extra bwrap arg build when verbose logging is off
|
|
|
|
fmsg.VPrintln("message bus proxy bwrap args:", d.proxy.Bwrap())
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// background wait for proxy instance and notify completion
|
|
|
|
go func() {
|
|
|
|
if err := d.proxy.Wait(); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("message bus proxy exited with error:", err)
|
2024-10-16 01:31:23 +09:00
|
|
|
go func() { ready <- err }()
|
|
|
|
} else {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("message bus proxy exit")
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure socket removal so ephemeral directory is empty at revert
|
|
|
|
if err := os.Remove(d.proxy.Session()[1]); err != nil && !errors.Is(err, os.ErrNotExist) {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("cannot remove dangling session bus socket:", err)
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
if d.system {
|
|
|
|
if err := os.Remove(d.proxy.System()[1]); err != nil && !errors.Is(err, os.ErrNotExist) {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("cannot remove dangling system bus socket:", err)
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify proxy completion
|
|
|
|
close(d.done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// ready is not nil if the proxy process faulted
|
|
|
|
if err := <-ready; err != nil {
|
|
|
|
// note that err here is either an I/O error or a predetermined unexpected behaviour error
|
|
|
|
return fmsg.WrapErrorSuffix(err,
|
|
|
|
"message bus proxy fault after start:")
|
|
|
|
}
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("message bus proxy ready")
|
2024-10-16 01:31:23 +09:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBus) revert(_ *I, _ *Criteria) error {
|
|
|
|
// criteria ignored here since dbus is always process-scoped
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("terminating message bus proxy")
|
2024-10-16 01:31:23 +09:00
|
|
|
|
|
|
|
if err := d.proxy.Close(); err != nil {
|
|
|
|
if errors.Is(err, os.ErrClosed) {
|
|
|
|
return fmsg.WrapError(err,
|
|
|
|
"message bus proxy already closed")
|
|
|
|
} else {
|
|
|
|
return fmsg.WrapErrorSuffix(err,
|
|
|
|
"cannot stop message bus proxy:")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// block until proxy wait returns
|
|
|
|
<-d.done
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBus) Is(o Op) bool {
|
|
|
|
d0, ok := o.(*DBus)
|
2024-10-27 11:56:20 +09:00
|
|
|
return ok && d0 != nil &&
|
|
|
|
((d.proxy == nil && d0.proxy == nil) ||
|
|
|
|
(d.proxy != nil && d0.proxy != nil && d.proxy.String() == d0.proxy.String()))
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBus) Path() string {
|
|
|
|
return "(dbus proxy)"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBus) String() string {
|
|
|
|
return d.proxy.String()
|
|
|
|
}
|
2024-11-03 03:07:02 +09:00
|
|
|
|
|
|
|
type scanToFmsg struct {
|
|
|
|
msg *strings.Builder
|
|
|
|
msgbuf []string
|
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scanToFmsg) Write(p []byte) (n int, err error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.write(p, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scanToFmsg) write(p []byte, a int) (int, error) {
|
|
|
|
if i := bytes.IndexByte(p, '\n'); i == -1 {
|
|
|
|
n, _ := s.msg.Write(p)
|
|
|
|
return a + n, nil
|
|
|
|
} else {
|
|
|
|
n, _ := s.msg.Write(p[:i])
|
|
|
|
s.msgbuf = append(s.msgbuf, s.msg.String())
|
|
|
|
s.msg.Reset()
|
|
|
|
return s.write(p[i+1:], a+n+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scanToFmsg) F(f func(msgbuf []string)) {
|
|
|
|
s.mu.RLock()
|
|
|
|
f(s.msgbuf)
|
|
|
|
s.mu.RUnlock()
|
|
|
|
}
|