[9.18] fix: dev: Stop using malloc_usable_size and malloc_size

The `malloc_usable_size()` can return size larger than originally allocated and when these sizes disagree the fortifier enabled by `_FORTIFY_SOURCE=3` detects overflow and stops the `named` execution abruptly.  Stop using these convenience functions as they are primary used for introspection-only.

Closes #4880

Backport of MR !9400

Merge branch 'backport-4880-dont-use-malloc_usable_size-9.18' into 'bind-9.18'

See merge request isc-projects/bind9!9419
This commit is contained in:
Ondřej Surý 2024-08-27 03:46:13 +00:00
commit cccd26e942
3 changed files with 5 additions and 72 deletions

View file

@ -1301,8 +1301,7 @@ AS_CASE([$with_jemalloc],
AS_IF([test "$with_jemalloc" = "no"],
[AS_CASE([$host],
[*-freebsd*],[AC_MSG_ERROR([You cannot compile without jemalloc; jemalloc is the system allocator on FreeBSD])])
AC_CHECK_FUNCS([malloc_size malloc_usable_size])])
[*-freebsd*],[AC_MSG_ERROR([You cannot compile without jemalloc; jemalloc is the system allocator on FreeBSD])])])
AM_CONDITIONAL([HAVE_JEMALLOC], [test "$with_jemalloc" = "yes"])

View file

@ -80,3 +80,5 @@
#define ISC_ATTR_MALLOC_DEALLOCATOR(deallocator)
#define ISC_ATTR_MALLOC_DEALLOCATOR_IDX(deallocator, idx)
#endif /* HAVE_FUNC_ATTRIBUTE_MALLOC */
#define ISC_ATTR_UNUSED __attribute__((__unused__))

View file

@ -27,67 +27,6 @@ const char *malloc_conf = NULL;
#define MALLOCX_TCACHE_NONE (0)
#define MALLOCX_ARENA(a) (0)
#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
#include <stdlib.h>
static inline void *
mallocx(size_t size, int flags) {
UNUSED(flags);
return (malloc(size));
}
static inline void
sdallocx(void *ptr, size_t size, int flags) {
UNUSED(size);
UNUSED(flags);
free(ptr);
}
static inline void *
rallocx(void *ptr, size_t size, int flags) {
UNUSED(flags);
REQUIRE(size != 0);
return (realloc(ptr, size));
}
#ifdef HAVE_MALLOC_SIZE
#include <malloc/malloc.h>
static inline size_t
sallocx(void *ptr, int flags) {
UNUSED(flags);
return (malloc_size(ptr));
}
#elif HAVE_MALLOC_USABLE_SIZE
#ifdef __DragonFly__
/*
* On DragonFly BSD 'man 3 malloc' advises us to include the following
* header to have access to malloc_usable_size().
*/
#include <malloc_np.h>
#else
#include <malloc.h>
#endif
static inline size_t
sallocx(void *ptr, int flags) {
UNUSED(flags);
return (malloc_usable_size(ptr));
}
#endif /* HAVE_MALLOC_SIZE */
#else /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
#include <stdlib.h>
typedef union {
@ -111,21 +50,16 @@ mallocx(size_t size, int flags) {
}
static inline void
sdallocx(void *ptr, size_t size, int flags) {
sdallocx(void *ptr, size_t size ISC_ATTR_UNUSED, int flags ISC_ATTR_UNUSED) {
size_info *si = &(((size_info *)ptr)[-1]);
UNUSED(size);
UNUSED(flags);
free(si);
}
static inline size_t
sallocx(void *ptr, int flags) {
sallocx(void *ptr, int flags ISC_ATTR_UNUSED) {
size_info *si = &(((size_info *)ptr)[-1]);
UNUSED(flags);
return (si[0].size);
}
@ -144,6 +78,4 @@ rallocx(void *ptr, size_t size, int flags) {
return (ptr);
}
#endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
#endif /* !defined(HAVE_JEMALLOC) */