Skip to content

Commit fbc72bb

Browse files
mount tracefs path if it's not mounted
Some kernels or systems may not have tracefs mounted or available by default. We need the path /sys/kernel/tracing to be mounted, in order to add syscalls ebpf probes. So from now on we'll try to mount /sys/kernel/tracing if it's not already mounted. -check-requirements parameter updated to check if this option is available. Closes #1450.
1 parent c57114d commit fbc72bb

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

daemon/core/system.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ func GetKernelVersion() string {
2828
return strings.Replace(string(version), "\n", "", -1)
2929
}
3030

31+
// GetMounts returns the mounts of the system
32+
func GetMounts() []string {
33+
buf, _ := ioutil.ReadFile("/proc/mounts")
34+
return strings.Split(string(buf), "\n")
35+
}
36+
37+
// HasTraceFS returns if tracefs is mounted
38+
func IsTraceFSMounted() bool {
39+
for _, line := range GetMounts() {
40+
if strings.Contains(line, "tracefs") {
41+
return true
42+
}
43+
}
44+
45+
return false
46+
}
47+
3148
// CheckSysRequirements checks system features we need to work properly
3249
func CheckSysRequirements() {
3350
type checksT struct {
@@ -109,17 +126,19 @@ func CheckSysRequirements() {
109126
"Regexps": [
110127
"CONFIG_FTRACE=y"
111128
],
112-
"Reason": " - CONFIG_TRACE=y not set. Common error => Error while loading kprobes: invalid argument."
129+
"Reason": " - CONFIG_FTRACE=y not set. Common error => Error while loading kprobes: invalid argument."
113130
}
114131
},
115132
{
116133
"Item": "syscalls",
117134
"Checks": {
118135
"Regexps": [
119136
"CONFIG_HAVE_SYSCALL_TRACEPOINTS=y",
120-
"CONFIG_FTRACE_SYSCALLS=y"
137+
"CONFIG_FTRACE_SYSCALLS=y",
138+
"CONFIG_TRACING=[my]",
139+
"CONFIG_EVENT_TRACING=[my]"
121140
],
122-
"Reason": " - CONFIG_FTRACE_SYSCALLS or CONFIG_HAVE_SYSCALL_TRACEPOINTS not set. Common error => error enabling tracepoint tracepoint/syscalls/sys_enter_execve: cannot read tracepoint id"
141+
"Reason": " - CONFIG_FTRACE_SYSCALLS, CONFIG_HAVE_SYSCALL_TRACEPOINTS, CONFIG_TRACE or CONFIG_EVENT_TRACING not set. Common error => error enabling tracepoint tracepoint/syscalls/sys_enter_execve: cannot read tracepoint id"
123142
}
124143
},
125144
{
@@ -193,6 +212,14 @@ func CheckSysRequirements() {
193212
fmt.Println()
194213
}
195214
}
215+
216+
if IsTraceFSMounted() {
217+
fmt.Printf("\t* %s\t %s\n\n", log.Bold(log.Green("tracefs mount")), log.Bold(log.Green("✔")))
218+
} else {
219+
reqsFullfiled = false
220+
fmt.Printf("\t* %s\t %s\n\n", log.Bold(log.Red("tracefs mount not found, needed for syscalls (mount -t tracefs none /sys/kernel/tracing/)")), log.Bold(log.Red("✘")))
221+
}
222+
196223
if !reqsFullfiled {
197224
log.Raw("\n%sWARNING:%s Your kernel doesn't support some of the features OpenSnitch needs:\nRead more: https://github.com/evilsocket/opensnitch/issues/774\n", log.FG_WHITE+log.BG_YELLOW, log.RESET)
198225
}

daemon/procmon/ebpf/events.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ type eventsDefsT struct {
8787
}
8888

8989
func initEventsStreamer() *Error {
90+
if !core.IsTraceFSMounted() {
91+
if err := mountTraceFS(); err != nil {
92+
return &Error{err, EventsNotAvailable}
93+
}
94+
}
95+
9096
eventsColl, err := core.LoadEbpfModule("opensnitch-procs.o", ebpfCfg.ModulesPath)
9197
if err != nil {
9298
return &Error{err, EventsNotAvailable}

daemon/procmon/ebpf/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ func determineHostByteOrder() {
2626
lock.Unlock()
2727
}
2828

29-
func mountDebugFS() error {
30-
debugfsPath := "/sys/kernel/debug/"
31-
kprobesPath := fmt.Sprint(debugfsPath, "tracing/kprobe_events")
29+
func mountTraceFS() error {
30+
tracefsPath := "/sys/kernel/tracing/"
31+
kprobesPath := fmt.Sprint(tracefsPath, "kprobe_events")
3232
if core.Exists(kprobesPath) == false {
33-
if _, err := core.Exec("mount", []string{"-t", "debugfs", "none", debugfsPath}); err != nil {
34-
log.Warning("eBPF debugfs error: %s", err)
33+
if _, err := core.Exec("mount", []string{"-t", "tracefs", "none", tracefsPath}); err != nil {
34+
log.Warning("eBPF tracefs error: %s", err)
3535
return fmt.Errorf(`%s
3636
Unable to access debugfs filesystem, needed for eBPF to work, likely caused by a hardened or customized kernel.
3737
Change process monitor method to 'proc' to stop receiving this alert

0 commit comments

Comments
 (0)