forked from skiptomyliu/capsule8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
258 lines (223 loc) · 7.14 KB
/
mount.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
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
// Copyright 2018 Capsule8, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package procfs
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/Happyholic1203/capsule8/pkg/sys/proc"
"github.com/golang/glog"
"golang.org/x/sys/unix"
)
func parseMount(line string) (proc.Mount, error) {
fields := strings.Fields(line)
mountID, err := strconv.Atoi(fields[0])
if err != nil {
return proc.Mount{}, fmt.Errorf("Couldn't parse mountID %q", fields[0])
}
parentID, err := strconv.Atoi(fields[1])
if err != nil {
return proc.Mount{}, fmt.Errorf("Couldn't parse parentID %q", fields[1])
}
mm := strings.Split(fields[2], ":")
major, err := strconv.Atoi(mm[0])
if err != nil {
return proc.Mount{}, fmt.Errorf("Couldn't parse major %q", mm[0])
}
minor, err := strconv.Atoi(mm[1])
if err != nil {
return proc.Mount{}, fmt.Errorf("Couldn't parse minor %q", mm[1])
}
mountOptions := strings.Split(fields[5], ",")
optionalFieldsMap := make(map[string]string)
var i int
for i = 6; fields[i] != "-"; i++ {
tagValue := strings.Split(fields[i], ":")
if len(tagValue) == 1 {
optionalFieldsMap[tagValue[0]] = ""
} else {
optionalFieldsMap[tagValue[0]] = strings.Join(tagValue[1:], ":")
}
}
filesystemType := fields[i+1]
mountSource := fields[i+2]
superOptions := fields[i+3]
superOptionsMap := make(map[string]string)
for _, option := range strings.Split(superOptions, ",") {
nameValue := strings.Split(option, "=")
if len(nameValue) == 1 {
superOptionsMap[nameValue[0]] = ""
} else {
superOptionsMap[nameValue[0]] = strings.Join(nameValue[1:], ":")
}
}
return proc.Mount{
MountID: uint(mountID),
ParentID: uint(parentID),
Major: uint(major),
Minor: uint(minor),
Root: fields[3],
MountPoint: fields[4],
MountOptions: mountOptions,
OptionalFields: optionalFieldsMap,
FilesystemType: filesystemType,
MountSource: mountSource,
SuperOptions: superOptionsMap,
}, nil
}
// Mounts returns the list of currently mounted filesystems.
func (fs *FileSystem) Mounts() []proc.Mount {
var mounts []proc.Mount
data := string(fs.ReadFileOrPanic("self/mountinfo"))
scanner := bufio.NewScanner(strings.NewReader(data))
for scanner.Scan() {
if m, err := parseMount(scanner.Text()); err != nil {
glog.Fatal(err)
} else {
mounts = append(mounts, m)
}
}
if err := scanner.Err(); err != nil {
glog.Fatal(err)
}
return mounts
}
func (fs *FileSystem) findHostFileSystem() proc.FileSystem {
for _, mi := range fs.Mounts() {
if mi.FilesystemType == "proc" && mi.MountPoint != fs.MountPoint {
hfs, err := NewFileSystem(mi.MountPoint)
if err != nil {
glog.Warning(err)
continue
}
id, err := hfs.ProcessContainerID(1)
if err != nil {
glog.Warningf("Cannot get container ID for pid 1 on procfs mounted at %s: %v",
mi.MountPoint, err)
} else if id == "" {
return hfs
}
}
}
return nil
}
// HostFileSystem returns a FileSystem representing the underlying host's
// procfs from the perspective of the active proc.FileSystem. If the calling
// process is running in the host pid namespace, the receiver may return
// itself. If the calling process is running in a container and no host proc
// filesystem is mounted in, the return will be nil. If cgroups are not enabled
// on the system, the host filesystem is the same as the calling filesystem.
func (fs *FileSystem) HostFileSystem() proc.FileSystem {
// Care should be taken here to never simply return /proc as the host
// filesystem without being absolutely sure that's the host filesystem.
// If the sensor is running inside of a container, /proc will be the
// container's proc filesystem and so the sensor will have no visibility
// outside of it. The host proc filesystem must be properly mounted
// into a container running a sensor. It is better for the sensor to
// fail to start than to run with the wrong proc filesystem. This logic
// should never fall back to simply returning /proc when all else fails.
//
// If new cases are found that cause this detection logic to fail for
// some reason (such as cgroups not being enabled on the system, which
// inspired a new fix and this comment), take care to keep the scope
// of the fix as narrow as possible.
fs.hostProcFSOnce.Do(func() {
// If this filesystem's init process (pid 1) is not in a
// container, it is the host filesystem.
id, err := fs.ProcessContainerID(1)
if err == unix.ENOENT || id == "" {
fs.hostProcFS = fs
return
}
if err != nil {
glog.Fatalf("Cannot get cgroups for pid 1: %v", err)
}
// Scan this filesystem's view of mounts to look for a host
// procfs mount.
fs.hostProcFS = fs.findHostFileSystem()
})
return fs.hostProcFS
}
// PerfEventDir returns the perf_event cgroup mountpoint to use to monitor
// specific cgroups. Return the empty string if no perf_event cgroup filesystem
// is mounted.
func (fs *FileSystem) PerfEventDir() string {
for _, mi := range fs.Mounts() {
if mi.FilesystemType == "cgroup" {
for option := range mi.SuperOptions {
if option == "perf_event" {
return mi.MountPoint
}
}
}
}
return ""
}
// TracingDir returns the tracefs mountpoint to use to control the Linux kernel
// trace event subsystem. Returns the empty string if no tracefs filesystem is
// mounted.
func (fs *FileSystem) TracingDir() string {
mounts := fs.Mounts()
// Look for an existing tracefs
for _, m := range mounts {
if m.FilesystemType == "tracefs" {
glog.V(1).Infof("Found tracefs at %s", m.MountPoint)
return m.MountPoint
}
}
// If no mounted tracefs has been found, look for it as a
// subdirectory of the older debugfs
for _, m := range mounts {
if m.FilesystemType == "debugfs" {
d := filepath.Join(m.MountPoint, "tracing")
s, err := os.Stat(filepath.Join(d, "events"))
if err == nil && s.IsDir() {
glog.V(1).Infof("Found debugfs w/ tracing at %s", d)
return d
}
}
}
return ""
}
// SupportedFilesystems returns a list of filesystem types supported by the
// system.
func (fs *FileSystem) SupportedFilesystems() []string {
var systems []string
data := string(fs.ReadFileOrPanic("filesystems"))
scanner := bufio.NewScanner(strings.NewReader(data))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
fields := strings.Fields(line)
switch len(fields) {
case 1:
// e.g., ext4, iso9660, etc.
systems = append(systems, fields[0])
case 2:
// e.g., debugfs, autofs, etc.
systems = append(systems, fields[1])
default:
glog.Fatalf("Unexpected filesystems line: %s", line)
}
}
if err := scanner.Err(); err != nil {
glog.Fatal(err)
}
return systems
}