forked from johnjohnsp1/capsule8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
288 lines (245 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
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
// Copyright 2017 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 sys
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"golang.org/x/sys/unix"
"github.com/capsule8/capsule8/pkg/sys/proc"
"github.com/golang/glog"
)
var (
// Host procfs mounted into our namespace when running as a container
hostProcFSOnce sync.Once
hostProcFS *proc.FileSystem
)
// Mount holds information about a mount in the process's mount namespace.
type Mount struct {
MountID uint
ParentID uint
Major uint
Minor uint
Root string
MountPoint string
MountOptions []string
OptionalFields map[string]string
FilesystemType string
MountSource string
SuperOptions map[string]string
}
func readMounts() []Mount {
//
// We don't return an error, we just crash if the data format from the
// Linux kernel has changed incompatibly.
//
data, err := proc.ReadFile("self/mountinfo")
if err != nil {
glog.Fatalf("Couldn't read self/mountinfo from proc")
}
var mounts []Mount
scanner := bufio.NewScanner(strings.NewReader(string(data)))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, " ")
mountID, err := strconv.Atoi(fields[0])
if err != nil {
glog.Fatalf("Couldn't parse mountID %s", fields[0])
}
parentID, err := strconv.Atoi(fields[1])
if err != nil {
glog.Fatalf("Couldn't parse parentID %s", fields[1])
}
mm := strings.Split(fields[2], ":")
major, err := strconv.Atoi(mm[0])
if err != nil {
glog.Fatalf("Couldn't parse major %s", mm[0])
}
minor, err := strconv.Atoi(mm[1])
if err != nil {
glog.Fatalf("Couldn't parse minor %s", 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], ":")
optionalFieldsMap[tagValue[0]] = 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]] = nameValue[1]
} else {
superOptionsMap[nameValue[0]] = ""
}
}
m := 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,
}
mounts = append(mounts, m)
}
return mounts
}
// Mounts returns the list of currently mounted filesystems.
func Mounts() []Mount {
return readMounts()
}
// ProcFS creates a proc.FileSystem representing the default procfs
// mountpoint /proc. When running inside a container, this will
// contain information from the container's pid namespace.
func ProcFS() *proc.FileSystem {
return proc.FS()
}
// HostProcFS creates a proc.FileSystem representing the underlying
// host's procfs. If we are running in the host pid namespace, it uses
// /proc. Otherwise, it identifies a mounted-in host procfs by it
// being mounted on a directory that isn't /proc and /proc/self
// linking to a differing PID than that returned by os.Getpid(). If we
// are running in a container and no mounted-in host procfs was
// identified, then it returns nil.
func HostProcFS() *proc.FileSystem {
hostProcFSOnce.Do(func() {
hostProcFS = findHostProcFS()
})
return hostProcFS
}
func findHostProcFS() *proc.FileSystem {
//
// Look at /proc's init to see if it is in one or more root
// cgroup paths.
//
procFS := ProcFS()
initCgroups, err := procFS.Cgroups(1)
if err != nil {
glog.Fatalf("Couldn't get cgroups for pid 1: %s", err)
}
for _, cg := range initCgroups {
if cg.Path == "/" {
// /proc is a host procfs, return it
return procFS
}
}
//
// /proc isn't a host procfs, so search all mounted filesystems for it
//
for _, mi := range Mounts() {
if mi.FilesystemType == "proc" {
if mi.MountPoint != "/proc" {
fs := proc.FileSystem{MountPoint: mi.MountPoint}
initCgroups, err := fs.Cgroups(1)
if err != nil {
glog.Warningf("Couldn't get cgroups for pid 1 on procfs %s: %s",
mi.MountPoint, err)
}
for _, cg := range initCgroups {
if cg.Path == "/" {
return &fs
}
}
}
}
}
return nil
}
// TracingDir returns the directory on either the debugfs or tracefs
// used to control the Linux kernel trace event subsystem.
func TracingDir() string {
mounts := 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 m.MountPoint
}
}
return ""
}
// PerfEventDir returns the mountpoint of the perf_event cgroup
// pseudo-filesystem or an empty string if it wasn't found.
func PerfEventDir() string {
for _, mi := range Mounts() {
if mi.FilesystemType == "cgroup" {
for option := range mi.SuperOptions {
if option == "perf_event" {
return mi.MountPoint
}
}
}
}
return ""
}
// MountTempFS mounts a filesystem. It is primarily a wrapper around unix.Mount,
// but it also ensures that the mountpoint exists before attempting to mount to
// it.
func MountTempFS(source string, target string, fstype string, flags uintptr, data string) error {
// Make sure that `target` exists.
err := os.MkdirAll(target, 0500)
if err != nil {
glog.V(2).Infof("Couldn't create temp %s mountpoint: %s", fstype, err)
return err
}
err = unix.Mount(source, target, fstype, flags, data)
if err != nil {
glog.V(2).Infof("Couldn't mount %s on %s: %s", fstype, target, err)
return err
}
return nil
}
// UnmountTempFS unmounts a filesystem. It is primarily a wrapper around
// unix.Unmount, but it also removes the mountpoint after the filesystem
// is unmounted.
func UnmountTempFS(dir string, fstype string) error {
err := unix.Unmount(dir, 0)
if err != nil {
glog.V(2).Infof("Couldn't unmount %s at %s: %s", fstype, dir, err)
return err
}
err = os.Remove(dir)
if err != nil {
glog.V(2).Infof("Couldn't remove %s: %s", dir, err)
return err
}
return nil
}