Skip to content

Commit

Permalink
Alignment cleanup for arch code and parasite args
Browse files Browse the repository at this point in the history
  • Loading branch information
mkozlowski authored and Mariusz Kozłowski committed Dec 21, 2023
1 parent 4f288ab commit c1fde68
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
16 changes: 8 additions & 8 deletions arch/arm/enter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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)
Expand All @@ -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 */
Expand All @@ -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),
Expand All @@ -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"
Expand All @@ -84,23 +84,23 @@ 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"
);

/* 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 */
"mov r1, r9 \n" /* @len */
"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"
);
Expand Down
7 changes: 4 additions & 3 deletions arch/arm/parasite-head.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions arch/arm64/enter.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <sys/mman.h>
#include <linux/sched.h>

/*
* blobs are 2^3 aligned (rather than 2^2) for ptrace() peek/poke
*/
static void __attribute__((used)) container(void)
{
/*
Expand Down
7 changes: 4 additions & 3 deletions arch/arm64/parasite-head.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions arch/x86_64/parasite-head.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions memcr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
#define __MEMCR_H__

#include <stdint.h>
#include <assert.h>

#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,
Expand Down

0 comments on commit c1fde68

Please sign in to comment.