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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2022 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"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/mount"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
)
const sharedMemorySummary = `allows two snaps to use predefined shared memory objects`
// The plug side of shared-memory can operate in two modes: if the
// private attribute is set to true, then it can be connected to the
// implicit system slot to be given a private version of /dev/shm.
//
// For a plug without that attribute set, it will connect to a
// matching application snap slot - this is permitted even though the
// interface is super-privileged because using a slot requires a store
// declaration anyways so just declaring a plug will not grant access
// unless a slot was also granted at some point.
const sharedMemoryBaseDeclarationPlugs = `
shared-memory:
allow-connection:
-
plug-attributes:
private: false
slot-attributes:
shared-memory: $PLUG(shared-memory)
-
plug-attributes:
private: true
slot-snap-type:
- core
allow-auto-connection:
-
plug-attributes:
private: false
slot-publisher-id:
- $PLUG_PUBLISHER_ID
slot-attributes:
shared-memory: $PLUG(shared-memory)
-
plug-attributes:
private: true
slot-snap-type:
- core
`
// shared-memory slots can appear either as an implicit system slot,
// or as a slot on an application snap.
//
// The implicit version of the slot is intended to auto-connect with
// plugs that have the private attribute set to true.
//
// Slots on app snaps connect to non-private plugs. They are are
// super-privileged and thus denied to any snap except those that get
// a store declaration to do so, but the intent is for application or
// gadget snaps to use the slot much like the content interface.
const sharedMemoryBaseDeclarationSlots = `
shared-memory:
allow-installation:
slot-snap-type:
- app
- gadget
- core
deny-installation:
slot-snap-type:
- app
- gadget
deny-auto-connection: true
`
const sharedMemoryPrivateConnectedPlugAppArmor = `
# Description: Allow access to everything in private /dev/shm
"/dev/shm/**" mrwlkix,
`
func validateSharedMemoryPath(path string) error {
if len(path) == 0 {
return fmt.Errorf("shared-memory interface path is empty")
}
if strings.TrimSpace(path) != path {
return fmt.Errorf("shared-memory interface path has leading or trailing spaces: %q", path)
}
// allow specifically only "*" globbing character, but disallow all other
// AARE characters
// same as from ValidateNoAppArmorRegexp, but with globbing
const aareWithoutGlob = `?[]{}^"` + "\x00"
if strings.ContainsAny(path, aareWithoutGlob) {
return fmt.Errorf("shared-memory interface path is invalid: %q contains a reserved apparmor char from %s", path, aareWithoutGlob)
}
// in addition to only allowing "*", we don't want to allow double "**"
// because "**" can traverse sub-directories as well which we don't want
if strings.Contains(path, "**") {
return fmt.Errorf("shared-memory interface path is invalid: %q contains ** which is unsupported", path)
}
// TODO: consider whether we should remove this check and allow full SHM path
if strings.Contains(path, "/") {
return fmt.Errorf("shared-memory interface path should not contain '/': %q", path)
}
// The check above protects from most unclean paths, but one could still specify ".."
if !cleanSubPath(path) {
return fmt.Errorf("shared-memory interface path is not clean: %q", path)
}
return nil
}
// sharedMemoryInterface allows sharing sharedMemory between snaps
type sharedMemoryInterface struct{}
func (iface *sharedMemoryInterface) Name() string {
return "shared-memory"
}
func (iface *sharedMemoryInterface) StaticInfo() interfaces.StaticInfo {
return interfaces.StaticInfo{
Summary: sharedMemorySummary,
BaseDeclarationPlugs: sharedMemoryBaseDeclarationPlugs,
BaseDeclarationSlots: sharedMemoryBaseDeclarationSlots,
AffectsPlugOnRefresh: true,
ImplicitOnCore: true,
ImplicitOnClassic: true,
}
}
func (iface *sharedMemoryInterface) BeforePrepareSlot(slot *snap.SlotInfo) error {
sharedMemoryAttr, isSet := slot.Attrs["shared-memory"]
sharedMemory, ok := sharedMemoryAttr.(string)
if isSet && !ok {
return fmt.Errorf(`shared-memory "shared-memory" attribute must be a string, not %v`,
slot.Attrs["shared-memory"])
}
if sharedMemory == "" {
if slot.Attrs == nil {
slot.Attrs = make(map[string]any)
}
// shared-memory defaults to "slot" name if unspecified
slot.Attrs["shared-memory"] = slot.Name
}
readPaths, err := stringListAttribute(slot, "read")
if err != nil {
return fmt.Errorf("shared-memory %v", err)
}
writePaths, err := stringListAttribute(slot, "write")
if err != nil {
return fmt.Errorf("shared-memory %v", err)
}
// We perform the same validation for read-only and writable paths, so
// let's just put them all in the same array
allPaths := append(readPaths, writePaths...)
if len(allPaths) == 0 {
return errors.New(`shared memory interface requires at least a valid "read" or "write" attribute`)
}
for _, path := range allPaths {
if err := validateSharedMemoryPath(path); err != nil {
return err
}
}
return nil
}
type sharedMemorySnippetType int
const (
snippetForSlot sharedMemorySnippetType = iota
snippetForPlug
)
func writeSharedMemoryPaths(w io.Writer, slot *interfaces.ConnectedSlot,
snippetType sharedMemorySnippetType) {
emitWritableRule := func(path string) {
// Ubuntu 14.04 uses /run/shm instead of the most common /dev/shm
fmt.Fprintf(w, "\"/{dev,run}/shm/%s\" mrwlk,\n", path)
}
// All checks were already done in BeforePrepare{Plug,Slot}
writePaths, _ := stringListAttribute(slot, "write")
for _, path := range writePaths {
emitWritableRule(path)
}
readPaths, _ := stringListAttribute(slot, "read")
for _, path := range readPaths {
if snippetType == snippetForPlug {
// grant read-only access
fmt.Fprintf(w, "\"/{dev,run}/shm/%s\" r,\n", path)
} else {
// the slot must still be granted write access, because the "read"
// and "write" attributes are meant to affect the plug only
emitWritableRule(path)
}
}
}
func (iface *sharedMemoryInterface) BeforePreparePlug(plug *snap.PlugInfo) error {
privateAttr, isPrivateSet := plug.Attrs["private"]
private, ok := privateAttr.(bool)
if isPrivateSet && !ok {
return fmt.Errorf(`shared-memory "private" attribute must be a bool, not %v`, privateAttr)
}
if plug.Attrs == nil {
plug.Attrs = make(map[string]any)
}
plug.Attrs["private"] = private
sharedMemoryAttr, isSet := plug.Attrs["shared-memory"]
sharedMemory, ok := sharedMemoryAttr.(string)
if isSet && !ok {
return fmt.Errorf(`shared-memory "shared-memory" attribute must be a string, not %v`,
plug.Attrs["shared-memory"])
}
if private {
if isSet {
return fmt.Errorf(`shared-memory "shared-memory" attribute must not be set together with "private: true"`)
}
// A private shared-memory plug cannot coexist with
// other shared-memory plugs/slots.
for _, other := range plug.Snap.Plugs {
if other != plug && other.Interface == "shared-memory" {
return fmt.Errorf(`shared-memory plug with "private: true" set cannot be used with other shared-memory plugs`)
}
}
for _, other := range plug.Snap.Slots {
if other.Interface == "shared-memory" {
return fmt.Errorf(`shared-memory plug with "private: true" set cannot be used with shared-memory slots`)
}
}
} else {
if sharedMemory == "" {
// shared-memory defaults to "plug" name if unspecified
plug.Attrs["shared-memory"] = plug.Name
}
}
return nil
}
func (iface *sharedMemoryInterface) isPrivate(plug *interfaces.ConnectedPlug) (bool, error) {
// Note that private may not be set even if
// "SanitizePlugsSlots()" is called (which in turn calls
// BeforePreparePlug() which will set this).
//
// The code-path is an upgrade from snapd 2.54.4 where
// shared-memory did not have the "private" attribute
// yet. Then the ConnectedPlug data is written into the
// interface repo without this attribute and on regeneration
// of security profiles the connectedPlug is loaded from the
// interface repository in the state and not from the
// snap.yaml so this attribute is missing.
var private bool
if err := plug.Attr("private", &private); err != nil && !errors.Is(err, snap.AttributeNotFoundError{}) {
return false, err
}
return private, nil
}
func (iface *sharedMemoryInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
private, err := iface.isPrivate(plug)
if err != nil {
return err
}
if private {
spec.AddSnippet(sharedMemoryPrivateConnectedPlugAppArmor)
spec.AddUpdateNSf(` # Private /dev/shm
/dev/ r,
/dev/shm/{,**} rw,
mount options=(bind, rw) /dev/shm/snap.%s/ -> /dev/shm/,
umount /dev/shm/,`, plug.Snap().InstanceName())
} else {
sharedMemorySnippet := &bytes.Buffer{}
writeSharedMemoryPaths(sharedMemorySnippet, slot, snippetForPlug)
spec.AddSnippet(sharedMemorySnippet.String())
}
return nil
}
func (iface *sharedMemoryInterface) AppArmorConnectedSlot(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
if slot.Snap().Type() == snap.TypeOS || slot.Snap().Type() == snap.TypeSnapd {
return nil
}
sharedMemorySnippet := &bytes.Buffer{}
writeSharedMemoryPaths(sharedMemorySnippet, slot, snippetForSlot)
spec.AddSnippet(sharedMemorySnippet.String())
return nil
}
func (iface *sharedMemoryInterface) MountConnectedPlug(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
private, err := iface.isPrivate(plug)
if err != nil {
return err
}
if !private {
return nil
}
devShm := filepath.Join(dirs.GlobalRootDir, "/dev/shm")
if osutil.IsSymlink(devShm) {
return fmt.Errorf(`shared-memory plug with "private: true" cannot be connected if %q is a symlink`, devShm)
}
return spec.AddMountEntry(osutil.MountEntry{
Name: filepath.Join(devShm, "snap."+plug.Snap().InstanceName()),
Dir: "/dev/shm",
Options: []string{"bind", "rw"},
})
}
func (iface *sharedMemoryInterface) AutoConnect(plug *snap.PlugInfo, slot *snap.SlotInfo) bool {
// allow what declarations allowed
return true
}
func init() {
registerIface(&sharedMemoryInterface{})
}
|