-
Notifications
You must be signed in to change notification settings - Fork 141
/
linux_masked_paths.go
182 lines (152 loc) · 4.34 KB
/
linux_masked_paths.go
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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/mndrix/tap-go"
"github.com/opencontainers/runtime-tools/validation/util"
"golang.org/x/sys/unix"
)
func checkMaskedPaths(t *tap.T) error {
g, err := util.GetDefaultGenerator()
if err != nil {
return err
}
maskedDir := "masked-dir"
maskedSubDir := "masked-subdir"
maskedFile := "masked-file"
maskedDirTop := filepath.Join("/", maskedDir)
maskedFileTop := filepath.Join("/", maskedFile)
maskedDirSub := filepath.Join(maskedDirTop, maskedSubDir)
maskedFileSub := filepath.Join(maskedDirTop, maskedFile)
maskedFileSubSub := filepath.Join(maskedDirSub, maskedFile)
g.AddLinuxMaskedPaths(maskedDirTop)
g.AddLinuxMaskedPaths(maskedFileTop)
g.AddLinuxMaskedPaths(maskedDirSub)
g.AddLinuxMaskedPaths(maskedFileSub)
g.AddLinuxMaskedPaths(maskedFileSubSub)
g.AddAnnotation("TestName", "check masked paths")
err = util.RuntimeInsideValidate(g, t, func(path string) error {
testDir := filepath.Join(path, maskedDirSub)
err = os.MkdirAll(testDir, 0o777)
if err != nil {
return err
}
// create a temp file to make testDir non-empty
tmpfile, err := os.CreateTemp(testDir, "tmp")
if err != nil {
return err
}
defer os.Remove(tmpfile.Name())
// runtimetest cannot check the readability of empty files, so
// write something.
testSubSubFile := filepath.Join(path, maskedFileSubSub)
if err := os.WriteFile(testSubSubFile, []byte("secrets"), 0o777); err != nil {
return err
}
testSubFile := filepath.Join(path, maskedFileSub)
if err := os.WriteFile(testSubFile, []byte("secrets"), 0o777); err != nil {
return err
}
testFile := filepath.Join(path, maskedFile)
return os.WriteFile(testFile, []byte("secrets"), 0o777)
})
return err
}
func checkMaskedRelPaths(t *tap.T) error {
g, err := util.GetDefaultGenerator()
if err != nil {
return err
}
// Deliberately set a relative path to be masked, and expect an error
maskedRelPath := "masked-relpath"
g.AddLinuxMaskedPaths(maskedRelPath)
g.AddAnnotation("TestName", "check masked relative paths")
err = util.RuntimeInsideValidate(g, t, func(path string) error {
testFile := filepath.Join(path, maskedRelPath)
if _, err := os.Stat(testFile); err != nil && os.IsNotExist(err) {
return err
}
return nil
})
if err != nil {
return nil
}
return fmt.Errorf("expected: err != nil, actual: err == nil")
}
func checkMaskedSymlinks(t *tap.T) error {
g, err := util.GetDefaultGenerator()
if err != nil {
return err
}
// Deliberately create a masked symlink that points an invalid file,
// and expect an error.
maskedSymlink := "/masked-symlink"
g.AddLinuxMaskedPaths(maskedSymlink)
g.AddAnnotation("TestName", "check masked symlinks")
err = util.RuntimeInsideValidate(g, t, func(path string) error {
testFile := filepath.Join(path, maskedSymlink)
// ln -s .. /masked-symlink ; readlink -f /masked-symlink; ls -L /masked-symlink
if err := os.Symlink("../masked-symlink", testFile); err != nil {
return err
}
rPath, errR := os.Readlink(testFile)
if errR != nil {
return errR
}
_, errS := os.Stat(rPath)
if errS != nil && os.IsNotExist(errS) {
return errS
}
return nil
})
if err != nil {
return nil
}
return fmt.Errorf("expected: err != nil, actual: err == nil")
}
func checkMaskedDeviceNodes(t *tap.T, mode uint32) error {
g, err := util.GetDefaultGenerator()
if err != nil {
return err
}
maskedDevice := "/masked-device"
g.AddLinuxMaskedPaths(maskedDevice)
g.AddAnnotation("TestName", "check masked device nodes")
return util.RuntimeInsideValidate(g, t, func(path string) error {
testFile := filepath.Join(path, maskedDevice)
if err := unix.Mknod(testFile, mode, 0); err != nil {
return err
}
if _, err := os.Stat(testFile); err != nil && os.IsNotExist(err) {
return err
}
return nil
})
}
func main() {
t := tap.New()
t.Header(0)
defer t.AutoPlan()
if err := checkMaskedPaths(t); err != nil {
util.Fatal(err)
}
if err := checkMaskedRelPaths(t); err != nil {
util.Fatal(err)
}
if err := checkMaskedSymlinks(t); err != nil {
util.Fatal(err)
}
// test creation of different type of devices, i.e. block device,
// character device, and FIFO.
modes := []uint32{
unix.S_IFBLK | 0o666,
unix.S_IFCHR | 0o666,
unix.S_IFIFO | 0o666,
}
for _, m := range modes {
if err := checkMaskedDeviceNodes(t, m); err != nil {
util.Fatal(err)
}
}
}