Skip to content

Commit ad78fee

Browse files
committed
[runtimes][PAC] Harden unwinding when possible (#138571)
This hardens the unwinding logic and datastructures on systems that support pointer authentication. The approach taken to hardening is to harden the schemas of as many high value fields in the myriad structs as possible, and then also explicitly qualify local variables referencing privileged or security critical values. This ABI is exposed to the personality functions, and so updating to conform to that is a mandatory change, but to reduce the risk of oracles, the adoption also hardened the locals and datastructures in compiler-rt and libcxxabi.
1 parent eed98e1 commit ad78fee

File tree

14 files changed

+580
-86
lines changed

14 files changed

+580
-86
lines changed

compiler-rt/lib/builtins/gcc_personality_v0.c

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,45 @@ EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, void *, PCONTEXT,
3030
_Unwind_Personality_Fn);
3131
#endif
3232

33+
#if __has_include(<ptrauth.h>)
34+
#include <ptrauth.h>
35+
#endif
36+
37+
#if defined(__APPLE__) && __has_feature(ptrauth_qualifier)
38+
#if __has_feature(ptrauth_restricted_intptr_qualifier)
39+
#define PERSONALITY_PTRAUTH_RESTRICTED_INTPTR(key, addressDiscriminated, \
40+
discriminatorString) \
41+
__ptrauth_restricted_intptr(key, addressDiscriminated, \
42+
ptrauth_string_discriminator(discriminatorString))
43+
#else
44+
#define PERSONALITY_PTRAUTH_RESTRICTED_INTPTR(key, addressDiscriminated, \
45+
discriminatorString) \
46+
__ptrauth(key, addressDiscriminated, \
47+
ptrauth_string_discriminator(discriminatorString))
48+
#endif
49+
#else
50+
#define PERSONALITY_PTRAUTH_RESTRICTED_INTPTR(key, addressDiscriminated, \
51+
discriminatorString)
52+
#endif
53+
54+
// Helper wrappers for pointer auth qualifiers because we use a lot of variants
55+
// Suffixes:
56+
// * PDC : ptrauth_key_process_dependent_code
57+
// * RA : ptrauth_key_return_address
58+
// * FN : ptrauth_key_function_pointer
59+
#define PERSONALITY_PTRAUTH_RI_FN(__discriminator) \
60+
PERSONALITY_PTRAUTH_RESTRICTED_INTPTR(ptrauth_key_function_pointer, \
61+
/*__address_discriminated=*/1, \
62+
__discriminator)
63+
#define PERSONALITY_PTRAUTH_RI_PDC(__discriminator) \
64+
PERSONALITY_PTRAUTH_RESTRICTED_INTPTR(ptrauth_key_process_dependent_code, \
65+
/*__address_discriminated=*/1, \
66+
__discriminator)
67+
#define PERSONALITY_PTRAUTH_RI_RA(__discriminator) \
68+
PERSONALITY_PTRAUTH_RESTRICTED_INTPTR(ptrauth_key_return_address, \
69+
/*__address_discriminated=*/1, \
70+
__discriminator)
71+
3372
// Pointer encodings documented at:
3473
// http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
3574

@@ -205,7 +244,8 @@ COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
205244
return continueUnwind(exceptionObject, context);
206245

207246
uintptr_t pc = (uintptr_t)_Unwind_GetIP(context) - 1;
208-
uintptr_t funcStart = (uintptr_t)_Unwind_GetRegionStart(context);
247+
uintptr_t PERSONALITY_PTRAUTH_RI_FN("__gcc_personality_v0'funcStart")
248+
funcStart = (uintptr_t)_Unwind_GetRegionStart(context);
209249
uintptr_t pcOffset = pc - funcStart;
210250

211251
// Parse LSDA header.
@@ -224,11 +264,14 @@ COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
224264
const uint8_t *callSiteTableEnd = callSiteTableStart + callSiteTableLength;
225265
const uint8_t *p = callSiteTableStart;
226266
while (p < callSiteTableEnd) {
227-
uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
228-
size_t length = readEncodedPointer(&p, callSiteEncoding);
229-
size_t landingPad = readEncodedPointer(&p, callSiteEncoding);
267+
uintptr_t PERSONALITY_PTRAUTH_RI_PDC("__gcc_personality_v0'start")
268+
start = readEncodedPointer(&p, callSiteEncoding);
269+
size_t PERSONALITY_PTRAUTH_RI_PDC("__gcc_personality_v0'length")
270+
length = readEncodedPointer(&p, callSiteEncoding);
271+
size_t PERSONALITY_PTRAUTH_RI_PDC("__gcc_personality_v0'landingPadOffset")
272+
landingPadOffset = readEncodedPointer(&p, callSiteEncoding);
230273
readULEB128(&p); // action value not used for C code
231-
if (landingPad == 0)
274+
if (landingPadOffset == 0)
232275
continue; // no landing pad for this entry
233276
if ((start <= pcOffset) && (pcOffset < (start + length))) {
234277
// Found landing pad for the PC.
@@ -238,7 +281,21 @@ COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
238281
_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
239282
(uintptr_t)exceptionObject);
240283
_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
241-
_Unwind_SetIP(context, (funcStart + landingPad));
284+
#define LANDING_PAD_DISCRIMINATOR "__gcc_personality_v0'landingPad"
285+
size_t PERSONALITY_PTRAUTH_RI_RA(LANDING_PAD_DISCRIMINATOR)
286+
landingPad = funcStart + landingPadOffset;
287+
#if defined(__APPLE__) && __has_feature(ptrauth_qualifier)
288+
uintptr_t stack_pointer = _Unwind_GetGR(context, -2);
289+
const uintptr_t existingDiscriminator = ptrauth_blend_discriminator(
290+
&landingPad,
291+
ptrauth_string_discriminator(LANDING_PAD_DISCRIMINATOR));
292+
uintptr_t newIP = (uintptr_t)ptrauth_auth_and_resign(
293+
*(void **)&landingPad, ptrauth_key_function_pointer,
294+
existingDiscriminator, ptrauth_key_return_address, stack_pointer);
295+
_Unwind_SetIP(context, newIP);
296+
#else
297+
_Unwind_SetIP(context, landingPad);
298+
#endif
242299
return _URC_INSTALL_CONTEXT;
243300
}
244301
}

compiler-rt/lib/profile/InstrProfilingValue.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ __llvm_profile_iterate_data(const __llvm_profile_data *Data) {
8383
/* This method is only used in value profiler mock testing. */
8484
COMPILER_RT_VISIBILITY void *
8585
__llvm_get_function_addr(const __llvm_profile_data *Data) {
86-
return Data->FunctionPointer;
86+
void *FP = Data->FunctionPointer;
87+
#if __has_feature(ptrauth_calls)
88+
// This is only used for tests where we compare against what happens to be
89+
// signed pointers.
90+
FP = ptrauth_sign_unauthenticated(FP, VALID_CODE_KEY, 0);
91+
#endif
92+
return FP;
8793
}
8894

8995
/* Allocate an array that holds the pointers to the linked lists of

libcxxabi/include/__cxxabi_config.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
#endif
3333

3434
#if defined(_WIN32)
35-
#if defined(_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS) || (defined(__MINGW32__) && !defined(_LIBCXXABI_BUILDING_LIBRARY))
35+
#if defined(_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS) || \
36+
(defined(__MINGW32__) && !defined(_LIBCXXABI_BUILDING_LIBRARY))
3637
#define _LIBCXXABI_HIDDEN
3738
#define _LIBCXXABI_DATA_VIS
3839
#define _LIBCXXABI_FUNC_VIS
@@ -109,4 +110,49 @@
109110
# define _LIBCXXABI_NOEXCEPT noexcept
110111
#endif
111112

113+
#if __has_include(<ptrauth.h>)
114+
#include <ptrauth.h>
115+
#endif
116+
117+
#if defined(__APPLE__) && __has_feature(ptrauth_qualifier)
118+
# define _LIBCXXABI_PTRAUTH(__key, __address_discriminated, __discriminator) \
119+
__ptrauth(__key,__address_discriminated, \
120+
ptrauth_string_discriminator(__discriminator))
121+
// This work around is required to support divergence in spelling
122+
// during the ptrauth upstreaming process.
123+
# if __has_feature(ptrauth_restricted_intptr_qualifier)
124+
# define _LIBCXXABI_PTRAUTH_RESTRICTED_INTPTR(__key, __address_discriminated, \
125+
__discriminator) \
126+
__ptrauth_restricted_intptr(__key,__address_discriminated, \
127+
ptrauth_string_discriminator(__discriminator))
128+
# else
129+
# define _LIBCXXABI_PTRAUTH_RESTRICTED_INTPTR(__key, __address_discriminated, \
130+
__discriminator) \
131+
__ptrauth(__key,__address_discriminated, \
132+
ptrauth_string_discriminator(__discriminator))
133+
# endif
134+
#else
135+
# define _LIBCXXABI_PTRAUTH(__key, __address_discriminated, __discriminator)
136+
# define _LIBCXXABI_PTRAUTH_RESTRICTED_INTPTR(__key, __address_discriminated, \
137+
__discriminator)
138+
#endif
139+
140+
// Helper wrappers for pointer auth qualifiers because we use a lot of variants
141+
// Suffixes:
142+
// * _RI : qualifier is __ptrauth_restricted_intptr
143+
// * PDD : key is ptrauth_key_process_dependent_data
144+
// * FN : key is ptrauth_key_function_pointer
145+
#define _LIBCXXABI_PTRAUTH_PDD(__discriminator) \
146+
_LIBCXXABI_PTRAUTH(ptrauth_key_process_dependent_data, \
147+
/*__address_discriminated=*/1, \
148+
__discriminator)
149+
#define _LIBCXXABI_PTRAUTH_FN(__discriminator) \
150+
_LIBCXXABI_PTRAUTH(ptrauth_key_function_pointer, \
151+
/*__address_discriminated=*/1, \
152+
__discriminator)
153+
#define _LIBCXXABI_PTRAUTH_RI_PDD(__discriminator) \
154+
_LIBCXXABI_PTRAUTH_RESTRICTED_INTPTR(ptrauth_key_process_dependent_data, \
155+
/*__address_discriminated=*/1, \
156+
__discriminator)
157+
112158
#endif // ____CXXABI_CONFIG_H

libcxxabi/src/cxa_exception.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ struct _LIBCXXABI_HIDDEN __cxa_exception {
4747
// In Wasm, a destructor returns its argument
4848
void *(_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
4949
#else
50-
void (_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
50+
void(_LIBCXXABI_DTOR_FUNC* _LIBCXXABI_PTRAUTH_FN("__cxa_exception::exceptionDestructor") exceptionDestructor)(void*);
5151
#endif
52-
std::unexpected_handler unexpectedHandler;
53-
std::terminate_handler terminateHandler;
52+
std::unexpected_handler _LIBCXXABI_PTRAUTH_FN("__cxa_exception::unexpectedHandler") unexpectedHandler;
53+
std::terminate_handler _LIBCXXABI_PTRAUTH_FN("__cxa_exception::terminateHandler") terminateHandler;
5454

5555
__cxa_exception *nextException;
5656

@@ -61,10 +61,10 @@ struct _LIBCXXABI_HIDDEN __cxa_exception {
6161
int propagationCount;
6262
#else
6363
int handlerSwitchValue;
64-
const unsigned char *actionRecord;
65-
const unsigned char *languageSpecificData;
66-
void *catchTemp;
67-
void *adjustedPtr;
64+
const unsigned char* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::actionRecord") actionRecord;
65+
const unsigned char* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::languageSpecificData") languageSpecificData;
66+
void* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::catchTemp") catchTemp;
67+
void* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::adjustedPtr") adjustedPtr;
6868
#endif
6969

7070
#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
@@ -79,16 +79,18 @@ struct _LIBCXXABI_HIDDEN __cxa_exception {
7979
// http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
8080
// The layout of this structure MUST match the layout of __cxa_exception, with
8181
// primaryException instead of referenceCount.
82+
// The tags used in the pointer authentication qualifiers also need to match
83+
// those of the corresponding members in __cxa_exception.
8284
struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
8385
#if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
8486
void* reserve; // padding.
8587
void* primaryException;
8688
#endif
8789

8890
std::type_info *exceptionType;
89-
void (_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
90-
std::unexpected_handler unexpectedHandler;
91-
std::terminate_handler terminateHandler;
91+
void(_LIBCXXABI_DTOR_FUNC* _LIBCXXABI_PTRAUTH_FN("__cxa_exception::exceptionDestructor") exceptionDestructor)(void*);
92+
std::unexpected_handler _LIBCXXABI_PTRAUTH_FN("__cxa_exception::unexpectedHandler") unexpectedHandler;
93+
std::terminate_handler _LIBCXXABI_PTRAUTH_FN("__cxa_exception::terminateHandler") terminateHandler;
9294

9395
__cxa_exception *nextException;
9496

@@ -99,10 +101,10 @@ struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
99101
int propagationCount;
100102
#else
101103
int handlerSwitchValue;
102-
const unsigned char *actionRecord;
103-
const unsigned char *languageSpecificData;
104-
void * catchTemp;
105-
void *adjustedPtr;
104+
const unsigned char* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::actionRecord") actionRecord;
105+
const unsigned char* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::languageSpecificData") languageSpecificData;
106+
void* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::catchTemp") catchTemp;
107+
void* _LIBCXXABI_PTRAUTH_PDD("__cxa_exception::adjustedPtr") adjustedPtr;
106108
#endif
107109

108110
#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)

libcxxabi/src/cxa_personality.cpp

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
#include "private_typeinfo.h"
2323
#include "unwind.h"
2424

25+
#if __has_include(<ptrauth.h>)
26+
#include <ptrauth.h>
27+
#endif
28+
29+
#include "libunwind.h"
30+
2531
// TODO: This is a temporary workaround for libc++abi to recognize that it's being
2632
// built against LLVM's libunwind. LLVM's libunwind started reporting _LIBUNWIND_VERSION
2733
// in LLVM 15 -- we can remove this workaround after shipping LLVM 17. Once we remove
@@ -527,12 +533,19 @@ get_thrown_object_ptr(_Unwind_Exception* unwind_exception)
527533
namespace
528534
{
529535

536+
#define _LIBCXXABI_PTRAUTH_KEY ptrauth_key_process_dependent_code
537+
typedef const uint8_t* _LIBCXXABI_PTRAUTH_PDD("scan_results::languageSpecificData") lsd_ptr_t;
538+
typedef const uint8_t* _LIBCXXABI_PTRAUTH_PDD("scan_results::actionRecord") action_ptr_t;
539+
#define _LIBCXXABI_PTRAUTH_SCANRESULT_LANDINGPAD_DISC "scan_results::landingPad"
540+
typedef uintptr_t _LIBCXXABI_PTRAUTH_RI_PDD(_LIBCXXABI_PTRAUTH_SCANRESULT_LANDINGPAD_DISC) landing_pad_t;
541+
typedef void* _LIBCXXABI_PTRAUTH_PDD(_LIBCXXABI_PTRAUTH_SCANRESULT_LANDINGPAD_DISC) landing_pad_ptr_t;
542+
530543
struct scan_results
531544
{
532545
int64_t ttypeIndex; // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup
533-
const uint8_t* actionRecord; // Currently unused. Retained to ease future maintenance.
534-
const uint8_t* languageSpecificData; // Needed only for __cxa_call_unexpected
535-
uintptr_t landingPad; // null -> nothing found, else something found
546+
action_ptr_t actionRecord; // Currently unused. Retained to ease future maintenance.
547+
lsd_ptr_t languageSpecificData; // Needed only for __cxa_call_unexpected
548+
landing_pad_t landingPad; // null -> nothing found, else something found
536549
void* adjustedPtr; // Used in cxa_exception.cpp
537550
_Unwind_Reason_Code reason; // One of _URC_FATAL_PHASE1_ERROR,
538551
// _URC_FATAL_PHASE2_ERROR,
@@ -541,7 +554,33 @@ struct scan_results
541554
};
542555

543556
} // unnamed namespace
557+
}
544558

559+
namespace {
560+
// The logical model for casting authenticated function pointers makes
561+
// it impossible to directly cast them without breaking the authentication,
562+
// as a result we need this pair of helpers.
563+
template <typename PtrType>
564+
void set_landing_pad_as_ptr(scan_results& results, const PtrType& out) {
565+
union {
566+
landing_pad_t* as_landing_pad;
567+
landing_pad_ptr_t* as_pointer;
568+
} u;
569+
u.as_landing_pad = &results.landingPad;
570+
*u.as_pointer = out;
571+
}
572+
573+
static const landing_pad_ptr_t& get_landing_pad_as_ptr(const scan_results& results) {
574+
union {
575+
const landing_pad_t* as_landing_pad;
576+
const landing_pad_ptr_t* as_pointer;
577+
} u;
578+
u.as_landing_pad = &results.landingPad;
579+
return *u.as_pointer;
580+
}
581+
} // unnamed namespace
582+
583+
extern "C" {
545584
static
546585
void
547586
set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,
@@ -557,7 +596,22 @@ set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,
557596
reinterpret_cast<uintptr_t>(unwind_exception));
558597
_Unwind_SetGR(context, __builtin_eh_return_data_regno(1),
559598
static_cast<uintptr_t>(results.ttypeIndex));
599+
#if defined(__APPLE__) && __has_feature(ptrauth_qualifier)
600+
auto stack_pointer = _Unwind_GetGR(context, UNW_REG_SP);
601+
// We manually re-sign the IP as the __ptrauth qualifiers cannot
602+
// express the required relationship with the destination address
603+
const auto existingDiscriminator = ptrauth_blend_discriminator(
604+
&results.landingPad,
605+
ptrauth_string_discriminator(_LIBCXXABI_PTRAUTH_SCANRESULT_LANDINGPAD_DISC));
606+
unw_word_t newIP = (unw_word_t)ptrauth_auth_and_resign(*(void**)&results.landingPad,
607+
_LIBCXXABI_PTRAUTH_KEY,
608+
existingDiscriminator,
609+
ptrauth_key_return_address,
610+
stack_pointer);
611+
_Unwind_SetIP(context, newIP);
612+
#else
560613
_Unwind_SetIP(context, results.landingPad);
614+
#endif
561615
}
562616

563617
/*
@@ -691,12 +745,12 @@ static void scan_eh_tab(scan_results &results, _Unwind_Action actions,
691745
// The call sites are ordered in increasing value of start
692746
uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding);
693747
uintptr_t length = readEncodedPointer(&callSitePtr, callSiteEncoding);
694-
uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding);
748+
landing_pad_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding);
695749
uintptr_t actionEntry = readULEB128(&callSitePtr);
696750
if ((start <= ipOffset) && (ipOffset < (start + length)))
697751
#else // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__
698752
// ip is 1-based index into this table
699-
uintptr_t landingPad = readULEB128(&callSitePtr);
753+
landing_pad_t landingPad = readULEB128(&callSitePtr);
700754
uintptr_t actionEntry = readULEB128(&callSitePtr);
701755
if (--ip == 0)
702756
#endif // __USING_SJLJ_EXCEPTIONS__ || __WASM_EXCEPTIONS__
@@ -935,8 +989,7 @@ __gxx_personality_v0
935989
results.ttypeIndex = exception_header->handlerSwitchValue;
936990
results.actionRecord = exception_header->actionRecord;
937991
results.languageSpecificData = exception_header->languageSpecificData;
938-
results.landingPad =
939-
reinterpret_cast<uintptr_t>(exception_header->catchTemp);
992+
set_landing_pad_as_ptr(results, exception_header->catchTemp);
940993
results.adjustedPtr = exception_header->adjustedPtr;
941994

942995
// Jump to the handler.
@@ -970,7 +1023,7 @@ __gxx_personality_v0
9701023
exc->handlerSwitchValue = static_cast<int>(results.ttypeIndex);
9711024
exc->actionRecord = results.actionRecord;
9721025
exc->languageSpecificData = results.languageSpecificData;
973-
exc->catchTemp = reinterpret_cast<void*>(results.landingPad);
1026+
exc->catchTemp = get_landing_pad_as_ptr(results);
9741027
exc->adjustedPtr = results.adjustedPtr;
9751028
#ifdef __WASM_EXCEPTIONS__
9761029
// Wasm only uses a single phase (_UA_SEARCH_PHASE), so save the

0 commit comments

Comments
 (0)