2024-09-22 00:29:36 +09:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"path"
|
|
|
|
|
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/system"
|
2024-09-22 00:29:36 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pulseServer = "PULSE_SERVER"
|
|
|
|
pulseCookie = "PULSE_COOKIE"
|
|
|
|
|
|
|
|
home = "HOME"
|
|
|
|
xdgConfigHome = "XDG_CONFIG_HOME"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrPulseCookie = errors.New("pulse cookie not present")
|
|
|
|
ErrPulseSocket = errors.New("pulse socket not present")
|
|
|
|
ErrPulseMode = errors.New("unexpected pulse socket mode")
|
|
|
|
)
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
func (seal *appSeal) sharePulse(os linux.System) error {
|
2024-10-16 14:38:57 +09:00
|
|
|
if !seal.et.Has(system.EPulse) {
|
2024-09-22 00:29:36 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-10 12:44:08 +09:00
|
|
|
// check PulseAudio directory presence (e.g. `/run/user/%d/pulse`)
|
2024-09-22 00:29:36 +09:00
|
|
|
pd := path.Join(seal.RuntimePath, "pulse")
|
|
|
|
ps := path.Join(pd, "native")
|
|
|
|
if _, err := os.Stat(pd); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapErrorSuffix(err,
|
|
|
|
fmt.Sprintf("cannot access PulseAudio directory %q:", pd))
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrPulseSocket,
|
|
|
|
fmt.Sprintf("PulseAudio directory %q not found", pd))
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
2024-10-10 12:44:08 +09:00
|
|
|
// check PulseAudio socket permission (e.g. `/run/user/%d/pulse/native`)
|
2024-09-22 00:29:36 +09:00
|
|
|
if s, err := os.Stat(ps); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapErrorSuffix(err,
|
|
|
|
fmt.Sprintf("cannot access PulseAudio socket %q:", ps))
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrPulseSocket,
|
|
|
|
fmt.Sprintf("PulseAudio directory %q found but socket does not exist", pd))
|
2024-09-22 00:29:36 +09:00
|
|
|
} else {
|
|
|
|
if m := s.Mode(); m&0o006 != 0o006 {
|
2024-10-16 01:38:59 +09:00
|
|
|
return fmsg.WrapError(ErrPulseMode,
|
|
|
|
fmt.Sprintf("unexpected permissions on %q:", ps), m)
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 12:44:08 +09:00
|
|
|
// hard link pulse socket into target-executable share
|
|
|
|
psi := path.Join(seal.shareLocal, "pulse")
|
2024-10-11 04:18:15 +09:00
|
|
|
p := path.Join(seal.sys.runtime, "pulse", "native")
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.Link(ps, psi)
|
2024-10-15 02:15:55 +09:00
|
|
|
seal.sys.bwrap.Bind(psi, p)
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.bwrap.SetEnv[pulseServer] = "unix:" + p
|
2024-10-10 12:44:08 +09:00
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
// publish current user's pulse cookie for target user
|
2024-10-23 21:46:21 +09:00
|
|
|
if src, err := discoverPulseCookie(os); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
dst := path.Join(seal.share, "pulse-cookie")
|
2024-10-16 01:38:59 +09:00
|
|
|
seal.sys.bwrap.SetEnv[pulseCookie] = dst
|
|
|
|
seal.sys.CopyFile(dst, src)
|
|
|
|
seal.sys.bwrap.Bind(dst, dst)
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// discoverPulseCookie attempts various standard methods to discover the current user's PulseAudio authentication cookie
|
2024-11-02 03:03:44 +09:00
|
|
|
func discoverPulseCookie(os linux.System) (string, error) {
|
2024-09-22 00:29:36 +09:00
|
|
|
if p, ok := os.LookupEnv(pulseCookie); ok {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dotfile $HOME/.pulse-cookie
|
|
|
|
if p, ok := os.LookupEnv(home); ok {
|
|
|
|
p = path.Join(p, ".pulse-cookie")
|
|
|
|
if s, err := os.Stat(p); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-10-16 01:38:59 +09:00
|
|
|
return p, fmsg.WrapErrorSuffix(err,
|
|
|
|
fmt.Sprintf("cannot access PulseAudio cookie %q:", p))
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
// not found, try next method
|
|
|
|
} else if !s.IsDir() {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// $XDG_CONFIG_HOME/pulse/cookie
|
|
|
|
if p, ok := os.LookupEnv(xdgConfigHome); ok {
|
|
|
|
p = path.Join(p, "pulse", "cookie")
|
|
|
|
if s, err := os.Stat(p); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-10-16 01:38:59 +09:00
|
|
|
return p, fmsg.WrapErrorSuffix(err,
|
|
|
|
fmt.Sprintf("cannot access PulseAudio cookie %q:", p))
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
// not found, try next method
|
|
|
|
} else if !s.IsDir() {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 01:38:59 +09:00
|
|
|
return "", fmsg.WrapError(ErrPulseCookie,
|
2024-09-22 00:29:36 +09:00
|
|
|
fmt.Sprintf("cannot locate PulseAudio cookie (tried $%s, $%s/pulse/cookie, $%s/.pulse-cookie)",
|
2024-10-16 01:38:59 +09:00
|
|
|
pulseCookie, xdgConfigHome, home))
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|