-
Notifications
You must be signed in to change notification settings - Fork 837
/
mysqld_kern.c
233 lines (202 loc) · 7.09 KB
/
mysqld_kern.c
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
#include "ecapture.h"
#define DISPATCH_COMMAND_V57_FAILED -2
struct data_t {
u64 pid;
u64 timestamp;
char query[MAX_DATA_SIZE_MYSQL];
u64 alllen;
u64 len;
char comm[TASK_COMM_LEN];
s8 retval; // dispatch_command return value
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
__type(value, struct data_t);
__uint(max_entries, 1024);
} sql_hash SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
} events SEC(".maps");
SEC("uprobe/dispatch_command")
int mysql56_query(struct pt_regs *ctx) {
/*
Trace only packets with enum_server_command == COM_QUERY
https://dev.mysql.com/doc/internals/en/com-query.html COM_QUERT command 03
*/
// MYSQL57
// https://github.com/MariaDB/server/blob/b5852ffbeebc3000982988383daeefb0549e058a/sql/sql_parse.h#L112
// dispatch_command_return dispatch_command(enum enum_server_command
// command, THD *thd,
// char* packet, uint
// packet_length, bool
// blocking = true);
// https://blog.csdn.net/u010502974/article/details/96362601
// mysql_parse
u64 command = (u64)PT_REGS_PARM1(ctx);
if (command != COM_QUERY) {
return 0;
}
u64 current_pid_tgid = bpf_get_current_pid_tgid();
u32 pid = current_pid_tgid >> 32;
#ifndef KERNEL_LESS_5_2
// if target_ppid is 0 then we target all pids
if (target_pid != 0 && target_pid != pid) {
return 0;
}
#endif
u64 len = (u64)PT_REGS_PARM4(ctx);
if (len < 0) {
return 0;
}
struct data_t data = {};
data.pid = pid; // only process id
data.alllen = len; // origin query sql length
data.timestamp = bpf_ktime_get_ns();
data.retval = -1;
len = (len < MAX_DATA_SIZE_MYSQL ? (len & (MAX_DATA_SIZE_MYSQL - 1))
: MAX_DATA_SIZE_MYSQL);
data.len = len; // only process id
bpf_get_current_comm(&data.comm, sizeof(data.comm));
bpf_probe_read_user(&data.query, len, (void *)PT_REGS_PARM3(ctx));
bpf_map_update_elem(&sql_hash, &pid, &data, BPF_ANY);
// bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU,
// &data,sizeof(data));
return 0;
}
SEC("uretprobe/dispatch_command")
int mysql56_query_return(struct pt_regs *ctx) {
// https://github.com/MariaDB/server/blob/b5852ffbeebc3000982988383daeefb0549e058a/sql/sql_parse.h#L112
// enum dispatch_command_return
// {
// DISPATCH_COMMAND_SUCCESS=0,
// DISPATCH_COMMAND_CLOSE_CONNECTION= 1,
// DISPATCH_COMMAND_WOULDBLOCK= 2
// };
// dispatch_command_return dispatch_command(enum enum_server_command
// command, THD *thd,
// char* packet, uint
// packet_length, bool
// blocking = true);
u64 current_pid_tgid = bpf_get_current_pid_tgid();
u32 pid = current_pid_tgid >> 32;
#ifndef KERNEL_LESS_5_2
// if target_ppid is 0 then we target all pids
if (target_pid != 0 && target_pid != pid) {
return 0;
}
#endif
s8 command_return = (u64)PT_REGS_RC(ctx);
struct data_t *data = bpf_map_lookup_elem(&sql_hash, &pid);
if (!data) {
return 0; // missed start
}
debug_bpf_printk("mysql query:%s\n", data->query);
data->retval = command_return;
debug_bpf_printk("mysql query return :%d\n", command_return);
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, data,
sizeof(struct data_t));
return 0;
}
// mysql 8.0
/*
https://github.com/mysql/mysql-server/blob/8.0/sql/sql_parse.h
bool dispatch_command(THD *thd, const COM_DATA *com_data,
enum enum_server_command command);
// https://github.com/mysql/mysql-server/blob/8.0/include/mysql/com_data.h
struct PS_PARAM {
unsigned char null_bit;
// enum enum_field_types type;
int type;
unsigned char unsigned_type;
const unsigned char *value;
unsigned long length;
const unsigned char *name;
unsigned long name_length;
};
// for 5.7
struct COM_QUERY_DATA {
const char *query;
unsigned int length;
PS_PARAM *parameters; // for 8.0
unsigned long parameter_count; // for 8.0
};
*/
// mysql 5.7
// https://github.com/mysql/mysql-server/blob/5.7/include/mysql/com_data.h
struct COM_QUERY_DATA {
const char *query;
unsigned int length;
// struct PS_PARAM *parameters; TODO
// unsigned long parameter_count;
};
// https://github.com/mysql/mysql-server/blob/5.7/sql/sql_parse.h
// bool dispatch_command(THD *thd, const COM_DATA *com_data,
// enum enum_server_command command);
// hook function _Z16dispatch_commandP3THDPK8COM_DATA19enum_server_command at
// version:8.0.28-0ubuntu0.20.04.3
//
SEC("uprobe/dispatch_command_57")
int mysql57_query(struct pt_regs *ctx) {
u64 command = (u64)PT_REGS_PARM3(ctx);
if (command != COM_QUERY) {
return 0;
}
u64 current_pid_tgid = bpf_get_current_pid_tgid();
u32 pid = current_pid_tgid >> 32;
#ifndef KERNEL_LESS_5_2
// if target_ppid is 0 then we target all pids
if (target_pid != 0 && target_pid != pid) {
return 0;
}
#endif
u64 len = 0;
struct data_t data = {};
data.pid = pid; // only process id
data.timestamp = bpf_ktime_get_ns();
void *st = (void *)PT_REGS_PARM2(ctx);
struct COM_QUERY_DATA query;
bpf_probe_read_user(&query, sizeof(query), st);
bpf_probe_read_user(&data.query, sizeof(data.query), query.query);
bpf_probe_read_user(&data.alllen, sizeof(data.alllen), &query.length);
len = data.alllen;
len = (len < MAX_DATA_SIZE_MYSQL ? (len & (MAX_DATA_SIZE_MYSQL - 1))
: MAX_DATA_SIZE_MYSQL);
data.len = len;
bpf_get_current_comm(&data.comm, sizeof(data.comm));
bpf_map_update_elem(&sql_hash, &pid, &data, BPF_ANY);
// bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU,
// &data,sizeof(data));
return 0;
}
//@retval
// 0 ok
// @retval
// 1 request of thread shutdown, i. e. if command is
// COM_QUIT
SEC("uretprobe/dispatch_command_57")
int mysql57_query_return(struct pt_regs *ctx) {
u64 current_pid_tgid = bpf_get_current_pid_tgid();
u32 pid = current_pid_tgid >> 32;
#ifndef KERNEL_LESS_5_2
// if target_ppid is 0 then we target all pids
if (target_pid != 0 && target_pid != pid) {
return 0;
}
#endif
u8 command_return = (u64)PT_REGS_RC(ctx);
struct data_t *data = bpf_map_lookup_elem(&sql_hash, &pid);
if (!data) {
return 0; // missed start
}
debug_bpf_printk("mysql57+ query:%s\n", data->query);
debug_bpf_printk("mysql57+ query return :%d\n", command_return);
if (command_return == 1) {
data->retval = DISPATCH_COMMAND_V57_FAILED;
} else {
data->retval = command_return;
}
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, data,
sizeof(struct data_t));
return 0;
}