Skip to content

Commit 1c929b2

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 749640c commit 1c929b2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

h_malloc.c

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

12861286
atomic_store_explicit(&ro.slab_region_end, slab_region_end, memory_order_release);
12871287

1288+
#if defined(__ANDROID__) && defined(HAS_ARM_MTE)
1289+
/* Do not seal to support disabling memory tagging */
12881290
if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
1291+
#else
1292+
if (unlikely(memory_protect_seal(&ro, sizeof(ro)))) {
1293+
#endif
12891294
fatal_error("failed to protect allocator data");
12901295
}
12911296
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>
@@ -83,6 +85,22 @@ bool memory_protect_rw_metadata(void *ptr, size_t size) {
8385
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
8486
}
8587

88+
bool memory_protect_seal(void *ptr, size_t size) {
89+
#if defined(__linux__) && defined(__NR_mseal)
90+
/* supported since Linux 6.10 */
91+
int ret = syscall(__NR_mseal, ptr, size, 0);
92+
if (ret == 0)
93+
return false;
94+
if (unlikely(errno == ENOMEM))
95+
return true;
96+
if (likely(errno == ENOSYS))
97+
return memory_protect_ro(ptr, size);
98+
fatal_error("non-ENOMEM and non_ENOSYS mseal failure");
99+
#else
100+
return memory_protect_ro(ptr, size);
101+
#endif
102+
}
103+
86104
#ifdef HAVE_COMPATIBLE_MREMAP
87105
bool memory_remap(void *old, size_t old_size, size_t new_size) {
88106
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
@@ -19,6 +19,7 @@ bool memory_unmap(void *ptr, size_t size);
1919
bool memory_protect_ro(void *ptr, size_t size);
2020
bool memory_protect_rw(void *ptr, size_t size);
2121
bool memory_protect_rw_metadata(void *ptr, size_t size);
22+
bool memory_protect_seal(void *ptr, size_t size);
2223
#ifdef HAVE_COMPATIBLE_MREMAP
2324
bool memory_remap(void *old, size_t old_size, size_t new_size);
2425
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);

0 commit comments

Comments
 (0)