blob: 4761d886214b2f20170f3eab2cf7a331f6c691f5 [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
Peter Collingbourne9eb85bf2020-11-02 13:35:28 -080039#include <bionic/malloc.h>
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070040#include <bionic/mte.h>
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070041
Mitch Phillips2210b8d2020-11-25 16:48:54 -080042#include <private/ScopedPthreadMutexLocker.h>
43#include <private/ScopedRWLock.h>
44
Peter Collingbourne9eb85bf2020-11-02 13:35:28 -080045#include "heap_tagging.h"
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070046#include "pthread_internal.h"
47
48extern "C" void scudo_malloc_set_zero_contents(int zero_contents);
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070049
50bool DisableMemoryMitigations(void* arg, size_t arg_size) {
51 if (arg || arg_size) {
52 return false;
53 }
54
55#ifdef USE_SCUDO
56 scudo_malloc_set_zero_contents(0);
Peter Collingbourne9eb85bf2020-11-02 13:35:28 -080057#endif
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070058
Mitch Phillips2210b8d2020-11-25 16:48:54 -080059 ScopedPthreadMutexLocker locker(&g_heap_tagging_lock);
60
61 HeapTaggingLevel current_level = GetHeapTaggingLevel();
62 if (current_level != M_HEAP_TAGGING_LEVEL_NONE && current_level != M_HEAP_TAGGING_LEVEL_TBI) {
63 HeapTaggingLevel level = M_HEAP_TAGGING_LEVEL_NONE;
64 SetHeapTaggingLevel(reinterpret_cast<void*>(&level), sizeof(level));
65 }
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070066
67 return true;
68}