Skip to content

Commit

Permalink
compel: Add test to check parasite stack setup
Browse files Browse the repository at this point in the history
Some ABIs allow functions to store data in caller frame, which
means that we have to allocate an initial stack frame before
executing code on the parasite stack.

This test saves the contents of writable memory that follows the stack
after the victim has been infected but before we start using the
parasite stack. It later checks that the saved data matches the
current contents of the two memory areas. This is done while the
victim is halted so we expect a match unless executing parasite code
caused memory corruption. The test doesn't detect cases where we
corrupted memory by writing the same value.

Signed-off-by: Younes Manton <ymanton@ca.ibm.com>
  • Loading branch information
ymanton authored and avagin committed Oct 2, 2022
1 parent 50dda15 commit a7cbdcb
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compel/test/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: fdspy infect rsys
all: fdspy infect rsys stack

fdspy:
$(Q) $(MAKE) -C fdspy
Expand All @@ -10,8 +10,12 @@ infect:
$(Q) $(MAKE) -C infect run
.PHONY: infect


rsys:
$(Q) $(MAKE) -C rsys
$(Q) $(MAKE) -C rsys run
.PHONY: rsys

stack:
$(Q) $(MAKE) -C stack
$(Q) $(MAKE) -C stack run
.PHONY: stack
4 changes: 4 additions & 0 deletions compel/test/stack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parasite.h
parasite.po
spy
victim
32 changes: 32 additions & 0 deletions compel/test/stack/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CC := gcc
CFLAGS ?= -O2 -g -Wall -Werror

COMPEL := ../../../compel/compel-host

all: victim spy

run:
./spy
.PHONY: run

clean:
rm -f victim
rm -f spy
rm -f parasite.h
rm -f parasite.po
rm -f parasite.o

victim: victim.c
$(CC) $(CFLAGS) -o $@ $^

spy: spy.c parasite.h
$(CC) $(CFLAGS) $(shell $(COMPEL) includes) -o $@ $< $(shell $(COMPEL) --static libs)

parasite.h: parasite.po
$(COMPEL) hgen -o $@ -f $<

parasite.po: parasite.o
ld $(shell $(COMPEL) ldflags) -o $@ $^ $(shell $(COMPEL) plugins)

parasite.o: parasite.c
$(CC) $(CFLAGS) -c $(shell $(COMPEL) cflags) -o $@ $^
38 changes: 38 additions & 0 deletions compel/test/stack/parasite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <errno.h>

#include <compel/plugins/std.h>
#include <infect-rpc.h>

/*
* Stubs for std compel plugin.
*/
int parasite_trap_cmd(int cmd, void *args)
{
return 0;
}
void parasite_cleanup(void)
{
}

#define PARASITE_CMD_INC PARASITE_USER_CMDS
#define PARASITE_CMD_DEC PARASITE_USER_CMDS + 1

int parasite_daemon_cmd(int cmd, void *args)
{
int v;

switch (cmd) {
case PARASITE_CMD_INC:
v = (*(int *)args) + 1;
break;
case PARASITE_CMD_DEC:
v = (*(int *)args) - 1;
break;
default:
v = -1;
break;
}

sys_write(1, &v, sizeof(int));
return 0;
}
Loading

0 comments on commit a7cbdcb

Please sign in to comment.