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) 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"
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/compatibility"
"github.com/snapcore/snapd/interfaces/mount"
"github.com/snapcore/snapd/osutil"
apparmor_sandbox "github.com/snapcore/snapd/sandbox/apparmor"
"github.com/snapcore/snapd/snap"
)
const contentSummary = `allows sharing code and data with other snaps`
const contentBaseDeclarationSlots = `
content:
allow-installation:
slot-snap-type:
- app
- gadget
- kernel
allow-connection:
plug-attributes:
-
content: $SLOT(content)
-
compatibility: $SLOT_COMPAT(compatibility)
allow-auto-connection:
plug-publisher-id:
- $SLOT_PUBLISHER_ID
plug-attributes:
-
content: $SLOT(content)
-
compatibility: $SLOT_COMPAT(compatibility)
`
// contentInterface allows sharing content between snaps
type contentInterface struct{}
func (iface *contentInterface) Name() string {
return "content"
}
func (iface *contentInterface) StaticInfo() interfaces.StaticInfo {
return interfaces.StaticInfo{
Summary: contentSummary,
BaseDeclarationSlots: contentBaseDeclarationSlots,
AffectsPlugOnRefresh: true,
}
}
func cleanSubPath(path string) bool {
return filepath.Clean(path) == path && path != ".." && !strings.HasPrefix(path, "../")
}
func validatePath(path string) error {
if err := apparmor_sandbox.ValidateNoAppArmorRegexp(path); err != nil {
return fmt.Errorf("content interface path is invalid: %v", err)
}
if ok := cleanSubPath(path); !ok {
return fmt.Errorf("content interface path is not clean: %q", path)
}
return nil
}
func checkLabelAttributes(attrs map[string]any, nameDef string) error {
// The ContentCompatLabel feature is checked only at matching time,
// here we allow the compatibility labels to exist in any case as it
// has no further side effect.
content, okContent := attrs["content"].(string)
compat, okCompat := attrs["compatibility"].(string)
hasContent := okContent && len(content) > 0
hasCompat := okCompat && len(compat) > 0
if hasCompat && hasContent {
return errors.New("cannot have both content and compatibility labels")
}
if hasCompat {
_, err := compatibility.DecodeCompatField(compat, nil)
return err
}
if hasContent {
return nil
}
// content defaults to nameDef if unspecified and no compatibility label either
attrs["content"] = nameDef
return nil
}
func (iface *contentInterface) BeforePrepareSlot(slot *snap.SlotInfo) error {
if slot.Attrs == nil {
slot.Attrs = make(map[string]any)
}
if err := checkLabelAttributes(slot.Attrs, slot.Name); err != nil {
return err
}
// Error if "read" or "write" are present alongside "source".
if _, found := slot.Lookup("source"); found {
if _, found := slot.Lookup("read"); found {
return fmt.Errorf(`move the "read" attribute into the "source" section`)
}
if _, found := slot.Lookup("write"); found {
return fmt.Errorf(`move the "write" attribute into the "source" section`)
}
}
// check that we have either a read or write path
rpath := iface.path(slot, "read")
wpath := iface.path(slot, "write")
if len(rpath) == 0 && len(wpath) == 0 {
return fmt.Errorf("read or write path must be set")
}
// go over both paths
paths := rpath
paths = append(paths, wpath...)
for _, p := range paths {
if err := validatePath(p); err != nil {
return err
}
}
return nil
}
func (iface *contentInterface) BeforePreparePlug(plug *snap.PlugInfo) error {
if plug.Attrs == nil {
plug.Attrs = make(map[string]any)
}
if err := checkLabelAttributes(plug.Attrs, plug.Name); err != nil {
return err
}
target, ok := plug.Attrs["target"].(string)
if !ok || len(target) == 0 {
return fmt.Errorf("content plug must contain target path")
}
if err := validatePath(target); err != nil {
return err
}
return nil
}
// path is an internal helper that extract the "read" and "write" attribute
// of the slot
func (iface *contentInterface) path(attrs interfaces.Attrer, name string) []string {
if name != "read" && name != "write" {
panic("internal error, path can only be used with read/write")
}
var paths []any
var source map[string]any
if err := attrs.Attr("source", &source); err == nil {
// Access either "source.read" or "source.write" attribute.
var ok bool
if paths, ok = source[name].([]any); !ok {
return nil
}
} else {
// Access either "read" or "write" attribute directly (legacy).
if err := attrs.Attr(name, &paths); err != nil {
return nil
}
}
out := make([]string, len(paths))
for i, p := range paths {
var ok bool
out[i], ok = p.(string)
if !ok {
return nil
}
}
return out
}
// resolveSpecialVariable resolves one of the three $SNAP* variables at the
// beginning of a given path. The variables are $SNAP, $SNAP_DATA and
// $SNAP_COMMON. If there are no variables then $SNAP is implicitly assumed
// (this is the behavior that was used before the variables were supporter).
func resolveSpecialVariable(path string, snapInfo *snap.Info) string {
// Content cannot be mounted at arbitrary locations, validate the path
// for extra safety.
if err := snap.ValidatePathVariables(path); err == nil && strings.HasPrefix(path, "$") {
// The path starts with $ and ValidatePathVariables() ensures
// path contains only $SNAP, $SNAP_DATA, $SNAP_COMMON, and no
// other $VARs are present. It is ok to use
// ExpandSnapVariables() since it only expands $SNAP, $SNAP_DATA
// and $SNAP_COMMON
return snapInfo.ExpandSnapVariables(path)
}
// Always prefix with $SNAP if nothing else is provided or the path
// contains invalid variables.
return snapInfo.ExpandSnapVariables(filepath.Join("$SNAP", path))
}
func sourceTarget(plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot, relSrc string) (string, string) {
var target string
// The 'target' attribute has already been verified in BeforePreparePlug.
_ = plug.Attr("target", &target)
source := resolveSpecialVariable(relSrc, slot.Snap())
target = resolveSpecialVariable(target, plug.Snap())
// Check if the "source" section is present.
var unused map[string]any
if err := slot.Attr("source", &unused); err == nil {
_, sourceName := filepath.Split(source)
target = filepath.Join(target, sourceName)
}
return source, target
}
func mountEntry(plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot, relSrc string, extraOptions ...string) osutil.MountEntry {
options := make([]string, 0, len(extraOptions)+1)
options = append(options, "bind")
options = append(options, extraOptions...)
source, target := sourceTarget(plug, slot, relSrc)
return osutil.MountEntry{
Name: source,
Dir: target,
Options: options,
}
}
func (iface *contentInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
contentSnippet := bytes.NewBuffer(nil)
writePaths := iface.path(slot, "write")
emit := spec.AddUpdateNSf
if len(writePaths) > 0 {
fmt.Fprintf(contentSnippet, `
# In addition to the bind mount, add any AppArmor rules so that
# snaps may directly access the slot implementation's files. Due
# to a limitation in the kernel's LSM hooks for AF_UNIX, these
# are needed for using named sockets within the exported
# directory.
`)
for i, w := range writePaths {
fmt.Fprintf(contentSnippet, "\"%s/**\" mrwklix,\n",
resolveSpecialVariable(w, slot.Snap()))
source, target := sourceTarget(plug, slot, w)
emit(" # Read-write content sharing %s -> %s (w#%d)\n", plug.Ref(), slot.Ref(), i)
emit(" mount options=(bind, rw) \"%s/\" -> \"%s{,-[0-9]*}/\",\n", source, target)
emit(" mount options=(rprivate) -> \"%s{,-[0-9]*}/\",\n", target)
emit(" umount \"%s{,-[0-9]*}/\",\n", target)
// TODO: The assumed prefix depth could be optimized to be more
// precise since content sharing can only take place in a fixed
// list of places with well-known paths (well, constrained set of
// paths). This can be done when the prefix is actually consumed.
apparmor.GenWritableProfile(emit, source, 1)
apparmor.GenWritableProfile(emit, target, 1)
apparmor.GenWritableProfile(emit, fmt.Sprintf("%s-[0-9]*", target), 1)
}
}
readPaths := iface.path(slot, "read")
if len(readPaths) > 0 {
fmt.Fprintf(contentSnippet, `
# In addition to the bind mount, add any AppArmor rules so that
# snaps may directly access the slot implementation's files
# read-only.
`)
for i, r := range readPaths {
fmt.Fprintf(contentSnippet, "\"%s/**\" mrkix,\n",
resolveSpecialVariable(r, slot.Snap()))
source, target := sourceTarget(plug, slot, r)
emit(" # Read-only content sharing %s -> %s (r#%d)\n", plug.Ref(), slot.Ref(), i)
emit(" mount options=(bind) \"%s/\" -> \"%s{,-[0-9]*}/\",\n", source, target)
emit(" remount options=(bind, ro) \"%s{,-[0-9]*}/\",\n", target)
emit(" mount options=(rprivate) -> \"%s{,-[0-9]*}/\",\n", target)
emit(" umount \"%s{,-[0-9]*}/\",\n", target)
// Look at the TODO comment above.
apparmor.GenWritableProfile(emit, source, 1)
apparmor.GenWritableProfile(emit, target, 1)
apparmor.GenWritableProfile(emit, fmt.Sprintf("%s-[0-9]*", target), 1)
}
}
spec.AddSnippet(contentSnippet.String())
return nil
}
func (iface *contentInterface) AppArmorConnectedSlot(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
contentSnippet := bytes.NewBuffer(nil)
writePaths := iface.path(slot, "write")
if len(writePaths) > 0 {
fmt.Fprintf(contentSnippet, `
# When the content interface is writable, allow this slot
# implementation to access the slot's exported files at the plugging
# snap's mountpoint to accommodate software where the plugging app
# tells the slotting app about files to share.
`)
for _, w := range writePaths {
_, target := sourceTarget(plug, slot, w)
fmt.Fprintf(contentSnippet, "\"%s/**\" mrwklix,\n",
target)
}
}
spec.AddSnippet(contentSnippet.String())
return nil
}
func (iface *contentInterface) AutoConnect(plug *snap.PlugInfo, slot *snap.SlotInfo) bool {
// allow what declarations allowed
return true
}
// Interactions with the mount backend.
func (iface *contentInterface) MountConnectedPlug(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
for _, r := range iface.path(slot, "read") {
err := spec.AddMountEntry(mountEntry(plug, slot, r, "ro"))
if err != nil {
return err
}
}
for _, w := range iface.path(slot, "write") {
err := spec.AddMountEntry(mountEntry(plug, slot, w))
if err != nil {
return err
}
}
return nil
}
func init() {
registerIface(&contentInterface{})
}
|