2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2022-04-20 16:56:53 +00:00
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-05-16 20:20:08 +00:00
|
|
|
#define ShouldUseMsabiAttribute() 1
|
2022-10-14 16:52:35 +00:00
|
|
|
#include "ape/sections.internal.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
2022-09-09 11:07:08 +00:00
|
|
|
#include "libc/intrin/asmflag.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-10-05 13:37:15 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/nt/enum/pageflags.h"
|
|
|
|
#include "libc/nt/memory.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2022-07-20 04:18:33 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/sysv/consts/nr.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-16 20:20:08 +00:00
|
|
|
__msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect;
|
|
|
|
|
2023-05-02 02:43:59 +00:00
|
|
|
static inline int __morph_rt_sigprocmask(int h, const sigset_t *s, sigset_t *o,
|
|
|
|
size_t c) {
|
|
|
|
#ifdef __aarch64__
|
|
|
|
register long r0 asm("x0") = (long)h;
|
|
|
|
register long r1 asm("x1") = (long)s;
|
|
|
|
register long r2 asm("x2") = (long)o;
|
|
|
|
register long r3 asm("x3") = (long)c;
|
2023-05-13 05:42:57 +00:00
|
|
|
register long r8 asm("x8") = (long)__NR_sigprocmask;
|
2023-05-02 02:43:59 +00:00
|
|
|
register long res_x0 asm("x0");
|
2023-05-13 05:42:57 +00:00
|
|
|
asm volatile("svc\t0"
|
2023-05-02 02:43:59 +00:00
|
|
|
: "=r"(res_x0)
|
2023-05-13 05:42:57 +00:00
|
|
|
: "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r8)
|
|
|
|
: "memory");
|
2023-05-02 02:43:59 +00:00
|
|
|
return res_x0;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int __morph_sigprocmask(int how, const sigset_t *set,
|
|
|
|
sigset_t *oldset) {
|
|
|
|
return __morph_rt_sigprocmask(how, set, oldset, sizeof(*set));
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:20:08 +00:00
|
|
|
static privileged void __morph_mprotect(void *addr, size_t size, int prot,
|
|
|
|
int ntprot) {
|
2023-05-02 02:43:59 +00:00
|
|
|
#ifdef __x86_64__
|
2022-07-20 04:18:33 +00:00
|
|
|
bool cf;
|
2022-05-16 20:20:08 +00:00
|
|
|
int ax, dx;
|
|
|
|
uint32_t op;
|
|
|
|
if (!IsWindows()) {
|
2022-07-20 04:18:33 +00:00
|
|
|
asm volatile(CFLAG_ASM("clc\n\t"
|
|
|
|
"syscall")
|
|
|
|
: CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx)
|
|
|
|
: "1"(__NR_mprotect), "D"(addr), "S"(size), "2"(prot)
|
2022-09-09 11:07:08 +00:00
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
2022-07-20 04:18:33 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (cf) ax = -ax;
|
|
|
|
if (ax == -EPERM) {
|
|
|
|
kprintf("error: need pledge(prot_exec) permission to code morph\n");
|
|
|
|
_Exit(26);
|
|
|
|
}
|
|
|
|
#endif
|
2022-10-17 18:02:04 +00:00
|
|
|
_npassert(!ax);
|
2022-04-20 16:56:53 +00:00
|
|
|
} else {
|
2022-05-16 20:20:08 +00:00
|
|
|
__imp_VirtualProtect(addr, size, ntprot, &op);
|
2021-02-08 12:04:42 +00:00
|
|
|
}
|
2023-05-02 02:43:59 +00:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
register long r0 asm("x0") = (long)addr;
|
|
|
|
register long r1 asm("x1") = (long)size;
|
|
|
|
register long r2 asm("x2") = (long)prot;
|
2023-05-13 05:42:57 +00:00
|
|
|
register long r8 asm("x8") = (long)__NR_mprotect;
|
2023-05-02 02:43:59 +00:00
|
|
|
register long res_x0 asm("x0");
|
2023-05-13 05:42:57 +00:00
|
|
|
asm volatile("svc\t0"
|
2023-05-02 02:43:59 +00:00
|
|
|
: "=r"(res_x0)
|
2023-05-13 05:42:57 +00:00
|
|
|
: "r"(r0), "r"(r1), "r"(r2), "r"(r8)
|
|
|
|
: "memory");
|
2023-05-02 02:43:59 +00:00
|
|
|
_npassert(!res_x0);
|
|
|
|
#endif
|
2022-05-16 20:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-22 03:53:30 +00:00
|
|
|
* Begins code morphing executable.
|
2022-05-16 20:20:08 +00:00
|
|
|
*
|
|
|
|
* @return 0 on success, or -1 w/ errno
|
|
|
|
*/
|
2022-10-17 18:02:04 +00:00
|
|
|
privileged void __morph_begin(sigset_t *save) {
|
2022-07-19 05:26:11 +00:00
|
|
|
int ax;
|
|
|
|
bool cf;
|
|
|
|
intptr_t dx;
|
2022-06-13 02:33:42 +00:00
|
|
|
sigset_t ss = {{-1, -1}};
|
2022-07-10 15:27:50 +00:00
|
|
|
STRACE("__morph_begin()");
|
2023-05-02 02:43:59 +00:00
|
|
|
#ifdef __x86_64__
|
2022-10-17 18:02:04 +00:00
|
|
|
if (IsOpenbsd()) {
|
|
|
|
asm volatile(CFLAG_ASM("syscall")
|
|
|
|
: CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx)
|
|
|
|
: "1"(__NR_sigprocmask), "D"(SIG_BLOCK), "S"(-1u)
|
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
|
|
|
save->__bits[0] = ax & 0xffffffff;
|
|
|
|
_npassert(!cf);
|
|
|
|
} else if (!IsWindows() && !IsMetal()) {
|
|
|
|
asm volatile("mov\t$8,%%r10d\n\t"
|
|
|
|
"syscall"
|
|
|
|
: "=a"(ax), "=d"(dx)
|
|
|
|
: "0"(__NR_sigprocmask), "D"(SIG_BLOCK), "S"(&ss), "1"(save)
|
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
|
|
|
|
_npassert(!ax);
|
2022-05-16 20:20:08 +00:00
|
|
|
}
|
2023-05-02 02:43:59 +00:00
|
|
|
#else
|
|
|
|
__morph_sigprocmask(SIG_BLOCK, &ss, save);
|
|
|
|
#endif
|
2022-05-16 20:20:08 +00:00
|
|
|
__morph_mprotect(_base, __privileged_addr - _base, PROT_READ | PROT_WRITE,
|
|
|
|
kNtPageWritecopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-22 03:53:30 +00:00
|
|
|
* Begins code morphing executable.
|
2022-05-16 20:20:08 +00:00
|
|
|
*/
|
2022-10-17 18:02:04 +00:00
|
|
|
privileged void __morph_end(sigset_t *save) {
|
2022-07-19 05:26:11 +00:00
|
|
|
int ax;
|
|
|
|
long dx;
|
|
|
|
bool cf;
|
2022-05-16 20:20:08 +00:00
|
|
|
__morph_mprotect(_base, __privileged_addr - _base, PROT_READ | PROT_EXEC,
|
|
|
|
kNtPageExecuteRead);
|
2023-05-02 02:43:59 +00:00
|
|
|
#ifdef __x86_64__
|
2022-10-17 18:02:04 +00:00
|
|
|
if (IsOpenbsd()) {
|
|
|
|
asm volatile(CFLAG_ASM("syscall")
|
|
|
|
: CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx)
|
|
|
|
: "1"(__NR_sigprocmask), "D"(SIG_SETMASK), "S"(save->__bits[0])
|
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
|
|
|
_npassert(!cf);
|
|
|
|
} else if (!IsWindows() && !IsMetal()) {
|
|
|
|
asm volatile("mov\t$8,%%r10d\n\t"
|
|
|
|
"syscall"
|
|
|
|
: "=a"(ax), "=d"(dx)
|
|
|
|
: "0"(__NR_sigprocmask), "D"(SIG_SETMASK), "S"(save), "1"(0)
|
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
|
|
|
|
_npassert(!ax);
|
2022-04-20 16:56:53 +00:00
|
|
|
}
|
2023-05-02 02:43:59 +00:00
|
|
|
#else
|
|
|
|
__morph_sigprocmask(SIG_SETMASK, save, 0);
|
|
|
|
#endif
|
2022-07-10 15:27:50 +00:00
|
|
|
STRACE("__morph_end()");
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|