forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avahi_observe.go
484 lines (409 loc) · 13.6 KB
/
avahi_observe.go
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package builtin
import (
"strings"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/dbus"
"github.com/snapcore/snapd/snap"
)
const avahiObserveSummary = `allows discovery on a local network via the mDNS/DNS-SD protocol suite`
const avahiObserveBaseDeclarationSlots = `
avahi-observe:
allow-installation:
slot-snap-type:
- app
- core
deny-auto-connection: true
deny-connection:
on-classic: false
`
const avahiObservePermanentSlotAppArmor = `
network netlink,
# Allow access to daemon to create socket
/{,var/}run/avahi-daemon/ w,
/{,var/}run/avahi-daemon/{pid,socket} rwk,
# Description: Allow operating as the avahi service. This gives
# privileged access to the system.
#include <abstractions/dbus-strict>
dbus (send)
bus=system
path=/org/freedesktop/DBus
interface=org.freedesktop.DBus
member={Request,Release}Name
peer=(name=org.freedesktop.DBus, label=unconfined),
dbus (receive, send)
bus=system
path=/org/freedesktop/DBus
interface=org.freedesktop.DBus
member=GetConnectionUnixProcessID
peer=(label=unconfined),
dbus (receive, send)
bus=system
path=/org/freedesktop/DBus
interface=org.freedesktop.DBus
member=GetConnectionUnixUser
peer=(label=unconfined),
# Allow binding the service to the requested connection name
dbus (bind)
bus=system
name="org.freedesktop.Avahi",
# Allow traffic to/from our path and interface with any method for unconfined
# clients to talk to our service.
dbus (receive, send)
bus=system
path=/org/freedesktop/Avahi{,/**}
interface=org.freedesktop.Avahi*
peer=(label=unconfined),
# Allow traffic to/from org.freedesktop.DBus for Avahi service
dbus (receive, send)
bus=system
path=/org/freedesktop/Avahi{,/**}
interface=org.freedesktop.DBus.*
peer=(label=unconfined),
`
// Note: avahiObserveConnectedSlotAppArmor is also used by avahi-control in AppArmorConnectedSlot
const avahiObserveConnectedSlotAppArmor = `
# Description: Allow operating as the avahi service. This gives
# privileged access to the system.
#include <abstractions/dbus-strict>
# Allow all access to Avahi service
dbus (receive)
bus=system
path=/
interface=org.freedesktop.DBus.Peer
member=Ping
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (receive)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.Server
member=StateChanged
peer=(label=###PLUG_SECURITY_TAGS###),
# address resolving
dbus (receive)
bus=system
path=/Client*/AddressResolver*
interface=org.freedesktop.Avahi.AddressResolver
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.AddressResolver
peer=(name=org.freedesktop.Avahi, label=###PLUG_SECURITY_TAGS###),
# host name resolving
dbus (receive)
bus=system
path=/Client*/HostNameResolver*
interface=org.freedesktop.Avahi.HostNameResolver
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.HostNameResolver
peer=(label=###PLUG_SECURITY_TAGS###),
# service resolving
dbus (receive)
bus=system
path=/Client*/ServiceResolver*
interface=org.freedesktop.Avahi.ServiceResolver
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.ServiceResolver
peer=(label=###PLUG_SECURITY_TAGS###),
# domain browsing
dbus (receive)
bus=system
path=/Client*/DomainBrowser*
interface=org.freedesktop.Avahi.DomainBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.DomainBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
# record browsing
dbus (receive)
bus=system
path=/Client*/RecordBrowser*
interface=org.freedesktop.Avahi.RecordBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.RecordBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
# service browsing
dbus (receive)
bus=system
path=/Client*/ServiceBrowser*
interface=org.freedesktop.Avahi.ServiceBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.ServiceBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
# service type browsing
dbus (receive)
bus=system
path=/Client*/ServiceTypeBrowser*
interface=org.freedesktop.Avahi.ServiceTypeBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
dbus (send)
bus=system
interface=org.freedesktop.Avahi.ServiceTypeBrowser
peer=(label=###PLUG_SECURITY_TAGS###),
`
// Note: avahiObservePermanentSlotDBus is used by avahi-control in DBusPermanentSlot
const avahiObservePermanentSlotDBus = `
<!-- Only root can own the Avahi service -->
<policy user="root">
<allow own="org.freedesktop.Avahi"/>
</policy>
<!-- Allow anyone to invoke methods on Avahi server, except SetHostName -->
<policy context="default">
<allow send_destination="org.freedesktop.Avahi"/>
<allow receive_sender="org.freedesktop.Avahi"/>
<deny send_destination="org.freedesktop.Avahi"
send_interface="org.freedesktop.Avahi.Server" send_member="SetHostName"/>
</policy>
<!-- bus policy for "netdev" group is removed as it does not apply to Ubuntu core -->
<!-- Allow root to set SetHostName -->
<policy user="root">
<allow send_destination="org.freedesktop.Avahi"/>
<allow receive_sender="org.freedesktop.Avahi"/>
</policy>
`
// Note: avahiObserveConnectedPlugAppArmor is also used by avahi-control in AppArmorConnectedPlug
const avahiObserveConnectedPlugAppArmor = `
# Description: allows domain, record, service, and service type browsing
# as well as address, host and service resolving
/{,var/}run/avahi-daemon/socket rw,
#include <abstractions/dbus-strict>
dbus (send)
bus=system
path=/
interface=org.freedesktop.DBus.Peer
member=Ping
peer=(name=org.freedesktop.Avahi,label=###SLOT_SECURITY_TAGS###),
# Allow accessing DBus properties and resolving
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member={Get*,Resolve*,IsNSSSupportAvailable}
peer=(name=org.freedesktop.Avahi,label=###SLOT_SECURITY_TAGS###),
# Allow receiving anything from the slot server
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.Server
peer=(label=###SLOT_SECURITY_TAGS###),
# Don't allow introspection since it reveals too much (path is not service
# specific for unconfined)
# do not use peer=(label=unconfined) here since this is DBus activated
#dbus (send)
# bus=system
# path=/
# interface=org.freedesktop.DBus.Introspectable
# member=Introspect,
# These allows tampering with other snap's browsers, so don't autoconnect for
# now.
# address resolving
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=AddressResolverNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/AddressResolver*
interface=org.freedesktop.Avahi.AddressResolver
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.AddressResolver
peer=(label=###SLOT_SECURITY_TAGS###),
# host name resolving
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=HostNameResolverNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/HostNameResolver*
interface=org.freedesktop.Avahi.HostNameResolver
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.HostNameResolver
peer=(label=###SLOT_SECURITY_TAGS###),
# service resolving
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=ServiceResolverNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/ServiceResolver*
interface=org.freedesktop.Avahi.ServiceResolver
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.ServiceResolver
peer=(label=###SLOT_SECURITY_TAGS###),
# domain browsing
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=DomainBrowserNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/DomainBrowser*
interface=org.freedesktop.Avahi.DomainBrowser
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.DomainBrowser
peer=(label=###SLOT_SECURITY_TAGS###),
# record browsing
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=RecordBrowserNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/RecordBrowser*
interface=org.freedesktop.Avahi.RecordBrowser
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.RecordBrowser
peer=(label=###SLOT_SECURITY_TAGS###),
# service browsing
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=ServiceBrowserNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/ServiceBrowser*
interface=org.freedesktop.Avahi.ServiceBrowser
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.ServiceBrowser
peer=(label=###SLOT_SECURITY_TAGS###),
# Service type browsing
dbus (send)
bus=system
path=/
interface=org.freedesktop.Avahi.Server
member=ServiceTypeBrowserNew
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (send)
bus=system
path=/Client*/ServiceTypeBrowser*
interface=org.freedesktop.Avahi.ServiceTypeBrowser
member=Free
peer=(name=org.freedesktop.Avahi, label=###SLOT_SECURITY_TAGS###),
dbus (receive)
bus=system
interface=org.freedesktop.Avahi.ServiceTypeBrowser
peer=(label=###SLOT_SECURITY_TAGS###),
`
type avahiObserveInterface struct{}
func (iface *avahiObserveInterface) Name() string {
return "avahi-observe"
}
func (iface *avahiObserveInterface) StaticInfo() interfaces.StaticInfo {
return interfaces.StaticInfo{
Summary: avahiObserveSummary,
ImplicitOnClassic: true,
BaseDeclarationSlots: avahiObserveBaseDeclarationSlots,
}
}
func (iface *avahiObserveInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
old := "###SLOT_SECURITY_TAGS###"
var new string
// If we're running on classic, Avahi may be installed either as a snap of
// as part of the OS. If it is part of the OS, it will not have a security
// label like it would when installed as a snap.
if implicitSystemConnectedSlot(slot) {
// avahi from the OS is typically unconfined but known to sometimes be confined
// with stock apparmor 2.13.2+ profiles the label is avahi-daemon
new = "\"{unconfined,/usr/sbin/avahi-daemon,avahi-daemon}\""
} else {
new = slotAppLabelExpr(slot)
}
snippet := strings.Replace(avahiObserveConnectedPlugAppArmor, old, new, -1)
spec.AddSnippet(snippet)
return nil
}
func (iface *avahiObserveInterface) AppArmorPermanentSlot(spec *apparmor.Specification, slot *snap.SlotInfo) error {
// Only apply slot snippet when running as application snap
// on classic, slot side can be system or application
if !implicitSystemPermanentSlot(slot) {
spec.AddSnippet(avahiObservePermanentSlotAppArmor)
}
return nil
}
func (iface *avahiObserveInterface) AppArmorConnectedSlot(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
// Only apply slot snippet when running as application snap
// on classic, slot side can be system or application
if !implicitSystemConnectedSlot(slot) {
old := "###PLUG_SECURITY_TAGS###"
new := plugAppLabelExpr(plug)
snippet := strings.Replace(avahiObserveConnectedSlotAppArmor, old, new, -1)
spec.AddSnippet(snippet)
}
return nil
}
func (iface *avahiObserveInterface) DBusPermanentSlot(spec *dbus.Specification, slot *snap.SlotInfo) error {
// Only apply slot snippet when running as application snap
// on classic, slot side can be system or application
if !implicitSystemPermanentSlot(slot) {
spec.AddSnippet(avahiObservePermanentSlotDBus)
}
return nil
}
func (iface *avahiObserveInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool {
// allow what declarations allowed
return true
}
func init() {
registerIface(&avahiObserveInterface{})
}