blob: 82b0b7b5bd768d504f82b0868d8ffe9cda701172 [file] [log] [blame]
Peter Collingbourne5d3aa862020-09-11 15:05:17 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "memory_mitigation_state.h"
30
31#include <dirent.h>
32#include <pthread.h>
33#include <semaphore.h>
34#include <stdatomic.h>
35#include <stdlib.h>
36#include <sys/prctl.h>
37#include <sys/types.h>
38
39#include <bionic/mte.h>
40#include <bionic/reserved_signals.h>
41
42#include "private/ScopedRWLock.h"
43#include "pthread_internal.h"
44
45extern "C" void scudo_malloc_set_zero_contents(int zero_contents);
46extern "C" void scudo_malloc_disable_memory_tagging();
47
48#ifdef ANDROID_EXPERIMENTAL_MTE
49static bool set_tcf_on_all_threads(int tcf) {
50 static int g_tcf;
51 g_tcf = tcf;
52
53 return android_run_on_all_threads(
54 [](void*) {
55 int tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
56 if (tagged_addr_ctrl < 0) {
57 return false;
58 }
59
60 tagged_addr_ctrl = (tagged_addr_ctrl & ~PR_MTE_TCF_MASK) | g_tcf;
61 if (prctl(PR_SET_TAGGED_ADDR_CTRL, tagged_addr_ctrl, 0, 0, 0) < 0) {
62 return false;
63 }
64 return true;
65 },
66 nullptr);
67}
68#endif
69
70bool DisableMemoryMitigations(void* arg, size_t arg_size) {
71 if (arg || arg_size) {
72 return false;
73 }
74
75#ifdef USE_SCUDO
76 scudo_malloc_set_zero_contents(0);
77
78#ifdef ANDROID_EXPERIMENTAL_MTE
79 if (mte_supported() && set_tcf_on_all_threads(PR_MTE_TCF_NONE)) {
80 scudo_malloc_disable_memory_tagging();
81 }
82#endif
83#endif
84
85 return true;
86}