Skip to content

Commit c8ddec7

Browse files
committed
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).
1 parent 3bee8d3 commit c8ddec7

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

h_malloc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,12 @@ COLD static void init_slow_path(void) {
12951295

12961296
atomic_store_explicit(&ro.slab_region_end, slab_region_end, memory_order_release);
12971297

1298+
#if defined(__ANDROID__) && defined(HAS_ARM_MTE)
1299+
/* Do not seal to support disabling memory tagging */
12981300
if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
1301+
#else
1302+
if (unlikely(memory_protect_seal(&ro, sizeof(ro)))) {
1303+
#endif
12991304
fatal_error("failed to protect allocator data");
13001305
}
13011306
memory_set_name(&ro, sizeof(ro), "malloc read-only after init");

memory.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <errno.h>
2+
#include <unistd.h>
23

34
#include <sys/mman.h>
5+
#include <sys/syscall.h>
46

57
#ifdef LABEL_MEMORY
68
#include <sys/prctl.h>
@@ -91,6 +93,22 @@ bool memory_protect_rw_metadata(void *ptr, size_t size) {
9193
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
9294
}
9395

96+
COLD bool memory_protect_seal(void *ptr, size_t size) {
97+
#if defined(__linux__) && defined(__NR_mseal)
98+
/* supported since Linux 6.10 */
99+
int ret = syscall(__NR_mseal, ptr, size, 0);
100+
if (ret == 0)
101+
return false;
102+
if (unlikely(errno == ENOMEM))
103+
return true;
104+
if (errno == ENOSYS)
105+
return memory_protect_ro(ptr, size);
106+
fatal_error("non-ENOMEM and non-ENOSYS mseal failure");
107+
#else
108+
return memory_protect_ro(ptr, size);
109+
#endif
110+
}
111+
94112
#ifdef HAVE_COMPATIBLE_MREMAP
95113
bool memory_remap(void *old, size_t old_size, size_t new_size) {
96114
void *ptr = mremap(old, old_size, new_size, 0);

memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bool memory_unmap(void *ptr, size_t size);
2222
bool memory_protect_ro(void *ptr, size_t size);
2323
bool memory_protect_rw(void *ptr, size_t size);
2424
bool memory_protect_rw_metadata(void *ptr, size_t size);
25+
bool memory_protect_seal(void *ptr, size_t size);
2526
#ifdef HAVE_COMPATIBLE_MREMAP
2627
bool memory_remap(void *old, size_t old_size, size_t new_size);
2728
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);

0 commit comments

Comments
 (0)