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-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/errno.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-08-20 19:32:51 +00:00
|
|
|
#include "libc/intrin/likely.h"
|
2022-10-03 20:43:52 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-04-24 16:59:22 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
/**
|
|
|
|
* Modifies restrictions on virtual memory address range.
|
|
|
|
*
|
2022-04-13 05:11:00 +00:00
|
|
|
* @param addr needs to be 4kb aligned
|
|
|
|
* @param prot can have PROT_{NONE,READ,WRITE,EXEC,GROWSDOWN,GROWSUP}
|
2020-08-25 11:23:25 +00:00
|
|
|
* @return 0 on success, or -1 w/ errno
|
|
|
|
* @see mmap()
|
|
|
|
*/
|
2022-05-16 20:20:08 +00:00
|
|
|
int mprotect(void *addr, size_t size, int prot) {
|
2020-06-15 14:18:57 +00:00
|
|
|
int64_t rc;
|
2023-06-06 16:12:30 +00:00
|
|
|
if (prot &
|
|
|
|
~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_GROWSDOWN | PROT_GROWSUP)) {
|
2022-05-16 20:20:08 +00:00
|
|
|
errno = EINVAL; // unix checks prot before checking size
|
|
|
|
rc = -1;
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (!size) {
|
|
|
|
return 0; // make new technology consistent with unix
|
|
|
|
} else if (UNLIKELY((intptr_t)addr & 4095)) {
|
2022-05-16 20:20:08 +00:00
|
|
|
errno = EINVAL; // unix checks prot before checking size
|
|
|
|
rc = -1;
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (!IsWindows()) {
|
2022-04-24 16:59:22 +00:00
|
|
|
rc = sys_mprotect(addr, size, prot);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-04-24 16:59:22 +00:00
|
|
|
rc = sys_mprotect_nt(addr, size, prot);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("mprotect(%p, %'zu, %s) → %d% m", addr, size, DescribeProtFlags(prot),
|
|
|
|
rc);
|
2022-03-19 10:37:00 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|