-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrewrite.c
More file actions
4588 lines (4085 loc) · 148 KB
/
rewrite.c
File metadata and controls
4588 lines (4085 loc) · 148 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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: MIT */
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "kbox/elf.h"
#include "io-util.h"
#include "kbox/x86-decode.h"
#include "procmem.h"
#include "rewrite.h"
#include "syscall-nr.h"
#include "syscall-trap.h"
#define EM_X86_64 62
#define EM_AARCH64 183
#define AARCH64_B_OPCODE 0x14000000u
#define AARCH64_B_IMM26_MASK 0x03ffffffu
#define AARCH64_B_RANGE ((int64_t) 128 * 1024 * 1024)
#define AARCH64_LDR_LITERAL_OPCODE 0x58000000u
#define AARCH64_BR_OPCODE 0xd61f0000u
#define AARCH64_NOP_OPCODE 0xd503201fu
#define AARCH64_REWRITE_SLOT_SIZE 32u
#define X86_64_REWRITE_PAGE_ZERO_SIZE (64u * 1024u)
#define X86_64_REWRITE_WRAPPER_SLOT_SIZE 32u
#define X86_64_MOVABS_R11_OPCODE_LEN 10u
#define X86_64_JMP_R11_OPCODE_LEN 3u
#define X86_64_PAGE_ZERO_TAIL_LEN \
(X86_64_MOVABS_R11_OPCODE_LEN + X86_64_JMP_R11_OPCODE_LEN)
#define X86_64_JMP_REL32_OPCODE_LEN 5u
#define X86_64_WRAPPER_SITE_LEN 8u
#define X86_64_TRAMPOLINE_SEARCH_STEP (64u * 1024u)
#define X86_64_TRAMPOLINE_SEARCH_LIMIT ((uint64_t) INT32_MAX - 4096u)
#define AARCH64_VENEER_SIZE 16u /* LDR x16, +8; BR x16; .quad target */
#define AARCH64_VENEER_SEARCH_STEP (64u * 1024u)
#define AARCH64_VENEER_SEARCH_LIMIT ((uint64_t) 127 * 1024 * 1024)
#define KBOX_REWRITE_SCAN_CHUNK (64u * 1024u)
#ifndef MAP_FIXED_NOREPLACE
#define MAP_FIXED_NOREPLACE 0x100000
#endif
struct analyze_ctx {
enum kbox_rewrite_arch arch;
kbox_rewrite_site_cb cb;
kbox_rewrite_planned_site_cb planned_cb;
void *opaque;
size_t candidates;
size_t segments;
};
struct runtime_planned_site {
struct kbox_rewrite_planned_site planned;
uint64_t actual_site_addr;
uint64_t actual_trampoline_addr;
enum kbox_loader_mapping_source source;
enum kbox_rewrite_wrapper_candidate_kind wrapper_kind;
uint64_t wrapper_nr;
};
struct runtime_site_array {
struct runtime_planned_site *sites;
size_t count;
size_t cap;
};
struct runtime_collect_ctx {
struct runtime_site_array *array;
uint64_t load_bias;
enum kbox_loader_mapping_source source;
};
static struct kbox_rewrite_runtime *active_rewrite_runtime;
static inline struct kbox_rewrite_runtime *load_active_rewrite_runtime(void)
{
return __atomic_load_n(&active_rewrite_runtime, __ATOMIC_ACQUIRE);
}
static inline void store_active_rewrite_runtime(
struct kbox_rewrite_runtime *runtime)
{
__atomic_store_n(&active_rewrite_runtime, runtime, __ATOMIC_RELEASE);
}
struct kbox_rewrite_runtime *kbox_rewrite_runtime_active(void)
{
return load_active_rewrite_runtime();
}
static void write_le32(unsigned char out[4], uint32_t value);
static int rewrite_is_wrapper_site(const struct kbox_rewrite_origin_map *map,
uint64_t origin_addr);
static uint32_t wrapper_family_mask_for_nr(const struct kbox_host_nrs *host_nrs,
uint64_t nr);
static int planned_site_matches_wrapper_candidate(
const struct kbox_rewrite_planned_site *planned,
const struct kbox_rewrite_wrapper_candidate *candidate);
#if defined(__aarch64__)
extern char kbox_syscall_rewrite_aarch64_entry[];
extern char kbox_syscall_rewrite_aarch64_cancel_entry[];
extern int64_t kbox_syscall_rewrite_aarch64_dispatch(uint64_t origin_addr,
uint64_t nr,
uint64_t a0,
uint64_t a1,
uint64_t a2,
uint64_t a3,
uint64_t a4,
uint64_t a5);
__asm__(
".text\n"
".globl kbox_syscall_rewrite_aarch64_entry\n"
".type kbox_syscall_rewrite_aarch64_entry,%function\n"
"kbox_syscall_rewrite_aarch64_entry:\n"
"sub sp, sp, #144\n"
"stp x1, x2, [sp, #0]\n"
"stp x3, x4, [sp, #16]\n"
"stp x5, x6, [sp, #32]\n"
"stp x7, x8, [sp, #48]\n"
"stp x9, x10, [sp, #64]\n"
"stp x11, x12, [sp, #80]\n"
"stp x13, x14, [sp, #96]\n"
"stp x15, x18, [sp, #112]\n"
"stp x19, x30, [sp, #128]\n"
"mov x19, x17\n"
"mov x2, x0\n"
"ldr x3, [sp, #0]\n"
"ldr x4, [sp, #8]\n"
"ldr x5, [sp, #16]\n"
"ldr x6, [sp, #24]\n"
"ldr x7, [sp, #32]\n"
"ldr x1, [sp, #56]\n"
"mov x0, x19\n"
"bl kbox_syscall_rewrite_aarch64_dispatch\n"
"add x16, x19, #4\n"
"ldp x1, x2, [sp, #0]\n"
"ldp x3, x4, [sp, #16]\n"
"ldp x5, x6, [sp, #32]\n"
"ldp x7, x8, [sp, #48]\n"
"ldp x9, x10, [sp, #64]\n"
"ldp x11, x12, [sp, #80]\n"
"ldp x13, x14, [sp, #96]\n"
"ldp x15, x18, [sp, #112]\n"
"ldp x19, x30, [sp, #128]\n"
"add sp, sp, #144\n"
"br x16\n"
".size kbox_syscall_rewrite_aarch64_entry, "
".-kbox_syscall_rewrite_aarch64_entry\n");
__asm__(
".text\n"
".globl kbox_syscall_rewrite_aarch64_cancel_entry\n"
".type kbox_syscall_rewrite_aarch64_cancel_entry,%function\n"
"kbox_syscall_rewrite_aarch64_cancel_entry:\n"
"sub sp, sp, #144\n"
"stp x1, x2, [sp, #0]\n"
"stp x3, x4, [sp, #16]\n"
"stp x5, x6, [sp, #32]\n"
"stp x7, x8, [sp, #48]\n"
"stp x9, x10, [sp, #64]\n"
"stp x11, x12, [sp, #80]\n"
"stp x13, x14, [sp, #96]\n"
"stp x15, x18, [sp, #112]\n"
"stp x19, x30, [sp, #128]\n"
"mov x19, x17\n"
"mov x2, x0\n"
"ldr x3, [sp, #0]\n"
"ldr x4, [sp, #8]\n"
"ldr x5, [sp, #16]\n"
"ldr x6, [sp, #24]\n"
"ldr x7, [sp, #32]\n"
"ldr x1, [sp, #40]\n"
"mov x0, x19\n"
"bl kbox_syscall_rewrite_aarch64_dispatch\n"
"add x16, x19, #4\n"
"ldp x1, x2, [sp, #0]\n"
"ldp x3, x4, [sp, #16]\n"
"ldp x5, x6, [sp, #32]\n"
"ldp x7, x8, [sp, #48]\n"
"ldp x9, x10, [sp, #64]\n"
"ldp x11, x12, [sp, #80]\n"
"ldp x13, x14, [sp, #96]\n"
"ldp x15, x18, [sp, #112]\n"
"ldp x19, x30, [sp, #128]\n"
"add sp, sp, #144\n"
"br x16\n"
".size kbox_syscall_rewrite_aarch64_cancel_entry, "
".-kbox_syscall_rewrite_aarch64_cancel_entry\n");
#endif
#if defined(__x86_64__)
extern char kbox_syscall_rewrite_x86_64_entry[];
extern char kbox_syscall_rewrite_x86_64_wrapper_entry[];
extern int64_t kbox_syscall_rewrite_x86_64_dispatch(uint64_t origin_addr,
uint64_t nr,
const uint64_t *args);
__asm__(
".text\n"
".globl kbox_syscall_rewrite_x86_64_entry\n"
".type kbox_syscall_rewrite_x86_64_entry,@function\n"
"kbox_syscall_rewrite_x86_64_entry:\n"
"mov (%rsp), %r11\n"
"sub $56, %rsp\n"
"mov %rdi, 8(%rsp)\n"
"mov %rsi, 16(%rsp)\n"
"mov %rdx, 24(%rsp)\n"
"mov %r10, 32(%rsp)\n"
"mov %r8, 40(%rsp)\n"
"mov %r9, 48(%rsp)\n"
"mov %r11, %rdi\n"
"mov %rax, %rsi\n"
"lea 8(%rsp), %rdx\n"
"call kbox_syscall_rewrite_x86_64_dispatch\n"
"mov 8(%rsp), %rdi\n"
"mov 16(%rsp), %rsi\n"
"mov 24(%rsp), %rdx\n"
"mov 32(%rsp), %r10\n"
"mov 40(%rsp), %r8\n"
"mov 48(%rsp), %r9\n"
"add $56, %rsp\n"
"ret\n"
".size kbox_syscall_rewrite_x86_64_entry, "
".-kbox_syscall_rewrite_x86_64_entry\n");
__asm__(
".text\n"
".globl kbox_syscall_rewrite_x86_64_wrapper_entry\n"
".type kbox_syscall_rewrite_x86_64_wrapper_entry,@function\n"
"kbox_syscall_rewrite_x86_64_wrapper_entry:\n"
"sub $56, %rsp\n"
"mov %rdi, 8(%rsp)\n"
"mov %rsi, 16(%rsp)\n"
"mov %rdx, 24(%rsp)\n"
"mov %r10, 32(%rsp)\n"
"mov %r8, 40(%rsp)\n"
"mov %r9, 48(%rsp)\n"
"mov %r11, %rdi\n"
"mov %rax, %rsi\n"
"lea 8(%rsp), %rdx\n"
"call kbox_syscall_rewrite_x86_64_dispatch\n"
"mov 8(%rsp), %rdi\n"
"mov 16(%rsp), %rsi\n"
"mov 24(%rsp), %rdx\n"
"mov 32(%rsp), %r10\n"
"mov 40(%rsp), %r8\n"
"mov 48(%rsp), %r9\n"
"add $56, %rsp\n"
"ret\n"
".size kbox_syscall_rewrite_x86_64_wrapper_entry, "
".-kbox_syscall_rewrite_x86_64_wrapper_entry\n");
#endif
static int x86_64_is_wrapper_site(const unsigned char *segment_bytes,
size_t file_size,
size_t offset)
{
if (!segment_bytes || offset + X86_64_WRAPPER_SITE_LEN > file_size)
return 0;
return segment_bytes[offset] == 0xb8 && segment_bytes[offset + 5] == 0x0f &&
(segment_bytes[offset + 6] == 0x05 ||
segment_bytes[offset + 6] == 0x34) &&
segment_bytes[offset + 7] == 0xc3;
}
static uint32_t x86_64_wrapper_syscall_nr(const unsigned char original[8])
{
return (uint32_t) original[1] | ((uint32_t) original[2] << 8) |
((uint32_t) original[3] << 16) | ((uint32_t) original[4] << 24);
}
static uint32_t aarch64_wrapper_syscall_nr(const struct kbox_rewrite_site *site)
{
uint32_t insn;
if (!site || site->width != 4)
return UINT32_MAX;
insn = (uint32_t) site->original[0] | ((uint32_t) site->original[1] << 8) |
((uint32_t) site->original[2] << 16) |
((uint32_t) site->original[3] << 24);
/* movz x8, #imm16 or movz w8, #imm16 */
if ((insn & 0xffe0001fu) == 0xd2800008u ||
(insn & 0xffe0001fu) == 0x52800008u) {
return (insn >> 5) & 0xffffu;
}
return UINT32_MAX;
}
static int aarch64_movz_reg_imm16(uint32_t insn,
unsigned reg,
uint32_t *imm_out)
{
if (!imm_out || reg > 31)
return -1;
if ((insn & 0xffe0001fu) == (0xd2800000u | reg) ||
(insn & 0xffe0001fu) == (0x52800000u | reg)) {
*imm_out = (insn >> 5) & 0xffffu;
return 0;
}
return -1;
}
/* Decode an aarch64 BL instruction and return the signed byte offset
* from the BL itself to its target. BL encodes a 26-bit signed word
* displacement.
*/
static int aarch64_bl_target_offset(uint32_t insn, int64_t *offset_out)
{
int32_t imm26;
if (!offset_out)
return -1;
if ((insn & 0xfc000000u) != 0x94000000u)
return -1;
{
uint32_t raw = insn & 0x03ffffffu;
if (raw & (1u << 25))
raw |= 0xfc000000u;
imm26 = (int32_t) raw;
}
*offset_out = ((int64_t) imm26) * 4;
return 0;
}
/* Match `mov Xd, Xm` (reg-to-reg move), encoded as
* `orr Xd, xzr, Xm, lsl #0`: 0xaa0003e0 | (m<<16) | d, with bits 15:10
* (shift amount) zero and bits 9:5 (Rn) = xzr (31).
*/
static int aarch64_is_mov_reg_reg(uint32_t insn)
{
return (insn & 0xffe0ffe0u) == 0xaa0003e0u;
}
/* Tight signature for musl __syscall_cancel_arch: an `svc #0`
* immediately preceded by at least 4 consecutive `mov Xd, Xm`
* register-to-register moves (the arg-shuffle the function performs
* to move kernel syscall arguments into place). Returns 1 if the svc
* at @svc_off matches this signature. This is the discriminator
* between a real musl cancel-wrapper call chain and an arbitrary
* function that happens to issue a syscall.
*/
static int aarch64_svc_is_cancel_arch(const unsigned char *segment_bytes,
size_t segment_size,
size_t svc_off)
{
const size_t required_moves = 4;
size_t matched = 0;
size_t off;
if (!segment_bytes || svc_off + 4 > segment_size)
return 0;
if (svc_off < required_moves * 4)
return 0;
off = svc_off - 4;
while (matched < required_moves) {
uint32_t insn = (uint32_t) segment_bytes[off + 0] |
((uint32_t) segment_bytes[off + 1] << 8) |
((uint32_t) segment_bytes[off + 2] << 16) |
((uint32_t) segment_bytes[off + 3] << 24);
if (!aarch64_is_mov_reg_reg(insn))
return 0;
matched++;
if (off < 4)
break;
off -= 4;
}
return matched >= required_moves;
}
/* Walk the BL chain from @start_off looking for a `svc #0` whose
* immediate context matches the __syscall_cancel_arch arg-shuffle
* signature. Bounded in both window size (per level) and depth. Stops
* scanning at the first `ret` in each function to avoid crossing
* function boundaries.
*/
static int aarch64_scan_reaches_cancel_svc(const unsigned char *segment_bytes,
size_t segment_size,
size_t start_off,
size_t max_insns,
int depth_remaining)
{
size_t end = start_off + (max_insns * 4);
int saw_bl_candidate = 0;
size_t bl_candidate_target = 0;
if (!segment_bytes || start_off + 4 > segment_size)
return 0;
if (end > segment_size)
end = segment_size;
for (size_t off = start_off; off + 4 <= end; off += 4) {
uint32_t insn = (uint32_t) segment_bytes[off] |
((uint32_t) segment_bytes[off + 1] << 8) |
((uint32_t) segment_bytes[off + 2] << 16) |
((uint32_t) segment_bytes[off + 3] << 24);
if (insn == 0xd4000001u) {
if (aarch64_svc_is_cancel_arch(segment_bytes, segment_size, off))
return 1;
/* svc in this function is not cancel_arch's: keep
* walking. The svc is rare enough that this is cheap.
*/
continue;
}
if (insn == 0xd65f03c0u) /* ret terminates this function */
break;
/* First BL encountered. Record the target and recurse once
* this function is exhausted.
*/
if (!saw_bl_candidate && (insn & 0xfc000000u) == 0x94000000u) {
int64_t delta;
int64_t tgt;
if (aarch64_bl_target_offset(insn, &delta) == 0) {
tgt = (int64_t) off + delta;
if (tgt >= 0 && (uint64_t) tgt + 4 <= (uint64_t) segment_size) {
bl_candidate_target = (size_t) tgt;
saw_bl_candidate = 1;
}
}
}
}
if (saw_bl_candidate && depth_remaining > 0) {
return aarch64_scan_reaches_cancel_svc(segment_bytes, segment_size,
bl_candidate_target, max_insns,
depth_remaining - 1);
}
return 0;
}
static int aarch64_target_is_syscall_cancel(const unsigned char *segment_bytes,
size_t segment_size,
size_t target_off)
{
/* Depth-2 BL walk. Musl static __syscall_cancel is:
* __syscall_cancel -> __internal_syscall_cancel -> __syscall_cancel_arch
* so two BL hops are enough to reach the arch-specific svc. The
* per-level window is 128 instructions (512 bytes), which covers
* realistic musl function bodies without wandering into
* unrelated code. The signature match at the svc (4+ consecutive
* reg-to-reg moves) distinguishes the arch function from ordinary
* syscall wrappers.
*/
return aarch64_scan_reaches_cancel_svc(segment_bytes, segment_size,
target_off, 128, 2);
}
/* Translate a BL at @bl_off inside @segment_bytes to its target offset
* within the same segment. Returns 0 on success with *target_off_out
* set, -1 on malformed BL or cross-segment targets.
*/
static int aarch64_bl_target_in_segment(const unsigned char *segment_bytes,
size_t segment_size,
size_t bl_off,
size_t *target_off_out)
{
uint32_t insn;
int64_t delta;
int64_t target;
if (!segment_bytes || !target_off_out || bl_off + 4 > segment_size)
return -1;
insn = (uint32_t) segment_bytes[bl_off] |
((uint32_t) segment_bytes[bl_off + 1] << 8) |
((uint32_t) segment_bytes[bl_off + 2] << 16) |
((uint32_t) segment_bytes[bl_off + 3] << 24);
if (aarch64_bl_target_offset(insn, &delta) < 0)
return -1;
target = (int64_t) bl_off + delta;
if (target < 0 || (uint64_t) target + 4 > (uint64_t) segment_size)
return -1;
*target_off_out = (size_t) target;
return 0;
}
/* Combined heuristic + target validator. Returns 1 if the BL at
* segment offset @bl_off is a promote-eligible cancel-wrapper call,
* 0 otherwise.
*/
static int aarch64_bl_is_cancel_wrapper(const unsigned char *segment_bytes,
size_t segment_size,
size_t bl_off)
{
size_t target_off;
if (aarch64_bl_target_in_segment(segment_bytes, segment_size, bl_off,
&target_off) < 0)
return 0;
return aarch64_target_is_syscall_cancel(segment_bytes, segment_size,
target_off);
}
static int encode_x86_64_virtual_procinfo_patch(
const struct kbox_rewrite_site *site,
struct kbox_rewrite_patch *patch)
{
uint32_t nr;
uint32_t value;
if (!site || !patch)
return -1;
if (site->width != X86_64_WRAPPER_SITE_LEN || site->original[0] != 0xb8 ||
site->original[5] != 0x0f ||
(site->original[6] != 0x05 && site->original[6] != 0x34) ||
site->original[7] != 0xc3) {
return -1;
}
nr = x86_64_wrapper_syscall_nr(site->original);
if (nr == (uint32_t) HOST_NRS_X86_64.getpid ||
nr == (uint32_t) HOST_NRS_X86_64.gettid) {
value = 1;
} else if (nr == (uint32_t) HOST_NRS_X86_64.getppid) {
value = 0;
} else {
return -1;
}
patch->width = X86_64_WRAPPER_SITE_LEN;
patch->bytes[0] = 0xb8;
patch->bytes[1] = (unsigned char) (value & 0xff);
patch->bytes[2] = (unsigned char) ((value >> 8) & 0xff);
patch->bytes[3] = (unsigned char) ((value >> 16) & 0xff);
patch->bytes[4] = (unsigned char) ((value >> 24) & 0xff);
patch->bytes[5] = 0xc3;
patch->bytes[6] = 0x90;
patch->bytes[7] = 0x90;
return 0;
}
static int aarch64_prev_insn_syscall_nr(const unsigned char *image,
size_t image_len,
size_t site_off,
uint32_t *nr_out)
{
uint32_t insn;
if (!image || !nr_out || site_off < 4 || site_off + 4 > image_len)
return -1;
insn = (uint32_t) image[site_off - 4] |
((uint32_t) image[site_off - 3] << 8) |
((uint32_t) image[site_off - 2] << 16) |
((uint32_t) image[site_off - 1] << 24);
/* movz x8, #imm16 */
if ((insn & 0xffe0001fu) == 0xd2800008u) {
*nr_out = (insn >> 5) & 0xffffu;
return 0;
}
/* movz w8, #imm16 */
if ((insn & 0xffe0001fu) == 0x52800008u) {
*nr_out = (insn >> 5) & 0xffffu;
return 0;
}
return -1;
}
static int encode_aarch64_virtual_procinfo_patch(
const struct kbox_rewrite_site *site,
const unsigned char *image,
size_t image_len,
struct kbox_rewrite_patch *patch)
{
uint32_t nr;
uint32_t value;
uint32_t next_insn;
size_t site_off;
uint32_t movz_x0;
if (!site || !image || !patch || site->width != 4)
return -1;
if (site->original[0] != 0x01 || site->original[1] != 0x00 ||
site->original[2] != 0x00 || site->original[3] != 0xd4) {
return -1;
}
site_off = (size_t) site->file_offset;
if (site_off + 8 > image_len)
return -1;
next_insn = (uint32_t) image[site_off + 4] |
((uint32_t) image[site_off + 5] << 8) |
((uint32_t) image[site_off + 6] << 16) |
((uint32_t) image[site_off + 7] << 24);
if (next_insn != 0xd65f03c0u)
return -1;
if (aarch64_prev_insn_syscall_nr(image, image_len, site_off, &nr) < 0)
return -1;
if (nr == (uint32_t) HOST_NRS_GENERIC.getpid ||
nr == (uint32_t) HOST_NRS_GENERIC.gettid) {
value = 1;
} else if (nr == (uint32_t) HOST_NRS_GENERIC.getppid) {
value = 0;
} else {
return -1;
}
movz_x0 = 0xd2800000u | ((value & 0xffffu) << 5);
patch->width = 4;
write_le32(patch->bytes, movz_x0);
return 0;
}
static int encode_virtual_procinfo_patch(const struct kbox_rewrite_site *site,
const unsigned char *image,
size_t image_len,
enum kbox_rewrite_arch arch,
struct kbox_rewrite_patch *patch)
{
switch (arch) {
case KBOX_REWRITE_ARCH_X86_64:
return encode_x86_64_virtual_procinfo_patch(site, patch);
case KBOX_REWRITE_ARCH_AARCH64:
return encode_aarch64_virtual_procinfo_patch(site, image, image_len,
patch);
default:
return -1;
}
}
static int rewrite_read_fd_all(int fd, unsigned char **buf_out, size_t *len_out)
{
struct stat st;
unsigned char *buf;
size_t len;
size_t total = 0;
if (fd < 0 || !buf_out || !len_out)
return -1;
if (fstat(fd, &st) < 0 || st.st_size < 0)
return -1;
len = (size_t) st.st_size;
if (len == 0)
return -1;
buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (buf == MAP_FAILED)
return -1;
while (total < len) {
ssize_t nr = pread(fd, buf + total, len - total, (off_t) total);
if (nr < 0) {
if (errno == EINTR)
continue;
munmap(buf, len);
return -1;
}
if (nr == 0) {
munmap(buf, len);
return -1;
}
total += (size_t) nr;
}
*buf_out = buf;
*len_out = len;
return 0;
}
static uint64_t align_up_u64_or_zero(uint64_t value, uint64_t align)
{
uint64_t mask;
if (align == 0 || (align & (align - 1)) != 0)
return 0;
mask = align - 1;
if (value > UINT64_MAX - mask)
return 0;
return (value + mask) & ~mask;
}
static int origin_map_is_sealed(const struct kbox_rewrite_origin_map *map)
{
return map && map->sealed;
}
static int rewrite_origin_addr(const struct kbox_rewrite_site *site,
uint64_t *origin_out)
{
uint64_t origin;
if (!site || !origin_out)
return -1;
if (site->width == 2 && site->original[0] == 0x0f &&
(site->original[1] == 0x05 || site->original[1] == 0x34)) {
if (__builtin_add_overflow(site->vaddr, 2u, &origin))
return -1;
*origin_out = origin;
return 0;
}
if (site->width == X86_64_WRAPPER_SITE_LEN && site->original[0] == 0xb8 &&
site->original[5] == 0x0f &&
(site->original[6] == 0x05 || site->original[6] == 0x34) &&
site->original[7] == 0xc3) {
*origin_out = site->vaddr;
return 0;
}
if (site->width == 4 && site->original[0] == 0x01 &&
site->original[1] == 0x00 && site->original[2] == 0x00 &&
site->original[3] == 0xd4) {
*origin_out = site->vaddr;
return 0;
}
if (site->width == 4) {
uint32_t insn = (uint32_t) site->original[0] |
((uint32_t) site->original[1] << 8) |
((uint32_t) site->original[2] << 16) |
((uint32_t) site->original[3] << 24);
if ((insn & 0xfc000000u) == 0x14000000u ||
(insn & 0xfc000000u) == 0x94000000u) {
*origin_out = site->vaddr;
return 0;
}
}
errno = EINVAL;
return -1;
}
/* Site classification for caller-aware rewrite dispatch.
*
* x86-64 wrapper pattern: the 8-byte sequence "mov $NR, %eax; syscall; ret"
* (B8 xx xx xx xx 0F 05 C3) is always WRAPPER: the function's only purpose
* is to execute the syscall and return the result. This is the musl __syscall0
* pattern and covers the vast majority of libc syscall sites.
*
* For 2-byte syscall sites (bare 0F 05), we look at the byte immediately
* after the syscall: if it is C3 (ret) or 0F 1F (NOP), the site is inside a
* leaf wrapper. Otherwise (conditional jump, further computation, another
* syscall) the site is COMPLEX.
*
* aarch64 wrapper pattern: SVC #0 followed by RET (D65F03C0) within the next
* 2 instructions (8 bytes). If the ret is immediate or separated only by a
* MOV (return value adjustment), it's WRAPPER.
*/
enum kbox_rewrite_site_class kbox_rewrite_classify_x86_64_site(
const unsigned char *segment_bytes,
size_t segment_size,
size_t site_offset,
unsigned char site_width)
{
if (!segment_bytes)
return KBOX_REWRITE_SITE_UNKNOWN;
/* 8-byte wrapper: mov $NR, %eax; syscall; ret → always WRAPPER. */
if (site_width == X86_64_WRAPPER_SITE_LEN) {
if (site_offset + X86_64_WRAPPER_SITE_LEN <= segment_size &&
segment_bytes[site_offset] == 0xb8 &&
segment_bytes[site_offset + 5] == 0x0f &&
(segment_bytes[site_offset + 6] == 0x05 ||
segment_bytes[site_offset + 6] == 0x34) &&
segment_bytes[site_offset + 7] == 0xc3) {
return KBOX_REWRITE_SITE_WRAPPER;
}
return KBOX_REWRITE_SITE_UNKNOWN;
}
/* 2-byte syscall (0F 05): check what follows. */
if (site_width == 2) {
size_t after = site_offset + 2;
if (after >= segment_size)
return KBOX_REWRITE_SITE_UNKNOWN;
/* Immediate ret after syscall → wrapper. */
if (segment_bytes[after] == 0xc3)
return KBOX_REWRITE_SITE_WRAPPER;
/* "cmp $0xfffffffffffff001, %rax; jae <error>" is the musl
* __syscall_ret pattern: syscall; cmp; jae; ret. Still a wrapper
* (the error path does not feed the result into another syscall).
* Pattern: 48 3d 01 f0 ff ff (6 bytes for CMP rax, -4095)
*/
if (after + 6 <= segment_size && segment_bytes[after] == 0x48 &&
segment_bytes[after + 1] == 0x3d &&
segment_bytes[after + 2] == 0x01 &&
segment_bytes[after + 3] == 0xf0 &&
segment_bytes[after + 4] == 0xff &&
segment_bytes[after + 5] == 0xff) {
return KBOX_REWRITE_SITE_WRAPPER;
}
/* NOP after syscall (alignment padding) then check next. */
if (segment_bytes[after] == 0x90) {
if (after + 1 < segment_size && segment_bytes[after + 1] == 0xc3)
return KBOX_REWRITE_SITE_WRAPPER;
}
/* Anything else: inside a complex function. */
return KBOX_REWRITE_SITE_COMPLEX;
}
return KBOX_REWRITE_SITE_UNKNOWN;
}
enum kbox_rewrite_site_class kbox_rewrite_classify_aarch64_site(
const unsigned char *segment_bytes,
size_t segment_size,
size_t site_offset)
{
uint32_t next_insn;
if (!segment_bytes)
return KBOX_REWRITE_SITE_UNKNOWN;
/* SVC #0 is 4 bytes. Check the instruction(s) that follow. */
if (site_offset + 8 > segment_size)
return KBOX_REWRITE_SITE_UNKNOWN;
/* Read the next instruction (little-endian). */
next_insn = (uint32_t) segment_bytes[site_offset + 4] |
((uint32_t) segment_bytes[site_offset + 5] << 8) |
((uint32_t) segment_bytes[site_offset + 6] << 16) |
((uint32_t) segment_bytes[site_offset + 7] << 24);
/* RET (D65F03C0) immediately after SVC → wrapper. */
if (next_insn == 0xd65f03c0u)
return KBOX_REWRITE_SITE_WRAPPER;
/* CMN x0, #0xFFF (musl __syscall_ret error check):
* Encoding: B1 00 3C 1F (CMN x0, #0xf, LSL #12) or
* 31 00 10 01 (CMN x0, #4, common variant)
* If followed by B.HI/B.CS → wrapper pattern.
* Be conservative: check for a common NEG/MOV + RET within 2 insns.
*/
if (site_offset + 12 <= segment_size) {
uint32_t insn2 = (uint32_t) segment_bytes[site_offset + 8] |
((uint32_t) segment_bytes[site_offset + 9] << 8) |
((uint32_t) segment_bytes[site_offset + 10] << 16) |
((uint32_t) segment_bytes[site_offset + 11] << 24);
/* NEG x0, x0 (CB0003E0) followed by RET: error path wrapper. */
if (next_insn == 0xCB0003E0u && insn2 == 0xd65f03c0u)
return KBOX_REWRITE_SITE_WRAPPER;
/* Second instruction is RET: wrapper (with one intermediate insn). */
if (insn2 == 0xd65f03c0u)
return KBOX_REWRITE_SITE_WRAPPER;
/* CMN x0/w0, #imm followed by conditional branch: error-check
* wrapper.
* CMN x0 encoding: 0xB100xxxx where bits [21:10] are imm12.
* CMN w0 encoding: 0x3100xxxx with the same immediate layout.
* Then B.cond: 0x54xxxxxx.
*/
if (((next_insn & 0xFF000000u) == 0xB1000000u ||
(next_insn & 0xFF000000u) == 0x31000000u) &&
(insn2 & 0xFF000000u) == 0x54000000u) {
return KBOX_REWRITE_SITE_WRAPPER;
}
if (site_offset + 20 <= segment_size) {
uint32_t insn3 =
(uint32_t) segment_bytes[site_offset + 12] |
((uint32_t) segment_bytes[site_offset + 13] << 8) |
((uint32_t) segment_bytes[site_offset + 14] << 16) |
((uint32_t) segment_bytes[site_offset + 15] << 24);
uint32_t insn4 =
(uint32_t) segment_bytes[site_offset + 16] |
((uint32_t) segment_bytes[site_offset + 17] << 8) |
((uint32_t) segment_bytes[site_offset + 18] << 16) |
((uint32_t) segment_bytes[site_offset + 19] << 24);
/* musl __internal_syscall_cancel epilogue:
* svc #0
* ldr x19, [sp, #imm]
* ldp x29, x30, [sp], #imm
* autiasp
* ret
*/
if (next_insn == 0xF9400BF3u && insn2 == 0xA8C27BFDu &&
insn3 == 0xD50323BFu && insn4 == 0xD65F03C0u) {
return KBOX_REWRITE_SITE_WRAPPER;
}
}
}
/* Anything else: complex function. */
return KBOX_REWRITE_SITE_COMPLEX;
}
/* Look up site classification from the origin map.
*/
int kbox_rewrite_origin_map_find_class(
const struct kbox_rewrite_origin_map *map,
uint64_t origin_addr,
enum kbox_rewrite_site_class *out)
{
struct kbox_rewrite_origin_entry entry;
if (!map || !out)
return -1;
if (!kbox_rewrite_origin_map_find(map, origin_addr, &entry))
return -1;
*out = entry.site_class;
return 0;
}
/* Try the rewrite fast path for a WRAPPER-site process-info syscall.
*
* WRAPPER sites can safely return virtualized PID values directly without
* going through the full dispatch machinery. The result goes directly to
* the caller and is not consumed internally by signal/thread helpers.
*
* Returns 1 if fast-pathed (with result written to *out), 0 if the
* syscall must go through full dispatch.
*
* kbox virtualizes: getpid=1, gettid=1, getppid=0. These must match
* the values in kbox_dispatch_request() to maintain PID model consistency.
*/
int kbox_rewrite_is_site_fast_eligible(
const struct kbox_rewrite_origin_map *map,
uint64_t origin_addr,
const struct kbox_host_nrs *host_nrs,
uint64_t nr)
{
if (!map || !host_nrs)
return 0;
/* Only zero-argument process-info syscalls are eligible. */
if ((int) nr != host_nrs->getpid && (int) nr != host_nrs->getppid &&
(int) nr != host_nrs->gettid) {
return 0;
}
return rewrite_is_wrapper_site(map, origin_addr);
}
/* Return the virtualized value for a fast-path-eligible process-info syscall.
* Must match the values produced by kbox_dispatch_request() for the same
* syscall numbers (getpid=1, gettid=1, getppid=0).
*/
static int64_t rewrite_fast_procinfo_value(const struct kbox_host_nrs *host_nrs,
uint64_t nr)
{
if ((int) nr == host_nrs->getpid || (int) nr == host_nrs->gettid)
return 1;
if ((int) nr == host_nrs->getppid)
return 0;
return -ENOSYS; /* Should not be reached; caller checked eligibility. */
}
int kbox_rewrite_init_trampoline_layout(
enum kbox_rewrite_arch arch,
const struct kbox_elf_exec_segment *seg,
struct kbox_rewrite_trampoline_layout *layout)
{
uint64_t seg_end;
if (!seg || !layout)
return -1;
memset(layout, 0, sizeof(*layout));
layout->arch = arch;
switch (arch) {
case KBOX_REWRITE_ARCH_X86_64:
if (__builtin_add_overflow(seg->vaddr, seg->mem_size, &seg_end))
return -1;
layout->base_addr = align_up_u64_or_zero(seg_end, 16);
if (layout->base_addr == 0 && seg_end != 0)
return -1;
layout->slot_size = X86_64_REWRITE_WRAPPER_SLOT_SIZE;
return 0;
case KBOX_REWRITE_ARCH_AARCH64:
if (__builtin_add_overflow(seg->vaddr, seg->mem_size, &seg_end))
return -1;
layout->base_addr = align_up_u64_or_zero(seg_end, 16);
if (layout->base_addr == 0 && seg_end != 0)
return -1;
layout->slot_size = AARCH64_REWRITE_SLOT_SIZE;
return 0;
default: