blob: c3116512d92897421a5ebddc0559e6169cc5dca4 [file] [log] [blame]
Peter Collingbournea5c4b172020-10-30 11:38:55 -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#pragma once
30
31#include <stddef.h>
Peter Collingbournebf52e882022-01-19 13:35:54 -080032#include <sys/prctl.h>
Peter Collingbournea5c4b172020-10-30 11:38:55 -070033
34inline uintptr_t __bionic_clear_pac_bits(uintptr_t ptr) {
35#if defined(__aarch64__)
36 register uintptr_t x30 __asm("x30") = ptr;
37 // This is a NOP on pre-Armv8.3-A architectures.
38 asm("xpaclri" : "+r"(x30));
39 return x30;
40#else
41 return ptr;
42#endif
43}
Peter Collingbournebf52e882022-01-19 13:35:54 -080044
45#ifdef __aarch64__
46// The default setting for branch-protection enables both PAC and BTI, so by
47// overriding it to only enable BTI we disable PAC.
48#define __BIONIC_DISABLE_PAUTH __attribute__((target("branch-protection=bti")))
49#else
50#define __BIONIC_DISABLE_PAUTH
51#endif
52
53#ifdef __aarch64__
54// Disable PAC (i.e. make the signing and authentication instructions into no-ops) for the lifetime
55// of this object.
56class ScopedDisablePAC {
57 int prev_enabled_keys_;
58
59 public:
60 // Disabling IA will invalidate the return address in this function if it is signed, so we need to
61 // make sure that this function does not sign its return address. Likewise for the destructor.
62 __BIONIC_DISABLE_PAUTH
63 ScopedDisablePAC() {
64 // These prctls will fail (resulting in a no-op, the intended behavior) if PAC is not supported.
65 prev_enabled_keys_ = prctl(PR_PAC_GET_ENABLED_KEYS, 0, 0, 0, 0);
66 prctl(PR_PAC_SET_ENABLED_KEYS, prev_enabled_keys_, 0, 0, 0);
67 }
68
69 __BIONIC_DISABLE_PAUTH
70 ~ScopedDisablePAC() {
71 prctl(PR_PAC_SET_ENABLED_KEYS, prev_enabled_keys_, prev_enabled_keys_, 0, 0);
72 }
73};
74#else
75struct ScopedDisablePAC {
76 // Silence unused variable warnings in non-aarch64 builds.
77 ScopedDisablePAC() {}
78};
79#endif