|
30 | 30 | #include <elf.h> |
31 | 31 | #include <errno.h> |
32 | 32 | #include <malloc.h> |
| 33 | +#include <signal.h> |
33 | 34 | #include <stddef.h> |
34 | 35 | #include <stdint.h> |
35 | 36 | #include <stdio.h> |
@@ -293,6 +294,28 @@ static HeapTaggingLevel __get_tagging_level(const memtag_dynamic_entries_t* memt |
293 | 294 | return level; |
294 | 295 | } |
295 | 296 |
|
| 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 | + |
296 | 319 | static int64_t __get_memtag_upgrade_secs() { |
297 | 320 | char* env = getenv("BIONIC_MEMTAG_UPGRADE_SECS"); |
298 | 321 | if (!env) return 0; |
@@ -366,7 +389,10 @@ __attribute__((no_sanitize("hwaddress", "memtag"))) void __libc_init_mte( |
366 | 389 | async_safe_fatal("error: failed to set PROT_MTE on main thread stack: %m"); |
367 | 390 | } |
368 | 391 | } |
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); |
370 | 396 | return; |
371 | 397 | } |
372 | 398 | } |
|
0 commit comments