Commit 23970fab063 for php

commit 23970fab063ecba9f63f86a5a788e7cd14e35f0e
Author: Florian Engelhardt <florian.engelhardt@datadoghq.com>
Date:   Wed Nov 25 18:48:47 2026 +0100

    Fix alignment in `zend_string_safe_alloc()`/`zend_string_safe_realloc()` (#23869)

    * Fix alignment in `zend_string_safe_alloc()`/`zend_string_safe_realloc()`

    * Update chunk_split 32bit test for new overflow offset

    * Fix allignement in mbstring functions

    * fix overflow

    * Mark fatal string allocation overflow as unreachable

    Let GCC see that non-fallible overflow handling aborts, avoiding false
    array-bounds errors in callers that copy into the allocated string.

    * Reuse safe allocators for aligned strings

    Apply ndossche's suggestion and check realloc alignment overflow.

    https://github.com/php/php-src/pull/23869#issuecomment-6795117595

    * Make string allocation bounds explicit to GCC

    Assert bounds after successful allocation to avoid stringop-overflow warnings.

    * Check zend_string alignment under ASan in zend_string_equal_val()

    Inline asm in zend_string_equal_val() is not instrumented by ASan.
    Check explicitly with __asan_region_is_poisoned() that the aligned
    buffer size the asm reads up to is addressable.

diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c
index f5bf3a001b4..1f9aa8fc826 100644
--- a/Zend/zend_alloc.c
+++ b/Zend/zend_alloc.c
@@ -1834,6 +1834,10 @@ static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *pt
 #if ZEND_DEBUG
 	real_size = size;
 	size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
+	if (UNEXPECTED(size < real_size)) {
+		zend_mm_delayed_error_or_panic(heap->fallible, "Possible integer overflow in memory allocation (%zu + %zu)", ZEND_MM_ALIGNED_SIZE(real_size), ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
+		return NULL;
+	}
 #endif
 	if (size > ZEND_MM_MAX_LARGE_SIZE) {
 #if ZEND_DEBUG
@@ -1844,10 +1848,15 @@ static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *pt
 		 * We allocate them with 2MB size granularity, to avoid many
 		 * reallocations when they are extended by small pieces
 		 */
-		new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE));
+		size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE);
 #else
-		new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
+		size_t alignment = REAL_PAGE_SIZE;
 #endif
+		new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment);
+		if (UNEXPECTED(new_size < size)) {
+			zend_mm_delayed_error_or_panic(heap->fallible, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment);
+			return NULL;
+		}
 		if (new_size == old_size) {
 #if ZEND_DEBUG
 			zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
@@ -1929,6 +1938,10 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
 		size_t real_size = size;

 		size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
+		if (UNEXPECTED(size < real_size)) {
+			zend_mm_delayed_error_or_panic(heap->fallible, "Possible integer overflow in memory allocation (%zu + %zu)", ZEND_MM_ALIGNED_SIZE(real_size), ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
+			return NULL;
+		}
 #endif

 		ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
diff --git a/Zend/zend_string.c b/Zend/zend_string.c
index daf952e4af0..d04bf4b4fa3 100644
--- a/Zend/zend_string.c
+++ b/Zend/zend_string.c
@@ -27,6 +27,10 @@
 # include <sanitizer/msan_interface.h>
 #endif

+#ifdef __SANITIZE_ADDRESS__
+# include <sanitizer/asan_interface.h>
+#endif
+
 ZEND_API zend_new_interned_string_func_t zend_new_interned_string;
 ZEND_API zend_string_init_interned_func_t zend_string_init_interned;
 ZEND_API zend_string_init_existing_interned_func_t zend_string_init_existing_interned;
@@ -435,11 +439,29 @@ ZEND_API bool ZEND_FASTCALL I_REPLACE_SONAME_FNNAME_ZU(NONE,zend_string_equal_va
 {
 	return !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1));
 }
+
+#ifdef __SANITIZE_ADDRESS__
+/* Inline asm in zend_string_equal_val() is not instrumented by ASan, so check
+ * explicitly that the aligned buffer the asm reads up to is addressable. */
+static zend_always_inline void zend_string_asan_check_aligned(const zend_string *s)
+{
+	size_t aligned_size = ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(ZSTR_LEN(s)));
+	void *poisoned = __asan_region_is_poisoned((void *)s, aligned_size);
+	if (UNEXPECTED(poisoned)) {
+		__asan_describe_address(poisoned);
+		abort();
+	}
+}
+#endif
 #endif

 #if defined(__GNUC__) && defined(__i386__)
 ZEND_API zend_never_inline NOIPA bool ZEND_FASTCALL zend_string_equal_val(const zend_string *s1, const zend_string *s2)
 {
+#ifdef __SANITIZE_ADDRESS__
+	zend_string_asan_check_aligned(s1);
+	zend_string_asan_check_aligned(s2);
+#endif
 	const char *ptr = ZSTR_VAL(s1);
 	uintptr_t delta = (uintptr_t) s2 - (uintptr_t) s1;
 	size_t len = ZSTR_LEN(s1);
@@ -478,6 +500,10 @@ ZEND_API zend_never_inline NOIPA bool ZEND_FASTCALL zend_string_equal_val(const
 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(__ILP32__)
 ZEND_API zend_never_inline NOIPA bool ZEND_FASTCALL zend_string_equal_val(const zend_string *s1, const zend_string *s2)
 {
+#ifdef __SANITIZE_ADDRESS__
+	zend_string_asan_check_aligned(s1);
+	zend_string_asan_check_aligned(s2);
+#endif
 	const char *ptr = ZSTR_VAL(s1);
 	uintptr_t delta = (uintptr_t) s2 - (uintptr_t) s1;
 	size_t len = ZSTR_LEN(s1);
diff --git a/Zend/zend_string.h b/Zend/zend_string.h
index 5bd805aa8dd..91edd5b5ba1 100644
--- a/Zend/zend_string.h
+++ b/Zend/zend_string.h
@@ -131,6 +131,16 @@ static zend_always_inline zend_string *ZSTR_KNOWN(size_t idx) {
 #define ZSTR_MAX_OVERHEAD (ZEND_MM_ALIGNED_SIZE(_ZSTR_HEADER_SIZE + 1))
 #define ZSTR_MAX_LEN (SIZE_MAX - ZSTR_MAX_OVERHEAD)

+/* Offset for safe_*alloc(n, m, offset) to size the string like zend_string_alloc(n * m + l). */
+static zend_always_inline size_t zend_string_safe_offset(size_t n, size_t m, size_t l)
+{
+	if (UNEXPECTED(l > ZSTR_MAX_LEN)) {
+		return SIZE_MAX; /* Rejected by the allocator. */
+	}
+	/* Works even if n * m wraps, allocator detects that overflow. */
+	return ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(n * m + l)) - n * m;
+}
+
 #define ZSTR_ALLOCA_ALLOC(str, _len, use_heap) do { \
 	(str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE_EX(_ZSTR_STRUCT_SIZE(_len), 8), (use_heap)); \
 	GC_SET_REFCOUNT(str, 1); \
@@ -209,10 +219,12 @@ static zend_always_inline zend_string *zend_string_alloc(size_t len, bool persis
 /* Fallible variant of zend_string_safe_alloc */
 static zend_always_inline zend_string *zend_string_safe_falloc(size_t n, size_t m, size_t l, bool persistent, bool fallible)
 {
-	zend_string *ret = (zend_string *)safe_fpemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent, fallible);
+	zend_string *ret = (zend_string *)safe_fpemalloc(n, m, zend_string_safe_offset(n, m, l), persistent, fallible);
 	if (ZEND_CONST_COND(fallible, true) && UNEXPECTED(ret == NULL)) {
 		return ret;
 	}
+	/* GCC can't see that allocation fails if l is too large, so make it explicit. */
+	ZEND_ASSERT(l <= ZSTR_MAX_LEN);

 	GC_SET_REFCOUNT(ret, 1);
 	GC_TYPE_INFO(ret) = GC_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT);
@@ -394,10 +406,12 @@ static zend_always_inline zend_string *zend_string_safe_frealloc(zend_string *s,

 	if (!ZSTR_IS_INTERNED(s)) {
 		if (GC_REFCOUNT(s) == 1) {
-			ret = (zend_string *)safe_fperealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent, fallible);
+			ret = (zend_string *)safe_fperealloc(s, n, m, zend_string_safe_offset(n, m, l), persistent, fallible);
 			if (ZEND_CONST_COND(fallible, true) && !ret) {
 				return NULL;
 			}
+			/* GCC can't see that allocation fails if l is too large, so make it explicit. */
+			ZEND_ASSERT(l <= ZSTR_MAX_LEN);
 			ZSTR_LEN(ret) = (n * m) + l;
 			zend_string_forget_hash_val(ret);
 			return ret;
diff --git a/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h b/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h
index e71737318bf..594e2f3ebd2 100644
--- a/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h
+++ b/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h
@@ -155,7 +155,7 @@ typedef zend_string* (*mb_cut_fn)(unsigned char *str, size_t from, size_t len, u
 static inline void mb_convert_buf_init(mb_convert_buf *buf, size_t initsize, uint32_t repl_char, unsigned int err_mode)
 {
 	buf->state = buf->errors = 0;
-	buf->str = emalloc(_ZSTR_STRUCT_SIZE(initsize));
+	buf->str = emalloc(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(initsize)));
 	buf->out = (unsigned char*)ZSTR_VAL(buf->str);
 	buf->limit = buf->out + initsize;
 	buf->replacement_char = repl_char;
@@ -167,16 +167,18 @@ static inline zend_result mb_convert_buf_ensure(mb_convert_buf *buf, unsigned ch
 	ZEND_ASSERT(*out <= *limit);
 	if ((size_t)(*limit - *out) < needed) {
 		size_t oldsize = *limit - (unsigned char*)ZSTR_VAL((buf)->str);
-		size_t newsize = oldsize + MAX(oldsize >> 1, needed);
-		zend_string *newstr = safe_ferealloc((buf)->str, 1, newsize, _ZSTR_STRUCT_SIZE(0), /* fallible */ true);
+		size_t growth = MAX(oldsize >> 1, needed);
+		zend_string *newstr = safe_ferealloc((buf)->str, 1, oldsize, zend_string_safe_offset(1, oldsize, growth), /* fallible */ true);
 		if (!newstr) {
 			/* An earlier growth may have moved the buffer. */
 			buf->out = *out;
 			buf->limit = *limit;
 			return FAILURE;
 		}
+		/* GCC can't see that allocation fails if growth is too large, so make it explicit. */
+		ZEND_ASSERT(growth <= ZSTR_MAX_LEN);
 		*out = (unsigned char*)ZSTR_VAL(newstr) + (*out - (unsigned char*)ZSTR_VAL((buf)->str));
-		*limit = (unsigned char*)ZSTR_VAL(newstr) + newsize;
+		*limit = (unsigned char*)ZSTR_VAL(newstr) + oldsize + growth;
 		(buf)->str = newstr;
 	}
 	return SUCCESS;
diff --git a/ext/standard/tests/strings/chunk_split_variation2_32bit.phpt b/ext/standard/tests/strings/chunk_split_variation2_32bit.phpt
index c83a37f00ed..1f378a739d5 100644
--- a/ext/standard/tests/strings/chunk_split_variation2_32bit.phpt
+++ b/ext/standard/tests/strings/chunk_split_variation2_32bit.phpt
@@ -16,4 +16,4 @@
 --EXPECTF--
 *** Testing chunk_split() : unexpected large 'end' string argument variation 2 ***

-Fatal error: Possible integer overflow in memory allocation (65537 * 65537 + %r65556|65560%r) in %s on line %d
+Fatal error: Possible integer overflow in memory allocation (65537 * 65537 + %d) in %s on line %d