Skip to content

Commit

Permalink
Packaging: support >4K code length without LZMA
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongkeunahn committed Dec 26, 2024
1 parent 02f8ab1 commit 08e1ecf
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion scripts/static-pie-gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
# Since we append a little-endian 8-byte nonnegative integer, we can practically ensure that the last byte is zero.
code_raw = memory_bin[:-8]
code_raw += (len(code_raw) + 8 - loader_fdict['entrypoint_offset']).to_bytes(8, byteorder='little')
code_raw_len = len(code_raw)
code_raw_b91 = base91.encode(code_raw, use_rle=True).decode('ascii')
code_raw_b91_len = len(code_raw_b91)
code_raw_b91 = '"' + code_raw_b91 + '"'
Expand Down Expand Up @@ -121,7 +122,7 @@

# template
template_candidates = [template_path]
if lang_name in ["C", "Rust"] and "x86_64" in target_name and "short" in template_path and len(code_raw) <= 4096 - 256:
if lang_name in ["C", "Rust"] and "x86_64" in target_name and "short" in template_path:
template_candidates.append(template_path.replace("short", "shorter"))

# exports
Expand Down Expand Up @@ -149,6 +150,7 @@
"$$$$binary_raw_base91$$$$": code_raw_b91,
"$$$$binary_raw_base91_len$$$$": str(code_raw_b91_len),
"$$$$binary_base91_chunked$$$$": r,
"$$$$code_raw_len$$$$": str(code_raw_len),
"$$$$min_len_4096$$$$": str(min(len(code_b91)+1, 4096)),
"$$$$entrypoint_offset$$$$": str(loader_fdict['entrypoint_offset']),
"$$$$exports_cpp$$$$": exports_cpp
Expand Down
2 changes: 1 addition & 1 deletion scripts/templates/static-pie-prestub-amd64-print.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
prestub = bytearray(prestub)
if len(prestub) > 0 and prestub[-1] == 0:
prestub = prestub[:-1]
asciz = True
asciz = '--no-asciz' not in sys.argv
else:
asciz = False

Expand Down
8 changes: 5 additions & 3 deletions scripts/templates/static-pie-prestub-amd64-shorter-c.asm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
; (prestub: the code that runs before the stub and sets the stage)
;
; build: nasm -f bin -O9 static-pie-prestub-amd64-shorter-c.asm -o static-pie-prestub-amd64-shorter-c.bin
; note: after building with the above command, run static-pie-prestub-amd64-print.py static-pie-prestub-amd64-shorter-c.bin --c
; note: after building with the above command, run static-pie-prestub-amd64-print.py static-pie-prestub-amd64-shorter-c.bin --c --no-asciz
; to obtain the form that can be embedded in C.

BITS 64
Expand All @@ -29,6 +29,7 @@ section .text
lea rdi, [rsp + 8]
push rdi ; _start of relocated stub
mov ecx, _end - _start
add ecx, 8 ; binary size in bytes
rep movsb

; Jump to stack
Expand All @@ -51,7 +52,7 @@ _svc_alloc_rwx:
cdq ; rdx=0
xor r9d, r9d ; offset
xor edi, edi ; rdi=0
mov esi, eax ; size (anything in [1, 4096])
mov rsi, qword [rel _end] ; size in bytes
mov dl, 7 ; protect (safe since we have ensured rdx=0)
push 0x22
pop r10 ; flags
Expand Down Expand Up @@ -97,4 +98,5 @@ _jump_to_entrypoint:
push rcx
call rdi

_end:
align 8, db 0x0 ; zero padding
_end:
Binary file modified scripts/templates/static-pie-prestub-amd64-shorter-c.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion scripts/templates/static-pie-template-amd64-shorter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SOLUTION BEGIN
$$$$solution_src$$$$
// SOLUTION END
__attribute__((section(".text#")))unsigned long long s[]={0xab880e48348,0x485700001000be00,0x485a076a08247c8d,0x50ffffff000e781,0x4800000010358d48,0x60b95708247c8d,0x5fd0ff58a4f30000,0xbefffff000e78148,0xbb800001000,0x459958096a050f00,0x7b2c689ff31c931,0x5841ff6a5a41226a,0xc11fb097485e050f,0x76232cc931ac0de0,0x6b242cac91c8fe16,0x8e8c1aac8015bc0,0x86e0ebf77510c4f6,0x2b48dc74aaf3ff4f,0xd7ff51c931f87f};char t[]=$$$$binary_raw_base91$$$$;int main(){return 0;}
__attribute__((section(".text#")))unsigned long long s[]={0xab880e48348,0x485700001000be00,0x485a076a08247c8d,0x50ffffff000e781,0x4800000013358d48,0x66b95708247c8d,0x58a4f308c1830000,0xf000e781485fd0ff,0xb800001000beffff,0x96a050f0000000b,0x48ff31c931459958,0x7b200000042358b,0x5841ff6a5a41226a,0xc11fb097485e050f,0x76232cc931ac0de0,0x6b242cac91c8fe16,0x8e8c1aac8015bc0,0x86e0ebf77510c4f6,0x2b48dc74aaf3ff4f,0xd7ff51c931f87f,$$$$code_raw_len$$$$};char t[]=$$$$binary_raw_base91$$$$;int main(){return 0;}
#if defined(__cplusplus)
extern "C"
#endif
Expand Down

0 comments on commit 08e1ecf

Please sign in to comment.