1 /*
2 * Copyright (C) 2019 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 "heap_tagging.h"
30 #include "malloc_common.h"
31 #include "malloc_tagged_pointers.h"
32
33 #include <platform/bionic/malloc.h>
34 #include <platform/bionic/mte_kernel.h>
35
36 extern "C" void scudo_malloc_disable_memory_tagging();
37
38 static HeapTaggingLevel heap_tagging_level = M_HEAP_TAGGING_LEVEL_NONE;
39
SetDefaultHeapTaggingLevel()40 void SetDefaultHeapTaggingLevel() {
41 #if defined(__aarch64__)
42 #define PR_SET_TAGGED_ADDR_CTRL 55
43 #define PR_TAGGED_ADDR_ENABLE (1UL << 0)
44 #ifdef ANDROID_EXPERIMENTAL_MTE
45 // First, try enabling MTE in asynchronous mode, with tag 0 excluded. This will fail if the kernel
46 // or hardware doesn't support MTE, and we will fall back to just enabling tagged pointers in
47 // syscall arguments.
48 if (prctl(PR_SET_TAGGED_ADDR_CTRL,
49 PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_ASYNC | (1 << PR_MTE_EXCL_SHIFT), 0, 0, 0) == 0) {
50 heap_tagging_level = M_HEAP_TAGGING_LEVEL_ASYNC;
51 return;
52 }
53 #endif // ANDROID_EXPERIMENTAL_MTE
54
55 // Allow the kernel to accept tagged pointers in syscall arguments. This is a no-op (kernel
56 // returns -EINVAL) if the kernel doesn't understand the prctl.
57 if (prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0) == 0) {
58 #if !__has_feature(hwaddress_sanitizer)
59 heap_tagging_level = M_HEAP_TAGGING_LEVEL_TBI;
60 __libc_globals.mutate([](libc_globals* globals) {
61 // Arrange for us to set pointer tags to POINTER_TAG, check tags on
62 // deallocation and untag when passing pointers to the allocator.
63 globals->heap_pointer_tag = (reinterpret_cast<uintptr_t>(POINTER_TAG) << TAG_SHIFT) |
64 (0xffull << CHECK_SHIFT) | (0xffull << UNTAG_SHIFT);
65 });
66 #endif // hwaddress_sanitizer
67 }
68 #endif // aarch64
69 }
70
SetHeapTaggingLevel(void * arg,size_t arg_size)71 bool SetHeapTaggingLevel(void* arg, size_t arg_size) {
72 if (arg_size != sizeof(HeapTaggingLevel)) {
73 return false;
74 }
75
76 auto tag_level = *reinterpret_cast<HeapTaggingLevel*>(arg);
77 if (tag_level == heap_tagging_level) {
78 return true;
79 }
80
81 switch (tag_level) {
82 case M_HEAP_TAGGING_LEVEL_NONE:
83 break;
84 case M_HEAP_TAGGING_LEVEL_TBI:
85 case M_HEAP_TAGGING_LEVEL_ASYNC:
86 if (heap_tagging_level == M_HEAP_TAGGING_LEVEL_NONE) {
87 error_log(
88 "SetHeapTaggingLevel: re-enabling tagging after it was disabled is not supported");
89 } else {
90 error_log("SetHeapTaggingLevel: switching between TBI and ASYNC is not supported");
91 }
92 return false;
93 default:
94 error_log("SetHeapTaggingLevel: unknown tagging level");
95 return false;
96 }
97 heap_tagging_level = tag_level;
98 info_log("SetHeapTaggingLevel: tag level set to %d", tag_level);
99
100 if (heap_tagging_level == M_HEAP_TAGGING_LEVEL_NONE) {
101 #if defined(USE_SCUDO)
102 scudo_malloc_disable_memory_tagging();
103 #endif
104 __libc_globals.mutate([](libc_globals* globals) {
105 // Preserve the untag mask (we still want to untag pointers when passing them to the
106 // allocator if we were doing so before), but clear the fixed tag and the check mask,
107 // so that pointers are no longer tagged and checks no longer happen.
108 globals->heap_pointer_tag &= 0xffull << UNTAG_SHIFT;
109 });
110 }
111
112 return true;
113 }
114