File: shared_memory_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (523 lines) | stat: -rw-r--r-- 15,537 bytes parent folder | download | duplicates (2)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 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_test

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/interfaces"
	"github.com/snapcore/snapd/interfaces/apparmor"
	"github.com/snapcore/snapd/interfaces/builtin"
	"github.com/snapcore/snapd/interfaces/mount"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/testutil"
)

type SharedMemoryInterfaceSuite struct {
	testutil.BaseTest

	iface            interfaces.Interface
	slotInfo         *snap.SlotInfo
	slot             *interfaces.ConnectedSlot
	plugInfo         *snap.PlugInfo
	plug             *interfaces.ConnectedPlug
	wildcardPlugInfo *snap.PlugInfo
	wildcardPlug     *interfaces.ConnectedPlug
	wildcardSlotInfo *snap.SlotInfo
	wildcardSlot     *interfaces.ConnectedSlot
	privatePlugInfo  *snap.PlugInfo
	privatePlug      *interfaces.ConnectedPlug
	privateSlotInfo  *snap.SlotInfo
	privateSlot      *interfaces.ConnectedSlot
}

var _ = Suite(&SharedMemoryInterfaceSuite{
	iface: builtin.MustInterface("shared-memory"),
})

const sharedMemoryConsumerYaml = `name: consumer
version: 0
plugs:
 shmem:
  interface: shared-memory
  shared-memory: foo
  private: false
 shmem-wildcard:
  interface: shared-memory
  shared-memory: foo-wildcard
  private: false
 shmem-private:
  interface: shared-memory
  private: true
apps:
 app:
  plugs: [shmem]
`

const sharedMemoryProviderYaml = `name: provider
version: 0
slots:
 shmem:
  interface: shared-memory
  shared-memory: foo
  write: [ bar ]
  read: [ bar-ro ]
  private: false
 shmem-wildcard:
  interface: shared-memory
  shared-memory: foo-wildcard
  write: [ bar* ]
  read: [ bar-ro* ]
  private: false
apps:
 app:
  slots: [shmem]
`

const sharedMemoryCoreYaml = `name: core
version: 0
type: os
slots:
 shared-memory:
  interface: shared-memory
apps:
 app:
`

func (s *SharedMemoryInterfaceSuite) SetUpTest(c *C) {
	s.BaseTest.SetUpTest(c)

	s.plug, s.plugInfo = MockConnectedPlug(c, sharedMemoryConsumerYaml, nil, "shmem")
	s.slot, s.slotInfo = MockConnectedSlot(c, sharedMemoryProviderYaml, nil, "shmem")

	s.wildcardPlug, s.wildcardPlugInfo = MockConnectedPlug(c, sharedMemoryConsumerYaml, nil, "shmem-wildcard")
	s.wildcardSlot, s.wildcardSlotInfo = MockConnectedSlot(c, sharedMemoryProviderYaml, nil, "shmem-wildcard")

	s.privatePlug, s.privatePlugInfo = MockConnectedPlug(c, sharedMemoryConsumerYaml, nil, "shmem-private")
	s.privateSlot, s.privateSlotInfo = MockConnectedSlot(c, sharedMemoryCoreYaml, nil, "shared-memory")
}

func (s *SharedMemoryInterfaceSuite) TestName(c *C) {
	c.Assert(s.iface.Name(), Equals, "shared-memory")
}

func (s *SharedMemoryInterfaceSuite) TestSanitizePlug(c *C) {
	c.Check(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
	c.Check(interfaces.BeforeConnectPlug(s.iface, s.plug), IsNil)

	c.Check(interfaces.BeforePreparePlug(s.iface, s.wildcardPlugInfo), IsNil)
	c.Check(interfaces.BeforeConnectPlug(s.iface, s.wildcardPlug), IsNil)
}

func (s *SharedMemoryInterfaceSuite) TestSanitizePlugUnhappy(c *C) {
	var sharedMemoryYaml = `name: consumer
version: 0
plugs:
 shmem:
  interface: shared-memory
  %s
apps:
 app:
  plugs: [shmem]
`
	data := []struct {
		plugYaml      string
		expectedError string
	}{
		{
			"shared-memory: [one two]",
			`shared-memory "shared-memory" attribute must be a string, not \[one two\]`,
		},
		{
			"private: hello",
			`shared-memory "private" attribute must be a bool, not hello`,
		},
		{
			"private: true\n  shared-memory: foo",
			`shared-memory "shared-memory" attribute must not be set together with "private: true"`,
		},
	}

	for _, testData := range data {
		snapYaml := fmt.Sprintf(sharedMemoryYaml, testData.plugYaml)
		_, plug := MockConnectedPlug(c, snapYaml, nil, "shmem")
		err := interfaces.BeforePreparePlug(s.iface, plug)
		c.Check(err, ErrorMatches, testData.expectedError, Commentf("yaml: %s", testData.plugYaml))
	}
}

func (s *SharedMemoryInterfaceSuite) TestPlugPrivateAttribute(c *C) {
	const snapYaml = `name: consumer
version: 0
plugs:
 shmem:
  interface: shared-memory
  private: true
apps:
 app:
  plugs: [shmem]
`
	_, plug := MockConnectedPlug(c, snapYaml, nil, "shmem")
	err := interfaces.BeforePreparePlug(s.iface, plug)
	c.Assert(err, IsNil)
	c.Check(plug.Attrs["private"], Equals, true)
	c.Check(plug.Attrs["shared-memory"], Equals, nil)
}

func (s *SharedMemoryInterfaceSuite) TestPlugPrivateConflictsWithNonPrivate(c *C) {
	const snapYaml1 = `name: consumer
version: 0
plugs:
  shmem:
    interface: shared-memory
  shmem-private:
    interface: shared-memory
    private: true
`
	_, plug := MockConnectedPlug(c, snapYaml1, nil, "shmem-private")
	err := interfaces.BeforePreparePlug(s.iface, plug)
	c.Check(err, ErrorMatches, `shared-memory plug with "private: true" set cannot be used with other shared-memory plugs`)

	const snapYaml2 = `name: consumer
version: 0
plugs:
  shmem-private:
    interface: shared-memory
    private: true
slots:
  shmem:
    interface: shared-memory
`
	_, plug = MockConnectedPlug(c, snapYaml2, nil, "shmem-private")
	err = interfaces.BeforePreparePlug(s.iface, plug)
	c.Check(err, ErrorMatches, `shared-memory plug with \"private: true\" set cannot be used with shared-memory slots`)
}

func (s *SharedMemoryInterfaceSuite) TestPlugShmAttribute(c *C) {
	var plugYamlTemplate = `name: consumer
version: 0
plugs:
 shmem:
  interface: shared-memory
  %s
apps:
 app:
  plugs: [shmem]
`

	data := []struct {
		plugYaml     string
		expectedName string
	}{
		{
			"",      // missing "shared-memory" attribute
			"shmem", // use the name of the plug
		},
		{
			"shared-memory: shmemFoo",
			"shmemFoo",
		},
	}

	for _, testData := range data {
		snapYaml := fmt.Sprintf(plugYamlTemplate, testData.plugYaml)
		_, plug := MockConnectedPlug(c, snapYaml, nil, "shmem")
		err := interfaces.BeforePreparePlug(s.iface, plug)
		c.Assert(err, IsNil)
		c.Check(plug.Attrs["private"], Equals, false,
			Commentf(`yaml: %q`, testData.plugYaml))
		c.Check(plug.Attrs["shared-memory"], Equals, testData.expectedName,
			Commentf(`yaml: %q`, testData.plugYaml))
	}
}

func (s *SharedMemoryInterfaceSuite) TestSanitizeSlot(c *C) {
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.wildcardSlotInfo), IsNil)
}

func (s *SharedMemoryInterfaceSuite) TestSanitizeSlotUnhappy(c *C) {
	var sharedMemoryYaml = `name: provider
version: 0
slots:
 shmem:
  interface: shared-memory
  %s
apps:
 app:
  slots: [shmem]
`
	data := []struct {
		slotYaml      string
		expectedError string
	}{
		{
			"shared-memory: 12",
			`shared-memory "shared-memory" attribute must be a string, not 12`,
		},
		{
			"", // missing "write" attribute
			`shared memory interface requires at least a valid "read" or "write" attribute`,
		},
		{
			"write: a string",
			`shared-memory "write" attribute must be a list of strings, not "a string"`,
		},
		{
			"read: [Mixed, 12, False, list]",
			`shared-memory "read" attribute must be a list of strings, not "\[Mixed 12 false list\]"`,
		},
		{
			`read: ["ok", "trailing-space "]`,
			`shared-memory interface path has leading or trailing spaces: "trailing-space "`,
		},
		{
			`write: [" leading-space"]`,
			`shared-memory interface path has leading or trailing spaces: " leading-space"`,
		},
		{
			`write: [""]`,
			`shared-memory interface path is empty`,
		},
		{
			`write: [mem**]`,
			`shared-memory interface path is invalid: "mem\*\*" contains \*\* which is unsupported.*`,
		},
		{
			`read: [..]`,
			`shared-memory interface path is not clean: ".."`,
		},
		{
			`write: [/dev/shm/bar]`,
			`shared-memory interface path should not contain '/': "/dev/shm/bar"`,
		},
		{
			`write: [mem/../etc]`,
			`shared-memory interface path should not contain '/': "mem/../etc"`,
		},
		{
			"write: [valid]\n  read: [../invalid]",
			`shared-memory interface path should not contain '/': "../invalid"`,
		},
		{
			"read: [valid]\n  write: [../invalid]",
			`shared-memory interface path should not contain '/': "../invalid"`,
		},
	}

	for _, testData := range data {
		snapYaml := fmt.Sprintf(sharedMemoryYaml, testData.slotYaml)
		_, slot := MockConnectedSlot(c, snapYaml, nil, "shmem")
		err := interfaces.BeforePrepareSlot(s.iface, slot)
		c.Check(err, ErrorMatches, testData.expectedError, Commentf("yaml: %s", testData.slotYaml))
	}
}

func (s *SharedMemoryInterfaceSuite) TestSlotShmAttribute(c *C) {
	var slotYamlTemplate = `name: consumer
version: 0
slots:
 shmem:
  interface: shared-memory
  write: [foo]
  %s
apps:
 app:
  slots: [shmem]
`

	data := []struct {
		slotYaml     string
		expectedName string
	}{
		{
			"",      // missing "shared-memory" attribute
			"shmem", // use the name of the slot
		},
		{
			"shared-memory: shmemBar",
			"shmemBar",
		},
	}

	for _, testData := range data {
		snapYaml := fmt.Sprintf(slotYamlTemplate, testData.slotYaml)
		_, slot := MockConnectedSlot(c, snapYaml, nil, "shmem")
		err := interfaces.BeforePrepareSlot(s.iface, slot)
		c.Assert(err, IsNil)
		c.Check(slot.Attrs["shared-memory"], Equals, testData.expectedName,
			Commentf(`yaml: %q`, testData.slotYaml))
	}
}

func (s *SharedMemoryInterfaceSuite) TestStaticInfo(c *C) {
	si := interfaces.StaticInfoOf(s.iface)
	c.Check(si.ImplicitOnCore, Equals, true)
	c.Check(si.ImplicitOnClassic, Equals, true)
	c.Check(si.Summary, Equals, `allows two snaps to use predefined shared memory objects`)
	c.Check(si.BaseDeclarationSlots, testutil.Contains, "shared-memory")
}

func (s *SharedMemoryInterfaceSuite) TestAppArmorSpec(c *C) {
	appSet, err := interfaces.NewSnapAppSet(s.plug.Snap(), nil)
	c.Assert(err, IsNil)
	spec := apparmor.NewSpecification(appSet)
	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
	plugSnippet := spec.SnippetForTag("snap.consumer.app")

	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})

	c.Check(plugSnippet, testutil.Contains, `"/{dev,run}/shm/bar" mrwlk,`)
	c.Check(plugSnippet, testutil.Contains, `"/{dev,run}/shm/bar-ro" r,`)

	appSet, err = interfaces.NewSnapAppSet(s.slot.Snap(), nil)
	c.Assert(err, IsNil)
	spec = apparmor.NewSpecification(appSet)
	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.slot), IsNil)
	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.provider.app"})

	slotSnippet := spec.SnippetForTag("snap.provider.app")

	// Slot has read-write permissions to all paths
	c.Check(slotSnippet, testutil.Contains, `"/{dev,run}/shm/bar" mrwlk,`)
	c.Check(slotSnippet, testutil.Contains, `"/{dev,run}/shm/bar-ro" mrwlk,`)

	appSet, err = interfaces.NewSnapAppSet(s.wildcardPlug.Snap(), nil)
	c.Assert(err, IsNil)
	wildcardSpec := apparmor.NewSpecification(appSet)
	c.Assert(wildcardSpec.AddConnectedPlug(s.iface, s.wildcardPlug, s.wildcardSlot), IsNil)
	wildcardPlugSnippet := wildcardSpec.SnippetForTag("snap.consumer.app")

	c.Assert(wildcardSpec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})

	c.Check(wildcardPlugSnippet, testutil.Contains, `"/{dev,run}/shm/bar*" mrwlk,`)
	c.Check(wildcardPlugSnippet, testutil.Contains, `"/{dev,run}/shm/bar-ro*" r,`)

	appSet, err = interfaces.NewSnapAppSet(s.wildcardSlot.Snap(), nil)
	c.Assert(err, IsNil)
	wildcardSpec = apparmor.NewSpecification(appSet)
	c.Assert(wildcardSpec.AddConnectedSlot(s.iface, s.wildcardPlug, s.wildcardSlot), IsNil)

	c.Assert(wildcardSpec.SecurityTags(), DeepEquals, []string{"snap.provider.app"})

	wildcardSlotSnippet := wildcardSpec.SnippetForTag("snap.provider.app")

	// Slot has read-write permissions to all paths
	c.Check(wildcardSlotSnippet, testutil.Contains, `"/{dev,run}/shm/bar*" mrwlk,`)
	c.Check(wildcardSlotSnippet, testutil.Contains, `"/{dev,run}/shm/bar-ro*" mrwlk,`)

	appSet, err = interfaces.NewSnapAppSet(s.privatePlug.Snap(), nil)
	c.Assert(err, IsNil)
	spec = apparmor.NewSpecification(appSet)
	c.Assert(spec.AddConnectedPlug(s.iface, s.privatePlug, s.privateSlot), IsNil)
	privatePlugSnippet := spec.SnippetForTag("snap.consumer.app")
	privateUpdateNS := spec.UpdateNS()

	c.Check(privatePlugSnippet, testutil.Contains, `"/dev/shm/**" mrwlkix`)
	c.Check(strings.Join(privateUpdateNS, ""), Equals, `  # Private /dev/shm
  /dev/ r,
  /dev/shm/{,**} rw,
  mount options=(bind, rw) /dev/shm/snap.consumer/ -> /dev/shm/,
  umount /dev/shm/,`)

	appSet, err = interfaces.NewSnapAppSet(s.privateSlot.Snap(), nil)
	c.Assert(err, IsNil)
	spec = apparmor.NewSpecification(appSet)
	c.Assert(spec.AddConnectedSlot(s.iface, s.privatePlug, s.privateSlot), IsNil)
	privateSlotSnippet := spec.SnippetForTag("snap.core.app")

	c.Check(privateSlotSnippet, Equals, "")
}

func (s *SharedMemoryInterfaceSuite) TestMountSpec(c *C) {
	tmpdir := c.MkDir()
	dirs.SetRootDir(tmpdir)
	defer dirs.SetRootDir("/")
	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/dev/shm"), 0777), IsNil)

	// No mount entries for non-private shared-memory plugs
	spec := &mount.Specification{}
	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
	c.Check(spec.MountEntries(), HasLen, 0)

	spec = &mount.Specification{}
	c.Assert(spec.AddConnectedPlug(s.iface, s.privatePlug, s.privateSlot), IsNil)
	mounts := []osutil.MountEntry{
		{
			Name:    filepath.Join(tmpdir, "/dev/shm/snap.consumer"),
			Dir:     "/dev/shm",
			Options: []string{"bind", "rw"},
		},
	}
	c.Check(spec.MountEntries(), DeepEquals, mounts)

	// Cannot set up mount entries if /dev/shm is a symlink
	c.Assert(os.Remove(filepath.Join(tmpdir, "/dev/shm")), IsNil)
	c.Assert(os.Symlink("/run/shm", filepath.Join(tmpdir, "/dev/shm")), IsNil)
	spec = &mount.Specification{}
	err := spec.AddConnectedPlug(s.iface, s.privatePlug, s.privateSlot)
	c.Check(err, ErrorMatches, `shared-memory plug with "private: true" cannot be connected if ".*/dev/shm" is a symlink`)
}

func (s *SharedMemoryInterfaceSuite) TestAutoConnect(c *C) {
	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
}

func (s *SharedMemoryInterfaceSuite) TestInterfaces(c *C) {
	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
}

func (s *SharedMemoryInterfaceSuite) TestNoErrorOnMissingPrivate(c *C) {
	consumerYaml := `name: consumer
version: 0
plugs:
 shmem-missing:
  interface: shared-memory
  shared-memory: foo
`
	plug, _ := MockConnectedPlug(c, consumerYaml, nil, "shmem-missing")

	spec := &mount.Specification{}
	err := spec.AddConnectedPlug(s.iface, plug, nil)
	c.Assert(err, IsNil)
}

func (s *SharedMemoryInterfaceSuite) TestErrorOnBadPlug(c *C) {
	consumerYaml := `name: consumer
version: 0
plugs:
 shmem-missing:
  interface: shared-memory
  shared-memory: foo
  private: xxx
`
	plug, _ := MockConnectedPlug(c, consumerYaml, nil, "shmem-missing")

	spec := &mount.Specification{}
	err := spec.AddConnectedPlug(s.iface, plug, nil)
	c.Assert(err, ErrorMatches, `snap "consumer" has interface "shared-memory" with invalid value type string for "private" attribute: \*bool`)
}