mirror of
https://github.com/GrapheneOS/hardened_malloc.git
synced 2025-05-23 16:31:20 -04:00
guard metadata with Memory Protection Keys (MPK)
This commit is contained in:
parent
ac8c68de53
commit
0b963078d5
6 changed files with 126 additions and 18 deletions
18
memory.c
18
memory.c
|
@ -35,20 +35,28 @@ int memory_unmap(void *ptr, size_t size) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int memory_protect_prot(void *ptr, size_t size, int prot) {
|
||||
static int memory_protect_prot(void *ptr, size_t size, int prot, UNUSED int pkey) {
|
||||
#ifdef USE_PKEY
|
||||
int ret = pkey_mprotect(ptr, size, prot, pkey);
|
||||
#else
|
||||
int ret = mprotect(ptr, size, prot);
|
||||
#endif
|
||||
if (unlikely(ret) && errno != ENOMEM) {
|
||||
fatal_error("non-ENOMEM mprotect failure");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int memory_protect_rw(void *ptr, size_t size) {
|
||||
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE);
|
||||
int memory_protect_ro(void *ptr, size_t size) {
|
||||
return memory_protect_prot(ptr, size, PROT_READ, -1);
|
||||
}
|
||||
|
||||
int memory_protect_ro(void *ptr, size_t size) {
|
||||
return memory_protect_prot(ptr, size, PROT_READ);
|
||||
int memory_protect_rw(void *ptr, size_t size) {
|
||||
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, -1);
|
||||
}
|
||||
|
||||
int memory_protect_rw_metadata(void *ptr, size_t size) {
|
||||
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
|
||||
}
|
||||
|
||||
int memory_remap(void *old, size_t old_size, size_t new_size) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue