aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/system.go
blob: aa62cfb0d982c618a2d440efde89325af83c0129 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package app

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"
	"git.ophivana.moe/cat/fortify/internal/state"
	"git.ophivana.moe/cat/fortify/internal/verbose"
	"git.ophivana.moe/cat/fortify/xcb"
)

// appSeal seals the application with child-related information
type appSeal struct {
	// application unique identifier
	id *appID

	// freedesktop application ID
	fid string
	// argv to start process with in the final confined environment
	command []string
	// environment variables of fortified process
	env []string
	// persistent process state store
	store state.Store

	// uint8 representation of launch method sealed from config
	launchOption uint8
	// process-specific share directory path
	share string
	// process-specific share directory path local to XDG_RUNTIME_DIR
	shareLocal string

	// path to launcher program
	toolPath string
	// pass-through enablement tracking from config
	et state.Enablements

	// prevents sharing from happening twice
	shared bool
	// seal system-level component
	sys *appSealTx

	// used in various sealing operations
	internal.SystemConstants

	// protected by upstream mutex
}

// appendEnv appends an environment variable for the child process
func (seal *appSeal) appendEnv(k, v string) {
	seal.env = append(seal.env, k+"="+v)
}

// appSealTx contains the system-level component of the app seal
type appSealTx struct {
	// reference to D-Bus proxy instance, nil if disabled
	dbus *dbus.Proxy
	// notification from goroutine waiting for dbus.Proxy
	dbusWait chan struct{}
	// upstream address/downstream path used to initialise dbus.Proxy
	dbusAddr *[2][2]string
	// whether system bus proxy is enabled
	dbusSystem bool

	// paths to append/strip ACLs (of target user) from
	acl []*appACLEntry
	// X11 ChangeHosts commands to perform
	xhost []string
	// paths of directories to ensure
	mkdir []appEnsureEntry
	// dst, src pairs of temporarily shared files
	tmpfiles [][2]string
	// dst, src pairs of temporarily hard linked files
	hardlinks [][2]string

	// sealed path to fortify executable, used by shim
	executable string
	// target user UID as an integer
	uid int
	// target user sealed from config
	*user.User

	// prevents commit from happening twice
	complete bool
	// prevents cleanup from happening twice
	closed bool

	// protected by upstream mutex
}

type appEnsureEntry struct {
	path   string
	perm   os.FileMode
	remove bool
}

// ensure appends a directory ensure action
func (tx *appSealTx) ensure(path string, perm os.FileMode) {
	tx.mkdir = append(tx.mkdir, appEnsureEntry{path, perm, false})
}

// ensureEphemeral appends a directory ensure action with removal in rollback
func (tx *appSealTx) ensureEphemeral(path string, perm os.FileMode) {
	tx.mkdir = append(tx.mkdir, appEnsureEntry{path, perm, true})
}

// appACLEntry contains information for applying/reverting an ACL entry
type appACLEntry struct {
	tag   state.Enablement
	path  string
	perms []acl.Perm
}

func (e *appACLEntry) ts() string {
	switch e.tag {
	case state.EnableLength:
		return "Global"
	case state.EnableLength + 1:
		return "Process"
	default:
		return e.tag.String()
	}
}

func (e *appACLEntry) String() string {
	var s = []byte("---")
	for _, p := range e.perms {
		switch p {
		case acl.Read:
			s[0] = 'r'
		case acl.Write:
			s[1] = 'w'
		case acl.Execute:
			s[2] = 'x'
		}
	}
	return string(s)
}

// updatePerm appends an untagged acl update action
func (tx *appSealTx) updatePerm(path string, perms ...acl.Perm) {
	tx.updatePermTag(state.EnableLength+1, path, perms...)
}

// updatePermTag appends an acl update action
// Tagging with state.EnableLength sets cleanup to happen at final active launcher exit,
// while tagging with state.EnableLength+1 will unconditionally clean up on exit.
func (tx *appSealTx) updatePermTag(tag state.Enablement, path string, perms ...acl.Perm) {
	tx.acl = append(tx.acl, &appACLEntry{tag, path, perms})
}

// changeHosts appends target username of an X11 ChangeHosts action
func (tx *appSealTx) changeHosts(username string) {
	tx.xhost = append(tx.xhost, username)
}

// copyFile appends a tmpfiles action
func (tx *appSealTx) copyFile(dst, src string) {
	tx.tmpfiles = append(tx.tmpfiles, [2]string{dst, src})
	tx.updatePerm(dst, acl.Read)
}

// link appends a hardlink action
func (tx *appSealTx) link(oldname, newname string) {
	tx.hardlinks = append(tx.hardlinks, [2]string{oldname, newname})
}

type (
	ChangeHostsError BaseError
	EnsureDirError   BaseError
	TmpfileError     BaseError
	DBusStartError   BaseError
	ACLUpdateError   BaseError
)

// commit applies recorded actions
// order: xhost, mkdir, tmpfiles, hardlinks, dbus, acl
func (tx *appSealTx) commit() error {
	if tx.complete {
		panic("seal transaction committed twice")
	}
	tx.complete = true

	txp := &appSealTx{}
	defer func() {
		// rollback partial commit
		if txp != nil {
			// global changes (x11, ACLs) are always repeated and check for other launchers cannot happen here
			// attempting cleanup here will cause other fortified processes to lose access to them
			// a better (and more secure) fix is to proxy access to these resources and eliminate the ACLs altogether
			tags := new(state.Enablements)
			for e := state.Enablement(0); e < state.EnableLength+2; e++ {
				tags.Set(e)
			}
			if err := txp.revert(tags); err != nil {
				fmt.Println("fortify: errors returned reverting partial commit:", err)
			}
		}
	}()

	// insert xhost entries
	for _, username := range tx.xhost {
		verbose.Printf("inserting XHost entry SI:localuser:%s\n", username)
		if err := xcb.ChangeHosts(xcb.HostModeInsert, xcb.FamilyServerInterpreted, "localuser\x00"+username); err != nil {
			return (*ChangeHostsError)(wrapError(err,
				fmt.Sprintf("cannot insert XHost entry SI:localuser:%s, %s", username, err)))
		} else {
			// register partial commit
			txp.changeHosts(username)
		}
	}

	// ensure directories
	for _, dir := range tx.mkdir {
		verbose.Println("ensuring directory mode:", dir.perm.String(), "path:", dir.path)
		if err := os.Mkdir(dir.path, dir.perm); err != nil && !errors.Is(err, fs.ErrExist) {
			return (*EnsureDirError)(wrapError(err,
				fmt.Sprintf("cannot create directory '%s': %s", dir.path, err)))
		} else {
			// only ephemeral dirs require rollback
			if dir.remove {
				// register partial commit
				txp.ensureEphemeral(dir.path, dir.perm)
			}
		}
	}

	// publish tmpfiles
	for _, tmpfile := range tx.tmpfiles {
		verbose.Println("publishing tmpfile", tmpfile[0], "from", tmpfile[1])
		if err := copyFile(tmpfile[0], tmpfile[1]); err != nil {
			return (*TmpfileError)(wrapError(err,
				fmt.Sprintf("cannot publish tmpfile '%s' from '%s': %s", tmpfile[0], tmpfile[1], err)))
		} else {
			// register partial commit
			txp.copyFile(tmpfile[0], tmpfile[1])
		}
	}

	// create hardlinks
	for _, link := range tx.hardlinks {
		verbose.Println("creating hardlink", link[1], "from", link[0])
		if err := os.Link(link[0], link[1]); err != nil {
			return (*TmpfileError)(wrapError(err,
				fmt.Sprintf("cannot create hardlink '%s' from '%s': %s", link[1], link[0], err)))
		} else {
			// register partial commit
			txp.link(link[0], link[1])
		}
	}

	if tx.dbus != nil {
		// start dbus proxy
		verbose.Printf("session bus proxy on '%s' for upstream '%s'\n", tx.dbusAddr[0][1], tx.dbusAddr[0][0])
		if tx.dbusSystem {
			verbose.Printf("system bus proxy on '%s' for upstream '%s'\n", tx.dbusAddr[1][1], tx.dbusAddr[1][0])
		}
		if err := tx.startDBus(); err != nil {
			return (*DBusStartError)(wrapError(err, "cannot start message bus proxy:", err))
		} else {
			txp.dbus = tx.dbus
			txp.dbusAddr = tx.dbusAddr
			txp.dbusSystem = tx.dbusSystem
			txp.dbusWait = tx.dbusWait
		}
	}

	// apply ACLs
	for _, e := range tx.acl {
		verbose.Println("applying ACL", e, "uid:", tx.Uid, "tag:", e.ts(), "path:", e.path)
		if err := acl.UpdatePerm(e.path, tx.uid, e.perms...); err != nil {
			return (*ACLUpdateError)(wrapError(err,
				fmt.Sprintf("cannot apply ACL to '%s': %s", e.path, err)))
		} else {
			// register partial commit
			txp.updatePermTag(e.tag, e.path, e.perms...)
		}
	}

	// disarm partial commit rollback
	txp = nil
	return nil
}

// revert rolls back recorded actions
// order: acl, dbus, hardlinks, tmpfiles, mkdir, xhost
// errors are printed but not treated as fatal
func (tx *appSealTx) revert(tags *state.Enablements) error {
	if tx.closed {
		panic("seal transaction reverted twice")
	}
	tx.closed = true

	// will be slightly over-sized with ephemeral dirs
	errs := make([]error, 0, len(tx.acl)+1+len(tx.tmpfiles)+len(tx.mkdir)+len(tx.xhost))
	joinError := func(err error, a ...any) {
		var e error
		if err != nil {
			e = wrapError(err, a...)
		}
		errs = append(errs, e)
	}

	// revert ACLs
	for _, e := range tx.acl {
		if tags.Has(e.tag) {
			verbose.Println("stripping ACL", e, "uid:", tx.Uid, "tag:", e.ts(), "path:", e.path)
			err := acl.UpdatePerm(e.path, tx.uid)
			joinError(err, fmt.Sprintf("cannot strip ACL entry from '%s': %s", e.path, err))
		} else {
			verbose.Println("skipping ACL", e, "uid:", tx.Uid, "tag:", e.ts(), "path:", e.path)
		}
	}

	if tx.dbus != nil {
		// stop dbus proxy
		verbose.Println("terminating message bus proxy")
		err := tx.stopDBus()
		joinError(err, "cannot stop message bus proxy:", err)
	}

	// remove hardlinks
	for _, link := range tx.hardlinks {
		verbose.Println("removing hardlink", link[1])
		err := os.Remove(link[1])
		joinError(err, fmt.Sprintf("cannot remove hardlink '%s': %s", link[1], err))
	}

	// remove tmpfiles
	for _, tmpfile := range tx.tmpfiles {
		verbose.Println("removing tmpfile", tmpfile[0])
		err := os.Remove(tmpfile[0])
		joinError(err, fmt.Sprintf("cannot remove tmpfile '%s': %s", tmpfile[0], err))
	}

	// remove (empty) ephemeral directories
	for i := len(tx.mkdir); i > 0; i-- {
		dir := tx.mkdir[i-1]
		if !dir.remove {
			continue
		}

		verbose.Println("destroying ephemeral directory mode:", dir.perm.String(), "path:", dir.path)
		err := os.Remove(dir.path)
		joinError(err, fmt.Sprintf("cannot remove ephemeral directory '%s': %s", dir.path, err))
	}

	if tags.Has(state.EnableX) {
		// rollback xhost insertions
		for _, username := range tx.xhost {
			verbose.Printf("deleting XHost entry SI:localuser:%s\n", username)
			err := xcb.ChangeHosts(xcb.HostModeDelete, xcb.FamilyServerInterpreted, "localuser\x00"+username)
			joinError(err, "cannot remove XHost entry:", err)
		}
	}

	return errors.Join(errs...)
}

// shareAll calls all share methods in sequence
func (seal *appSeal) shareAll(bus [2]*dbus.Config) error {
	if seal.shared {
		panic("seal shared twice")
	}
	seal.shared = true

	seal.shareRuntime()
	if err := seal.shareDisplay(); err != nil {
		return err
	}
	if err := seal.sharePulse(); err != nil {
		return err
	}

	// ensure dbus session bus defaults
	if bus[0] == nil {
		bus[0] = dbus.NewConfig(seal.fid, true, true)
	}

	if err := seal.shareDBus(bus); err != nil {
		return err
	} else if seal.sys.dbusAddr != nil { // set if D-Bus enabled and share successful
		verbose.Println("sealed session proxy", bus[0].Args(seal.sys.dbusAddr[0]))
		if bus[1] != nil {
			verbose.Println("sealed system proxy", bus[1].Args(seal.sys.dbusAddr[1]))
		}
		verbose.Println("message bus proxy final args:", seal.sys.dbus)
	}

	// workaround for launch method sudo
	if seal.launchOption == LaunchMethodSudo {
		targetRuntime := seal.shareRuntimeChild()
		verbose.Printf("child runtime data dir '%s' configured\n", targetRuntime)
	}

	return nil
}