From c1fde6826446ebc46e8a4f7aaf2af8ff4affce6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Koz=C5=82owski?= Date: Thu, 21 Dec 2023 12:13:42 +0100 Subject: [PATCH] Alignment cleanup for arch code and parasite args --- arch/arm/enter.c | 16 ++++++++-------- arch/arm/parasite-head.S | 7 ++++--- arch/arm64/enter.c | 3 +++ arch/arm64/parasite-head.S | 7 ++++--- arch/x86_64/parasite-head.S | 7 ++++--- memcr.h | 8 ++++++-- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/arch/arm/enter.c b/arch/arm/enter.c index 457be67..c55e67d 100644 --- a/arch/arm/enter.c +++ b/arch/arm/enter.c @@ -29,7 +29,7 @@ static void __attribute__((used)) container(void) /* rt_sigprocmask(), expects pointer to area for masks in r8 */ asm volatile( ".global sigprocmask_blob \n" - ".align 3 \n" + ".align 2 \n" "sigprocmask_blob: \n" "mov r7, #175 \n" /* __NR_rt_sigprocmask */ "mov r0, %0 \n" /* @how */ @@ -39,7 +39,7 @@ static void __attribute__((used)) container(void) "svc 0x0 \n" "udf #16 \n" /* SIGTRAP */ ".global sigprocmask_blob_size \n" - ".align 3 \n" + ".align 2 \n" "sigprocmask_blob_size: \n" ".int sigprocmask_blob_size - sigprocmask_blob \n" :: "i" (SIG_SETMASK) @@ -48,7 +48,7 @@ static void __attribute__((used)) container(void) /* mmaps anon area for parasite_blob */ asm volatile( ".global mmap_blob \n" - ".align 3 \n" + ".align 2 \n" "mmap_blob: \n" "mov r7, #192 \n" /* __NR_mmap2 */ "mov r0, #0 \n" /* @addr */ @@ -60,7 +60,7 @@ static void __attribute__((used)) container(void) "svc 0x0 \n" "udf #16 \n" /* SIGTRAP */ ".global mmap_blob_size \n" - ".align 3 \n" + ".align 2 \n" "mmap_blob_size: \n" ".int mmap_blob_size - mmap_blob \n" :: "i" (PROT_EXEC | PROT_READ | PROT_WRITE), @@ -70,7 +70,7 @@ static void __attribute__((used)) container(void) /* clones parasite, expects parasite address in r8 */ asm volatile( ".global clone_blob \n" - ".align 3 \n" + ".align 2 \n" "clone_blob: \n" "mov r7, #120 \n" /* __NR_clone */ "ldr r0, CLONE_FLAGS \n" @@ -84,7 +84,7 @@ static void __attribute__((used)) container(void) "CLONE_FLAGS: \n" ".word 0x80050f00 \n" /* (CLONE_FILES | CLONE_FS | CLONE_IO | CLONE_SIGHAND | CLONE_SYSVSEM | CLONE_THREAD | CLONE_VM) */ ".global clone_blob_size \n" - ".align 3 \n" + ".align 2 \n" "clone_blob_size: \n" ".int clone_blob_size - clone_blob \n" ); @@ -92,7 +92,7 @@ static void __attribute__((used)) container(void) /* munmap anon area for parasite_blob, expects addr in r8 and len in r9 */ asm volatile( ".global munmap_blob \n" - ".align 3 \n" + ".align 2 \n" "munmap_blob: \n" "mov r7, #91 \n" /* __NR_munmap */ "mov r0, r8 \n" /* @addr */ @@ -100,7 +100,7 @@ static void __attribute__((used)) container(void) "svc 0x0 \n" "udf #16 \n" /* SIGTRAP */ ".global munmap_blob_size \n" - ".align 3 \n" + ".align 2 \n" "munmap_blob_size: \n" ".int munmap_blob_size - munmap_blob \n" ); diff --git a/arch/arm/parasite-head.S b/arch/arm/parasite-head.S index 7357f94..ed18589 100644 --- a/arch/arm/parasite-head.S +++ b/arch/arm/parasite-head.S @@ -20,19 +20,20 @@ #define PARASITE_ARG_SIZE 4096 .section .head.text, "ax" - .globl __parasite_head_start - .align 4, 0x00 +.globl __parasite_head_start +.align 2 __parasite_head_start: adr sp, __parasite_stack adr r0, __parasite_args bl service udf #16 @ SIGTRAP - .align 4, 0x00 +.align 2 __parasite_args: .space PARASITE_ARG_SIZE, 0 .space 56, 0 /* 8+4 fill */ +.align 2 /* sp must be 4-byte aligned */ __parasite_stack: .space PARASITE_STACK_SIZE, 0 diff --git a/arch/arm64/enter.c b/arch/arm64/enter.c index 272a767..94fa595 100644 --- a/arch/arm64/enter.c +++ b/arch/arm64/enter.c @@ -20,6 +20,9 @@ #include #include +/* + * blobs are 2^3 aligned (rather than 2^2) for ptrace() peek/poke + */ static void __attribute__((used)) container(void) { /* diff --git a/arch/arm64/parasite-head.S b/arch/arm64/parasite-head.S index e831871..f199f67 100644 --- a/arch/arm64/parasite-head.S +++ b/arch/arm64/parasite-head.S @@ -20,19 +20,20 @@ #define PARASITE_ARG_SIZE 4096 .section .head.text, "ax" - .globl __parasite_head_start - .align 4, 0x00 +.globl __parasite_head_start +.align 2 __parasite_head_start: adr x0, __parasite_stack mov sp, x0 adr x0, __parasite_args bl service brk #0 /* SIGTRAP */ - .align 4, 0x00 +.align 2 __parasite_args: .space PARASITE_ARG_SIZE, 0 +.align 4 /* sp must be 16-byte aligned */ __parasite_stack: .space PARASITE_STACK_SIZE, 0 diff --git a/arch/x86_64/parasite-head.S b/arch/x86_64/parasite-head.S index 347051c..230da8d 100644 --- a/arch/x86_64/parasite-head.S +++ b/arch/x86_64/parasite-head.S @@ -20,18 +20,19 @@ #define PARASITE_ARG_SIZE 4096 .section .head.text, "ax" - .global __parasite_head_start; - .align 8, 0x90; /* nop */ +.global __parasite_head_start; +.align 8 __parasite_head_start: leaq __parasite_stack(%rip), %rsp leaq __parasite_args(%rip), %rdi call service int $0x03 /* SIGTRAP */ - .align 8, 0x90 +.align 8 __parasite_args: .space PARASITE_ARG_SIZE, 0 +.align 16 /* sp must be 16-byte aligned */ __parasite_stack: .space PARASITE_STACK_SIZE, 0 diff --git a/memcr.h b/memcr.h index 94d94cb..05dce41 100644 --- a/memcr.h +++ b/memcr.h @@ -20,15 +20,19 @@ #define __MEMCR_H__ #include +#include #ifndef PAGE_SIZE #define PAGE_SIZE 4096 #endif -/* size is CPU word aligned for ptrace() peek/poke */ struct parasite_args { struct sockaddr_un addr; -} __attribute__((aligned(sizeof(unsigned long)))); + char padding[2]; +}; + +/* size must be CPU word aligned for ptrace() peek/poke */ +static_assert(sizeof(struct parasite_args) % sizeof(unsigned long) == 0, "invalid size"); typedef enum { CMD_MPROTECT = 1,