]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Unpoison Workspace Memory Before Freeing to Custom Free 3725/head
authorW. Felix Handte <w@felixhandte.com>
Wed, 16 Aug 2023 16:09:12 +0000 (12:09 -0400)
committerW. Felix Handte <w@felixhandte.com>
Wed, 16 Aug 2023 16:09:12 +0000 (12:09 -0400)
MSAN is hooked into the system malloc, but when the user provides a custom
allocator, it may not provide the same cleansing behavior. So if we leave
memory poisoned and return it to the user's allocator, where it is re-used
elsewhere, our poisoning can blow up in some other context.

lib/compress/zstd_cwksp.h

index 9aeed19438ebffa8cf10a0b07f5342e5a6f5f292..a3efc56e523430ae63c63d00a412d73e86736a68 100644 (file)
@@ -676,6 +676,11 @@ MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp* ws, size_t size, ZSTD_customMem
 MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) {
     void *ptr = ws->workspace;
     DEBUGLOG(4, "cwksp: freeing workspace");
+#if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
+    if (ptr != NULL && customMem.customFree != NULL) {
+        __msan_unpoison(ptr, ZSTD_cwksp_sizeof(ws));
+    }
+#endif
     ZSTD_memset(ws, 0, sizeof(ZSTD_cwksp));
     ZSTD_customFree(ptr, customMem);
 }