Skip to content

Instantly share code, notes, and snippets.

@shlevy
Last active February 21, 2018 19:16
Show Gist options
  • Select an option

  • Save shlevy/e58da5a0e58c3c8b66a7ce24bcdb96e6 to your computer and use it in GitHub Desktop.

Select an option

Save shlevy/e58da5a0e58c3c8b66a7ce24bcdb96e6 to your computer and use it in GitHub Desktop.
qemu-user binfmt risc-v

Compile qemu-binfmt-wrapper.c with QEMU_RISCV_BINDIR set to the bin directory qemu-riscv is installed in, and name the binary /run/binfmt/riscv64. Then riscv.conf in /etc/binfmt.d on a systemd system will have qemu-user interpret all riscv64 binaries, including respecting setuid bit. On non-systemd you'll need to add the binfmt registration some other way.

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/auxv.h>
int main(int argc,char ** argv) {
int fd = (int) getauxval(AT_EXECFD);
size_t sz = snprintf(NULL, 0, "/proc/self/fd/%d", fd) + 1;
char fn[sz];
snprintf(fn, sz, "/proc/self/fd/%d", fd);
char * realArgv[argc + 2];
realArgv[0] = "qemu-riscv64";
realArgv[1] = "-0";
realArgv[2] = argv[2];
realArgv[3] = fn;
memcpy(realArgv + 4, argv + 3, (argc - 3) * sizeof argv[0]);
realArgv[argc + 1] = NULL;
execv(QEMU_RISCV_BINDIR "/qemu-riscv64", realArgv);
perror("Running qemu-riscv64");
return -1;
}
:riscv64:M::\x7f\x45\x4c\x46\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf3\x00:\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff:/run/binfmt/riscv64:PC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment