File: probe.go

package info (click to toggle)
golang-github-evilsocket-ftrace 1.2.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 128 kB
  • sloc: sh: 71; makefile: 5
file content (195 lines) | stat: -rw-r--r-- 4,671 bytes parent folder | download | duplicates (2)
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
package ftrace

import (
	"errors"
	"fmt"
	"strings"
	"sync"
)

const (
	maxArguments      = 16
	enabledStatusFile = "/proc/sys/kernel/ftrace_enabled"
	systemProbesFile  = "/sys/kernel/debug/tracing/kprobe_events"
	eventsPipeFile    = "/sys/kernel/debug/tracing/trace_pipe"
	probeFileFormat   = "/sys/kernel/debug/tracing/events/kprobes/%s/enable"
	eventFileFormat   = "/sys/kernel/debug/tracing/events/%s/enable"
)

var errUnavailable = errors.New("FTRACE kernel framework not available on your system")

// Probe represents a FTRACE probe to a system call and optional sub events.
type Probe struct {
	sync.RWMutex
	// custom name of the probe
	name string
	// probe status file name
	fileName string
	// syscall to intercept
	syscall string
	// ftrace descriptor of the probe
	descriptor string
	// kernel sub events
	events map[string]string
	// probe status
	enabled bool
	// pipe file reader
	pipe chan string
	// channel used to signal from the worker
	done chan bool
	// channel where events are sent
	bus chan Event
}

// NewProbe creates a new probe with a custom name for the given syscall and optional sub events.
func NewProbe(name string, syscall string, subEvents []string) *Probe {
	return &Probe{
		name:       name,
		fileName:   fmt.Sprintf(probeFileFormat, name),
		syscall:    syscall,
		descriptor: makeDescriptor(name, syscall),
		events:     mapSubevents(subEvents),
		enabled:    false,
		pipe:       nil,
		done:       make(chan bool),
		bus:        make(chan Event),
	}
}

// Enabled return true if this probe is enabled and running, otherwise false.
func (p *Probe) Enabled() bool {
	p.RLock()
	defer p.RUnlock()
	return p.enabled
}

// Events returns a channel where FTRACE events will be written by this Probe worker routine.
func (p *Probe) Events() <-chan Event {
	return p.bus
}

func (p *Probe) selectEvent(event string) bool {
	// main probe event
	if strings.Contains(event, p.name) {
		return true
	}
	// one of the sub events
	for eventName := range p.events {
		if strings.Contains(event, eventName) {
			return true
		}
	}
	return false
}

func (p *Probe) worker() {
	// signal we're done when we exit
	defer func() {
		p.done <- true
	}()

	for eventLine := range p.pipe {
		// our parent go routine is telling us to quit
		if eventLine == "<quit>" {
			break
		}

		// check if we're interested in this event
		if p.selectEvent(eventLine) {
			// parse the raw event data
			if event, err := parseEvent(eventLine); err != nil {
				fmt.Printf("Error while parsing event: %s\n", err)
			} else {
				p.bus <- event
			}
		}
	}
}

// Enable enables this probe and starts its async worker routine in order to read FTRACE events.
func (p *Probe) Enable() (err error) {
	p.Lock()
	defer p.Unlock()

	if p.enabled == true {
		return nil
	}

	if Available() == false {
		return errUnavailable
	}

	// enable all events
	for eventName, eventFileName := range p.events {
		if err = writeFile(eventFileName, "1"); err != nil {
			return fmt.Errorf("Error while enabling event %s: %s", eventName, err)
		}
	}

	// create the custom kprobe consumer
	if err = writeFile(systemProbesFile, p.descriptor); err != nil {
		return fmt.Errorf("Error while enabling probe descriptor for %s: %s", p.name, err)
	}

	// enable the probe
	if err = writeFile(p.fileName, "1"); err != nil {
		return fmt.Errorf("Error while enable probe %s: %s", p.name, err)
	}

	// create the handle to the pipe file
	if p.pipe, err = asyncFileReader(eventsPipeFile); err != nil {
		return fmt.Errorf("Error while opening %s: %s", eventsPipeFile, err)
	}

	p.enabled = true

	// start the async worker that will read events from the
	// pipe file and send them to the `bus` channel
	go p.worker()

	return nil
}

// Reset disables this probe.
func (p *Probe) Reset() error {
	// disable all events
	for eventName, eventFileName := range p.events {
		if err := writeFile(eventFileName, "0"); err != nil {
			return fmt.Errorf("Error while disabling event %s: %s", eventName, err)
		}
	}

	// disable the probe itself
	if err := writeFile(p.fileName, "0"); err != nil {
		return fmt.Errorf("Error while disabling probe %s: %s", p.name, err)
	}

	// remove the probe from the system
	if err := appendFile(systemProbesFile, fmt.Sprintf("-:%s", p.name)); err != nil {
		return fmt.Errorf("Error while removing the probe %s: %s", p.name, err)
	}

	return nil
}

// Disable disables this probe and stops its async worker.
func (p *Probe) Disable() error {
	p.Lock()
	defer p.Unlock()

	if p.enabled == false {
		return nil
	}

	if err := p.Reset(); err != nil {
		return err
	}

	p.enabled = false
	p.pipe <- "<quit>"

	// wait for the worker to finish
	<-p.done

	return nil
}