Skip to content

Commit

Permalink
add XOR mangling mitigation for thread-local dtors
Browse files Browse the repository at this point in the history
Signed-off-by: anupritaisno1 <www.anuprita804@gmail.com>
  • Loading branch information
thestinger committed Jun 7, 2021
1 parent e239c7d commit 0b03d92
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
8 changes: 5 additions & 3 deletions libc/bionic/__cxa_thread_atexit_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <sys/cdefs.h>

#include <private/bionic_defs.h>
#include <private/bionic_globals.h>

#include "pthread_internal.h"

class thread_local_dtor {
public:
void (*func) (void *);
uintptr_t func;
void *arg;
void *dso_handle; // unused...
thread_local_dtor* next;
Expand All @@ -35,7 +37,7 @@ __BIONIC_WEAK_FOR_NATIVE_BRIDGE
int __cxa_thread_atexit_impl(void (*func) (void *), void *arg, void *dso_handle) {
thread_local_dtor* dtor = new thread_local_dtor();

dtor->func = func;
dtor->func = __libc_globals->dtor_cookie ^ reinterpret_cast<uintptr_t>(func);
dtor->arg = arg;
dtor->dso_handle = dso_handle;

Expand All @@ -54,7 +56,7 @@ extern "C" __LIBC_HIDDEN__ void __cxa_thread_finalize() {
thread_local_dtor* current = thread->thread_local_dtors;
thread->thread_local_dtors = current->next;

current->func(current->arg);
(reinterpret_cast<void (*)(void*)>(__libc_globals->dtor_cookie ^ current->func))(current->arg);
if (__loader_remove_thread_local_dtor != nullptr) {
__loader_remove_thread_local_dtor(current->dso_handle);
}
Expand Down
2 changes: 2 additions & 0 deletions libc/bionic/libc_init_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <async_safe/log.h>

#include "private/WriteProtected.h"
#include "private/bionic_arc4random.h"
#include "private/bionic_defs.h"
#include "private/bionic_globals.h"
#include "private/bionic_tls.h"
Expand All @@ -66,6 +67,7 @@ void __libc_init_globals() {
__libc_globals.mutate([](libc_globals* globals) {
__libc_init_vdso(globals);
__libc_init_setjmp_cookie(globals);
arc4random_buf(&globals->dtor_cookie, sizeof(globals->dtor_cookie));
});
}

Expand Down
1 change: 1 addition & 0 deletions libc/private/bionic_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

struct libc_globals {
vdso_entry vdso[VDSO_END];
long dtor_cookie;
long setjmp_cookie;
uintptr_t heap_pointer_tag;

Expand Down

0 comments on commit 0b03d92

Please sign in to comment.