github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/system_files_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package builtin_test 21 22 import ( 23 "strings" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/interfaces" 28 "github.com/snapcore/snapd/interfaces/apparmor" 29 "github.com/snapcore/snapd/interfaces/builtin" 30 "github.com/snapcore/snapd/snap" 31 "github.com/snapcore/snapd/snap/snaptest" 32 "github.com/snapcore/snapd/testutil" 33 ) 34 35 type systemFilesInterfaceSuite struct { 36 iface interfaces.Interface 37 slot *interfaces.ConnectedSlot 38 slotInfo *snap.SlotInfo 39 plug *interfaces.ConnectedPlug 40 plugInfo *snap.PlugInfo 41 } 42 43 var _ = Suite(&systemFilesInterfaceSuite{ 44 iface: builtin.MustInterface("system-files"), 45 }) 46 47 func (s *systemFilesInterfaceSuite) SetUpTest(c *C) { 48 const mockPlugSnapInfo = `name: other 49 version: 1.0 50 plugs: 51 system-files: 52 read: [/etc/read-dir2, /etc/read-file2] 53 write: [/etc/write-dir2, /etc/write-file2] 54 apps: 55 app: 56 command: foo 57 plugs: [system-files] 58 ` 59 s.slotInfo = &snap.SlotInfo{ 60 Snap: &snap.Info{SuggestedName: "core", SnapType: snap.TypeOS}, 61 Name: "system-files", 62 Interface: "system-files", 63 } 64 s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil) 65 plugSnap := snaptest.MockInfo(c, mockPlugSnapInfo, nil) 66 s.plugInfo = plugSnap.Plugs["system-files"] 67 s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil) 68 } 69 70 func (s *systemFilesInterfaceSuite) TestName(c *C) { 71 c.Assert(s.iface.Name(), Equals, "system-files") 72 } 73 74 func (s *systemFilesInterfaceSuite) TestConnectedPlugAppArmor(c *C) { 75 apparmorSpec := &apparmor.Specification{} 76 err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot) 77 c.Assert(err, IsNil) 78 c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"}) 79 c.Check(apparmorSpec.SnippetForTag("snap.other.app"), Equals, ` 80 # Description: Can access specific system files or directories. 81 # This is restricted because it gives file access to arbitrary locations. 82 "/etc/read-dir2{,/,/**}" rk, 83 "/etc/read-file2{,/,/**}" rk, 84 "/etc/write-dir2{,/,/**}" rwkl, 85 "/etc/write-file2{,/,/**}" rwkl, 86 `) 87 } 88 89 func (s *systemFilesInterfaceSuite) TestSanitizeSlot(c *C) { 90 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil) 91 } 92 93 func (s *systemFilesInterfaceSuite) TestSanitizePlug(c *C) { 94 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 95 } 96 97 func (s *systemFilesInterfaceSuite) TestSanitizePlugHappy(c *C) { 98 const mockSnapYaml = `name: system-files-plug-snap 99 version: 1.0 100 plugs: 101 system-files: 102 read: ["/etc/file1"] 103 write: ["/etc/dir1"] 104 ` 105 info := snaptest.MockInfo(c, mockSnapYaml, nil) 106 plug := info.Plugs["system-files"] 107 c.Assert(interfaces.BeforePreparePlug(s.iface, plug), IsNil) 108 } 109 110 func (s *systemFilesInterfaceSuite) TestSanitizePlugUnhappy(c *C) { 111 const mockSnapYaml = `name: system-files-plug-snap 112 version: 1.0 113 plugs: 114 system-files: 115 $t 116 ` 117 errPrefix := `cannot add system-files plug: ` 118 var testCases = []struct { 119 inp string 120 errStr string 121 }{ 122 {`read: ""`, `"read" must be a list of strings`}, 123 {`read: [ 123 ]`, `"read" must be a list of strings`}, 124 {`read: [ "/foo/./bar" ]`, `cannot use "/foo/./bar": try "/foo/bar"`}, 125 {`read: [ "../foo" ]`, `"../foo" must start with "/"`}, 126 {`read: [ "/foo[" ]`, `"/foo\[" contains a reserved apparmor char from .*`}, 127 {`write: ""`, `"write" must be a list of strings`}, 128 {`write: bar`, `"write" must be a list of strings`}, 129 {`read: [ "~/foo" ]`, `"~/foo" cannot contain "~"`}, 130 {`read: [ "/foo/~/foo" ]`, `"/foo/~/foo" cannot contain "~"`}, 131 {`read: [ "/foo/../foo" ]`, `cannot use "/foo/../foo": try "/foo"`}, 132 {`read: [ "/home/$HOME/foo" ]`, `\$HOME cannot be used in "/home/\$HOME/foo"`}, 133 {`read: [ "$HOME/sweet/$HOME" ]`, `"\$HOME/sweet/\$HOME" must start with "/"`}, 134 {`read: [ "/@{FOO}" ]`, `"/@{FOO}" contains a reserved apparmor char from .*`}, 135 {`read: [ "/home/@{HOME}/foo" ]`, `"/home/@{HOME}/foo" contains a reserved apparmor char from .*`}, 136 } 137 138 for _, t := range testCases { 139 yml := strings.Replace(mockSnapYaml, "$t", t.inp, -1) 140 info := snaptest.MockInfo(c, yml, nil) 141 plug := info.Plugs["system-files"] 142 143 c.Check(interfaces.BeforePreparePlug(s.iface, plug), ErrorMatches, errPrefix+t.errStr, Commentf("unexpected error for %q", t.inp)) 144 } 145 } 146 147 func (s *systemFilesInterfaceSuite) TestConnectedPlugAppArmorInternalError(c *C) { 148 const mockPlugSnapInfo = `name: other 149 version: 1.0 150 plugs: 151 system-files: 152 read: [ 123 , 345 ] 153 apps: 154 app: 155 command: foo 156 plugs: [system-files] 157 ` 158 s.slotInfo = &snap.SlotInfo{ 159 Snap: &snap.Info{SuggestedName: "core", SnapType: snap.TypeOS}, 160 Name: "system-files", 161 Interface: "system-files", 162 } 163 s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil) 164 plugSnap := snaptest.MockInfo(c, mockPlugSnapInfo, nil) 165 s.plugInfo = plugSnap.Plugs["system-files"] 166 s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil) 167 168 apparmorSpec := &apparmor.Specification{} 169 err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot) 170 c.Assert(err, ErrorMatches, `cannot connect plug system-files: 123 \(int64\) is not a string`) 171 } 172 173 func (s *systemFilesInterfaceSuite) TestInterfaces(c *C) { 174 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 175 }