aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/exit.go
blob: 35e866295e7e991f7f613c81214146023184a28b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package internal

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"os/user"

	"git.ophivana.moe/cat/fortify/acl"
	"git.ophivana.moe/cat/fortify/dbus"
	"git.ophivana.moe/cat/fortify/internal/verbose"
	"git.ophivana.moe/cat/fortify/xcb"
)

// ExitState keeps track of various changes fortify made to the system
// as well as other resources that need to be manually released.
// NOT thread safe.
type ExitState struct {
	// target fortified user inherited from app.App
	user *user.User
	// integer UID of targeted user
	uid int
	// returns amount of launcher states read
	launcherStateCount func() (int, error)

	// paths to strip ACLs (of target user) from
	aclCleanupCandidate []string
	// target process capability enablements
	enablements *Enablements
	// whether the xcb.ChangeHosts action was complete
	xcbActionComplete bool

	// reference to D-Bus proxy instance, nil if disabled
	dbusProxy *dbus.Proxy
	// D-Bus wait complete notification
	dbusDone *chan struct{}

	// path to fortify process state information
	statePath string

	// prevents cleanup from happening twice
	complete bool
}

// RegisterRevertPath registers a path with ACLs added by fortify
func (s *ExitState) RegisterRevertPath(p string) {
	s.aclCleanupCandidate = append(s.aclCleanupCandidate, p)
}

// SealEnablements submits the child process enablements
func (s *ExitState) SealEnablements(e Enablements) {
	if s.enablements != nil {
		panic("enablement exit state set twice")
	}
	s.enablements = &e
}

// XcbActionComplete submits xcb.ChangeHosts action completion
func (s *ExitState) XcbActionComplete() {
	if s.xcbActionComplete {
		Fatal("xcb inserted twice")
	}
	s.xcbActionComplete = true
}

// SealDBus submits the child's D-Bus proxy instance
func (s *ExitState) SealDBus(p *dbus.Proxy, done *chan struct{}) {
	if p == nil {
		Fatal("unexpected nil dbus proxy exit state submitted")
	}
	if s.dbusProxy != nil {
		Fatal("dbus proxy exit state set twice")
	}
	s.dbusProxy = p
	s.dbusDone = done
}

// SealStatePath submits filesystem path to the fortify process's state file
func (s *ExitState) SealStatePath(v string) {
	if s.statePath != "" {
		panic("statePath set twice")
	}

	s.statePath = v
}

// NewExit initialises a new ExitState containing basic, unchanging information
// about the fortify process required during cleanup
func NewExit(u *user.User, uid int, f func() (int, error)) *ExitState {
	return &ExitState{
		uid:  uid,
		user: u,

		launcherStateCount: f,
	}
}

func Fatal(msg ...any) {
	fmt.Println(msg...)
	BeforeExit()
	os.Exit(1)
}

var exitState *ExitState

func SealExit(s *ExitState) {
	if exitState != nil {
		panic("exit state submitted twice")
	}

	exitState = s
}

func BeforeExit() {
	if exitState == nil {
		fmt.Println("warn: cleanup attempted before exit state submission")
		return
	}

	exitState.beforeExit()
}

func (s *ExitState) beforeExit() {
	if s.complete {
		panic("beforeExit called twice")
	}

	if s.statePath == "" {
		verbose.Println("State path is unset")
	} else {
		if err := os.Remove(s.statePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
			fmt.Println("Error removing state file:", err)
		}
	}

	if count, err := s.launcherStateCount(); err != nil {
		fmt.Println("Error reading active launchers:", err)
		os.Exit(1)
	} else if count > 0 {
		// other launchers are still active
		verbose.Printf("Found %d active launchers, exiting without cleaning up\n", count)
		return
	}

	verbose.Println("No other launchers active, will clean up")

	if s.xcbActionComplete {
		verbose.Printf("X11: Removing XHost entry SI:localuser:%s\n", s.user.Username)
		if err := xcb.ChangeHosts(xcb.HostModeDelete, xcb.FamilyServerInterpreted, "localuser\x00"+s.user.Username); err != nil {
			fmt.Println("Error removing XHost entry:", err)
		}
	}

	for _, candidate := range s.aclCleanupCandidate {
		if err := acl.UpdatePerm(candidate, s.uid); err != nil {
			fmt.Printf("Error stripping ACL entry from '%s': %s\n", candidate, err)
		}
		verbose.Printf("Stripped ACL entry for user '%s' from '%s'\n", s.user.Username, candidate)
	}

	if s.dbusProxy != nil {
		verbose.Println("D-Bus proxy registered, cleaning up")

		if err := s.dbusProxy.Close(); err != nil {
			if errors.Is(err, os.ErrClosed) {
				verbose.Println("D-Bus proxy already closed")
			} else {
				fmt.Println("Error closing D-Bus proxy:", err)
			}
		}

		// wait for Proxy.Wait to return
		<-*s.dbusDone
	}
}