2024-10-11 01:55:33 +09:00
|
|
|
package shim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/acl"
|
|
|
|
"git.ophivana.moe/security/fortify/internal/verbose"
|
2024-10-11 01:55:33 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// called in the parent process
|
|
|
|
|
2024-10-20 22:54:47 +09:00
|
|
|
func ServeConfig(socket string, uid int, payload *Payload, wl *Wayland) error {
|
2024-10-11 01:55:33 +09:00
|
|
|
if payload.WL {
|
2024-10-20 22:54:47 +09:00
|
|
|
if f, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: wl.Path, Net: "unix"}); err != nil {
|
|
|
|
return err
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
|
|
|
verbose.Println("connected to wayland at", wl)
|
2024-10-20 22:54:47 +09:00
|
|
|
wl.UnixConn = f
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c, err := net.ListenUnix("unix", &net.UnixAddr{Name: socket, Net: "unix"}); err != nil {
|
2024-10-20 22:54:47 +09:00
|
|
|
return err
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
|
|
|
verbose.Println("configuring shim on socket", socket)
|
2024-10-15 02:15:55 +09:00
|
|
|
if err = acl.UpdatePerm(socket, uid, acl.Read, acl.Write, acl.Execute); err != nil {
|
2024-10-11 01:55:33 +09:00
|
|
|
fmt.Println("fortify: cannot change permissions of shim setup socket:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var conn *net.UnixConn
|
|
|
|
if conn, err = c.AcceptUnix(); err != nil {
|
|
|
|
fmt.Println("fortify: cannot accept connection from shim:", err)
|
|
|
|
} else {
|
|
|
|
if err = gob.NewEncoder(conn).Encode(*payload); err != nil {
|
|
|
|
fmt.Println("fortify: cannot stream shim payload:", err)
|
2024-10-15 02:15:55 +09:00
|
|
|
_ = os.Remove(socket)
|
2024-10-11 01:55:33 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.WL {
|
|
|
|
// get raw connection
|
|
|
|
var rc syscall.RawConn
|
2024-10-20 22:54:47 +09:00
|
|
|
if rc, err = wl.SyscallConn(); err != nil {
|
2024-10-11 01:55:33 +09:00
|
|
|
fmt.Println("fortify: cannot obtain raw wayland connection:", err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
// pass wayland socket fd
|
|
|
|
if err = rc.Control(func(fd uintptr) {
|
|
|
|
if _, _, err = conn.WriteMsgUnix(nil, syscall.UnixRights(int(fd)), nil); err != nil {
|
|
|
|
fmt.Println("fortify: cannot pass wayland connection to shim:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = conn.Close()
|
|
|
|
|
|
|
|
// block until shim exits
|
2024-10-20 22:54:47 +09:00
|
|
|
<-wl.done
|
2024-10-11 01:55:33 +09:00
|
|
|
verbose.Println("releasing wayland connection")
|
|
|
|
}); err != nil {
|
|
|
|
fmt.Println("fortify: cannot obtain wayland connection fd:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = c.Close(); err != nil {
|
|
|
|
fmt.Println("fortify: cannot close shim socket:", err)
|
|
|
|
}
|
|
|
|
if err = os.Remove(socket); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
fmt.Println("fortify: cannot remove dangling shim socket:", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-10-20 22:54:47 +09:00
|
|
|
return nil
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
}
|