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
|
// -*- 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 (
"bytes"
"fmt"
"regexp"
"strings"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/dbus"
"github.com/snapcore/snapd/interfaces/seccomp"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
)
const dbusSummary = `allows owning a specific name on DBus`
const dbusBaseDeclarationSlots = `
dbus:
allow-installation:
slot-snap-type:
- app
deny-connection:
slot-attributes:
name: .+
deny-auto-connection: true
`
const dbusPermanentSlotAppArmor = `
# Description: Allow owning a name on DBus public bus
#include <abstractions/###DBUS_ABSTRACTION###>
# register on DBus
dbus (send)
bus=###DBUS_BUS###
path=/org/freedesktop/DBus
interface=org.freedesktop.DBus
member="{Request,Release}Name"
peer=(name=org.freedesktop.DBus, label=unconfined),
dbus (send)
bus=###DBUS_BUS###
path=/org/freedesktop/DBus
interface=org.freedesktop.DBus
member="GetConnectionUnix{ProcessID,User}"
peer=(name=org.freedesktop.DBus, label=unconfined),
dbus (send)
bus=###DBUS_BUS###
path=/org/freedesktop/DBus
interface=org.freedesktop.DBus
member="GetConnectionCredentials"
peer=(name=org.freedesktop.DBus, label=unconfined),
# bind to a well-known DBus name: ###DBUS_NAME###
dbus (bind)
bus=###DBUS_BUS###
name=###DBUS_NAME###,
# For KDE applications and some other cases, also support alternation for:
# - using org.kde.foo-PID as the 'well-known' name
# - using org.foo.cmd_<num>_<num> as the 'well-known' name
# Note, snapd does not allow declaring a 'well-known' name that ends with
# '-[0-9]+' or that contains '_'. Parallel installs of DBus services aren't
# supported at this time, but if they were, this could allow a parallel
# install'swell-known name to overlap with the normal install.
dbus (bind)
bus=###DBUS_BUS###
name=###DBUS_NAME###{_,-}[1-9]{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9]}{,_[1-9]{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9_]}{,[0-9]}},
# For Firefox, support using org.mozilla.firefox.<id> as the 'well-known' name
# where <id> is the base64-encoded profile name.
# See https://bugzilla.mozilla.org/1441894 for a discussion and details.
dbus (bind)
bus=###DBUS_BUS###
name="###DBUS_NAME###.*",
# Allow us to talk to dbus-daemon
dbus (receive)
bus=###DBUS_BUS###
path=###DBUS_PATH###
peer=(name=org.freedesktop.DBus, label=unconfined),
dbus (send)
bus=###DBUS_BUS###
path=###DBUS_PATH###
interface=org.freedesktop.DBus.Properties
peer=(name=org.freedesktop.DBus, label=unconfined),
# Allow us to introspect org.freedesktop.DBus (needed by pydbus)
dbus (send)
bus=###DBUS_BUS###
interface=org.freedesktop.DBus.Introspectable
member=Introspect
peer=(name=org.freedesktop.DBus, label=unconfined),
`
const dbusPermanentSlotAppArmorClassic = `
# allow unconfined clients to introspect us on classic
dbus (receive)
bus=###DBUS_BUS###
interface=org.freedesktop.DBus.Introspectable
member=Introspect
peer=(label=unconfined),
# allow us to respond to unconfined clients via ###DBUS_INTERFACE###
# on classic (send should be handled via another snappy interface).
dbus (receive)
bus=###DBUS_BUS###
interface=###DBUS_INTERFACE###
peer=(label=unconfined),
# allow us to respond to unconfined clients via ###DBUS_PATH### (eg,
# org.freedesktop.*, org.gtk.Application, etc) on classic (send should be
# handled via another snappy interface).
dbus (receive)
bus=###DBUS_BUS###
path=###DBUS_PATH###
peer=(label=unconfined),
`
const dbusPermanentSlotDBus = `
<policy user="root">
<allow own="###DBUS_NAME###"/>
<allow send_destination="###DBUS_NAME###"/>
</policy>
<policy context="default">
<allow send_destination="###DBUS_NAME###"/>
</policy>
`
const dbusPermanentSlotSecComp = `
# Description: Allow owning a name and listening on DBus public bus
listen
accept
accept4
`
const dbusConnectedSlotAppArmor = `
# allow snaps to introspect us. This allows clients to introspect all
# DBus interfaces of this service (but not use them).
dbus (receive)
bus=###DBUS_BUS###
interface=org.freedesktop.DBus.Introspectable
member=Introspect
peer=(label=###PLUG_SECURITY_TAGS###),
# allow connected snaps to all paths via ###DBUS_INTERFACE###
dbus (receive, send)
bus=###DBUS_BUS###
interface=###DBUS_INTERFACE###
peer=(label=###PLUG_SECURITY_TAGS###),
# allow connected snaps to all interfaces via ###DBUS_PATH### (eg,
# org.freedesktop.*, org.gtk.Application, etc) to allow full integration with
# connected snaps.
dbus (receive, send)
bus=###DBUS_BUS###
path=###DBUS_PATH###
peer=(label=###PLUG_SECURITY_TAGS###),
`
const dbusConnectedPlugAppArmor = `
#include <abstractions/###DBUS_ABSTRACTION###>
# allow snaps to introspect the slot servive. This allows us to introspect
# all DBus interfaces of the service (but not use them).
dbus (send)
bus=###DBUS_BUS###
interface=org.freedesktop.DBus.Introspectable
member=Introspect
peer=(label=###SLOT_SECURITY_TAGS###),
# allow connected snaps to ###DBUS_NAME###
dbus (receive, send)
bus=###DBUS_BUS###
peer=(name=###DBUS_NAME###, label=###SLOT_SECURITY_TAGS###),
# For KDE applications, also support alternation since they use org.kde.foo-PID
# as their 'well-known' name. snapd does not allow ###DBUS_NAME### to end with
# '-[0-9]+', so this is ok.
dbus (receive, send)
bus=###DBUS_BUS###
peer=(name="###DBUS_NAME###-[1-9]{,[0-9]}{,[0-9]}{,[0-9]}{,[0-9]}{,[0-9]}", label=###SLOT_SECURITY_TAGS###),
# allow connected snaps to all paths via ###DBUS_INTERFACE### to allow full
# integration with connected snaps.
dbus (receive, send)
bus=###DBUS_BUS###
interface=###DBUS_INTERFACE###
peer=(label=###SLOT_SECURITY_TAGS###),
# allow connected snaps to all interfaces via ###DBUS_PATH### (eg,
# org.freedesktop.*, org.gtk.Application, etc) to allow full integration with
# connected snaps.
dbus (receive, send)
bus=###DBUS_BUS###
path=###DBUS_PATH###
peer=(label=###SLOT_SECURITY_TAGS###),
`
type dbusInterface struct{}
func (iface *dbusInterface) Name() string {
return "dbus"
}
func (iface *dbusInterface) StaticInfo() interfaces.StaticInfo {
return interfaces.StaticInfo{
Summary: dbusSummary,
BaseDeclarationSlots: dbusBaseDeclarationSlots,
}
}
// snapd has AppArmor rules (see above) allowing binds to busName-PID
// so to avoid overlap with different snaps (eg, busName running as PID
// 123 and busName-123), don't allow busName to end with -PID. If that
// rule is removed, this limitation can be lifted.
var isInvalidSnappyBusName = regexp.MustCompile("-[0-9]+$").MatchString
// Obtain yaml-specified bus well-known name
func (iface *dbusInterface) getAttribs(attribs interfaces.Attrer) (string, string, error) {
// bus attribute
var bus string
if err := attribs.Attr("bus", &bus); err != nil {
return "", "", fmt.Errorf("cannot find attribute 'bus'")
}
if bus != "session" && bus != "system" {
return "", "", fmt.Errorf("bus '%s' must be one of 'session' or 'system'", bus)
}
// name attribute
var name string
if err := attribs.Attr("name", &name); err != nil {
return "", "", fmt.Errorf("cannot find attribute 'name'")
}
err := interfaces.ValidateDBusBusName(name)
if err != nil {
return "", "", err
}
if isInvalidSnappyBusName(name) {
return "", "", fmt.Errorf("DBus bus name must not end with -NUMBER")
}
return bus, name, nil
}
// Determine AppArmor dbus abstraction to use based on bus
func getAppArmorAbstraction(bus string) (string, error) {
var abstraction string
if bus == "system" {
abstraction = "dbus-strict"
} else if bus == "session" {
abstraction = "dbus-session-strict"
} else {
return "", fmt.Errorf("unknown abstraction for specified bus '%q'", bus)
}
return abstraction, nil
}
// Calculate individual snippet policy based on bus and name
func getAppArmorSnippet(policy string, bus string, name string) string {
old := "###DBUS_BUS###"
new := bus
snippet := strings.Replace(policy, old, new, -1)
old = "###DBUS_NAME###"
new = name
snippet = strings.Replace(snippet, old, new, -1)
// convert name to AppArmor dbus path (eg 'org.foo' to '/org/foo{,/**}')
var pathBuf bytes.Buffer
pathBuf.WriteString(`"/`)
pathBuf.WriteString(strings.Replace(name, ".", "/", -1))
pathBuf.WriteString(`{,/**}"`)
old = "###DBUS_PATH###"
new = pathBuf.String()
snippet = strings.Replace(snippet, old, new, -1)
// convert name to AppArmor dbus interface (eg, 'org.foo' to 'org.foo{,.*}')
var ifaceBuf bytes.Buffer
ifaceBuf.WriteString(`"`)
ifaceBuf.WriteString(name)
ifaceBuf.WriteString(`{,.*}"`)
old = "###DBUS_INTERFACE###"
new = ifaceBuf.String()
snippet = strings.Replace(snippet, old, new, -1)
return snippet
}
func (iface *dbusInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
bus, name, err := iface.getAttribs(plug)
if err != nil {
return err
}
busSlot, nameSlot, err := iface.getAttribs(slot)
if err != nil {
return err
}
// ensure that we only connect to slot with matching attributes
if bus != busSlot || name != nameSlot {
return nil
}
// well-known DBus name-specific connected plug policy
snippet := getAppArmorSnippet(dbusConnectedPlugAppArmor, bus, name)
// abstraction policy
abstraction, err := getAppArmorAbstraction(bus)
if err != nil {
return err
}
old := "###DBUS_ABSTRACTION###"
new := abstraction
snippet = strings.Replace(snippet, old, new, -1)
old = "###SLOT_SECURITY_TAGS###"
new = slot.LabelExpression()
snippet = strings.Replace(snippet, old, new, -1)
spec.AddSnippet(snippet)
return nil
}
func (iface *dbusInterface) DBusPermanentSlot(spec *dbus.Specification, slot *snap.SlotInfo) error {
bus, name, err := iface.getAttribs(slot)
if err != nil {
return err
}
// only system services need bus policy
if bus != "system" {
return nil
}
old := "###DBUS_NAME###"
new := name
spec.AddSnippet(strings.Replace(dbusPermanentSlotDBus, old, new, -1))
return nil
}
func (iface *dbusInterface) AppArmorPermanentSlot(spec *apparmor.Specification, slot *snap.SlotInfo) error {
bus, name, err := iface.getAttribs(slot)
if err != nil {
return err
}
// well-known DBus name-specific permanent slot policy
snippet := getAppArmorSnippet(dbusPermanentSlotAppArmor, bus, name)
// abstraction policy
abstraction, err := getAppArmorAbstraction(bus)
if err != nil {
return err
}
old := "###DBUS_ABSTRACTION###"
new := abstraction
snippet = strings.Replace(snippet, old, new, -1)
spec.AddSnippet(snippet)
if release.OnClassic {
// classic-only policy
spec.AddSnippet(getAppArmorSnippet(dbusPermanentSlotAppArmorClassic, bus, name))
}
return nil
}
func (iface *dbusInterface) SecCompPermanentSlot(spec *seccomp.Specification, slot *snap.SlotInfo) error {
spec.AddSnippet(dbusPermanentSlotSecComp)
return nil
}
func (iface *dbusInterface) AppArmorConnectedSlot(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
bus, name, err := iface.getAttribs(slot)
if err != nil {
return err
}
busPlug, namePlug, err := iface.getAttribs(plug)
if err != nil {
return err
}
// ensure that we only connect to slot with matching attributes. This
// makes sure that the security policy is correct, but does not ensure
// that 'snap interfaces' is correct.
// TODO: we can fix the 'snap interfaces' issue when interface/policy
// checkers when they are available
if bus != busPlug || name != namePlug {
return nil
}
// well-known DBus name-specific connected slot policy
snippet := getAppArmorSnippet(dbusConnectedSlotAppArmor, bus, name)
old := "###PLUG_SECURITY_TAGS###"
new := plug.LabelExpression()
snippet = strings.Replace(snippet, old, new, -1)
spec.AddSnippet(snippet)
return nil
}
func (iface *dbusInterface) BeforePreparePlug(plug *snap.PlugInfo) error {
_, _, err := iface.getAttribs(plug)
return err
}
func (iface *dbusInterface) BeforePrepareSlot(slot *snap.SlotInfo) error {
_, _, err := iface.getAttribs(slot)
return err
}
func (iface *dbusInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool {
// allow what declarations allowed
return true
}
func init() {
registerIface(&dbusInterface{})
}
|