linux: make use of mseal(2)

Instead of protecting the global read-only data structure after startup
via the read-only flag, which can be reverted, use the in Linux 6.10
introduced irreversible syscall mseal(2).
This commit is contained in:
Christian Göttsche 2024-07-24 17:20:07 +02:00
parent 749640c274
commit 96836f463b
3 changed files with 24 additions and 0 deletions

View file

@ -1,6 +1,8 @@
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#ifdef LABEL_MEMORY
#include <sys/prctl.h>
@ -83,6 +85,22 @@ bool memory_protect_rw_metadata(void *ptr, size_t size) {
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
}
COLD bool memory_protect_seal(void *ptr, size_t size) {
#if defined(__linux__) && defined(__NR_mseal)
/* supported since Linux 6.10 */
int ret = syscall(__NR_mseal, ptr, size, 0);
if (ret == 0)
return false;
if (unlikely(errno == ENOMEM))
return true;
if (errno == ENOSYS)
return memory_protect_ro(ptr, size);
fatal_error("non-ENOMEM and non-ENOSYS mseal failure");
#else
return memory_protect_ro(ptr, size);
#endif
}
#ifdef HAVE_COMPATIBLE_MREMAP
bool memory_remap(void *old, size_t old_size, size_t new_size) {
void *ptr = mremap(old, old_size, new_size, 0);