From 5e364632e03d0a47684040aae1037a14eb2f86db Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 7 Dec 2014 14:52:44 -0800 Subject: [PATCH] Make sodium_free() callable even if protection is PROT_NONE. Reported by @stouset, thanks! --- src/libsodium/sodium/utils.c | 2 +- test/default/sodium_utils2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 8886a9b8..9d927eb1 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -429,6 +429,7 @@ 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(); } @@ -436,7 +437,6 @@ sodium_free(void *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); #ifndef HAVE_PAGE_PROTECTION if (sodium_memcmp(unprotected_ptr + unprotected_size, canary, sizeof canary) != 0) { diff --git a/test/default/sodium_utils2.c b/test/default/sodium_utils2.c index 78c6f45e..502e322e 100644 --- a/test/default/sodium_utils2.c +++ b/test/default/sodium_utils2.c @@ -50,7 +50,7 @@ int main(void) buf = sodium_malloc(size); assert(buf != NULL); memset(buf, i, size); - sodium_mprotect_readonly(buf); + sodium_mprotect_noaccess(buf); sodium_free(buf); } printf("OK\n");