-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathelfldr.c
More file actions
828 lines (680 loc) · 17.6 KB
/
Copy pathelfldr.c
File metadata and controls
828 lines (680 loc) · 17.6 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* Copyright (C) 2024 John Törnblom
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 3, 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; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */
#include <elf.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <sys/un.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <ps5/kernel.h>
#include "elfldr.h"
#include "log.h"
#include "pt.h"
#ifndef IPV6_2292PKTOPTIONS
#define IPV6_2292PKTOPTIONS 25
#endif
/**
* Convenient macros.
**/
#define ROUND_PG(x) (((x) + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1))
#define TRUNC_PG(x) ((x) & ~(PAGE_SIZE - 1))
#define PFLAGS(x) ((((x) & PF_R) ? PROT_READ : 0) | \
(((x) & PF_W) ? PROT_WRITE : 0) | \
(((x) & PF_X) ? PROT_EXEC : 0))
/**
* Context structure for the ELF loader.
**/
typedef struct elfldr_ctx {
uint8_t* elf;
pid_t pid;
intptr_t base_addr;
size_t base_size;
void* base_mirror;
} elfldr_ctx_t;
/**
* Absolute path to the SceSpZeroConf eboot.
**/
static const char* SceSpZeroConf = "/system/vsh/app/NPXS40112/eboot.bin";
/**
* Parse a R_X86_64_RELATIVE relocatable.
**/
static int
r_relative(elfldr_ctx_t *ctx, Elf64_Rela* rela) {
intptr_t* loc = ctx->base_mirror + rela->r_offset;
intptr_t val = ctx->base_addr + rela->r_addend;
*loc = val;
return 0;
}
/**
* Parse a PT_LOAD program header.
**/
static int
data_load(elfldr_ctx_t *ctx, Elf64_Phdr *phdr) {
void* data = ctx->base_mirror + phdr->p_vaddr;
if(!phdr->p_memsz) {
return 0;
}
if(!phdr->p_filesz) {
return 0;
}
memcpy(data, ctx->elf+phdr->p_offset, phdr->p_filesz);
return 0;
}
int
elfldr_sanity_check(uint8_t *elf, size_t elf_size) {
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)elf;
Elf64_Phdr *phdr;
if(elf_size < sizeof(Elf64_Ehdr) ||
elf_size < sizeof(Elf64_Phdr) + ehdr->e_phoff ||
elf_size < sizeof(Elf64_Shdr) + ehdr->e_shoff) {
return -1;
}
if(ehdr->e_ident[0] != 0x7f || ehdr->e_ident[1] != 'E' ||
ehdr->e_ident[2] != 'L' || ehdr->e_ident[3] != 'F') {
return -1;
}
phdr = (Elf64_Phdr*)(elf + ehdr->e_phoff);
for(int i=0; i<ehdr->e_phnum; i++) {
if(phdr[i].p_offset + phdr[i].p_filesz > elf_size) {
return -1;
}
}
return 0;
}
/**
* Load an ELF into the address space of a process with the given pid.
**/
static intptr_t
elfldr_load(pid_t pid, uint8_t *elf) {
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)elf;
Elf64_Phdr *phdr = (Elf64_Phdr*)(elf + ehdr->e_phoff);
Elf64_Shdr *shdr = (Elf64_Shdr*)(elf + ehdr->e_shoff);
elfldr_ctx_t ctx = {.elf = elf, .pid=pid};
size_t min_vaddr = -1;
size_t max_vaddr = 0;
int error = 0;
// Compute size of virtual memory region.
for(int i=0; i<ehdr->e_phnum; i++) {
if(phdr[i].p_vaddr < min_vaddr) {
min_vaddr = phdr[i].p_vaddr;
}
if(max_vaddr < phdr[i].p_vaddr + phdr[i].p_memsz) {
max_vaddr = phdr[i].p_vaddr + phdr[i].p_memsz;
}
}
min_vaddr = TRUNC_PG(min_vaddr);
max_vaddr = ROUND_PG(max_vaddr);
ctx.base_size = max_vaddr - min_vaddr;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
int prot = PROT_READ | PROT_WRITE;
if(ehdr->e_type == ET_DYN) {
ctx.base_addr = 0;
} else if(ehdr->e_type == ET_EXEC) {
ctx.base_addr = min_vaddr;
flags |= MAP_FIXED;
} else {
LOG_PUTS("elfldr_load: ELF type not supported");
return 0;
}
if(!(ctx.base_mirror=malloc(ctx.base_size))) {
LOG_PERROR("malloc");
return 0;
}
// Reserve an address space of sufficient size.
if((ctx.base_addr=pt_mmap(pid, ctx.base_addr, ctx.base_size, prot,
flags, -1, 0)) == -1) {
LOG_PT_PERROR(pid, "pt_mmap");
free(ctx.base_mirror);
return 0;
}
// Parse program headers.
for(int i=0; i<ehdr->e_phnum && !error; i++) {
switch(phdr[i].p_type) {
case PT_LOAD:
error = data_load(&ctx, &phdr[i]);
break;
}
}
// Apply relocations.
for(int i=0; i<ehdr->e_shnum && !error; i++) {
if(shdr[i].sh_type != SHT_RELA) {
continue;
}
Elf64_Rela* rela = (Elf64_Rela*)(elf + shdr[i].sh_offset);
for(int j=0; j<shdr[i].sh_size/sizeof(Elf64_Rela); j++) {
switch(rela[j].r_info & 0xffffffffl) {
case R_X86_64_RELATIVE:
error = r_relative(&ctx, &rela[j]);
break;
}
}
}
if(pt_copyin(ctx.pid, ctx.base_mirror, ctx.base_addr, ctx.base_size)) {
LOG_PERROR("pt_copyin");
error = 1;
}
// Set protection bits on mapped segments.
for(int i=0; i<ehdr->e_phnum && !error; i++) {
if(phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0) {
continue;
}
if(phdr[i].p_flags & PF_X) {
if(kernel_mprotect(pid, ctx.base_addr + phdr[i].p_vaddr,
ROUND_PG(phdr[i].p_memsz),
PFLAGS(phdr[i].p_flags))) {
LOG_PERROR("kernel_mprotect");
error = 1;
}
} else {
if(pt_mprotect(pid, ctx.base_addr + phdr[i].p_vaddr,
ROUND_PG(phdr[i].p_memsz),
PFLAGS(phdr[i].p_flags))) {
LOG_PT_PERROR(pid, "pt_mprotect");
error = 1;
}
}
}
if(pt_msync(pid, ctx.base_addr, ctx.base_size, MS_SYNC)) {
LOG_PT_PERROR(pid, "pt_msync");
error = 1;
}
free(ctx.base_mirror);
if(error) {
pt_munmap(pid, ctx.base_addr, ctx.base_size);
return 0;
}
return ctx.base_addr + ehdr->e_entry;
}
/**
* Create payload args in the address space of the process with the given pid.
**/
static intptr_t
elfldr_payload_args(pid_t pid) {
int victim_sock;
int master_sock;
intptr_t buf;
int pipe0;
int pipe1;
if((buf=pt_mmap(pid, 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == -1) {
LOG_PT_PERROR(pid, "pt_mmap");
return 0;
}
if((master_sock=pt_socket(pid, AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
LOG_PT_PERROR(pid, "pt_socket");
return 0;
}
pt_setint(pid, buf+0x00, 20);
pt_setint(pid, buf+0x04, IPPROTO_IPV6);
pt_setint(pid, buf+0x08, IPV6_TCLASS);
pt_setint(pid, buf+0x0c, 0);
pt_setint(pid, buf+0x10, 0);
pt_setint(pid, buf+0x14, 0);
if(pt_setsockopt(pid, master_sock, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, buf, 24)) {
LOG_PT_PERROR(pid, "pt_setsockopt");
return 0;
}
if((victim_sock=pt_socket(pid, AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
LOG_PT_PERROR(pid, "pt_socket");
return 0;
}
pt_setint(pid, buf+0x00, 0);
pt_setint(pid, buf+0x04, 0);
pt_setint(pid, buf+0x08, 0);
pt_setint(pid, buf+0x0c, 0);
pt_setint(pid, buf+0x10, 0);
if(pt_setsockopt(pid, victim_sock, IPPROTO_IPV6, IPV6_PKTINFO, buf, 20)) {
LOG_PT_PERROR(pid, "pt_setsockopt");
return 0;
}
if(kernel_overlap_sockets(pid, master_sock, victim_sock)) {
LOG_PUTS("kernel_overlap_sockets failed");
return 0;
}
if(pt_pipe(pid, buf)) {
LOG_PT_PERROR(pid, "pt_pipe");
return 0;
}
pipe0 = pt_getint(pid, buf);
pipe1 = pt_getint(pid, buf+4);
intptr_t args = buf;
intptr_t rwpipe = buf + 0x100;
intptr_t rwpair = buf + 0x200;
intptr_t kpipe_addr = kernel_get_proc_file(pid, pipe0);
intptr_t payloadout = buf + 0x300;
intptr_t getpid = pt_resolve(pid, "HoLVWNanBBc");
pt_setlong(pid, args + 0x00, getpid);
pt_setlong(pid, args + 0x08, rwpipe);
pt_setlong(pid, args + 0x10, rwpair);
pt_setlong(pid, args + 0x18, kpipe_addr);
pt_setlong(pid, args + 0x20, KERNEL_ADDRESS_DATA_BASE);
pt_setlong(pid, args + 0x28, payloadout);
pt_setint(pid, rwpipe + 0, pipe0);
pt_setint(pid, rwpipe + 4, pipe1);
pt_setint(pid, rwpair + 0, master_sock);
pt_setint(pid, rwpair + 4, victim_sock);
pt_setint(pid, payloadout, 0);
return args;
}
/**
* Prepare registers of a process for execution of an ELF.
**/
static int
elfldr_prepare_exec(pid_t pid, uint8_t *elf) {
intptr_t entry;
intptr_t args;
struct reg r;
if(pt_getregs(pid, &r)) {
LOG_PERROR("pt_getregs");
return -1;
}
if(!(entry=elfldr_load(pid, elf))) {
LOG_PUTS("elfldr_load failed");
return -1;
}
if(!(args=elfldr_payload_args(pid))) {
LOG_PUTS("elfldr_payload_args failed");
return -1;
}
pt_setlong(pid, r.r_rsp-8, r.r_rip);
r.r_rsp -= 8;
r.r_rip = entry;
r.r_rdi = args;
if(pt_setregs(pid, &r)) {
LOG_PERROR("pt_setregs");
return -1;
}
return 0;
}
/**
* Set the name of a process.
**/
static int
elfldr_set_procname(pid_t pid, const char* name) {
intptr_t buf;
if((buf=pt_mmap(pid, 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == -1) {
LOG_PT_PERROR(pid, "pt_mmap");
return -1;
}
pt_copyin(pid, name, buf, strlen(name)+1);
pt_syscall(pid, SYS_thr_set_name, -1, buf);
pt_msync(pid, buf, PAGE_SIZE, MS_SYNC);
pt_munmap(pid, buf, PAGE_SIZE);
return 0;
}
/**
* Escape jail and raise privileges.
**/
int
elfldr_raise_privileges(pid_t pid) {
static const uint8_t caps[16] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
intptr_t vnode;
if(!(vnode=kernel_get_root_vnode())) {
return -1;
}
if(kernel_set_proc_rootdir(pid, vnode)) {
return -1;
}
if(kernel_set_proc_jaildir(pid, 0)) {
return -1;
}
if(kernel_set_ucred_uid(pid, 0)) {
return -1;
}
if(kernel_set_ucred_caps(pid, caps)) {
return -1;
}
return 0;
}
/**
* Execute an ELF inside the process with the given pid.
**/
int
elfldr_exec(pid_t pid, int stdio, uint8_t* elf) {
uint8_t caps[16];
intptr_t jaildir;
intptr_t rootdir;
uint64_t authid;
int error = 0;
// backup privileges
jaildir = kernel_get_proc_jaildir(pid);
if(!(rootdir=kernel_get_proc_rootdir(pid))) {
LOG_PUTS("kernel_get_proc_rootdir failed");
pt_detach(pid, 0);
return -1;
}
if(kernel_get_ucred_caps(pid, caps)) {
LOG_PUTS("kernel_get_ucred_caps failed");
pt_detach(pid, 0);
return -1;
}
if(!(authid=kernel_get_ucred_authid(pid))) {
LOG_PUTS("kernel_get_ucred_authid failed");
pt_detach(pid, 0);
return -1;
}
if(elfldr_raise_privileges(pid)) {
LOG_PUTS("Unable to raise privileges");
pt_detach(pid, 0);
return -1;
}
if(stdio > 0) {
stdio = pt_rdup(pid, getpid(), stdio);
pt_close(pid, STDERR_FILENO);
pt_close(pid, STDOUT_FILENO);
pt_close(pid, STDIN_FILENO);
pt_dup2(pid, stdio, STDIN_FILENO);
pt_dup2(pid, stdio, STDOUT_FILENO);
pt_dup2(pid, stdio, STDERR_FILENO);
pt_close(pid, stdio);
}
if(elfldr_prepare_exec(pid, elf)) {
error = -1;
}
// restore privileges
if(kernel_set_proc_jaildir(pid, jaildir)) {
LOG_PUTS("kernel_set_proc_jaildir failed");
error = -1;
}
if(kernel_set_proc_rootdir(pid, rootdir)) {
LOG_PUTS("kernel_set_proc_rootdir failed");
error = -1;
}
if(kernel_set_ucred_caps(pid, caps)) {
LOG_PUTS("kernel_set_ucred_caps failed");
error = -1;
}
if(kernel_set_ucred_authid(pid, authid)) {
LOG_PUTS("kernel_set_ucred_authid failed");
error = -1;
}
if(pt_detach(pid, 0)) {
LOG_PERROR("pt_detach");
error = -1;
}
return error;
}
/**
* Set the heap size for libc.
**/
static int
elfldr_set_heap_size(pid_t pid, ssize_t size) {
intptr_t sceLibcHeapSize;
intptr_t sceLibcParam;
intptr_t sceProcParam;
intptr_t Need_sceLibc;
if(!(sceProcParam=pt_sceKernelGetProcParam(pid))) {
LOG_PT_PERROR(pid, "pt_sceKernelGetProcParam");
return -1;
}
if(pt_copyout(pid, sceProcParam+56, &sceLibcParam,
sizeof(sceLibcParam))) {
LOG_PERROR("pt_copyout");
return -1;
}
if(pt_copyout(pid, sceLibcParam+16, &sceLibcHeapSize,
sizeof(sceLibcHeapSize))) {
LOG_PERROR("pt_copyout");
return -1;
}
if(pt_setlong(pid, sceLibcHeapSize, size)) {
LOG_PERROR("pt_setlong");
return -1;
}
if(size != -1) {
return 0;
}
if(pt_copyout(pid, sceLibcParam+72, &Need_sceLibc,
sizeof(Need_sceLibc))) {
LOG_PERROR("pt_copyout");
return -1;
}
return pt_setlong(pid, sceLibcParam+32, Need_sceLibc);
}
static int
sys_budget_set(long budget) {
return __syscall(0x23b, budget);
}
static int
elfldr_rfork_entry(void* argv) {
if(sys_budget_set(0)) {
klog_perror("sys_budget_set");
return -1;
}
if(open("/dev/deci_stdin", O_RDONLY) < 0) {
klog_perror("open");
return -1;
}
if(open("/dev/deci_stdout", O_WRONLY) < 0) {
klog_perror("open");
return -1;
}
if(open("/dev/deci_stderr", O_WRONLY) < 0) {
klog_perror("open");
return -1;
}
if(ptrace(PT_TRACE_ME, 0, 0, 0)) {
klog_perror("ptrace");
return -1;
}
execve(SceSpZeroConf, argv, 0);
klog_perror("execve");
return -1;
}
/**
* Execute an ELF inside a new process.
**/
pid_t
elfldr_spawn(int stdio, char* const argv[], uint8_t* elf, size_t payload_size) {
uint8_t int3instr = 0xcc;
struct kevent evt;
intptr_t brkpoint;
uint8_t orginstr;
void *stack;
pid_t pid;
int kq;
if((kq=kqueue()) < 0) {
LOG_PERROR("kqueue");
return -1;
}
if(!(stack=malloc(PAGE_SIZE))) {
LOG_PERROR("malloc");
close(kq);
return -1;
}
if((pid=rfork_thread(RFPROC | RFCFDG | RFMEM, stack+PAGE_SIZE-8,
elfldr_rfork_entry, (void*)argv)) < 0) {
LOG_PERROR("rfork_thread");
free(stack);
close(kq);
return -1;
}
EV_SET(&evt, pid, EVFILT_PROC, EV_ADD, NOTE_EXEC, 0, 0);
if(kevent(kq, &evt, 1, &evt, 1, 0) < 0) {
LOG_PERROR("kevent");
free(stack);
close(kq);
return -1;
}
if(waitpid(pid, 0, 0) < 0) {
LOG_PERROR("waitpid");
free(stack);
close(kq);
return -1;
}
free(stack);
close(kq);
// The proc is now in the STOP state, with the instruction pointer pointing
// at the libkernel entry. Let the kernel assign process parameters accessed
// via sceKernelGetProcParam()
if(pt_syscall(pid, 599)) {
LOG_PT_PERROR(pid, "sys_dynlib_process_needed_and_relocate");
pt_detach(pid, SIGKILL);
return -1;
}
// Allow libc to allocate arbitrary amount of memory.
elfldr_set_heap_size(pid, -1);
//Insert a breakpoint at the eboot entry.
if(!(brkpoint=kernel_dynlib_entry_addr(pid, 0))) {
LOG_PUTS("kernel_dynlib_entry_addr failed");
pt_detach(pid, SIGKILL);
return -1;
}
brkpoint += 58;// offset to invocation of main()
if(kernel_mprotect(pid, brkpoint, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC)) {
LOG_PUTS("kernel_mprotect failed");
pt_detach(pid, SIGKILL);
return -1;
}
if(pt_copyout(pid, brkpoint, &orginstr, sizeof(orginstr))) {
LOG_PERROR("pt_copyout");
pt_detach(pid, SIGKILL);
return -1;
}
if(pt_copyin(pid, &int3instr, brkpoint, sizeof(int3instr))) {
LOG_PERROR("pt_copyin");
pt_detach(pid, SIGKILL);
return -1;
}
// Continue execution until we hit the breakpoint, then remove it.
if(pt_continue(pid, SIGCONT)) {
LOG_PERROR("pt_continue");
pt_detach(pid, SIGKILL);
return -1;
}
if(waitpid(pid, 0, 0) == -1) {
LOG_PERROR("waitpid");
pt_detach(pid, SIGKILL);
return -1;
}
if(pt_copyin(pid, &orginstr, brkpoint, sizeof(orginstr))) {
LOG_PERROR("pt_copyin");
pt_detach(pid, SIGKILL);
return -1;
}
// Execute the ELF
elfldr_set_procname(pid, argv[0]);
if(elfldr_exec(pid, stdio, elf)) {
kill(pid, SIGKILL);
return -1;
}
return pid;
}
/**
* Fint the pid of a process with the given name.
**/
pid_t
elfldr_find_pid(const char* name) {
int mib[4] = {1, 14, 8, 0};
pid_t mypid = getpid();
pid_t pid = -1;
size_t buf_size;
uint8_t *buf;
if(sysctl(mib, 4, 0, &buf_size, 0, 0)) {
LOG_PERROR("sysctl");
return -1;
}
if(!(buf=malloc(buf_size))) {
LOG_PERROR("malloc");
return -1;
}
if(sysctl(mib, 4, buf, &buf_size, 0, 0)) {
LOG_PERROR("sysctl");
free(buf);
return -1;
}
for(uint8_t *ptr=buf; ptr<(buf+buf_size);) {
int ki_structsize = *(int*)ptr;
pid_t ki_pid = *(pid_t*)&ptr[72];
char *ki_tdname = (char*)&ptr[447];
ptr += ki_structsize;
if(!strcmp(name, ki_tdname) && ki_pid != mypid) {
pid = ki_pid;
}
}
free(buf);
return pid;
}
/**
* Read an ELF from a given socket.
**/
int
elfldr_read(int fd, uint8_t** elf, size_t* elf_size) {
Elf64_Shdr *shdr;
Elf64_Ehdr ehdr;
uint8_t* buf;
uint8_t* bak;
size_t size;
off_t shend;
size_t rem;
if(recv(fd, &ehdr, sizeof(ehdr), MSG_WAITALL) != sizeof(ehdr)) {
return -1;
}
if(ehdr.e_ident[0] != 0x7f || ehdr.e_ident[1] != 'E' ||
ehdr.e_ident[2] != 'L' || ehdr.e_ident[3] != 'F') {
errno = ENOEXEC;
return -1;
}
size = ehdr.e_shoff + ehdr.e_shnum * sizeof(Elf64_Ehdr);
if(!(buf=malloc(size))) {
return -1;
}
memcpy(buf, &ehdr, sizeof(ehdr));
rem = size - sizeof(ehdr);
if(recv(fd, buf + sizeof(ehdr), rem, MSG_WAITALL) != rem) {
free(buf);
return -1;
}
shend = 0;
shdr = (Elf64_Shdr*)(buf + ehdr.e_shoff);
for(int i=0; i<ehdr.e_shnum; i++) {
if(shdr[i].sh_type == SHT_NOBITS) {
continue;
}
size_t end = shdr[i].sh_offset + shdr[i].sh_size;
if(end > shend) {
shend = end;
}
}
// sections appear before section headers
if(shend <= size) {
*elf = buf;
*elf_size = size;
return 0;
}
bak = buf;
if(!(buf=realloc(buf, shend))) {
free(bak);
return -1;
}
rem = shend - size;
if(recv(fd, buf + size, rem, MSG_WAITALL) != rem) {
free(buf);
return -1;
}
*elf = buf;
*elf_size = shend;
return 0;
}