-
Notifications
You must be signed in to change notification settings - Fork 186
/
tracer.go
182 lines (156 loc) · 4.31 KB
/
tracer.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
// Copyright 2023 The Inspektor Gadget authors
//
// 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 kfilefields
import (
"errors"
"fmt"
"os"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"golang.org/x/sys/unix"
"github.com/inspektor-gadget/inspektor-gadget/pkg/btfgen"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET -cc clang -cflags ${CFLAGS} -no-global-types filefields ./bpf/filefields.bpf.c -- -I./bpf/
type FdType int
const (
FdTypeSocket FdType = iota
FdTypeEbpfProgram
)
var supportedFdTypesForFOp = map[FdType]struct{}{
FdTypeSocket: {},
FdTypeEbpfProgram: {},
}
func (fd FdType) String() string {
switch fd {
case FdTypeSocket:
return "socket"
case FdTypeEbpfProgram:
return "ebpf_program"
default:
return fmt.Sprintf("unknown(%d)", fd)
}
}
type fileFields struct {
PrivateData uint64
FOp uint64
RealInode uint64
}
type Tracer struct {
objs filefieldsObjects
links []link.Link
sock [2]int
installed bool
}
func creatAndInstallTracer() (*Tracer, error) {
t := &Tracer{}
if err := t.install(); err != nil {
t.close()
return nil, fmt.Errorf("installing tracer: %w", err)
}
return t, nil
}
func (t *Tracer) close() {
for _, l := range t.links {
gadgets.CloseLink(l)
}
for i := 0; i < len(t.sock); i++ {
if t.sock[i] != -1 {
unix.Close(t.sock[i])
}
}
t.objs.Close()
}
func (t *Tracer) install() error {
// Create a socket pair
var err error
t.sock, err = unix.Socketpair(unix.AF_UNIX, unix.SOCK_DGRAM, 0)
if err != nil {
return fmt.Errorf("creating socket pair: %w", err)
}
// Find the inode of the socket
fdFileInfo, err := os.Stat(fmt.Sprintf("/proc/self/fd/%d", t.sock[0]))
if err != nil {
return fmt.Errorf("reading file info from sock fd %d: %w", t.sock[0], err)
}
fdStat, ok := fdFileInfo.Sys().(*syscall.Stat_t)
if !ok {
return errors.New("not a syscall.Stat_t")
}
fdIno := fdStat.Ino
// Load ebpf program configured with the socket inode
spec, err := loadFilefields()
if err != nil {
return fmt.Errorf("load ebpf program to read file fields: %w", err)
}
consts := map[string]interface{}{
"socket_ino": uint64(fdIno),
}
if err := spec.RewriteConstants(consts); err != nil {
return fmt.Errorf("RewriteConstants: %w", err)
}
opts := ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfgen.GetBTFSpec(),
},
}
if err := spec.LoadAndAssign(&t.objs, &opts); err != nil {
return fmt.Errorf("loading maps and programs: %w", err)
}
// Attach ebpf programs
l, err := link.Kprobe("__scm_send", t.objs.IgScmSndE, nil)
if err != nil {
return fmt.Errorf("attaching kprobe __scm_send: %w", err)
}
t.links = append(t.links, l)
l, err = link.Kretprobe("fget_raw", t.objs.IgFgetX, nil)
if err != nil {
return fmt.Errorf("attaching kretprobe fget_raw: %w", err)
}
t.links = append(t.links, l)
t.installed = true
return nil
}
func (t *Tracer) getFdFromType(kind FdType) (int, error) {
if !t.installed {
return -1, errors.New("tracer not installed")
}
switch kind {
case FdTypeSocket:
return t.sock[0], nil
case FdTypeEbpfProgram:
return t.objs.IgFgetX.FD(), nil
default:
return -1, fmt.Errorf("unknown fd type %d", kind)
}
}
func (t *Tracer) readStructFileFields(fd int) (*fileFields, error) {
if !t.installed {
return nil, errors.New("tracer not installed")
}
// Send the fd through the socket with SCM_RIGHTS.
// This will trigger the __scm_send kprobe and fget_raw kretprobe
buf := make([]byte, 1)
err := unix.Sendmsg(t.sock[0], buf, unix.UnixRights(fd), nil, 0)
if err != nil {
return nil, fmt.Errorf("sending fd: %w", err)
}
var ff fileFields
err = t.objs.IgFileFields.Lookup(uint32(0), &ff)
if err != nil {
return nil, fmt.Errorf("reading file fields: %w", err)
}
return &ff, nil
}