forked from microsoft/SysinternalsEBPF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibsysinternalsEBPFinstaller.c
514 lines (438 loc) · 15.2 KB
/
libsysinternalsEBPFinstaller.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
SysinternalsEBPF
Copyright (c) Microsoft Corporation
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 021101301 USA.
*/
//====================================================================
//
// libsysinternalsEBPFinstaller.c
//
// Standalone installer executable that writes packed files to the
// correct locations. This installer can be copied to another host and
// when it is run it will install SysinternalsEBPF to that host
// without needing additional files.
//
//====================================================================
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "libsysinternalsEBPF.h"
#define SYSINTERNALSEBPF_DIR "/opt/sysinternalsEBPF"
#define GETOFFSETS_DIR SYSINTERNALSEBPF_DIR "/getOffsets"
#define EBPFKERN_DIR SYSINTERNALSEBPF_DIR "/ebpfKern"
#define LIBBPF_DIR SYSINTERNALSEBPF_DIR "/libbpf"
#define SYSINTERNALSEBPF_BINARY "libsysinternalsEBPFinstaller"
#define LIB_DEST "/usr/lib"
#define HEADER_DEST "/usr/include"
#define SYSINTERNALSEBPF_LIB "/libsysinternalsEBPF.so"
#define SYSINTERNALSEBPF_HEADER "libsysinternalsEBPF.h"
#define OFFSETS_DB_FILE "offsets.json"
#define MEM_DUMP_OBJ "sysinternalsEBPFmemDump.o"
#define RAW_SOCK_OBJ "sysinternalsEBPFrawSock.o"
#define DEB_x86_64 "/x86_64-linux-gnu"
#define LIB_SYM_LNK "/lib"
#define LIB64_SYM_LNK "/lib64"
extern char _binary_libsysinternalsEBPF_so_start[];
extern char _binary_libsysinternalsEBPF_so_end[];
extern char _binary_libsysinternalsEBPF_h_start[];
extern char _binary_libsysinternalsEBPF_h_end[];
extern char _binary_sysinternalsEBPFmemDump_o_start[];
extern char _binary_sysinternalsEBPFmemDump_o_end[];
extern char _binary_sysinternalsEBPFrawSock_o_start[];
extern char _binary_sysinternalsEBPFrawSock_o_end[];
extern char _binary_sysinternalsEBPFshared_h_start[];
extern char _binary_sysinternalsEBPFshared_h_end[];
extern char _binary_sysinternalsEBPFoffsets_h_start[];
extern char _binary_sysinternalsEBPFoffsets_h_end[];
extern char _binary_offsets_offsets_json_start[];
extern char _binary_offsets_offsets_json_end[];
extern char _binary_getOffsets_LICENSE_start[];
extern char _binary_getOffsets_LICENSE_end[];
extern char _binary_getOffsets_Makefile_start[];
extern char _binary_getOffsets_Makefile_end[];
extern char _binary_getOffsets_README_md_start[];
extern char _binary_getOffsets_README_md_end[];
extern char _binary_getOffsets_extractOffsets_c_start[];
extern char _binary_getOffsets_extractOffsets_c_end[];
extern char _binary_getOffsets_getOffsets_c_start[];
extern char _binary_getOffsets_getOffsets_c_end[];
extern char _binary_getOffsets_mount_h_start[];
extern char _binary_getOffsets_mount_h_end[];
extern char _binary_ebpfKern_LICENSE_start[];
extern char _binary_ebpfKern_LICENSE_end[];
extern char _binary_ebpfKern_sysinternalsEBPF_common_h_start[];
extern char _binary_ebpfKern_sysinternalsEBPF_common_h_end[];
extern char _binary_ebpfKern_sysinternalsEBPF_helpers_c_start[];
extern char _binary_ebpfKern_sysinternalsEBPF_helpers_c_end[];
extern char _binary_LICENSE_LGPL_2_1_start[];
extern char _binary_LICENSE_LGPL_2_1_end[];
extern char _binary_src_bpf_helpers_h_start[];
extern char _binary_src_bpf_helpers_h_end[];
extern char _binary_src_bpf_helper_defs_h_start[];
extern char _binary_src_bpf_helper_defs_h_end[];
extern char _binary_src_bpf_core_read_h_start[];
extern char _binary_src_bpf_core_read_h_end[];
mode_t dirMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
mode_t fileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
mode_t exeFileMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
//--------------------------------------------------------------------
//
// delDir
//
// Deletes all contents of specified directory
//
//--------------------------------------------------------------------
int delDir(char* path)
{
int ret = -1;
DIR* d = opendir(path);
size_t pathLen = strlen(path);
if(d)
{
struct dirent* p = NULL;
ret = 0;
while(!ret && (p=readdir(d)))
{
int ret2 = -1;
char* buf = NULL;
size_t len = 0;
if(!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
{
continue;
}
len = pathLen + strlen(p->d_name) + 2;
buf=malloc(len);
if(buf)
{
struct stat statbuf = {};
snprintf(buf, len, "%s/%s", path, p->d_name);
if(!stat(buf, &statbuf))
{
if(S_ISDIR(statbuf.st_mode))
{
ret2 = delDir(buf);
}
else
{
ret2 = unlink(buf);
}
}
free(buf);
}
ret = ret2;
}
closedir(d);
}
if(!ret)
{
ret = rmdir(path);
}
return ret;
}
//--------------------------------------------------------------------
//
// getLibInstallPath
//
// Returns the full install path for sysinternalsEBPF.
//
//--------------------------------------------------------------------
char* getLibInstallPath()
{
char libPath[PATH_MAX] = {0};
char fullPath[PATH_MAX] = {0};
fullPath[0]='/';
if (readlink(LIB_SYM_LNK, libPath, PATH_MAX) <= 0)
{
// if no symlink it's an old distro, just use /usr/lib
if (dirExists(LIB_DEST))
{
strcpy(fullPath, LIB_DEST);
}
else
{
return NULL;
}
}
else
{
// check if debian based
strcat(libPath, DEB_x86_64);
strcat(fullPath, libPath);
if (!dirExists(fullPath))
{
// check if fedora based
fullPath[0]='/';
fullPath[1]=0;
memset(libPath, 0, PATH_MAX); // readlink does not null terminate
if (readlink(LIB64_SYM_LNK, libPath, PATH_MAX))
{
strcat(fullPath, libPath);
}
else
{
return NULL;
}
}
}
strcat(fullPath, SYSINTERNALSEBPF_LIB);
return strdup(fullPath);
}
//--------------------------------------------------------------------
//
// printUsage
//
// Prints usage
//
//--------------------------------------------------------------------
void printUsage()
{
printf("Usage: libsysinternalsEBPFinstaller [-i | -u]\n");
printf("Installs the libsysinternalsEBPF shared library, header and EBPF objects\n");
printf("as an alternative to 'sudo make install'. This installer contains all the\n");
printf("files necessary so is a portable way of installing on a system other than\n");
printf("the one it was built on.\n");
printf("Requires root privileges.\n\n");
}
//--------------------------------------------------------------------
//
// main
//
// The whole installer. Uses functions in installer.c.
//
//--------------------------------------------------------------------
int main(int argc, char *argv[])
{
int fd;
void *addr = NULL;
struct stat st;
char exePath[] = "/proc/self/exe";
bool force = true;
if (argc != 2) {
printUsage();
return 1;
}
if(strlen(argv[1]) != 2)
{
printUsage();
return 1;
}
umask(0022);
if(argv[1][1] == 'i')
{
// Install
if (!createDir(SYSINTERNALSEBPF_DIR, dirMode)) {
fprintf(stderr, "Cannot create sysinternalsEBPF directory\n");
fprintf(stderr, "Make sure you are root or sudo\n");
return 1;
}
if (!createDir(GETOFFSETS_DIR, dirMode)) {
fprintf(stderr, "Cannot create sysinternalsEBPF getOffsets directory\n");
fprintf(stderr, "Make sure you are root or sudo\n");
return 1;
}
if (!createDir(EBPFKERN_DIR, dirMode)) {
fprintf(stderr, "Cannot create sysinternalsEBPF ebpfKern directory\n");
fprintf(stderr, "Make sure you are root or sudo\n");
return 1;
}
if (!createDir(LIBBPF_DIR, dirMode)) {
fprintf(stderr, "Cannot create sysinternalsEBPF libbpf directory\n");
fprintf(stderr, "Make sure you are root or sudo\n");
return 1;
}
fd = open(exePath, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Cannot open own executable\n");
return 1;
}
if (fstat(fd, &st) < 0) {
fprintf(stderr, "Cannot stat own executable\n");
close(fd);
return 1;
}
addr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
fprintf(stderr, "Cannot map own executable\n");
close(fd);
return 1;
}
if (!dropFile(SYSINTERNALSEBPF_DIR "/" SYSINTERNALSEBPF_BINARY,
addr,
addr + st.st_size,
force,
exeFileMode)) {
munmap(addr, st.st_size);
close(fd);
return 1;
}
munmap(addr, st.st_size);
close(fd);
char* libPath = getLibInstallPath();
if (!dropFile(libPath,
_binary_libsysinternalsEBPF_so_start,
_binary_libsysinternalsEBPF_so_end,
force,
fileMode))
return 1;
if (!dropFile(HEADER_DEST "/" SYSINTERNALSEBPF_HEADER,
_binary_libsysinternalsEBPF_h_start,
_binary_libsysinternalsEBPF_h_end,
force,
fileMode))
return 1;
if (!dropFile(SYSINTERNALSEBPF_DIR "/" MEM_DUMP_OBJ,
_binary_sysinternalsEBPFmemDump_o_start,
_binary_sysinternalsEBPFmemDump_o_end,
force,
fileMode))
return 1;
if (!dropFile(SYSINTERNALSEBPF_DIR "/" RAW_SOCK_OBJ,
_binary_sysinternalsEBPFrawSock_o_start,
_binary_sysinternalsEBPFrawSock_o_end,
force,
fileMode))
return 1;
if (!dropFile(SYSINTERNALSEBPF_DIR "/" OFFSETS_DB_FILE,
_binary_offsets_offsets_json_start,
_binary_offsets_offsets_json_end,
force,
fileMode))
return 1;
if (!dropFile(GETOFFSETS_DIR "/LICENSE",
_binary_getOffsets_LICENSE_start,
_binary_getOffsets_LICENSE_end,
force,
fileMode))
return 1;
if (!dropFile(GETOFFSETS_DIR "/Makefile",
_binary_getOffsets_Makefile_start,
_binary_getOffsets_Makefile_end,
force,
fileMode))
return 1;
if (!dropFile(GETOFFSETS_DIR "/README.md",
_binary_getOffsets_README_md_start,
_binary_getOffsets_README_md_end,
force,
fileMode))
return 1;
if (!dropFile(GETOFFSETS_DIR "/extractOffsets.c",
_binary_getOffsets_extractOffsets_c_start,
_binary_getOffsets_extractOffsets_c_end,
force,
fileMode))
return 1;
if (!dropFile(GETOFFSETS_DIR "/getOffsets.c",
_binary_getOffsets_getOffsets_c_start,
_binary_getOffsets_getOffsets_c_end,
force,
fileMode))
return 1;
if (!dropFile(GETOFFSETS_DIR "/mount.h",
_binary_getOffsets_mount_h_start,
_binary_getOffsets_mount_h_end,
force,
fileMode))
return 1;
if (!dropFile(EBPFKERN_DIR "/LICENSE",
_binary_ebpfKern_LICENSE_start,
_binary_ebpfKern_LICENSE_end,
force,
fileMode))
return 1;
if (!dropFile(EBPFKERN_DIR "/sysinternalsEBPF_common.h",
_binary_ebpfKern_sysinternalsEBPF_common_h_start,
_binary_ebpfKern_sysinternalsEBPF_common_h_end,
force,
fileMode))
return 1;
if (!dropFile(EBPFKERN_DIR "/sysinternalsEBPF_helpers.c",
_binary_ebpfKern_sysinternalsEBPF_helpers_c_start,
_binary_ebpfKern_sysinternalsEBPF_helpers_c_end,
force,
fileMode))
return 1;
if (!dropFile(EBPFKERN_DIR "/sysinternalsEBPFshared.h",
_binary_sysinternalsEBPFshared_h_start,
_binary_sysinternalsEBPFshared_h_end,
force,
fileMode))
return 1;
if (!dropFile(EBPFKERN_DIR "/sysinternalsEBPFoffsets.h",
_binary_sysinternalsEBPFoffsets_h_start,
_binary_sysinternalsEBPFoffsets_h_end,
force,
fileMode))
return 1;
if (!dropFile(LIBBPF_DIR "/LICENSE.LPGL-2.1",
_binary_LICENSE_LGPL_2_1_start,
_binary_LICENSE_LGPL_2_1_end,
force,
fileMode))
return 1;
if (!dropFile(LIBBPF_DIR "/bpf_helpers.h",
_binary_src_bpf_helpers_h_start,
_binary_src_bpf_helpers_h_end,
force,
fileMode))
return 1;
if (!dropFile(LIBBPF_DIR "/bpf_helper_defs.h",
_binary_src_bpf_helper_defs_h_start,
_binary_src_bpf_helper_defs_h_end,
force,
fileMode))
return 1;
if (!dropFile(LIBBPF_DIR "/bpf_core_read.h",
_binary_src_bpf_core_read_h_start,
_binary_src_bpf_core_read_h_end,
force,
fileMode))
return 1;
printf("Success!\n");
printf("Library installed to\t%s\n", libPath);
printf("Header installed to\t%s\n", HEADER_DEST "/" SYSINTERNALSEBPF_HEADER);
printf("Support files installed to %s\n", SYSINTERNALSEBPF_DIR);
free(libPath);
}
else if(argv[1][1] == 'u')
{
// Uninstall
// delete libsysinternalsEBPF.so
char* libPath = getLibInstallPath();
if(libPath)
{
fileDelete(libPath);
}
fileDelete(HEADER_DEST "/" SYSINTERNALSEBPF_HEADER);
// delete /opt/sysinternalsEBPF
delDir(SYSINTERNALSEBPF_DIR);
printf("Success!\n");
printf("Deleted library\t%s\n", libPath);
printf("Deleted header\t%s\n", HEADER_DEST "/" SYSINTERNALSEBPF_HEADER);
printf("Deleted directory\t%s\n", SYSINTERNALSEBPF_DIR);
free(libPath);
}
else
{
printUsage();
}
return 0;
}