From 8e3bebc736c224db4d7fc81ab9fa841c7015318a Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 30 Dec 2019 14:18:35 -0800 Subject: fixes #1075 WebSocket heap use after free This also introduces a new atomic boolean type, so we can use that to trigger whether we've added the HTTP handler or not. --- src/platform/posix/posix_atomic.c | 76 +++++++++++++++++++++++++++++++++++---- src/platform/posix/posix_impl.h | 12 +++++-- src/platform/windows/win_impl.h | 4 +++ src/platform/windows/win_thread.c | 24 +++++++++++++ 4 files changed, 107 insertions(+), 9 deletions(-) (limited to 'src/platform') diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c index 56e2c370..b1183865 100644 --- a/src/platform/posix/posix_atomic.c +++ b/src/platform/posix/posix_atomic.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. +// Copyright 2019 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -29,34 +29,61 @@ nni_atomic_flag_reset(nni_atomic_flag *f) atomic_flag_clear(&f->f); } +void +nni_atomic_set_bool(nni_atomic_bool *v, bool b) +{ + atomic_store(&v->v, b); +} + +bool +nni_atomic_get_bool(nni_atomic_bool *v) +{ + return (atomic_load(&v->v)); +} + +bool +nni_atomic_swap_bool(nni_atomic_bool *v, bool b) +{ + return (atomic_exchange(&v->v, b)); + +} + +void +nni_atomic_init_bool(nni_atomic_bool *v) +{ + atomic_init(&v->v, false); +} + void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { - (void) atomic_fetch_add_explicit(&v->v, bump, memory_order_relaxed); + (void) atomic_fetch_add_explicit( + &v->v, (uint_fast64_t) bump, memory_order_relaxed); } void nni_atomic_sub64(nni_atomic_u64 *v, uint64_t bump) { - (void) atomic_fetch_sub_explicit(&v->v, bump, memory_order_relaxed); + (void) atomic_fetch_sub_explicit( + &v->v, (uint_fast64_t) bump, memory_order_relaxed); } uint64_t nni_atomic_get64(nni_atomic_u64 *v) { - return (atomic_load(&v->v)); + return ((uint64_t) atomic_load(&v->v)); } void nni_atomic_set64(nni_atomic_u64 *v, uint64_t u) { - atomic_store(&v->v, u); + atomic_store(&v->v, (uint_fast64_t) u); } uint64_t nni_atomic_swap64(nni_atomic_u64 *v, uint64_t u) { - return (atomic_exchange(&v->v, u)); + return ((uint64_t) atomic_exchange(&v->v, (uint_fast64_t) u)); } void @@ -77,7 +104,7 @@ nni_atomic_dec64_nv(nni_atomic_u64 *v) uint64_t ov; // C11 atomics give the old rather than new value. - ov = atomic_fetch_sub(&v->v, 1); + ov = (uint64_t) atomic_fetch_sub(&v->v, 1); return (ov - 1); } @@ -106,6 +133,41 @@ nni_atomic_flag_reset(nni_atomic_flag *f) pthread_mutex_unlock(&plat_atomic_lock); } +void +nni_atomic_set_bool(nni_atomic_bool *b) +{ + pthread_mutex_lock(&plat_atomic_lock); + b->b = false; + pthread_mutex_unlock(&plat_atomic_lock); +} + +void +nni_atomic_get_bool(nni_atomic_bool *b) +{ + bool v; + pthread_mutex_lock(&plat_atomic_lock); + v = b->b; + pthread_mutex_unlock(&plat_atomic_lock); + return (v); +} + +void +nni_atomic_swap_bool(nni_atomic_bool *b, bool n) +{ + bool v; + pthread_mutex_lock(&plat_atomic_lock); + v = b->b; + b->b = n; + pthread_mutex_unlock(&plat_atomic_lock); + return (v); +} + +void +nni_atomic_initbool(nni_atomic_bool *b) +{ + b->b = false; +} + void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index b07d268b..a1cb62c5 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. +// Copyright 2019 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -84,7 +84,11 @@ struct nni_atomic_flag { }; struct nni_atomic_u64 { - _Atomic unsigned long long v; + atomic_uint_fast64_t v; +}; + +struct nni_atomic_bool { + atomic_bool v; }; #else // NNG_HAVE_C11_ATOMIC @@ -92,6 +96,10 @@ struct nni_atomic_flag { bool f; }; +struct nni_atomic_bol { + bool b; +}; + struct nni_atomic_u64 { uint64_t v; }; diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index cd15804a..befcd185 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -50,6 +50,10 @@ struct nni_atomic_flag { unsigned f; }; +struct nni_atomic_bool { + LONG v; +}; + struct nni_atomic_u64 { LONGLONG v; }; diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 076da9e4..763dcf7f 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -131,6 +131,30 @@ nni_atomic_flag_reset(nni_atomic_flag *f) InterlockedExchange(&f->f, 0); } +void +nni_atomic_set_bool(nni_atomic_bool *v, bool b) +{ + InterlockedExchange(&v->v, (LONG) b); +} + +bool +nni_atomic_get_bool(nni_atomic_bool *v) +{ + return ((bool) InterlockedAdd(&v->v, 0)); +} + +bool +nni_atomic_swap_bool(nni_atomic_bool *v, bool b) +{ + return ((bool) InterlockedExchange(&v->v, (LONG) b)); +} + +void +nni_atomic_init_bool(nni_atomic_bool *v) +{ + InterlockedExchange(&v->v, 0); +} + void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { -- cgit v1.2.3-70-g09d2