Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add support for arbitrary syscalls
Thanks for the idea, sleirsgoevy
  • Loading branch information
john-tornblom committed Jun 6, 2023
1 parent 0b13c7b commit e8f9b3b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crt/Makefile
Expand Up @@ -24,7 +24,7 @@ CFLAGS += -mcmodel=large -fPIC -fno-plt -masm=intel

all: crt1.o

crt1.o: crt.o kernel.o sprx.o
crt1.o: crt.o kernel.o sprx.o syscall.o
$(LD) -r -o $@ $^

%.o: %.c
Expand Down
43 changes: 43 additions & 0 deletions crt/syscall.c
@@ -0,0 +1,43 @@
/* Copyright (C) 2023 John Törnblom
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */

#include "payload.h"


static __attribute__ ((used)) long ptr_syscall;


asm(".intel_syntax noprefix\n"
".global syscall\n"
".type syscall @function\n"
"syscall:\n"
" mov rax, rdi\n" // sysno
" mov rdi, rsi\n" // arg1
" mov rsi, rdx\n" // arg2
" mov rdx, rcx\n" // arg3
" mov r10, r8\n" // arg4
" mov r8, r9\n" // arg5
" mov r9, qword ptr [rsp + 8]\n" // arg6
" jmp qword ptr [rip + ptr_syscall]\n" // syscall
" ret\n"
);


__attribute__((constructor(101))) static void
constructor(const payload_args_t *args) {
*args->payloadout = args->sceKernelDlsym(0x2001, "getpid", &ptr_syscall);
ptr_syscall += 0xa; // jump directly to syscall instruction
}
4 changes: 4 additions & 0 deletions samples/arbitrary_syscall/.gitignore
@@ -0,0 +1,4 @@
*.elf
*.o
*~

45 changes: 45 additions & 0 deletions samples/arbitrary_syscall/Makefile
@@ -0,0 +1,45 @@
# Copyright (C) 2023 John Törnblom
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not see
# <http://www.gnu.org/licenses/>.

ifndef PS5_PAYLOAD_SDK
$(error PS5_PAYLOAD_SDK is undefined)
endif

PS5_HOST ?= ps5
PS5_PORT ?= 9020

ELF := arbitrary_syscall.elf

CC := $(PS5_PAYLOAD_SDK)/host/x86_64-ps5-payload-cc
LD := $(PS5_PAYLOAD_SDK)/host/x86_64-ps5-payload-ld

CFLAGS := -O2
LDADD := -lSceLibcInternal

all: $(ELF)

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

$(ELF): main.o
$(LD) $^ $(LDADD) -o $@

clean:
rm -f *.o $(ELF)

test: $(ELF)
nc -q0 $(PS5_HOST) $(PS5_PORT) < $^

26 changes: 26 additions & 0 deletions samples/arbitrary_syscall/main.c
@@ -0,0 +1,26 @@
/* Copyright (C) 2023 John Törnblom
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */

#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>


int main() {
printf("%d\n", (int)syscall(SYS_getuid));

return 0;
}

0 comments on commit e8f9b3b

Please sign in to comment.