Permalink
Browse files

interfaces: add implementation for dbus-name interface

The interface allows a snap to own a specified name on dbus system
bus. It is provied as implicit interface of the OS snap which
contains the dbus-daemon and therefor responsible to manage this
resource.

The interface will auto-connect and puts a simplified dbus policy
file in place which allows the root user to own the bus and to
send messages to the bus owner. No further security mediation
is put in place so by default no other snap is allowed to talk
to the service. This will be implemented with another interface.

Signed-off-by: Simon Fels <simon.fels@canonical.com>
  • Loading branch information...
1 parent ee29576 commit 7d4a725b6fa72ee80558fbb1ab19b4340acb8640 Simon Fels committed May 13, 2016
Showing with 152 additions and 0 deletions.
  1. +1 −0 interfaces/builtin/all.go
  2. +150 −0 interfaces/builtin/dbus_name.go
  3. +1 −0 snap/implicit.go
@@ -26,6 +26,7 @@ import (
var allInterfaces = []interfaces.Interface{
&BoolFileInterface{},
&BluezInterface{},
+ &DBusNameInterface{},
&NetworkManagerInterface{},
NewFirewallControlInterface(),
NewHomeInterface(),
@@ -0,0 +1,150 @@
+// -*- 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 (
+ "fmt"
+ "bytes"
+
+ "github.com/ubuntu-core/snappy/interfaces"
+ "github.com/ubuntu-core/snappy/snap"
+)
+
+var dbusNamePermanentPlugAppArmor = []byte(`
+#include <abstractions/dbus-strict>
+
+# Allow request/release a dbus name
+dbus (send)
+ bus=system
+ path=/org/freedesktop/DBus
+ interface=org.freedesktop.DBus
+ member={Request,Release}Name
+ peer=(name=org.freedesktop.DBus),
+`)
+
+var dbusNameConnectedPlugAppArmor = []byte(`
+# Allow binding the service to the requested connection name
+dbus (bind)
+ bus=system
+ name="###PLUG_BUS_NAME###",
+`)
+
+var dbusNameConnectedPlugSecComp = []byte(`
+# Description: Allow dbus service.
+
+# Can communicate with DBus system service
+connect
+getsockname
+recv
+recvmsg
+send
+sendto
+sendmsg
+socket
+`)
+
+var dbusNameConnectedPlugDBus = []byte(`
+<policy user="root">
+ <allow own="###PLUG_BUS_NAME###"/>
+ <allow send_destination="###PLUG_BUS_NAME###"/>
+</policy>
+<policy context="default">
+ # By default deny all access for not-root
+ <deny send_destination="###PLUG_BUS_NAME###"/>
+</policy>
+`)
+
+type DBusNameInterface struct{}
+
+func (iface *DBusNameInterface) Name() string {
+ return "dbus-name"
+}
+
+func (iface *DBusNameInterface) PermanentPlugSnippet(plug *interfaces.Plug, securitySystem interfaces.SecuritySystem) ([]byte, error) {
+ switch securitySystem {
+ case interfaces.SecurityAppArmor:
+ return dbusNamePermanentPlugAppArmor, nil
+ case interfaces.SecurityDBus, interfaces.SecuritySecComp, interfaces.SecurityUDev:
+ return nil, nil
+ default:
+ return nil, interfaces.ErrUnknownSecurity
+ }
+}
+
+var plugBusNamePlaceholder = []byte("###PLUG_BUS_NAME###")
+
+func (iface *DBusNameInterface) ConnectedPlugSnippet(plug *interfaces.Plug, slot *interfaces.Slot, securitySystem interfaces.SecuritySystem) ([]byte, error) {
+ bus_name, _ := plug.Attrs["name"].(string)
+
+ switch securitySystem {
+ case interfaces.SecurityDBus:
+ snippet := bytes.Replace(dbusNameConnectedPlugDBus, plugBusNamePlaceholder, []byte(bus_name), -1)
+ return snippet, nil
+ case interfaces.SecurityAppArmor:
+ snippet := bytes.Replace(dbusNameConnectedPlugAppArmor, plugBusNamePlaceholder, []byte(bus_name), -1)
+ return snippet, nil
+ case interfaces.SecuritySecComp:
+ return dbusNameConnectedPlugSecComp, nil
+ case interfaces.SecurityUDev:
+ return nil, nil
+ default:
+ return nil, interfaces.ErrUnknownSecurity
+ }
+}
+
+func (iface *DBusNameInterface) PermanentSlotSnippet(slot *interfaces.Slot, securitySystem interfaces.SecuritySystem) ([]byte, error) {
+ switch securitySystem {
+ case interfaces.SecurityDBus, interfaces.SecurityAppArmor, interfaces.SecuritySecComp, interfaces.SecurityUDev:
+ return nil, nil
+ default:
+ return nil, interfaces.ErrUnknownSecurity
+ }
+}
+
+func (iface *DBusNameInterface) ConnectedSlotSnippet(plug *interfaces.Plug, slot *interfaces.Slot, securitySystem interfaces.SecuritySystem) ([]byte, error) {
+ switch securitySystem {
+ case interfaces.SecurityDBus, interfaces.SecurityAppArmor, interfaces.SecuritySecComp, interfaces.SecurityUDev:
+ return nil, nil
+ default:
+ return nil, interfaces.ErrUnknownSecurity
+ }
+}
+
+func (iface *DBusNameInterface) SanitizePlug(plug *interfaces.Plug) error {
+ if iface.Name() != plug.Interface {
+ panic(fmt.Sprintf("plug is not of interface %q", iface))
+ }
+ name, ok := plug.Attrs["name"].(string)
+ if !ok || name == "" {
+ return fmt.Errorf("dbus-name must contain name attribute")
+ }
+ return nil
+}
+
+func (iface *DBusNameInterface) SanitizeSlot(slot *interfaces.Slot) error {
+ if slot.Snap.Type != snap.TypeOS {
+ return fmt.Errorf("%s slots are reserved for the operating system snap", iface.Name())
@zyga

zyga May 13, 2016

Is that really what we want to enforce here?

@morphis

morphis May 13, 2016

Owner

Yes, only dbus-daemon (which is part of the OS snap) can give you a bus name so there is no other valid owner of an dbus-name slot unless we implement a snap which provides another dbus-daemon instance like a session bus. But I feel that is out of the picture for now. Also I would both be expected to be part of the OS snap (core, personal, desktop, ...).

+ }
+ return nil
+}
+
+func (iface *DBusNameInterface) AutoConnect() bool {
+ return true
+}
View
@@ -22,6 +22,7 @@ package snap
import "github.com/ubuntu-core/snappy/release"
var implicitSlots = []string{
+ "dbus-name",
"firewall-control",
"home",
"locale-control",

0 comments on commit 7d4a725

Please sign in to comment.