Skip to content

Commit

Permalink
interfaces: add implementation for dbus-name interface
Browse files Browse the repository at this point in the history
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
morphis committed May 13, 2016
1 parent ee29576 commit 7d4a725
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions interfaces/builtin/all.go
Expand Up @@ -26,6 +26,7 @@ import (
var allInterfaces = []interfaces.Interface{
&BoolFileInterface{},
&BluezInterface{},
&DBusNameInterface{},
&NetworkManagerInterface{},
NewFirewallControlInterface(),
NewHomeInterface(),
Expand Down
150 changes: 150 additions & 0 deletions interfaces/builtin/dbus_name.go
@@ -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())

This comment has been minimized.

Copy link
@zyga

zyga May 13, 2016

Is that really what we want to enforce here?

This comment has been minimized.

Copy link
@morphis

morphis May 13, 2016

Author 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
}
1 change: 1 addition & 0 deletions snap/implicit.go
Expand Up @@ -22,6 +22,7 @@ package snap
import "github.com/ubuntu-core/snappy/release"

var implicitSlots = []string{
"dbus-name",
"firewall-control",
"home",
"locale-control",
Expand Down

0 comments on commit 7d4a725

Please sign in to comment.