diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 9d927eb1..287202a4 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -429,14 +429,14 @@ sodium_free(void *ptr) return; } canary_ptr = ((unsigned char *) ptr) - sizeof canary; - sodium_mprotect_readwrite(ptr); - if (sodium_memcmp(canary_ptr, canary, sizeof canary) != 0) { - _out_of_bounds(); - } unprotected_ptr = _unprotected_ptr_from_user_ptr(ptr); base_ptr = unprotected_ptr - page_size * 2U; memcpy(&unprotected_size, base_ptr, sizeof unprotected_size); total_size = page_size + page_size + unprotected_size + page_size; + _mprotect_readwrite(base_ptr, total_size); + if (sodium_memcmp(canary_ptr, canary, sizeof canary) != 0) { + _out_of_bounds(); + } #ifndef HAVE_PAGE_PROTECTION if (sodium_memcmp(unprotected_ptr + unprotected_size, canary, sizeof canary) != 0) {