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│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
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-04-12 12:20:17 +00:00
|
|
|
#include "libc/bits/asmflag.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-03-19 10:37:00 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/nt/memory.h"
|
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2022-04-07 07:15:35 +00:00
|
|
|
#include "libc/runtime/directmap.internal.h"
|
2022-03-19 10:37:00 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/nr.h"
|
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
/**
|
|
|
|
* Modifies restrictions on virtual memory address range.
|
|
|
|
*
|
|
|
|
* @param prot can have PROT_{NONE,READ,WRITE,EXEC,GROWSDOWN}
|
|
|
|
* @return 0 on success, or -1 w/ errno
|
|
|
|
* @see mmap()
|
|
|
|
*/
|
2022-03-19 10:37:00 +00:00
|
|
|
noasan noubsan privileged int mprotect(void *addr, size_t len, int prot) {
|
2020-06-15 14:18:57 +00:00
|
|
|
bool cf;
|
|
|
|
int64_t rc;
|
|
|
|
uint32_t oldprot;
|
2022-03-19 10:37:00 +00:00
|
|
|
char protbuf[4];
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!IsWindows()) {
|
2021-04-18 18:34:59 +00:00
|
|
|
asm volatile(CFLAG_ASM("clc\n\tsyscall")
|
2020-07-01 02:55:47 +00:00
|
|
|
: CFLAG_CONSTRAINT(cf), "=a"(rc)
|
2020-06-15 14:18:57 +00:00
|
|
|
: "1"(__NR_mprotect), "D"(addr), "S"(len), "d"(prot)
|
|
|
|
: "rcx", "r11", "memory", "cc");
|
|
|
|
if (cf) {
|
2021-04-18 18:34:59 +00:00
|
|
|
errno = rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
rc = -1;
|
|
|
|
} else if (rc > -4096ul) {
|
|
|
|
errno = -rc;
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-20 15:01:14 +00:00
|
|
|
if (VirtualProtect(addr, len, __prot2nt(prot, 0), &oldprot)) {
|
2022-03-19 10:37:00 +00:00
|
|
|
rc = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-03-19 10:37:00 +00:00
|
|
|
rc = __winerr();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-19 10:37:00 +00:00
|
|
|
STRACE("mprotect(%p, %'zu, %s[%#x]) → %d% m", addr, len,
|
|
|
|
DescribeProt(prot, protbuf), prot, rc);
|
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|