Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 4530e56

Browse files
fmayerGerrit Code Review
authored andcommitted
Merge "Add signal to re-enable MTE for a thread" into main
2 parents f331c4b + 5d9725b commit 4530e56

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

libc/bionic/libc_init_static.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <elf.h>
3131
#include <errno.h>
3232
#include <malloc.h>
33+
#include <signal.h>
3334
#include <stddef.h>
3435
#include <stdint.h>
3536
#include <stdio.h>
@@ -293,6 +294,28 @@ static HeapTaggingLevel __get_tagging_level(const memtag_dynamic_entries_t* memt
293294
return level;
294295
}
295296

297+
static void __enable_mte_signal_handler(int, siginfo_t* info, void*) {
298+
if (info->si_code != SI_TIMER) {
299+
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Got BIONIC_ENABLE_MTE not from SI_TIMER");
300+
return;
301+
}
302+
int tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
303+
if (tagged_addr_ctrl < 0) {
304+
async_safe_fatal("failed to PR_GET_TAGGED_ADDR_CTRL: %m");
305+
}
306+
if ((tagged_addr_ctrl & PR_MTE_TCF_MASK) != PR_MTE_TCF_NONE) {
307+
return;
308+
}
309+
async_safe_format_log(ANDROID_LOG_INFO, "libc",
310+
"Re-enabling MTE, value: %x (tagged_addr_ctrl %lu)",
311+
info->si_value.sival_int, info->si_value.sival_int & PR_MTE_TCF_MASK);
312+
tagged_addr_ctrl =
313+
(tagged_addr_ctrl & ~PR_MTE_TCF_MASK) | (info->si_value.sival_int & PR_MTE_TCF_MASK);
314+
if (prctl(PR_SET_TAGGED_ADDR_CTRL, tagged_addr_ctrl, 0, 0, 0) < 0) {
315+
async_safe_fatal("failed to PR_SET_TAGGED_ADDR_CTRL %d: %m", tagged_addr_ctrl);
316+
}
317+
}
318+
296319
static int64_t __get_memtag_upgrade_secs() {
297320
char* env = getenv("BIONIC_MEMTAG_UPGRADE_SECS");
298321
if (!env) return 0;
@@ -366,7 +389,10 @@ __attribute__((no_sanitize("hwaddress", "memtag"))) void __libc_init_mte(
366389
async_safe_fatal("error: failed to set PROT_MTE on main thread stack: %m");
367390
}
368391
}
369-
392+
struct sigaction action = {};
393+
action.sa_flags = SA_SIGINFO | SA_RESTART;
394+
action.sa_sigaction = __enable_mte_signal_handler;
395+
sigaction(BIONIC_ENABLE_MTE, &action, nullptr);
370396
return;
371397
}
372398
}

0 commit comments

Comments
 (0)