]> git.pld-linux.org Git - packages/php.git/blame - suhosin.patch
- 5.3.1RC1-0.9.8:
[packages/php.git] / suhosin.patch
CommitLineData
65be62f2
ER
1diff -Nura php-5.3.1RC1/Zend/Makefile.am suhosin-patch-5.3.1RC1-0.9.8/Zend/Makefile.am
2--- php-5.3.1RC1/Zend/Makefile.am 2009-03-18 11:18:10.000000000 +0100
3+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/Makefile.am 2009-09-27 19:04:06.000000000 +0200
4@@ -17,7 +17,7 @@
5 zend_objects_API.c zend_ts_hash.c zend_stream.c \
6 zend_default_classes.c \
7 zend_iterators.c zend_interfaces.c zend_exceptions.c \
8- zend_strtod.c zend_closures.c zend_float.c
9+ zend_strtod.c zend_closures.c zend_float.c zend_canary.c zend_alloc_canary.c
10
11 libZend_la_LDFLAGS =
12 libZend_la_LIBADD = @ZEND_EXTRA_LIBS@
13diff -Nura php-5.3.1RC1/Zend/Zend.dsp suhosin-patch-5.3.1RC1-0.9.8/Zend/Zend.dsp
14--- php-5.3.1RC1/Zend/Zend.dsp 2009-03-18 11:18:10.000000000 +0100
15+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/Zend.dsp 2009-09-27 19:04:06.000000000 +0200
16@@ -247,6 +247,14 @@
17 # End Source File
18 # Begin Source File
19
20+SOURCE=.\zend_canary.c
21+# End Source File
22+# Begin Source File
23+
24+SOURCE=.\zend_alloc_canary.c
25+# End Source File
26+# Begin Source File
27+
28 SOURCE=.\zend_ts_hash.c
29 # End Source File
30 # Begin Source File
31diff -Nura php-5.3.1RC1/Zend/ZendTS.dsp suhosin-patch-5.3.1RC1-0.9.8/Zend/ZendTS.dsp
32--- php-5.3.1RC1/Zend/ZendTS.dsp 2008-07-14 11:49:03.000000000 +0200
33+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/ZendTS.dsp 2009-09-27 19:04:06.000000000 +0200
34@@ -277,6 +277,14 @@
35 # End Source File
36 # Begin Source File
37
38+SOURCE=.\zend_canary.c
39+# End Source File
40+# Begin Source File
41+
42+SOURCE=.\zend_alloc_canary.c
43+# End Source File
44+# Begin Source File
45+
46 SOURCE=.\zend_ts_hash.c
47 # End Source File
48 # Begin Source File
49diff -Nura php-5.3.1RC1/Zend/zend.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend.c
50--- php-5.3.1RC1/Zend/zend.c 2009-06-16 18:10:15.000000000 +0200
51+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend.c 2009-09-27 19:04:06.000000000 +0200
52@@ -60,6 +60,10 @@
53 ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
54 ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
55
56+#if SUHOSIN_PATCH
57+ZEND_API void (*zend_suhosin_log)(int loglevel, char *fmt, ...);
58+#endif
59+
60 void (*zend_on_timeout)(int seconds TSRMLS_DC);
61
62 static void (*zend_message_dispatcher_p)(long message, void *data TSRMLS_DC);
63@@ -88,6 +92,74 @@
64 }
65 /* }}} */
66
67+#if SUHOSIN_PATCH
68+static ZEND_INI_MH(OnUpdateSuhosin_log_syslog)
69+{
70+ if (!new_value) {
71+ SPG(log_syslog) = S_ALL & ~S_SQL | S_MEMORY;
72+ } else {
73+ SPG(log_syslog) = atoi(new_value) | S_MEMORY;
74+ }
75+ return SUCCESS;
76+}
77+static ZEND_INI_MH(OnUpdateSuhosin_log_syslog_facility)
78+{
79+ if (!new_value) {
80+ SPG(log_syslog_facility) = LOG_USER;
81+ } else {
82+ SPG(log_syslog_facility) = atoi(new_value);
83+ }
84+ return SUCCESS;
85+}
86+static ZEND_INI_MH(OnUpdateSuhosin_log_syslog_priority)
87+{
88+ if (!new_value) {
89+ SPG(log_syslog_priority) = LOG_ALERT;
90+ } else {
91+ SPG(log_syslog_priority) = atoi(new_value);
92+ }
93+ return SUCCESS;
94+}
95+static ZEND_INI_MH(OnUpdateSuhosin_log_sapi)
96+{
97+ if (!new_value) {
98+ SPG(log_sapi) = S_ALL & ~S_SQL;
99+ } else {
100+ SPG(log_sapi) = atoi(new_value);
101+ }
102+ return SUCCESS;
103+}
104+static ZEND_INI_MH(OnUpdateSuhosin_log_script)
105+{
106+ if (!new_value) {
107+ SPG(log_script) = S_ALL & ~S_MEMORY;
108+ } else {
109+ SPG(log_script) = atoi(new_value) & (~S_MEMORY) & (~S_INTERNAL);
110+ }
111+ return SUCCESS;
112+}
113+static ZEND_INI_MH(OnUpdateSuhosin_log_scriptname)
114+{
115+ if (SPG(log_scriptname)) {
116+ pefree(SPG(log_scriptname),1);
117+ }
118+ SPG(log_scriptname) = NULL;
119+ if (new_value) {
120+ SPG(log_scriptname) = pestrdup(new_value,1);
121+ }
122+ return SUCCESS;
123+}
124+static ZEND_INI_MH(OnUpdateSuhosin_log_phpscript)
125+{
126+ if (!new_value) {
127+ SPG(log_phpscript) = S_ALL & ~S_MEMORY;
128+ } else {
129+ SPG(log_phpscript) = atoi(new_value) & (~S_MEMORY) & (~S_INTERNAL);
130+ }
131+ return SUCCESS;
132+}
133+#endif
134+
135 ZEND_INI_BEGIN()
136 ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting)
137 STD_ZEND_INI_BOOLEAN("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, gc_enabled, zend_gc_globals, gc_globals)
138diff -Nura php-5.3.1RC1/Zend/zend.h suhosin-patch-5.3.1RC1-0.9.8/Zend/zend.h
139--- php-5.3.1RC1/Zend/zend.h 2009-08-06 03:33:54.000000000 +0200
140+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend.h 2009-09-27 19:04:06.000000000 +0200
141@@ -627,6 +627,9 @@
142 extern int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
143 extern ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
144 extern ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
145+#if SUHOSIN_PATCH
146+extern ZEND_API void (*zend_suhosin_log)(int loglevel, char *fmt, ...);
147+#endif
148
149 ZEND_API void zend_error(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
150
151@@ -766,6 +769,14 @@
152 ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current TSRMLS_DC);
153 ZEND_API void zend_restore_error_handling(zend_error_handling *saved TSRMLS_DC);
154
155+#if SUHOSIN_PATCH
156+#include "suhosin_globals.h"
157+#include "suhosin_patch.h"
158+#include "php_syslog.h"
159+
160+ZEND_API size_t zend_canary();
161+#endif
162+
163 #endif /* ZEND_H */
164
165 /*
166diff -Nura php-5.3.1RC1/Zend/zend_alloc.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_alloc.c
167--- php-5.3.1RC1/Zend/zend_alloc.c 2009-09-03 16:33:11.000000000 +0200
168+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_alloc.c 2009-09-27 19:08:35.000000000 +0200
169@@ -18,7 +18,7 @@
170 +----------------------------------------------------------------------+
171 */
172
173-/* $Id$ */
174+/* $Id$ */
175
176 #include "zend.h"
177 #include "zend_alloc.h"
178@@ -32,6 +32,10 @@
179 # include <unistd.h>
180 #endif
181
182+#if SUHOSIN_PATCH
183+#include "suhosin_patch.h"
184+#endif
185+
186 #ifdef ZEND_WIN32
187 # include <wincrypt.h>
188 # include <process.h>
189@@ -59,6 +63,7 @@
190 # define PTR_FMT "0x%0.8lx"
191 #endif
192
193+#ifndef SUHOSIN_MM_CLONE_FILE
194 #if ZEND_DEBUG
195 void zend_debug_alloc_output(char *format, ...)
196 {
197@@ -76,6 +81,7 @@
198 #endif
199 }
200 #endif
201+#endif
202
203 #if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
204 static void zend_mm_panic(const char *message) __attribute__ ((noreturn));
205@@ -324,13 +330,28 @@
206 #define MEM_BLOCK_GUARD 0x2A8FCC84
207 #define MEM_BLOCK_LEAK 0x6C5E8F2D
208
209+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
210+# define CANARY_SIZE sizeof(size_t)
211+#else
212+# define CANARY_SIZE 0
213+#endif
214+
215 /* mm block type */
216 typedef struct _zend_mm_block_info {
217 #if ZEND_MM_COOKIES
218 size_t _cookie;
219 #endif
220- size_t _size;
221- size_t _prev;
222+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
223+ size_t canary_1;
224+#endif
225+ size_t _size;
226+ size_t _prev;
227+#if SUHOSIN_PATCH
228+ size_t size;
229+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
230+ size_t canary_2;
231+#endif
232+#endif
233 } zend_mm_block_info;
234
235 #if ZEND_DEBUG
236@@ -404,7 +425,7 @@
237 # define ZEND_MM_CACHE_STAT 0
238 #endif
239
240-struct _zend_mm_heap {
241+typedef struct _zend_mm_heap {
242 int use_zend_alloc;
243 void *(*_malloc)(size_t);
244 void (*_free)(void*);
245@@ -439,6 +460,9 @@
246 int miss;
247 } cache_stat[ZEND_MM_NUM_BUCKETS+1];
248 #endif
249+#if SUHOSIN_PATCH
250+ size_t canary_1,canary_2,canary_3;
251+#endif
252 };
253
254 #define ZEND_MM_SMALL_FREE_BUCKET(heap, index) \
255@@ -512,18 +536,31 @@
256 /* optimized access */
257 #define ZEND_MM_FREE_BLOCK_SIZE(b) (b)->info._size
258
259+#ifndef ZEND_MM_ALIGNMENT
260+# define ZEND_MM_ALIGNMENT 8
261+# define ZEND_MM_ALIGNMENT_LOG2 3
262+#elif ZEND_MM_ALIGNMENT < 4
263+# undef ZEND_MM_ALIGNMENT
264+# undef ZEND_MM_ALIGNMENT_LOG2
265+# define ZEND_MM_ALIGNMENT 4
266+# define ZEND_MM_ALIGNMENT_LOG2 2
267+#endif
268+
269+#define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
270+
271 /* Aligned header size */
272+#define ZEND_MM_ALIGNED_SIZE(size) ((size + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
273 #define ZEND_MM_ALIGNED_HEADER_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block))
274 #define ZEND_MM_ALIGNED_FREE_HEADER_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_small_free_block))
275-#define ZEND_MM_MIN_ALLOC_BLOCK_SIZE ZEND_MM_ALIGNED_SIZE(ZEND_MM_ALIGNED_HEADER_SIZE + END_MAGIC_SIZE)
276+#define ZEND_MM_MIN_ALLOC_BLOCK_SIZE ZEND_MM_ALIGNED_SIZE(ZEND_MM_ALIGNED_HEADER_SIZE + END_MAGIC_SIZE + CANARY_SIZE)
277 #define ZEND_MM_ALIGNED_MIN_HEADER_SIZE (ZEND_MM_MIN_ALLOC_BLOCK_SIZE>ZEND_MM_ALIGNED_FREE_HEADER_SIZE?ZEND_MM_MIN_ALLOC_BLOCK_SIZE:ZEND_MM_ALIGNED_FREE_HEADER_SIZE)
278 #define ZEND_MM_ALIGNED_SEGMENT_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_segment))
279
280-#define ZEND_MM_MIN_SIZE ((ZEND_MM_ALIGNED_MIN_HEADER_SIZE>(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE))?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE-(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE)):0)
281+#define ZEND_MM_MIN_SIZE ((ZEND_MM_ALIGNED_MIN_HEADER_SIZE>(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE))?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE-(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE)):0)
282
283 #define ZEND_MM_MAX_SMALL_SIZE ((ZEND_MM_NUM_BUCKETS<<ZEND_MM_ALIGNMENT_LOG2)+ZEND_MM_ALIGNED_MIN_HEADER_SIZE)
284
285-#define ZEND_MM_TRUE_SIZE(size) ((size<ZEND_MM_MIN_SIZE)?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE):(ZEND_MM_ALIGNED_SIZE(size+ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE)))
286+#define ZEND_MM_TRUE_SIZE(size) ((size<ZEND_MM_MIN_SIZE)?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE):(ZEND_MM_ALIGNED_SIZE(size+ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE)))
287
288 #define ZEND_MM_BUCKET_INDEX(true_size) ((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))
289
290@@ -585,6 +622,44 @@
291
292 #endif
293
294+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
295+
296+# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION) do { \
297+ char *p = SUHOSIN_MM_END_CANARY_PTR(block); size_t check; \
298+ if (((block)->info.canary_1 != heap->canary_1) || ((block)->info.canary_2 != heap->canary_2)) { \
299+ canary_mismatch: \
300+ zend_suhosin_log(S_MEMORY, "canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
301+ if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { (block)->info.canary_1 = heap->canary_1; (block)->info.canary_2 = heap->canary_2; }\
302+ } \
303+ memcpy(&check, p, CANARY_SIZE); \
304+ if (check != heap->canary_3) { \
305+ zend_suhosin_log(S_MEMORY, "end canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
306+ if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { memcpy(p, heap->canary_3, CANARY_SIZE); } \
307+ } \
308+ } while (0)
309+
310+# define SUHOSIN_MM_SET_CANARIES(block) do { \
311+ (block)->info.canary_1 = heap->canary_1; \
312+ (block)->info.canary_2 = heap->canary_2; \
313+ } while (0)
314+
315+# define SUHOSIN_MM_END_CANARY_PTR(block) \
316+ (char *)(((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block*)(block))->info.size + END_MAGIC_SIZE)
317+
318+# define SUHOSIN_MM_SET_END_CANARY(block) do { \
319+ char *p = SUHOSIN_MM_END_CANARY_PTR(block); \
320+ memcpy(p, &heap->canary_3, CANARY_SIZE); \
321+ } while (0)
322+
323+#else
324+
325+# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION)
326+# define SUHOSIN_MM_SET_CANARIES(block)
327+# define SUHOSIN_MM_END_CANARY_PTR(block)
328+# define SUHOSIN_MM_SET_END_CANARY(block)
329+
330+#endif
331+
332
333 #if ZEND_MM_HEAP_PROTECTION
334
335@@ -707,7 +782,7 @@
336 #endif
337 }
338
339-static inline void zend_mm_add_to_rest_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
340+static void zend_mm_add_to_rest_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
341 {
342 zend_mm_free_block *prev, *next;
343
344@@ -724,7 +799,7 @@
345 prev->next_free_block = next->prev_free_block = mm_block;
346 }
347
348-static inline void zend_mm_add_to_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
349+static void zend_mm_add_to_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
350 {
351 size_t size;
352 size_t index;
353@@ -785,7 +860,7 @@
354 }
355 }
356
357-static inline void zend_mm_remove_from_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
358+static void zend_mm_remove_from_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
359 {
360 zend_mm_free_block *prev = mm_block->prev_free_block;
361 zend_mm_free_block *next = mm_block->next_free_block;
362@@ -795,6 +870,12 @@
363 if (EXPECTED(prev == mm_block)) {
364 zend_mm_free_block **rp, **cp;
365
366+#if SUHOSIN_PATCH
367+ if (next != mm_block) {
368+ zend_suhosin_log(S_MEMORY, "zend_mm_heap corrupted at %p", mm_block);
369+ _exit(1);
370+ }
371+#endif
372 #if ZEND_MM_SAFE_UNLINKING
373 if (UNEXPECTED(next != mm_block)) {
374 zend_mm_panic("zend_mm_heap corrupted");
375@@ -833,6 +914,13 @@
376 }
377 } else {
378
379+#if SUHOSIN_PATCH
380+ if (prev->next_free_block != mm_block || next->prev_free_block != mm_block) {
381+ zend_suhosin_log(S_MEMORY, "zend_mm_head corrupted at %p", mm_block);
382+ _exit(1);
383+ }
384+#endif
385+
386 #if ZEND_MM_SAFE_UNLINKING
387 if (UNEXPECTED(prev->next_free_block != mm_block) || UNEXPECTED(next->prev_free_block != mm_block)) {
388 zend_mm_panic("zend_mm_heap corrupted");
389@@ -856,7 +944,7 @@
390 }
391 }
392
393-static inline void zend_mm_init(zend_mm_heap *heap)
394+static void zend_mm_init(zend_mm_heap *heap)
395 {
396 zend_mm_free_block* p;
397 int i;
398@@ -880,6 +968,13 @@
399 heap->large_free_buckets[i] = NULL;
400 }
401 heap->rest_buckets[0] = heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(heap);
402+#if SUHOSIN_PATCH
403+ if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
404+ heap->canary_1 = zend_canary();
405+ heap->canary_2 = zend_canary();
406+ heap->canary_3 = zend_canary();
407+ }
408+#endif
409 }
410
411 static void zend_mm_del_segment(zend_mm_heap *heap, zend_mm_segment *segment)
412@@ -988,11 +1083,16 @@
413 }
414 #endif
415
416+
417 /* Notes:
418 * - This function may alter the block_sizes values to match platform alignment
419 * - This function does *not* perform sanity checks on the arguments
420 */
421-ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
422+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
423+zend_mm_heap *__zend_mm_startup_canary_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
424+#else
425+static zend_mm_heap *__zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
426+#endif
427 {
428 zend_mm_storage *storage;
429 zend_mm_heap *heap;
430@@ -1062,12 +1162,12 @@
431 heap->reserve = NULL;
432 heap->reserve_size = reserve_size;
433 if (reserve_size > 0) {
434- heap->reserve = _zend_mm_alloc_int(heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
435+ heap->reserve = _zend_mm_alloc(heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
436 }
437 if (internal) {
438 int i;
439 zend_mm_free_block *p, *q, *orig;
440- zend_mm_heap *mm_heap = _zend_mm_alloc_int(heap, sizeof(zend_mm_heap) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
441+ zend_mm_heap *mm_heap = _zend_mm_alloc(heap, sizeof(zend_mm_heap) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
442
443 *mm_heap = *heap;
444
445@@ -1098,7 +1198,11 @@
446 return heap;
447 }
448
449-ZEND_API zend_mm_heap *zend_mm_startup(void)
450+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
451+zend_mm_heap *__zend_mm_startup_canary(void)
452+#else
453+static zend_mm_heap *__zend_mm_startup(void)
454+#endif
455 {
456 int i;
457 size_t seg_size;
458@@ -1152,6 +1256,27 @@
459 return heap;
460 }
461
462+#ifndef SUHOSIN_MM_CLONE_FILE
463+zend_mm_heap_canary *__zend_mm_startup_canary_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params);
464+zend_mm_heap_canary *__zend_mm_startup_canary(void);
465+
466+ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
467+{
468+ if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
469+ return (zend_mm_heap *)__zend_mm_startup_canary_ex(handlers, block_size, reserve_size, internal, params);
470+ }
471+ return __zend_mm_startup_ex(handlers, block_size, reserve_size, internal, params);
472+}
473+ZEND_API zend_mm_heap *zend_mm_startup(void)
474+{
475+ if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
476+ return (zend_mm_heap *)__zend_mm_startup_canary();
477+ }
478+ return __zend_mm_startup();
479+}
480+
481+#endif
482+
483 #if ZEND_DEBUG
484 static long zend_mm_find_leaks(zend_mm_segment *segment, zend_mm_block *b)
485 {
486@@ -1520,7 +1645,11 @@
487 }
488 #endif
489
490-ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
491+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
492+void __zend_mm_shutdown_canary(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
493+#else
494+static void __zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
495+#endif
496 {
497 zend_mm_storage *storage;
498 zend_mm_segment *segment;
499@@ -1530,7 +1659,7 @@
500 if (heap->reserve) {
501 #if ZEND_DEBUG
502 if (!silent) {
503- _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
504+ _zend_mm_free(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
505 }
506 #endif
507 heap->reserve = NULL;
508@@ -1613,12 +1742,23 @@
509 heap->size = 0;
510 heap->peak = 0;
511 if (heap->reserve_size) {
512- heap->reserve = _zend_mm_alloc_int(heap, heap->reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
513+ heap->reserve = _zend_mm_alloc(heap, heap->reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
514 }
515 heap->overflow = 0;
516 }
517 }
518
519+#ifndef SUHOSIN_MM_CLONE_FILE
520+ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
521+{
522+ if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
523+ __zend_mm_shutdown_canary(heap, full_shutdown, silent TSRMLS_CC);
524+ return;
525+ }
526+ __zend_mm_shutdown(heap, full_shutdown, silent TSRMLS_CC);
527+}
528+#endif
529+
530 static void zend_mm_safe_error(zend_mm_heap *heap,
531 const char *format,
532 size_t limit,
533@@ -1629,7 +1769,11 @@
534 size_t size)
535 {
536 if (heap->reserve) {
537+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
538+ _zend_mm_free_canary_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
539+#else
540 _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
541+#endif
542 heap->reserve = NULL;
543 }
544 if (heap->overflow == 0) {
545@@ -1752,6 +1896,9 @@
546 return best_fit->next_free_block;
547 }
548
549+#if SUHOSIN_PATCH
550+void *_zend_mm_alloc_canary_int(zend_mm_heap_canary *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
551+#endif
552 static void *_zend_mm_alloc_int(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
553 {
554 zend_mm_free_block *best_fit;
555@@ -1761,7 +1908,7 @@
556 size_t segment_size;
557 zend_mm_segment *segment;
558 int keep_rest = 0;
559-
560+
561 if (EXPECTED(ZEND_MM_SMALL_SIZE(true_size))) {
562 size_t index = ZEND_MM_BUCKET_INDEX(true_size);
563 size_t bitmap;
564@@ -1779,6 +1926,11 @@
565 best_fit = heap->cache[index];
566 heap->cache[index] = best_fit->prev_free_block;
567 heap->cached -= true_size;
568+#if SUHOSIN_PATCH
569+ SUHOSIN_MM_SET_CANARIES(best_fit);
570+ ((zend_mm_block*)best_fit)->info.size = size;
571+ SUHOSIN_MM_SET_END_CANARY(best_fit);
572+#endif
573 ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
574 ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
575 return ZEND_MM_DATA_OF(best_fit);
576@@ -1918,13 +2070,19 @@
577
578 ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 1);
579
580+#if SUHOSIN_PATCH
581+ SUHOSIN_MM_SET_CANARIES(best_fit);
582+ ((zend_mm_block*)best_fit)->info.size = size;
583+ SUHOSIN_MM_SET_END_CANARY(best_fit);
584+#endif
585+
586 heap->size += true_size;
587 if (heap->peak < heap->size) {
588 heap->peak = heap->size;
589 }
590
591 HANDLE_UNBLOCK_INTERRUPTIONS();
592-
593+
594 return ZEND_MM_DATA_OF(best_fit);
595 }
596
597@@ -1941,12 +2099,19 @@
598
599 mm_block = ZEND_MM_HEADER_OF(p);
600 size = ZEND_MM_BLOCK_SIZE(mm_block);
601+#if SUHOSIN_PATCH
602+ SUHOSIN_MM_CHECK_CANARIES(mm_block, "efree()");
603+#endif
604 ZEND_MM_CHECK_PROTECTION(mm_block);
605
606 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
607 memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->debug.size);
608 #endif
609-
610+#if SUHOSIN_PATCH
611+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY))) {
612+ memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->info.size);
613+ }
614+#endif
615 #if ZEND_MM_CACHE
616 if (EXPECTED(ZEND_MM_SMALL_SIZE(size)) && EXPECTED(heap->cached < ZEND_MM_CACHE_SIZE)) {
617 size_t index = ZEND_MM_BUCKET_INDEX(size);
618@@ -1989,6 +2154,9 @@
619 HANDLE_UNBLOCK_INTERRUPTIONS();
620 }
621
622+#if SUHOSIN_PATCH
623+void *_zend_mm_realloc_canary_int(zend_mm_heap_canary *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
624+#endif
625 static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
626 {
627 zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
628@@ -1998,11 +2166,18 @@
629 void *ptr;
630
631 if (UNEXPECTED(!p) || !ZEND_MM_VALID_PTR(p)) {
632+#ifdef SUHOSIN_MM_WITH_CANARY_PROTECTION
633+ return _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
634+#else
635 return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
636+#endif
637 }
638 mm_block = ZEND_MM_HEADER_OF(p);
639 true_size = ZEND_MM_TRUE_SIZE(size);
640 orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
641+#if SUHOSIN_PATCH
642+ SUHOSIN_MM_CHECK_CANARIES(mm_block, "erealloc()");
643+#endif
644 ZEND_MM_CHECK_PROTECTION(mm_block);
645
646 if (UNEXPECTED(true_size < size)) {
647@@ -2034,6 +2209,11 @@
648 HANDLE_UNBLOCK_INTERRUPTIONS();
649 }
650 ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
651+#if SUHOSIN_PATCH
652+ SUHOSIN_MM_SET_CANARIES(mm_block);
653+ ((zend_mm_block*)mm_block)->info.size = size;
654+ SUHOSIN_MM_SET_END_CANARY(mm_block);
655+#endif
656 return p;
657 }
658
659@@ -2052,14 +2232,19 @@
660 best_fit = heap->cache[index];
661 heap->cache[index] = best_fit->prev_free_block;
662 ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
663- ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
664-
665+ ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
666+#if SUHOSIN_PATCH
667+ SUHOSIN_MM_SET_CANARIES(best_fit);
668+ ((zend_mm_block*)best_fit)->info.size = size;
669+ SUHOSIN_MM_SET_END_CANARY(best_fit);
670+#endif
671+
672 ptr = ZEND_MM_DATA_OF(best_fit);
673
674 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
675 memcpy(ptr, p, mm_block->debug.size);
676 #else
677- memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
678+ memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
679 #endif
680
681 heap->cached -= true_size - orig_size;
682@@ -2075,7 +2260,6 @@
683 heap->cache_stat[index].max_count = heap->cache_stat[index].count;
684 }
685 #endif
686-
687 return ptr;
688 }
689 }
690@@ -2118,6 +2302,11 @@
691 heap->peak = heap->size;
692 }
693 HANDLE_UNBLOCK_INTERRUPTIONS();
694+#if SUHOSIN_PATCH
695+ SUHOSIN_MM_SET_CANARIES(mm_block);
696+ ((zend_mm_block*)mm_block)->info.size = size;
697+ SUHOSIN_MM_SET_END_CANARY(mm_block);
698+#endif
699 return p;
700 } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
701 ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(next_block, ZEND_MM_FREE_BLOCK_SIZE(next_block)))) {
702@@ -2220,38 +2409,90 @@
703 }
704
705 HANDLE_UNBLOCK_INTERRUPTIONS();
706+#if SUHOSIN_PATCH
707+ SUHOSIN_MM_SET_CANARIES(mm_block);
708+ ((zend_mm_block*)mm_block)->info.size = size;
709+ SUHOSIN_MM_SET_END_CANARY(mm_block);
710+#endif
711 return ZEND_MM_DATA_OF(mm_block);
712 }
713
714+#ifdef SUHOSIN_MM_WITH_CANARY_PROTECTION
715+ ptr = _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
716+#else
717 ptr = _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
718+#endif
719 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
720 memcpy(ptr, p, mm_block->debug.size);
721 #else
722- memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
723+ memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
724 #endif
725+#ifdef SUHOSIN_MM_WITH_CANARY_PROTECTION
726+ _zend_mm_free_canary_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
727+#else
728 _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
729+#endif
730 return ptr;
731 }
732
733+#ifndef SUHOSIN_MM_CLONE_FILE
734 ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
735 {
736- return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
737+#if SUHOSIN_PATCH
738+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
739+#endif
740+ return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
741+#if SUHOSIN_PATCH
742+ return _zend_mm_alloc_canary_int((zend_mm_heap_canary *)heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
743+#endif
744 }
745
746 ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
747 {
748- _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
749+#if SUHOSIN_PATCH
750+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
751+#endif
752+ { _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); return; }
753+#if SUHOSIN_PATCH
754+ _zend_mm_free_canary_int((zend_mm_heap_canary *)heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
755+#endif
756 }
757
758 ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
759 {
760- return _zend_mm_realloc_int(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
761+#if SUHOSIN_PATCH
762+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
763+#endif
764+ return _zend_mm_realloc_int(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
765+#if SUHOSIN_PATCH
766+ return _zend_mm_realloc_canary_int((zend_mm_heap_canary *)heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
767+#endif
768 }
769
770 ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
771 {
772 zend_mm_block *mm_block;
773
774+ if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) != 0) {
775+ return _zend_mm_block_size_canary((zend_mm_heap_canary *)heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
776+ }
777+
778+ if (!ZEND_MM_VALID_PTR(p)) {
779+ return 0;
780+ }
781+ mm_block = ZEND_MM_HEADER_OF(p);
782+ ZEND_MM_CHECK_PROTECTION(mm_block);
783+#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
784+ return mm_block->debug.size;
785+#else
786+ return ZEND_MM_BLOCK_SIZE(mm_block);
787+#endif
788+}
789+#else
790+ZEND_API size_t _zend_mm_block_size_canary(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
791+{
792+ zend_mm_block *mm_block;
793+
794 if (!ZEND_MM_VALID_PTR(p)) {
795 return 0;
796 }
797@@ -2264,6 +2505,8 @@
798 #endif
799 }
800
801+#endif
802+
803 /**********************/
804 /* Allocation Manager */
805 /**********************/
806@@ -2280,6 +2523,7 @@
807 static zend_alloc_globals alloc_globals;
808 #endif
809
810+#ifndef SUHOSIN_MM_CLONE_FILE
811 ZEND_API int is_zend_mm(TSRMLS_D)
812 {
813 return AG(mm_heap)->use_zend_alloc;
814@@ -2292,7 +2536,13 @@
815 if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
816 return AG(mm_heap)->_malloc(size);
817 }
818+#if SUHOSIN_PATCH
819+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
820+#endif
821 return _zend_mm_alloc_int(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
822+#if SUHOSIN_PATCH
823+ return _zend_mm_alloc_canary_int((zend_mm_heap_canary *)AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
824+#endif
825 }
826
827 ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
828@@ -2303,7 +2553,13 @@
829 AG(mm_heap)->_free(ptr);
830 return;
831 }
832- _zend_mm_free_int(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
833+#if SUHOSIN_PATCH
834+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
835+#endif
836+ { _zend_mm_free_int(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); return; }
837+#if SUHOSIN_PATCH
838+ _zend_mm_free_canary_int((zend_mm_heap_canary *)AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
839+#endif
840 }
841
842 ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
843@@ -2313,7 +2569,13 @@
844 if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
845 return AG(mm_heap)->_realloc(ptr, size);
846 }
847+#if SUHOSIN_PATCH
848+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
849+#endif
850 return _zend_mm_realloc_int(AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
851+#if SUHOSIN_PATCH
852+ return _zend_mm_realloc_canary_int((zend_mm_heap_canary *)AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
853+#endif
854 }
855
856 ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
857@@ -2321,8 +2583,15 @@
858 if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
859 return 0;
860 }
861- return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
862+#if SUHOSIN_PATCH
863+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
864+#endif
865+ return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
866+#if SUHOSIN_PATCH
867+ return _zend_mm_block_size_canary((zend_mm_heap_canary *)AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
868+#endif
869 }
870+#endif
871
872 #if defined(__GNUC__) && defined(i386)
873
874@@ -2393,7 +2662,7 @@
875 }
876 #endif
877
878-
879+#ifndef SUHOSIN_MM_CLONE_FILE
880 ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
881 {
882 return emalloc_rel(safe_address(nmemb, size, offset));
883@@ -2506,6 +2775,7 @@
884 {
885 zend_mm_shutdown(AG(mm_heap), full_shutdown, silent TSRMLS_CC);
886 }
887+#endif
888
889 static void alloc_globals_ctor(zend_alloc_globals *alloc_globals TSRMLS_DC)
890 {
891@@ -2530,6 +2800,7 @@
892 }
893 #endif
894
895+#ifndef SUHOSIN_MM_CLONE_FILE
896 ZEND_API void start_memory_manager(TSRMLS_D)
897 {
898 #ifdef ZTS
899@@ -2594,6 +2865,7 @@
900 zend_debug_alloc_output("------------------------------------------------\n");
901 }
902 #endif
903+#endif
904
905 /*
906 * Local variables:
907diff -Nura php-5.3.1RC1/Zend/zend_alloc.h suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_alloc.h
908--- php-5.3.1RC1/Zend/zend_alloc.h 2009-09-03 16:33:11.000000000 +0200
909+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_alloc.h 2009-09-27 19:04:06.000000000 +0200
910@@ -203,6 +203,8 @@
911
912 /* Heap functions */
913 typedef struct _zend_mm_heap zend_mm_heap;
914+typedef struct _zend_mm_heap_canary zend_mm_heap_canary;
915+
916
917 ZEND_API zend_mm_heap *zend_mm_startup(void);
918 ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
919diff -Nura php-5.3.1RC1/Zend/zend_alloc_canary.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_alloc_canary.c
920--- php-5.3.1RC1/Zend/zend_alloc_canary.c 1970-01-01 01:00:00.000000000 +0100
921+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_alloc_canary.c 2009-09-27 19:08:51.000000000 +0200
922@@ -0,0 +1,2443 @@
923+/*
924+ +----------------------------------------------------------------------+
925+ | Suhosin-Patch for PHP |
926+ +----------------------------------------------------------------------+
927+ | Copyright (c) 2004-2009 Stefan Esser |
928+ +----------------------------------------------------------------------+
929+ | This source file is subject to version 2.02 of the PHP license, |
930+ | that is bundled with this package in the file LICENSE, and is |
931+ | available at through the world-wide-web at |
932+ | http://www.php.net/license/2_02.txt. |
933+ | If you did not receive a copy of the PHP license and are unable to |
934+ | obtain it through the world-wide-web, please send a note to |
935+ | license@php.net so we can mail you a copy immediately. |
936+ +----------------------------------------------------------------------+
937+ | Author: Stefan Esser <stefan.esser@sektioneins.de> |
938+ +----------------------------------------------------------------------+
939+ */
940+/* $Id$ */
941+
942+#include "zend.h"
943+#include "zend_alloc.h"
944+#include "zend_globals.h"
945+#include "zend_operators.h"
946+
947+#ifdef HAVE_SIGNAL_H
948+# include <signal.h>
949+#endif
950+#ifdef HAVE_UNISTD_H
951+# include <unistd.h>
952+#endif
953+
954+#if SUHOSIN_PATCH
955+#include "suhosin_patch.h"
956+#endif
957+
958+#ifdef ZEND_WIN32
959+# include <wincrypt.h>
960+# include <process.h>
961+#endif
962+
963+#ifndef ZEND_MM_HEAP_PROTECTION
964+# define ZEND_MM_HEAP_PROTECTION ZEND_DEBUG
965+#endif
966+
967+#ifndef ZEND_MM_SAFE_UNLINKING
968+# define ZEND_MM_SAFE_UNLINKING 1
969+#endif
970+
971+#ifndef ZEND_MM_COOKIES
972+# define ZEND_MM_COOKIES ZEND_DEBUG
973+#endif
974+
975+#ifdef _WIN64
976+# define PTR_FMT "0x%0.16I64x"
977+/*
978+#elif sizeof(long) == 8
979+# define PTR_FMT "0x%0.16lx"
980+*/
981+#else
982+# define PTR_FMT "0x%0.8lx"
983+#endif
984+
985+#define SUHOSIN_MM_WITH_CANARY_PROTECTION 1
986+
987+#if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
988+static void zend_mm_panic(const char *message) __attribute__ ((noreturn));
989+#endif
990+
991+static void zend_mm_panic(const char *message)
992+{
993+ fprintf(stderr, "%s\n", message);
994+#if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
995+ kill(getpid(), SIGSEGV);
996+#endif
997+ exit(1);
998+}
999+
1000+/*******************/
1001+/* Storage Manager */
1002+/*******************/
1003+
1004+#ifdef ZEND_WIN32
1005+# define HAVE_MEM_WIN32 /* use VirtualAlloc() to allocate memory */
1006+#endif
1007+#define HAVE_MEM_MALLOC /* use malloc() to allocate segments */
1008+
1009+#include <sys/types.h>
1010+#include <sys/stat.h>
1011+#if HAVE_LIMITS_H
1012+#include <limits.h>
1013+#endif
1014+#include <fcntl.h>
1015+#include <errno.h>
1016+
1017+#if defined(HAVE_MEM_MMAP_ANON) || defined(HAVE_MEM_MMAP_ZERO)
1018+# ifdef HAVE_MREMAP
1019+# ifndef _GNU_SOURCE
1020+# define _GNU_SOURCE
1021+# endif
1022+# ifndef __USE_GNU
1023+# define __USE_GNU
1024+# endif
1025+# endif
1026+# include <sys/mman.h>
1027+# ifndef MAP_ANON
1028+# ifdef MAP_ANONYMOUS
1029+# define MAP_ANON MAP_ANONYMOUS
1030+# endif
1031+# endif
1032+# ifndef MREMAP_MAYMOVE
1033+# define MREMAP_MAYMOVE 0
1034+# endif
1035+# ifndef MAP_FAILED
1036+# define MAP_FAILED ((void*)-1)
1037+# endif
1038+#endif
1039+
1040+static zend_mm_storage* zend_mm_mem_dummy_init(void *params)
1041+{
1042+ return malloc(sizeof(zend_mm_storage));
1043+}
1044+
1045+static void zend_mm_mem_dummy_dtor(zend_mm_storage *storage)
1046+{
1047+ free(storage);
1048+}
1049+
1050+static void zend_mm_mem_dummy_compact(zend_mm_storage *storage)
1051+{
1052+}
1053+
1054+#if defined(HAVE_MEM_MMAP_ANON) || defined(HAVE_MEM_MMAP_ZERO)
1055+
1056+static zend_mm_segment* zend_mm_mem_mmap_realloc(zend_mm_storage *storage, zend_mm_segment* segment, size_t size)
1057+{
1058+ zend_mm_segment *ret;
1059+#ifdef HAVE_MREMAP
1060+#if defined(__NetBSD__)
1061+ /* NetBSD 5 supports mremap but takes an extra newp argument */
1062+ ret = (zend_mm_segment*)mremap(segment, segment->size, segment, size, MREMAP_MAYMOVE);
1063+#else
1064+ ret = (zend_mm_segment*)mremap(segment, segment->size, size, MREMAP_MAYMOVE);
1065+#endif
1066+ if (ret == MAP_FAILED) {
1067+#endif
1068+ ret = storage->handlers->_alloc(storage, size);
1069+ if (ret) {
1070+ memcpy(ret, segment, size > segment->size ? segment->size : size);
1071+ storage->handlers->_free(storage, segment);
1072+ }
1073+#ifdef HAVE_MREMAP
1074+ }
1075+#endif
1076+ return ret;
1077+}
1078+
1079+static void zend_mm_mem_mmap_free(zend_mm_storage *storage, zend_mm_segment* segment)
1080+{
1081+ munmap((void*)segment, segment->size);
1082+}
1083+
1084+#endif
1085+
1086+#ifdef HAVE_MEM_MMAP_ANON
1087+
1088+static zend_mm_segment* zend_mm_mem_mmap_anon_alloc(zend_mm_storage *storage, size_t size)
1089+{
1090+ zend_mm_segment *ret = (zend_mm_segment*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
1091+ if (ret == MAP_FAILED) {
1092+ ret = NULL;
1093+ }
1094+ return ret;
1095+}
1096+
1097+# define ZEND_MM_MEM_MMAP_ANON_DSC {"mmap_anon", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_mmap_anon_alloc, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
1098+
1099+#endif
1100+
1101+#ifdef HAVE_MEM_MMAP_ZERO
1102+
1103+static int zend_mm_dev_zero_fd = -1;
1104+
1105+static zend_mm_storage* zend_mm_mem_mmap_zero_init(void *params)
1106+{
1107+ if (zend_mm_dev_zero_fd != -1) {
1108+ zend_mm_dev_zero_fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR);
1109+ }
1110+ if (zend_mm_dev_zero_fd >= 0) {
1111+ return malloc(sizeof(zend_mm_storage));
1112+ } else {
1113+ return NULL;
1114+ }
1115+}
1116+
1117+static void zend_mm_mem_mmap_zero_dtor(zend_mm_storage *storage)
1118+{
1119+ close(zend_mm_dev_zero_fd);
1120+ free(storage);
1121+}
1122+
1123+static zend_mm_segment* zend_mm_mem_mmap_zero_alloc(zend_mm_storage *storage, size_t size)
1124+{
1125+ zend_mm_segment *ret = (zend_mm_segment*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zend_mm_dev_zero_fd, 0);
1126+ if (ret == MAP_FAILED) {
1127+ ret = NULL;
1128+ }
1129+ return ret;
1130+}
1131+
1132+# define ZEND_MM_MEM_MMAP_ZERO_DSC {"mmap_zero", zend_mm_mem_mmap_zero_init, zend_mm_mem_mmap_zero_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_mmap_zero_alloc, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
1133+
1134+#endif
1135+
1136+#ifdef HAVE_MEM_WIN32
1137+
1138+static zend_mm_storage* zend_mm_mem_win32_init(void *params)
1139+{
1140+ HANDLE heap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
1141+ zend_mm_storage* storage;
1142+
1143+ if (heap == NULL) {
1144+ return NULL;
1145+ }
1146+ storage = (zend_mm_storage*)malloc(sizeof(zend_mm_storage));
1147+ storage->data = (void*) heap;
1148+ return storage;
1149+}
1150+
1151+static void zend_mm_mem_win32_dtor(zend_mm_storage *storage)
1152+{
1153+ HeapDestroy((HANDLE)storage->data);
1154+ free(storage);
1155+}
1156+
1157+static void zend_mm_mem_win32_compact(zend_mm_storage *storage)
1158+{
1159+ HeapDestroy((HANDLE)storage->data);
1160+ storage->data = (void*)HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
1161+}
1162+
1163+static zend_mm_segment* zend_mm_mem_win32_alloc(zend_mm_storage *storage, size_t size)
1164+{
1165+ return (zend_mm_segment*) HeapAlloc((HANDLE)storage->data, HEAP_NO_SERIALIZE, size);
1166+}
1167+
1168+static void zend_mm_mem_win32_free(zend_mm_storage *storage, zend_mm_segment* segment)
1169+{
1170+ HeapFree((HANDLE)storage->data, HEAP_NO_SERIALIZE, segment);
1171+}
1172+
1173+static zend_mm_segment* zend_mm_mem_win32_realloc(zend_mm_storage *storage, zend_mm_segment* segment, size_t size)
1174+{
1175+ return (zend_mm_segment*) HeapReAlloc((HANDLE)storage->data, HEAP_NO_SERIALIZE, segment, size);
1176+}
1177+
1178+# define ZEND_MM_MEM_WIN32_DSC {"win32", zend_mm_mem_win32_init, zend_mm_mem_win32_dtor, zend_mm_mem_win32_compact, zend_mm_mem_win32_alloc, zend_mm_mem_win32_realloc, zend_mm_mem_win32_free}
1179+
1180+#endif
1181+
1182+#ifdef HAVE_MEM_MALLOC
1183+
1184+static zend_mm_segment* zend_mm_mem_malloc_alloc(zend_mm_storage *storage, size_t size)
1185+{
1186+ return (zend_mm_segment*)malloc(size);
1187+}
1188+
1189+static zend_mm_segment* zend_mm_mem_malloc_realloc(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
1190+{
1191+ return (zend_mm_segment*)realloc(ptr, size);
1192+}
1193+
1194+static void zend_mm_mem_malloc_free(zend_mm_storage *storage, zend_mm_segment *ptr)
1195+{
1196+ free(ptr);
1197+}
1198+
1199+# define ZEND_MM_MEM_MALLOC_DSC {"malloc", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_malloc_alloc, zend_mm_mem_malloc_realloc, zend_mm_mem_malloc_free}
1200+
1201+#endif
1202+
1203+static const zend_mm_mem_handlers mem_handlers[] = {
1204+#ifdef HAVE_MEM_WIN32
1205+ ZEND_MM_MEM_WIN32_DSC,
1206+#endif
1207+#ifdef HAVE_MEM_MALLOC
1208+ ZEND_MM_MEM_MALLOC_DSC,
1209+#endif
1210+#ifdef HAVE_MEM_MMAP_ANON
1211+ ZEND_MM_MEM_MMAP_ANON_DSC,
1212+#endif
1213+#ifdef HAVE_MEM_MMAP_ZERO
1214+ ZEND_MM_MEM_MMAP_ZERO_DSC,
1215+#endif
1216+ {NULL, NULL, NULL, NULL, NULL, NULL}
1217+};
1218+
1219+# define ZEND_MM_STORAGE_DTOR() heap->storage->handlers->dtor(heap->storage)
1220+# define ZEND_MM_STORAGE_ALLOC(size) heap->storage->handlers->_alloc(heap->storage, size)
1221+# define ZEND_MM_STORAGE_REALLOC(ptr, size) heap->storage->handlers->_realloc(heap->storage, ptr, size)
1222+# define ZEND_MM_STORAGE_FREE(ptr) heap->storage->handlers->_free(heap->storage, ptr)
1223+
1224+/****************/
1225+/* Heap Manager */
1226+/****************/
1227+
1228+#define MEM_BLOCK_VALID 0x7312F8DC
1229+#define MEM_BLOCK_FREED 0x99954317
1230+#define MEM_BLOCK_CACHED 0xFB8277DC
1231+#define MEM_BLOCK_GUARD 0x2A8FCC84
1232+#define MEM_BLOCK_LEAK 0x6C5E8F2D
1233+
1234+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
1235+# define CANARY_SIZE sizeof(size_t)
1236+#else
1237+# define CANARY_SIZE 0
1238+#endif
1239+
1240+/* mm block type */
1241+typedef struct _zend_mm_block_info_canary {
1242+#if ZEND_MM_COOKIES
1243+ size_t _cookie;
1244+#endif
1245+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
1246+ size_t canary_1;
1247+#endif
1248+ size_t _size;
1249+ size_t _prev;
1250+#if SUHOSIN_PATCH
1251+ size_t size;
1252+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
1253+ size_t canary_2;
1254+#endif
1255+#endif
1256+} zend_mm_block_info_canary;
1257+
1258+#if ZEND_DEBUG
1259+
1260+typedef struct _zend_mm_debug_info_canary {
1261+ char *filename;
1262+ uint lineno;
1263+ char *orig_filename;
1264+ uint orig_lineno;
1265+ size_t size;
1266+#if ZEND_MM_HEAP_PROTECTION
1267+ unsigned int start_magic;
1268+#endif
1269+} zend_mm_debug_info_canary;
1270+
1271+#elif ZEND_MM_HEAP_PROTECTION
1272+
1273+typedef struct _zend_mm_debug_info_canary {
1274+ size_t size;
1275+ unsigned int start_magic;
1276+} zend_mm_debug_info_canary;
1277+
1278+#endif
1279+
1280+typedef struct _zend_mm_block_canary {
1281+ zend_mm_block_info_canary info;
1282+#if ZEND_DEBUG
1283+ unsigned int magic;
1284+# ifdef ZTS
1285+ THREAD_T thread_id;
1286+# endif
1287+ zend_mm_debug_info_canary debug;
1288+#elif ZEND_MM_HEAP_PROTECTION
1289+ zend_mm_debug_info_canary debug;
1290+#endif
1291+} zend_mm_block_canary;
1292+
1293+typedef struct _zend_mm_small_free_block_canary {
1294+ zend_mm_block_info_canary info;
1295+#if ZEND_DEBUG
1296+ unsigned int magic;
1297+# ifdef ZTS
1298+ THREAD_T thread_id;
1299+# endif
1300+#endif
1301+ struct _zend_mm_free_block_canary *prev_free_block;
1302+ struct _zend_mm_free_block_canary *next_free_block;
1303+} zend_mm_small_free_block_canary;
1304+
1305+typedef struct _zend_mm_free_block_canary {
1306+ zend_mm_block_info_canary info;
1307+#if ZEND_DEBUG
1308+ unsigned int magic;
1309+# ifdef ZTS
1310+ THREAD_T thread_id;
1311+# endif
1312+#endif
1313+ struct _zend_mm_free_block_canary *prev_free_block;
1314+ struct _zend_mm_free_block_canary *next_free_block;
1315+
1316+ struct _zend_mm_free_block_canary **parent;
1317+ struct _zend_mm_free_block_canary *child[2];
1318+} zend_mm_free_block_canary;
1319+
1320+#define ZEND_MM_NUM_BUCKETS (sizeof(size_t) << 3)
1321+
1322+#define ZEND_MM_CACHE 1
1323+#define ZEND_MM_CACHE_SIZE (ZEND_MM_NUM_BUCKETS * 4 * 1024)
1324+
1325+#ifndef ZEND_MM_CACHE_STAT
1326+# define ZEND_MM_CACHE_STAT 0
1327+#endif
1328+
1329+typedef struct _zend_mm_heap_canary {
1330+ int use_zend_alloc;
1331+ void *(*_malloc)(size_t);
1332+ void (*_free)(void*);
1333+ void *(*_realloc)(void*, size_t);
1334+ size_t free_bitmap;
1335+ size_t large_free_bitmap;
1336+ size_t block_size;
1337+ size_t compact_size;
1338+ zend_mm_segment *segments_list;
1339+ zend_mm_storage *storage;
1340+ size_t real_size;
1341+ size_t real_peak;
1342+ size_t limit;
1343+ size_t size;
1344+ size_t peak;
1345+ size_t reserve_size;
1346+ void *reserve;
1347+ int overflow;
1348+ int internal;
1349+#if ZEND_MM_CACHE
1350+ unsigned int cached;
1351+ zend_mm_free_block_canary *cache[ZEND_MM_NUM_BUCKETS];
1352+#endif
1353+ zend_mm_free_block_canary *free_buckets[ZEND_MM_NUM_BUCKETS*2];
1354+ zend_mm_free_block_canary *large_free_buckets[ZEND_MM_NUM_BUCKETS];
1355+ zend_mm_free_block_canary *rest_buckets[2];
1356+#if ZEND_MM_CACHE_STAT
1357+ struct {
1358+ int count;
1359+ int max_count;
1360+ int hit;
1361+ int miss;
1362+ } cache_stat[ZEND_MM_NUM_BUCKETS+1];
1363+#endif
1364+#if SUHOSIN_PATCH
1365+ size_t canary_1,canary_2,canary_3;
1366+#endif
1367+};
1368+
1369+#define ZEND_MM_SMALL_FREE_BUCKET(heap, index) \
1370+ (zend_mm_free_block_canary*) ((char*)&heap->free_buckets[index * 2] + \
1371+ sizeof(zend_mm_free_block_canary*) * 2 - \
1372+ sizeof(zend_mm_small_free_block_canary))
1373+
1374+#define ZEND_MM_REST_BUCKET(heap) \
1375+ (zend_mm_free_block_canary*)((char*)&heap->rest_buckets[0] + \
1376+ sizeof(zend_mm_free_block_canary*) * 2 - \
1377+ sizeof(zend_mm_small_free_block_canary))
1378+
1379+#if ZEND_MM_COOKIES
1380+
1381+static unsigned int _zend_mm_cookie = 0;
1382+
1383+# define ZEND_MM_COOKIE(block) \
1384+ (((size_t)(block)) ^ _zend_mm_cookie)
1385+# define ZEND_MM_SET_COOKIE(block) \
1386+ (block)->info._cookie = ZEND_MM_COOKIE(block)
1387+# define ZEND_MM_CHECK_COOKIE(block) \
1388+ if (UNEXPECTED((block)->info._cookie != ZEND_MM_COOKIE(block))) { \
1389+ zend_mm_panic("zend_mm_heap corrupted"); \
1390+ }
1391+#else
1392+# define ZEND_MM_SET_COOKIE(block)
1393+# define ZEND_MM_CHECK_COOKIE(block)
1394+#endif
1395+
1396+/* Default memory segment size */
1397+#define ZEND_MM_SEG_SIZE (256 * 1024)
1398+
1399+/* Reserved space for error reporting in case of memory overflow */
1400+#define ZEND_MM_RESERVE_SIZE (8*1024)
1401+
1402+#ifdef _WIN64
1403+# define ZEND_MM_LONG_CONST(x) (x##i64)
1404+#else
1405+# define ZEND_MM_LONG_CONST(x) (x##L)
1406+#endif
1407+
1408+#define ZEND_MM_TYPE_MASK ZEND_MM_LONG_CONST(0x3)
1409+
1410+#define ZEND_MM_FREE_BLOCK ZEND_MM_LONG_CONST(0x0)
1411+#define ZEND_MM_USED_BLOCK ZEND_MM_LONG_CONST(0x1)
1412+#define ZEND_MM_GUARD_BLOCK ZEND_MM_LONG_CONST(0x3)
1413+
1414+#define ZEND_MM_BLOCK(b, type, size) do { \
1415+ size_t _size = (size); \
1416+ (b)->info._size = (type) | _size; \
1417+ ZEND_MM_BLOCK_AT(b, _size)->info._prev = (type) | _size; \
1418+ ZEND_MM_SET_COOKIE(b); \
1419+ } while (0);
1420+#define ZEND_MM_LAST_BLOCK(b) do { \
1421+ (b)->info._size = ZEND_MM_GUARD_BLOCK | ZEND_MM_ALIGNED_HEADER_SIZE; \
1422+ ZEND_MM_SET_MAGIC(b, MEM_BLOCK_GUARD); \
1423+ } while (0);
1424+#define ZEND_MM_BLOCK_SIZE(b) ((b)->info._size & ~ZEND_MM_TYPE_MASK)
1425+#define ZEND_MM_IS_FREE_BLOCK(b) (!((b)->info._size & ZEND_MM_USED_BLOCK))
1426+#define ZEND_MM_IS_USED_BLOCK(b) ((b)->info._size & ZEND_MM_USED_BLOCK)
1427+#define ZEND_MM_IS_GUARD_BLOCK(b) (((b)->info._size & ZEND_MM_TYPE_MASK) == ZEND_MM_GUARD_BLOCK)
1428+
1429+#define ZEND_MM_NEXT_BLOCK(b) ZEND_MM_BLOCK_AT(b, ZEND_MM_BLOCK_SIZE(b))
1430+#define ZEND_MM_PREV_BLOCK(b) ZEND_MM_BLOCK_AT(b, -(int)((b)->info._prev & ~ZEND_MM_TYPE_MASK))
1431+
1432+#define ZEND_MM_PREV_BLOCK_IS_FREE(b) (!((b)->info._prev & ZEND_MM_USED_BLOCK))
1433+
1434+#define ZEND_MM_MARK_FIRST_BLOCK(b) ((b)->info._prev = ZEND_MM_GUARD_BLOCK)
1435+#define ZEND_MM_IS_FIRST_BLOCK(b) ((b)->info._prev == ZEND_MM_GUARD_BLOCK)
1436+
1437+/* optimized access */
1438+#define ZEND_MM_FREE_BLOCK_SIZE(b) (b)->info._size
1439+
1440+#ifndef ZEND_MM_ALIGNMENT
1441+# define ZEND_MM_ALIGNMENT 8
1442+# define ZEND_MM_ALIGNMENT_LOG2 3
1443+#elif ZEND_MM_ALIGNMENT < 4
1444+# undef ZEND_MM_ALIGNMENT
1445+# undef ZEND_MM_ALIGNMENT_LOG2
1446+# define ZEND_MM_ALIGNMENT 4
1447+# define ZEND_MM_ALIGNMENT_LOG2 2
1448+#endif
1449+
1450+#define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
1451+
1452+/* Aligned header size */
1453+#define ZEND_MM_ALIGNED_SIZE(size) ((size + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
1454+#define ZEND_MM_ALIGNED_HEADER_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block_canary))
1455+#define ZEND_MM_ALIGNED_FREE_HEADER_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_small_free_block_canary))
1456+#define ZEND_MM_MIN_ALLOC_BLOCK_SIZE ZEND_MM_ALIGNED_SIZE(ZEND_MM_ALIGNED_HEADER_SIZE + END_MAGIC_SIZE + CANARY_SIZE)
1457+#define ZEND_MM_ALIGNED_MIN_HEADER_SIZE (ZEND_MM_MIN_ALLOC_BLOCK_SIZE>ZEND_MM_ALIGNED_FREE_HEADER_SIZE?ZEND_MM_MIN_ALLOC_BLOCK_SIZE:ZEND_MM_ALIGNED_FREE_HEADER_SIZE)
1458+#define ZEND_MM_ALIGNED_SEGMENT_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_segment))
1459+
1460+#define ZEND_MM_MIN_SIZE ((ZEND_MM_ALIGNED_MIN_HEADER_SIZE>(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE))?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE-(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE)):0)
1461+
1462+#define ZEND_MM_MAX_SMALL_SIZE ((ZEND_MM_NUM_BUCKETS<<ZEND_MM_ALIGNMENT_LOG2)+ZEND_MM_ALIGNED_MIN_HEADER_SIZE)
1463+
1464+#define ZEND_MM_TRUE_SIZE(size) ((size<ZEND_MM_MIN_SIZE)?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE):(ZEND_MM_ALIGNED_SIZE(size+ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE)))
1465+
1466+#define ZEND_MM_BUCKET_INDEX(true_size) ((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))
1467+
1468+#define ZEND_MM_SMALL_SIZE(true_size) (true_size < ZEND_MM_MAX_SMALL_SIZE)
1469+
1470+/* Memory calculations */
1471+#define ZEND_MM_BLOCK_AT(blk, offset) ((zend_mm_block_canary *) (((char *) (blk))+(offset)))
1472+#define ZEND_MM_DATA_OF(p) ((void *) (((char *) (p))+ZEND_MM_ALIGNED_HEADER_SIZE))
1473+#define ZEND_MM_HEADER_OF(blk) ZEND_MM_BLOCK_AT(blk, -(int)ZEND_MM_ALIGNED_HEADER_SIZE)
1474+
1475+/* Debug output */
1476+#if ZEND_DEBUG
1477+
1478+# ifdef ZTS
1479+# define ZEND_MM_SET_THREAD_ID(block) \
1480+ ((zend_mm_block_canary*)(block))->thread_id = tsrm_thread_id()
1481+# define ZEND_MM_BAD_THREAD_ID(block) ((block)->thread_id != tsrm_thread_id())
1482+# else
1483+# define ZEND_MM_SET_THREAD_ID(block)
1484+# define ZEND_MM_BAD_THREAD_ID(block) 0
1485+# endif
1486+
1487+# define ZEND_MM_VALID_PTR(block) \
1488+ zend_mm_check_ptr(heap, block, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)
1489+
1490+# define ZEND_MM_SET_MAGIC(block, val) do { \
1491+ (block)->magic = (val); \
1492+ } while (0)
1493+
1494+# define ZEND_MM_CHECK_MAGIC(block, val) do { \
1495+ if ((block)->magic != (val)) { \
1496+ zend_mm_panic("zend_mm_heap corrupted"); \
1497+ } \
1498+ } while (0)
1499+
1500+# define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread) do { \
1501+ ((zend_mm_block_canary*)(block))->debug.filename = __zend_filename; \
1502+ ((zend_mm_block_canary*)(block))->debug.lineno = __zend_lineno; \
1503+ ((zend_mm_block_canary*)(block))->debug.orig_filename = __zend_orig_filename; \
1504+ ((zend_mm_block_canary*)(block))->debug.orig_lineno = __zend_orig_lineno; \
1505+ ZEND_MM_SET_BLOCK_SIZE(block, __size); \
1506+ if (set_valid) { \
1507+ ZEND_MM_SET_MAGIC(block, MEM_BLOCK_VALID); \
1508+ } \
1509+ if (set_thread) { \
1510+ ZEND_MM_SET_THREAD_ID(block); \
1511+ } \
1512+ } while (0)
1513+
1514+#else
1515+
1516+# define ZEND_MM_VALID_PTR(ptr) EXPECTED(ptr != NULL)
1517+
1518+# define ZEND_MM_SET_MAGIC(block, val)
1519+
1520+# define ZEND_MM_CHECK_MAGIC(block, val)
1521+
1522+# define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread) ZEND_MM_SET_BLOCK_SIZE(block, __size)
1523+
1524+#endif
1525+
1526+#if SUHOSIN_MM_WITH_CANARY_PROTECTION
1527+
1528+# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION) do { \
1529+ char *p = SUHOSIN_MM_END_CANARY_PTR(block); size_t check; \
1530+ if (((block)->info.canary_1 != heap->canary_1) || ((block)->info.canary_2 != heap->canary_2)) { \
1531+ canary_mismatch: \
1532+ zend_suhosin_log(S_MEMORY, "canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
1533+ if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { (block)->info.canary_1 = heap->canary_1; (block)->info.canary_2 = heap->canary_2; }\
1534+ } \
1535+ memcpy(&check, p, CANARY_SIZE); \
1536+ if (check != heap->canary_3) { \
1537+ zend_suhosin_log(S_MEMORY, "end canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
1538+ if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { memcpy(p, heap->canary_3, CANARY_SIZE); } \
1539+ } \
1540+ } while (0)
1541+
1542+# define SUHOSIN_MM_SET_CANARIES(block) do { \
1543+ (block)->info.canary_1 = heap->canary_1; \
1544+ (block)->info.canary_2 = heap->canary_2; \
1545+ } while (0)
1546+
1547+# define SUHOSIN_MM_END_CANARY_PTR(block) \
1548+ (char *)(((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block_canary*)(block))->info.size + END_MAGIC_SIZE)
1549+
1550+# define SUHOSIN_MM_SET_END_CANARY(block) do { \
1551+ char *p = SUHOSIN_MM_END_CANARY_PTR(block); \
1552+ memcpy(p, &heap->canary_3, CANARY_SIZE); \
1553+ } while (0)
1554+
1555+#else
1556+
1557+# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION)
1558+# define SUHOSIN_MM_SET_CANARIES(block)
1559+# define SUHOSIN_MM_END_CANARY_PTR(block)
1560+# define SUHOSIN_MM_SET_END_CANARY(block)
1561+
1562+#endif
1563+
1564+
1565+#if ZEND_MM_HEAP_PROTECTION
1566+
1567+# define ZEND_MM_CHECK_PROTECTION(block) \
1568+ do { \
1569+ if ((block)->debug.start_magic != _mem_block_start_magic || \
1570+ memcmp(ZEND_MM_END_MAGIC_PTR(block), &_mem_block_end_magic, END_MAGIC_SIZE) != 0) { \
1571+ zend_mm_panic("zend_mm_heap corrupted"); \
1572+ } \
1573+ } while (0)
1574+
1575+# define ZEND_MM_END_MAGIC_PTR(block) \
1576+ (((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block_canary*)(block))->debug.size)
1577+
1578+# define END_MAGIC_SIZE sizeof(unsigned int)
1579+
1580+# define ZEND_MM_SET_BLOCK_SIZE(block, __size) do { \
1581+ char *p; \
1582+ ((zend_mm_block_canary*)(block))->debug.size = (__size); \
1583+ p = ZEND_MM_END_MAGIC_PTR(block); \
1584+ ((zend_mm_block_canary*)(block))->debug.start_magic = _mem_block_start_magic; \
1585+ memcpy(p, &_mem_block_end_magic, END_MAGIC_SIZE); \
1586+ } while (0)
1587+
1588+static unsigned int _mem_block_start_magic = 0;
1589+static unsigned int _mem_block_end_magic = 0;
1590+
1591+#else
1592+
1593+# if ZEND_DEBUG
1594+# define ZEND_MM_SET_BLOCK_SIZE(block, _size) \
1595+ ((zend_mm_block_canary*)(block))->debug.size = (_size)
1596+# else
1597+# define ZEND_MM_SET_BLOCK_SIZE(block, _size)
1598+# endif
1599+
1600+# define ZEND_MM_CHECK_PROTECTION(block)
1601+
1602+# define END_MAGIC_SIZE 0
1603+
1604+#endif
1605+
1606+#if ZEND_MM_SAFE_UNLINKING
1607+# define ZEND_MM_CHECK_BLOCK_LINKAGE(block) \
1608+ if (UNEXPECTED((block)->info._size != ZEND_MM_BLOCK_AT(block, ZEND_MM_FREE_BLOCK_SIZE(block))->info._prev) || \
1609+ UNEXPECTED(!UNEXPECTED(ZEND_MM_IS_FIRST_BLOCK(block)) && \
1610+ UNEXPECTED(ZEND_MM_PREV_BLOCK(block)->info._size != (block)->info._prev))) { \
1611+ zend_mm_panic("zend_mm_heap corrupted"); \
1612+ }
1613+#define ZEND_MM_CHECK_TREE(block) \
1614+ if (UNEXPECTED(*((block)->parent) != (block))) { \
1615+ zend_mm_panic("zend_mm_heap corrupted"); \
1616+ }
1617+#else
1618+# define ZEND_MM_CHECK_BLOCK_LINKAGE(block)
1619+# define ZEND_MM_CHECK_TREE(block)
1620+#endif
1621+
1622+#define ZEND_MM_LARGE_BUCKET_INDEX(S) zend_mm_high_bit(S)
1623+
1624+void *_zend_mm_alloc_canary_int(zend_mm_heap_canary *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
1625+void _zend_mm_free_canary_int(zend_mm_heap_canary *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
1626+void *_zend_mm_realloc_canary_int(zend_mm_heap_canary *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
1627+
1628+
1629+static inline unsigned int zend_mm_high_bit(size_t _size)
1630+{
1631+#if defined(__GNUC__) && defined(i386)
1632+ unsigned int n;
1633+
1634+ __asm__("bsrl %1,%0\n\t" : "=r" (n) : "rm" (_size));
1635+ return n;
1636+#elif defined(__GNUC__) && defined(__x86_64__)
1637+ unsigned long n;
1638+
1639+ __asm__("bsrq %1,%0\n\t" : "=r" (n) : "rm" (_size));
1640+ return (unsigned int)n;
1641+#elif defined(_MSC_VER) && defined(_M_IX86)
1642+ __asm {
1643+ bsr eax, _size
1644+ }
1645+#else
1646+ unsigned int n = 0;
1647+ while (_size != 0) {
1648+ _size = _size >> 1;
1649+ n++;
1650+ }
1651+ return n-1;
1652+#endif
1653+}
1654+
1655+static inline unsigned int zend_mm_low_bit(size_t _size)
1656+{
1657+#if defined(__GNUC__) && defined(i386)
1658+ unsigned int n;
1659+
1660+ __asm__("bsfl %1,%0\n\t" : "=r" (n) : "rm" (_size));
1661+ return n;
1662+#elif defined(__GNUC__) && defined(__x86_64__)
1663+ unsigned long n;
1664+
1665+ __asm__("bsfq %1,%0\n\t" : "=r" (n) : "rm" (_size));
1666+ return (unsigned int)n;
1667+#elif defined(_MSC_VER) && defined(_M_IX86)
1668+ __asm {
1669+ bsf eax, _size
1670+ }
1671+#else
1672+ static const int offset[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
1673+ unsigned int n;
1674+ unsigned int index = 0;
1675+
1676+ n = offset[_size & 15];
1677+ while (n == 4) {
1678+ _size >>= 4;
1679+ index += n;
1680+ n = offset[_size & 15];
1681+ }
1682+
1683+ return index + n;
1684+#endif
1685+}
1686+
1687+static void zend_mm_add_to_rest_list(zend_mm_heap_canary *heap, zend_mm_free_block_canary *mm_block)
1688+{
1689+ zend_mm_free_block_canary *prev, *next;
1690+
1691+ ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);
1692+
1693+ if (!ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block))) {
1694+ mm_block->parent = NULL;
1695+ }
1696+
1697+ prev = heap->rest_buckets[0];
1698+ next = prev->next_free_block;
1699+ mm_block->prev_free_block = prev;
1700+ mm_block->next_free_block = next;
1701+ prev->next_free_block = next->prev_free_block = mm_block;
1702+}
1703+
1704+static void zend_mm_add_to_free_list(zend_mm_heap_canary *heap, zend_mm_free_block_canary *mm_block)
1705+{
1706+ size_t size;
1707+ size_t index;
1708+
1709+ ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);
1710+
1711+ size = ZEND_MM_FREE_BLOCK_SIZE(mm_block);
1712+ if (EXPECTED(!ZEND_MM_SMALL_SIZE(size))) {
1713+ zend_mm_free_block_canary **p;
1714+
1715+ index = ZEND_MM_LARGE_BUCKET_INDEX(size);
1716+ p = &heap->large_free_buckets[index];
1717+ mm_block->child[0] = mm_block->child[1] = NULL;
1718+ if (!*p) {
1719+ *p = mm_block;
1720+ mm_block->parent = p;
1721+ mm_block->prev_free_block = mm_block->next_free_block = mm_block;
1722+ heap->large_free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
1723+ } else {
1724+ size_t m;
1725+
1726+ for (m = size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
1727+ zend_mm_free_block_canary *prev = *p;
1728+
1729+ if (ZEND_MM_FREE_BLOCK_SIZE(prev) != size) {
1730+ p = &prev->child[(m >> (ZEND_MM_NUM_BUCKETS-1)) & 1];
1731+ if (!*p) {
1732+ *p = mm_block;
1733+ mm_block->parent = p;
1734+ mm_block->prev_free_block = mm_block->next_free_block = mm_block;
1735+ break;
1736+ }
1737+ } else {
1738+ zend_mm_free_block_canary *next = prev->next_free_block;
1739+
1740+ prev->next_free_block = next->prev_free_block = mm_block;
1741+ mm_block->next_free_block = next;
1742+ mm_block->prev_free_block = prev;
1743+ mm_block->parent = NULL;
1744+ break;
1745+ }
1746+ }
1747+ }
1748+ } else {
1749+ zend_mm_free_block_canary *prev, *next;
1750+
1751+ index = ZEND_MM_BUCKET_INDEX(size);
1752+
1753+ prev = ZEND_MM_SMALL_FREE_BUCKET(heap, index);
1754+ if (prev->prev_free_block == prev) {
1755+ heap->free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
1756+ }
1757+ next = prev->next_free_block;
1758+
1759+ mm_block->prev_free_block = prev;
1760+ mm_block->next_free_block = next;
1761+ prev->next_free_block = next->prev_free_block = mm_block;
1762+ }
1763+}
1764+
1765+static void zend_mm_remove_from_free_list(zend_mm_heap_canary *heap, zend_mm_free_block_canary *mm_block)
1766+{
1767+ zend_mm_free_block_canary *prev = mm_block->prev_free_block;
1768+ zend_mm_free_block_canary *next = mm_block->next_free_block;
1769+
1770+ ZEND_MM_CHECK_MAGIC(mm_block, MEM_BLOCK_FREED);
1771+
1772+ if (EXPECTED(prev == mm_block)) {
1773+ zend_mm_free_block_canary **rp, **cp;
1774+
1775+#if SUHOSIN_PATCH
1776+ if (next != mm_block) {
1777+ zend_suhosin_log(S_MEMORY, "zend_mm_heap corrupted at %p", mm_block);
1778+ _exit(1);
1779+ }
1780+#endif
1781+#if ZEND_MM_SAFE_UNLINKING
1782+ if (UNEXPECTED(next != mm_block)) {
1783+ zend_mm_panic("zend_mm_heap corrupted");
1784+ }
1785+#endif
1786+
1787+ rp = &mm_block->child[mm_block->child[1] != NULL];
1788+ prev = *rp;
1789+ if (EXPECTED(prev == NULL)) {
1790+ size_t index = ZEND_MM_LARGE_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));
1791+
1792+ ZEND_MM_CHECK_TREE(mm_block);
1793+ *mm_block->parent = NULL;
1794+ if (mm_block->parent == &heap->large_free_buckets[index]) {
1795+ heap->large_free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
1796+ }
1797+ } else {
1798+ while (*(cp = &(prev->child[prev->child[1] != NULL])) != NULL) {
1799+ prev = *cp;
1800+ rp = cp;
1801+ }
1802+ *rp = NULL;
1803+
1804+subst_block:
1805+ ZEND_MM_CHECK_TREE(mm_block);
1806+ *mm_block->parent = prev;
1807+ prev->parent = mm_block->parent;
1808+ if ((prev->child[0] = mm_block->child[0])) {
1809+ ZEND_MM_CHECK_TREE(prev->child[0]);
1810+ prev->child[0]->parent = &prev->child[0];
1811+ }
1812+ if ((prev->child[1] = mm_block->child[1])) {
1813+ ZEND_MM_CHECK_TREE(prev->child[1]);
1814+ prev->child[1]->parent = &prev->child[1];
1815+ }
1816+ }
1817+ } else {
1818+
1819+#if SUHOSIN_PATCH
1820+ if (prev->next_free_block != mm_block || next->prev_free_block != mm_block) {
1821+ zend_suhosin_log(S_MEMORY, "zend_mm_head corrupted at %p", mm_block);
1822+ _exit(1);
1823+ }
1824+#endif
1825+
1826+#if ZEND_MM_SAFE_UNLINKING
1827+ if (UNEXPECTED(prev->next_free_block != mm_block) || UNEXPECTED(next->prev_free_block != mm_block)) {
1828+ zend_mm_panic("zend_mm_heap corrupted");
1829+ }
1830+#endif
1831+
1832+ prev->next_free_block = next;
1833+ next->prev_free_block = prev;
1834+
1835+ if (EXPECTED(ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block)))) {
1836+ if (EXPECTED(prev == next)) {
1837+ size_t index = ZEND_MM_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));
1838+
1839+ if (EXPECTED(heap->free_buckets[index*2] == heap->free_buckets[index*2+1])) {
1840+ heap->free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
1841+ }
1842+ }
1843+ } else if (UNEXPECTED(mm_block->parent != NULL)) {
1844+ goto subst_block;
1845+ }
1846+ }
1847+}
1848+
1849+static void zend_mm_init(zend_mm_heap_canary *heap)
1850+{
1851+ zend_mm_free_block_canary* p;
1852+ int i;
1853+
1854+ heap->free_bitmap = 0;
1855+ heap->large_free_bitmap = 0;
1856+#if ZEND_MM_CACHE
1857+ heap->cached = 0;
1858+ memset(heap->cache, 0, sizeof(heap->cache));
1859+#endif
1860+#if ZEND_MM_CACHE_STAT
1861+ for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
1862+ heap->cache_stat[i].count = 0;
1863+ }
1864+#endif
1865+ p = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
1866+ for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
1867+ p->next_free_block = p;
1868+ p->prev_free_block = p;
1869+ p = (zend_mm_free_block_canary*)((char*)p + sizeof(zend_mm_free_block_canary*) * 2);
1870+ heap->large_free_buckets[i] = NULL;
1871+ }
1872+ heap->rest_buckets[0] = heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(heap);
1873+#if SUHOSIN_PATCH
1874+ if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
1875+ heap->canary_1 = zend_canary();
1876+ heap->canary_2 = zend_canary();
1877+ heap->canary_3 = zend_canary();
1878+ }
1879+#endif
1880+}
1881+
1882+static void zend_mm_del_segment(zend_mm_heap_canary *heap, zend_mm_segment *segment)
1883+{
1884+ zend_mm_segment **p = &heap->segments_list;
1885+
1886+ while (*p != segment) {
1887+ p = &(*p)->next_segment;
1888+ }
1889+ *p = segment->next_segment;
1890+ heap->real_size -= segment->size;
1891+ ZEND_MM_STORAGE_FREE(segment);
1892+}
1893+
1894+#if ZEND_MM_CACHE
1895+static void zend_mm_free_cache(zend_mm_heap_canary *heap)
1896+{
1897+ int i;
1898+
1899+ for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
1900+ if (heap->cache[i]) {
1901+ zend_mm_free_block_canary *mm_block = heap->cache[i];
1902+
1903+ while (mm_block) {
1904+ size_t size = ZEND_MM_BLOCK_SIZE(mm_block);
1905+ zend_mm_free_block_canary *q = mm_block->prev_free_block;
1906+ zend_mm_block_canary *next_block = ZEND_MM_NEXT_BLOCK(mm_block);
1907+
1908+ heap->cached -= size;
1909+
1910+ if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
1911+ mm_block = (zend_mm_free_block_canary*)ZEND_MM_PREV_BLOCK(mm_block);
1912+ size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
1913+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) mm_block);
1914+ }
1915+ if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
1916+ size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
1917+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
1918+ }
1919+ ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
1920+
1921+ if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
1922+ ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_NEXT_BLOCK(mm_block))) {
1923+ zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
1924+ } else {
1925+ zend_mm_add_to_free_list(heap, (zend_mm_free_block_canary *) mm_block);
1926+ }
1927+
1928+ mm_block = q;
1929+ }
1930+ heap->cache[i] = NULL;
1931+#if ZEND_MM_CACHE_STAT
1932+ heap->cache_stat[i].count = 0;
1933+#endif
1934+ }
1935+ }
1936+}
1937+#endif
1938+
1939+#if ZEND_MM_HEAP_PROTECTION || ZEND_MM_COOKIES
1940+static void zend_mm_random(unsigned char *buf, size_t size)
1941+{
1942+ size_t i = 0;
1943+ unsigned char t;
1944+
1945+#ifdef ZEND_WIN32
1946+ HCRYPTPROV hCryptProv;
1947+
1948+ if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
1949+ do {
1950+ BOOL ret = CryptGenRandom(hCryptProv, size, buf);
1951+ CryptReleaseContext(hCryptProv, 0);
1952+ if (ret) {
1953+ while (i < size && buf[i] != 0) {
1954+ i++;
1955+ }
1956+ if (i == size) {
1957+ return;
1958+ }
1959+ }
1960+ } while (0);
1961+ }
1962+#elif defined(HAVE_DEV_URANDOM)
1963+ int fd = open("/dev/urandom", 0);
1964+
1965+ if (fd >= 0) {
1966+ if (read(fd, buf, size) == size) {
1967+ while (i < size && buf[i] != 0) {
1968+ i++;
1969+ }
1970+ if (i == size) {
1971+ close(fd);
1972+ return;
1973+ }
1974+ }
1975+ close(fd);
1976+ }
1977+#endif
1978+ t = (unsigned char)getpid();
1979+ while (i < size) {
1980+ do {
1981+ buf[i] = ((unsigned char)rand()) ^ t;
1982+ } while (buf[i] == 0);
1983+ t = buf[i++] << 1;
1984+ }
1985+}
1986+#endif
1987+
1988+
1989+/* Notes:
1990+ * - This function may alter the block_sizes values to match platform alignment
1991+ * - This function does *not* perform sanity checks on the arguments
1992+ */
1993+zend_mm_heap_canary *__zend_mm_startup_canary_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
1994+{
1995+ zend_mm_storage *storage;
1996+ zend_mm_heap_canary *heap;
1997+
1998+#if 0
1999+ int i;
2000+
2001+ printf("ZEND_MM_ALIGNMENT=%d\n", ZEND_MM_ALIGNMENT);
2002+ printf("ZEND_MM_ALIGNMENT_LOG2=%d\n", ZEND_MM_ALIGNMENT_LOG2);
2003+ printf("ZEND_MM_MIN_SIZE=%d\n", ZEND_MM_MIN_SIZE);
2004+ printf("ZEND_MM_MAX_SMALL_SIZE=%d\n", ZEND_MM_MAX_SMALL_SIZE);
2005+ printf("ZEND_MM_ALIGNED_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_HEADER_SIZE);
2006+ printf("ZEND_MM_ALIGNED_FREE_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_FREE_HEADER_SIZE);
2007+ printf("ZEND_MM_MIN_ALLOC_BLOCK_SIZE=%d\n", ZEND_MM_MIN_ALLOC_BLOCK_SIZE);
2008+ printf("ZEND_MM_ALIGNED_MIN_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_MIN_HEADER_SIZE);
2009+ printf("ZEND_MM_ALIGNED_SEGMENT_SIZE=%d\n", ZEND_MM_ALIGNED_SEGMENT_SIZE);
2010+ for (i = 0; i < ZEND_MM_MAX_SMALL_SIZE; i++) {
2011+ printf("%3d%c: %3ld %d %2ld\n", i, (i == ZEND_MM_MIN_SIZE?'*':' '), (long)ZEND_MM_TRUE_SIZE(i), ZEND_MM_SMALL_SIZE(ZEND_MM_TRUE_SIZE(i)), (long)ZEND_MM_BUCKET_INDEX(ZEND_MM_TRUE_SIZE(i)));
2012+ }
2013+ exit(0);
2014+#endif
2015+
2016+#if ZEND_MM_HEAP_PROTECTION
2017+ if (_mem_block_start_magic == 0) {
2018+ zend_mm_random((unsigned char*)&_mem_block_start_magic, sizeof(_mem_block_start_magic));
2019+ }
2020+ if (_mem_block_end_magic == 0) {
2021+ zend_mm_random((unsigned char*)&_mem_block_end_magic, sizeof(_mem_block_end_magic));
2022+ }
2023+#endif
2024+#if ZEND_MM_COOKIES
2025+ if (_zend_mm_cookie == 0) {
2026+ zend_mm_random((unsigned char*)&_zend_mm_cookie, sizeof(_zend_mm_cookie));
2027+ }
2028+#endif
2029+
2030+ if (zend_mm_low_bit(block_size) != zend_mm_high_bit(block_size)) {
2031+ fprintf(stderr, "'block_size' must be a power of two\n");
2032+ exit(255);
2033+ }
2034+ storage = handlers->init(params);
2035+ if (!storage) {
2036+ fprintf(stderr, "Cannot initialize zend_mm storage [%s]\n", handlers->name);
2037+ exit(255);
2038+ }
2039+ storage->handlers = handlers;
2040+
2041+ heap = malloc(sizeof(struct _zend_mm_heap_canary));
2042+
2043+ heap->storage = storage;
2044+ heap->block_size = block_size;
2045+ heap->compact_size = 0;
2046+ heap->segments_list = NULL;
2047+ zend_mm_init(heap);
2048+# if ZEND_MM_CACHE_STAT
2049+ memset(heap->cache_stat, 0, sizeof(heap->cache_stat));
2050+# endif
2051+
2052+ heap->use_zend_alloc = 1;
2053+ heap->real_size = 0;
2054+ heap->overflow = 0;
2055+ heap->real_peak = 0;
2056+ heap->limit = ZEND_MM_LONG_CONST(1)<<(ZEND_MM_NUM_BUCKETS-2);
2057+ heap->size = 0;
2058+ heap->peak = 0;
2059+ heap->internal = internal;
2060+ heap->reserve = NULL;
2061+ heap->reserve_size = reserve_size;
2062+ if (reserve_size > 0) {
2063+ heap->reserve = _zend_mm_alloc((zend_mm_heap *)heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
2064+ }
2065+ if (internal) {
2066+ int i;
2067+ zend_mm_free_block_canary *p, *q, *orig;
2068+ zend_mm_heap_canary *mm_heap = _zend_mm_alloc((zend_mm_heap *)heap, sizeof(zend_mm_heap_canary) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
2069+
2070+ *mm_heap = *heap;
2071+
2072+ p = ZEND_MM_SMALL_FREE_BUCKET(mm_heap, 0);
2073+ orig = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
2074+ for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
2075+ q = p;
2076+ while (q->prev_free_block != orig) {
2077+ q = q->prev_free_block;
2078+ }
2079+ q->prev_free_block = p;
2080+ q = p;
2081+ while (q->next_free_block != orig) {
2082+ q = q->next_free_block;
2083+ }
2084+ q->next_free_block = p;
2085+ p = (zend_mm_free_block_canary*)((char*)p + sizeof(zend_mm_free_block_canary*) * 2);
2086+ orig = (zend_mm_free_block_canary*)((char*)orig + sizeof(zend_mm_free_block_canary*) * 2);
2087+ if (mm_heap->large_free_buckets[i]) {
2088+ mm_heap->large_free_buckets[i]->parent = &mm_heap->large_free_buckets[i];
2089+ }
2090+ }
2091+ mm_heap->rest_buckets[0]->next_free_block = mm_heap->rest_buckets[1]->prev_free_block = ZEND_MM_REST_BUCKET(mm_heap);
2092+
2093+ free(heap);
2094+ heap = mm_heap;
2095+ }
2096+ return heap;
2097+}
2098+
2099+zend_mm_heap_canary *__zend_mm_startup_canary(void)
2100+{
2101+ int i;
2102+ size_t seg_size;
2103+ char *mem_type = getenv("ZEND_MM_MEM_TYPE");
2104+ char *tmp;
2105+ const zend_mm_mem_handlers *handlers;
2106+ zend_mm_heap_canary *heap;
2107+
2108+ if (mem_type == NULL) {
2109+ i = 0;
2110+ } else {
2111+ for (i = 0; mem_handlers[i].name; i++) {
2112+ if (strcmp(mem_handlers[i].name, mem_type) == 0) {
2113+ break;
2114+ }
2115+ }
2116+ if (!mem_handlers[i].name) {
2117+ fprintf(stderr, "Wrong or unsupported zend_mm storage type '%s'\n", mem_type);
2118+ fprintf(stderr, " supported types:\n");
2119+ for (i = 0; mem_handlers[i].name; i++) {
2120+ fprintf(stderr, " '%s'\n", mem_handlers[i].name);
2121+ }
2122+ exit(255);
2123+ }
2124+ }
2125+ handlers = &mem_handlers[i];
2126+
2127+ tmp = getenv("ZEND_MM_SEG_SIZE");
2128+ if (tmp) {
2129+ seg_size = zend_atoi(tmp, 0);
2130+ if (zend_mm_low_bit(seg_size) != zend_mm_high_bit(seg_size)) {
2131+ fprintf(stderr, "ZEND_MM_SEG_SIZE must be a power of two\n");
2132+ exit(255);
2133+ } else if (seg_size < ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE) {
2134+ fprintf(stderr, "ZEND_MM_SEG_SIZE is too small\n");
2135+ exit(255);
2136+ }
2137+ } else {
2138+ seg_size = ZEND_MM_SEG_SIZE;
2139+ }
2140+
2141+ heap = __zend_mm_startup_canary_ex(handlers, seg_size, ZEND_MM_RESERVE_SIZE, 0, NULL);
2142+ if (heap) {
2143+ tmp = getenv("ZEND_MM_COMPACT");
2144+ if (tmp) {
2145+ heap->compact_size = zend_atoi(tmp, 0);
2146+ } else {
2147+ heap->compact_size = 2 * 1024 * 1024;
2148+ }
2149+ }
2150+ return heap;
2151+}
2152+
2153+#if ZEND_DEBUG
2154+static long zend_mm_find_leaks(zend_mm_segment *segment, zend_mm_block_canary *b)
2155+{
2156+ long leaks = 0;
2157+ zend_mm_block_canary *p, *q;
2158+
2159+ p = ZEND_MM_NEXT_BLOCK(b);
2160+ while (1) {
2161+ if (ZEND_MM_IS_GUARD_BLOCK(p)) {
2162+ ZEND_MM_CHECK_MAGIC(p, MEM_BLOCK_GUARD);
2163+ segment = segment->next_segment;
2164+ if (!segment) {
2165+ break;
2166+ }
2167+ p = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
2168+ continue;
2169+ }
2170+ q = ZEND_MM_NEXT_BLOCK(p);
2171+ if (q <= p ||
2172+ (char*)q > (char*)segment + segment->size ||
2173+ p->info._size != q->info._prev) {
2174+ zend_mm_panic("zend_mm_heap corrupted");
2175+ }
2176+ if (!ZEND_MM_IS_FREE_BLOCK(p)) {
2177+ if (p->magic == MEM_BLOCK_VALID) {
2178+ if (p->debug.filename==b->debug.filename && p->debug.lineno==b->debug.lineno) {
2179+ ZEND_MM_SET_MAGIC(p, MEM_BLOCK_LEAK);
2180+ leaks++;
2181+ }
2182+#if ZEND_MM_CACHE
2183+ } else if (p->magic == MEM_BLOCK_CACHED) {
2184+ /* skip it */
2185+#endif
2186+ } else if (p->magic != MEM_BLOCK_LEAK) {
2187+ zend_mm_panic("zend_mm_heap corrupted");
2188+ }
2189+ }
2190+ p = q;
2191+ }
2192+ return leaks;
2193+}
2194+
2195+static void zend_mm_check_leaks(zend_mm_heap_canary *heap TSRMLS_DC)
2196+{
2197+ zend_mm_segment *segment = heap->segments_list;
2198+ zend_mm_block_canary *p, *q;
2199+ zend_uint total = 0;
2200+
2201+ if (!segment) {
2202+ return;
2203+ }
2204+ p = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
2205+ while (1) {
2206+ q = ZEND_MM_NEXT_BLOCK(p);
2207+ if (q <= p ||
2208+ (char*)q > (char*)segment + segment->size ||
2209+ p->info._size != q->info._prev) {
2210+ zend_mm_panic("zend_mm_heap corrupted");
2211+ }
2212+ if (!ZEND_MM_IS_FREE_BLOCK(p)) {
2213+ if (p->magic == MEM_BLOCK_VALID) {
2214+ long repeated;
2215+ zend_leak_info leak;
2216+
2217+ ZEND_MM_SET_MAGIC(p, MEM_BLOCK_LEAK);
2218+
2219+ leak.addr = ZEND_MM_DATA_OF(p);
2220+ leak.size = p->debug.size;
2221+ leak.filename = p->debug.filename;
2222+ leak.lineno = p->debug.lineno;
2223+ leak.orig_filename = p->debug.orig_filename;
2224+ leak.orig_lineno = p->debug.orig_lineno;
2225+
2226+ zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
2227+ zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak TSRMLS_CC);
2228+ repeated = zend_mm_find_leaks(segment, p);
2229+ total += 1 + repeated;
2230+ if (repeated) {
2231+ zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated TSRMLS_CC);
2232+ }
2233+#if ZEND_MM_CACHE
2234+ } else if (p->magic == MEM_BLOCK_CACHED) {
2235+ /* skip it */
2236+#endif
2237+ } else if (p->magic != MEM_BLOCK_LEAK) {
2238+ zend_mm_panic("zend_mm_heap corrupted");
2239+ }
2240+ }
2241+ if (ZEND_MM_IS_GUARD_BLOCK(q)) {
2242+ segment = segment->next_segment;
2243+ if (!segment) {
2244+ break;
2245+ }
2246+ q = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
2247+ }
2248+ p = q;
2249+ }
2250+ if (total) {
2251+ zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total TSRMLS_CC);
2252+ }
2253+}
2254+
2255+static int zend_mm_check_ptr(zend_mm_heap_canary *heap, void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2256+{
2257+ zend_mm_block_canary *p;
2258+ int no_cache_notice = 0;
2259+ int had_problems = 0;
2260+ int valid_beginning = 1;
2261+
2262+ if (silent==2) {
2263+ silent = 1;
2264+ no_cache_notice = 1;
2265+ } else if (silent==3) {
2266+ silent = 0;
2267+ no_cache_notice = 1;
2268+ }
2269+ if (!silent) {
2270+ TSRMLS_FETCH();
2271+
2272+ zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
2273+ zend_debug_alloc_output("---------------------------------------\n");
2274+ zend_debug_alloc_output("%s(%d) : Block "PTR_FMT" status:\n" ZEND_FILE_LINE_RELAY_CC, ptr);
2275+ if (__zend_orig_filename) {
2276+ zend_debug_alloc_output("%s(%d) : Actual location (location was relayed)\n" ZEND_FILE_LINE_ORIG_RELAY_CC);
2277+ }
2278+ if (!ptr) {
2279+ zend_debug_alloc_output("NULL\n");
2280+ zend_debug_alloc_output("---------------------------------------\n");
2281+ return 0;
2282+ }
2283+ }
2284+
2285+ if (!ptr) {
2286+ if (silent) {
2287+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2288+ }
2289+ }
2290+
2291+ p = ZEND_MM_HEADER_OF(ptr);
2292+
2293+#ifdef ZTS
2294+ if (ZEND_MM_BAD_THREAD_ID(p)) {
2295+ if (!silent) {
2296+ zend_debug_alloc_output("Invalid pointer: ((thread_id=0x%0.8X) != (expected=0x%0.8X))\n", (long)p->thread_id, (long)tsrm_thread_id());
2297+ had_problems = 1;
2298+ } else {
2299+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2300+ }
2301+ }
2302+#endif
2303+
2304+ if (p->info._size != ZEND_MM_NEXT_BLOCK(p)->info._prev) {
2305+ if (!silent) {
2306+ zend_debug_alloc_output("Invalid pointer: ((size="PTR_FMT") != (next.prev="PTR_FMT"))\n", p->info._size, ZEND_MM_NEXT_BLOCK(p)->info._prev);
2307+ had_problems = 1;
2308+ } else {
2309+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2310+ }
2311+ }
2312+ if (p->info._prev != ZEND_MM_GUARD_BLOCK &&
2313+ ZEND_MM_PREV_BLOCK(p)->info._size != p->info._prev) {
2314+ if (!silent) {
2315+ zend_debug_alloc_output("Invalid pointer: ((prev="PTR_FMT") != (prev.size="PTR_FMT"))\n", p->info._prev, ZEND_MM_PREV_BLOCK(p)->info._size);
2316+ had_problems = 1;
2317+ } else {
2318+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2319+ }
2320+ }
2321+
2322+ if (had_problems) {
2323+ zend_debug_alloc_output("---------------------------------------\n");
2324+ return 0;
2325+ }
2326+
2327+ if (!silent) {
2328+ zend_debug_alloc_output("%10s\t","Beginning: ");
2329+ }
2330+
2331+ if (!ZEND_MM_IS_USED_BLOCK(p)) {
2332+ if (!silent) {
2333+ if (p->magic != MEM_BLOCK_FREED) {
2334+ zend_debug_alloc_output("Freed (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_FREED);
2335+ } else {
2336+ zend_debug_alloc_output("Freed\n");
2337+ }
2338+ had_problems = 1;
2339+ } else {
2340+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2341+ }
2342+ } else if (ZEND_MM_IS_GUARD_BLOCK(p)) {
2343+ if (!silent) {
2344+ if (p->magic != MEM_BLOCK_FREED) {
2345+ zend_debug_alloc_output("Guard (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_FREED);
2346+ } else {
2347+ zend_debug_alloc_output("Guard\n");
2348+ }
2349+ had_problems = 1;
2350+ } else {
2351+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2352+ }
2353+ } else {
2354+ switch (p->magic) {
2355+ case MEM_BLOCK_VALID:
2356+ case MEM_BLOCK_LEAK:
2357+ if (!silent) {
2358+ zend_debug_alloc_output("OK (allocated on %s:%d, %d bytes)\n", p->debug.filename, p->debug.lineno, (int)p->debug.size);
2359+ }
2360+ break; /* ok */
2361+ case MEM_BLOCK_CACHED:
2362+ if (!no_cache_notice) {
2363+ if (!silent) {
2364+ zend_debug_alloc_output("Cached\n");
2365+ had_problems = 1;
2366+ } else {
2367+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2368+ }
2369+ }
2370+ case MEM_BLOCK_FREED:
2371+ if (!silent) {
2372+ zend_debug_alloc_output("Freed (invalid)\n");
2373+ had_problems = 1;
2374+ } else {
2375+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2376+ }
2377+ break;
2378+ case MEM_BLOCK_GUARD:
2379+ if (!silent) {
2380+ zend_debug_alloc_output("Guard (invalid)\n");
2381+ had_problems = 1;
2382+ } else {
2383+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2384+ }
2385+ break;
2386+ default:
2387+ if (!silent) {
2388+ zend_debug_alloc_output("Unknown (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_VALID);
2389+ had_problems = 1;
2390+ valid_beginning = 0;
2391+ } else {
2392+ return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2393+ }
2394+ break;
2395+ }
2396+ }
2397+
2398+#if ZEND_MM_HEAP_PROTECTION
2399+ if (!valid_beginning) {
2400+ if (!silent) {
2401+ zend_debug_alloc_output("%10s\t", "Start:");
2402+ zend_debug_alloc_output("Unknown\n");
2403+ zend_debug_alloc_output("%10s\t", "End:");
2404+ zend_debug_alloc_output("Unknown\n");
2405+ }
2406+ } else {
2407+ char *end_magic = ZEND_MM_END_MAGIC_PTR(p);
2408+
2409+ if (p->debug.start_magic == _mem_block_start_magic) {
2410+ if (!silent) {
2411+ zend_debug_alloc_output("%10s\t", "Start:");
2412+ zend_debug_alloc_output("OK\n");
2413+ }
2414+ } else {
2415+ char *overflow_ptr, *magic_ptr=(char *) &_mem_block_start_magic;
2416+ int overflows=0;
2417+ int i;
2418+
2419+ if (silent) {
2420+ return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2421+ }
2422+ had_problems = 1;
2423+ overflow_ptr = (char *) &p->debug.start_magic;
2424+ i = END_MAGIC_SIZE;
2425+ while (--i >= 0) {
2426+ if (overflow_ptr[i]!=magic_ptr[i]) {
2427+ overflows++;
2428+ }
2429+ }
2430+ zend_debug_alloc_output("%10s\t", "Start:");
2431+ zend_debug_alloc_output("Overflown (magic=0x%0.8X instead of 0x%0.8X)\n", p->debug.start_magic, _mem_block_start_magic);
2432+ zend_debug_alloc_output("%10s\t","");
2433+ if (overflows >= END_MAGIC_SIZE) {
2434+ zend_debug_alloc_output("At least %d bytes overflown\n", END_MAGIC_SIZE);
2435+ } else {
2436+ zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
2437+ }
2438+ }
2439+ if (memcmp(end_magic, &_mem_block_end_magic, END_MAGIC_SIZE)==0) {
2440+ if (!silent) {
2441+ zend_debug_alloc_output("%10s\t", "End:");
2442+ zend_debug_alloc_output("OK\n");
2443+ }
2444+ } else {
2445+ char *overflow_ptr, *magic_ptr=(char *) &_mem_block_end_magic;
2446+ int overflows=0;
2447+ int i;
2448+
2449+ if (silent) {
2450+ return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
2451+ }
2452+ had_problems = 1;
2453+ overflow_ptr = (char *) end_magic;
2454+
2455+ for (i=0; i < END_MAGIC_SIZE; i++) {
2456+ if (overflow_ptr[i]!=magic_ptr[i]) {
2457+ overflows++;
2458+ }
2459+ }
2460+
2461+ zend_debug_alloc_output("%10s\t", "End:");
2462+ zend_debug_alloc_output("Overflown (magic=0x%0.8X instead of 0x%0.8X)\n", *end_magic, _mem_block_end_magic);
2463+ zend_debug_alloc_output("%10s\t","");
2464+ if (overflows >= END_MAGIC_SIZE) {
2465+ zend_debug_alloc_output("At least %d bytes overflown\n", END_MAGIC_SIZE);
2466+ } else {
2467+ zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
2468+ }
2469+ }
2470+ }
2471+#endif
2472+
2473+ if (!silent) {
2474+ zend_debug_alloc_output("---------------------------------------\n");
2475+ }
2476+ return ((!had_problems) ? 1 : 0);
2477+}
2478+
2479+static int zend_mm_check_heap(zend_mm_heap_canary *heap, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2480+{
2481+ zend_mm_segment *segment = heap->segments_list;
2482+ zend_mm_block_canary *p, *q;
2483+ int errors = 0;
2484+
2485+ if (!segment) {
2486+ return 0;
2487+ }
2488+ p = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
2489+ while (1) {
2490+ q = ZEND_MM_NEXT_BLOCK(p);
2491+ if (q <= p ||
2492+ (char*)q > (char*)segment + segment->size ||
2493+ p->info._size != q->info._prev) {
2494+ zend_mm_panic("zend_mm_heap corrupted");
2495+ }
2496+ if (!ZEND_MM_IS_FREE_BLOCK(p)) {
2497+ if (p->magic == MEM_BLOCK_VALID || p->magic == MEM_BLOCK_LEAK) {
2498+ if (!zend_mm_check_ptr(heap, ZEND_MM_DATA_OF(p), (silent?2:3) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)) {
2499+ errors++;
2500+ }
2501+#if ZEND_MM_CACHE
2502+ } else if (p->magic == MEM_BLOCK_CACHED) {
2503+ /* skip it */
2504+#endif
2505+ } else if (p->magic != MEM_BLOCK_LEAK) {
2506+ zend_mm_panic("zend_mm_heap corrupted");
2507+ }
2508+ }
2509+ if (ZEND_MM_IS_GUARD_BLOCK(q)) {
2510+ segment = segment->next_segment;
2511+ if (!segment) {
2512+ return errors;
2513+ }
2514+ q = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
2515+ }
2516+ p = q;
2517+ }
2518+}
2519+#endif
2520+
2521+void __zend_mm_shutdown_canary(zend_mm_heap_canary *heap, int full_shutdown, int silent TSRMLS_DC)
2522+{
2523+ zend_mm_storage *storage;
2524+ zend_mm_segment *segment;
2525+ zend_mm_segment *prev;
2526+ int internal;
2527+
2528+ if (heap->reserve) {
2529+#if ZEND_DEBUG
2530+ if (!silent) {
2531+ _zend_mm_free(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
2532+ }
2533+#endif
2534+ heap->reserve = NULL;
2535+ }
2536+
2537+#if ZEND_MM_CACHE_STAT
2538+ if (full_shutdown) {
2539+ FILE *f;
2540+
2541+ f = fopen("zend_mm.log", "w");
2542+ if (f) {
2543+ int i,j;
2544+ size_t size, true_size, min_size, max_size;
2545+ int hit = 0, miss = 0;
2546+
2547+ fprintf(f, "\nidx min_size max_size true_size max_len hits misses\n");
2548+ size = 0;
2549+ while (1) {
2550+ true_size = ZEND_MM_TRUE_SIZE(size);
2551+ if (ZEND_MM_SMALL_SIZE(true_size)) {
2552+ min_size = size;
2553+ i = ZEND_MM_BUCKET_INDEX(true_size);
2554+ size++;
2555+ while (1) {
2556+ true_size = ZEND_MM_TRUE_SIZE(size);
2557+ if (ZEND_MM_SMALL_SIZE(true_size)) {
2558+ j = ZEND_MM_BUCKET_INDEX(true_size);
2559+ if (j > i) {
2560+ max_size = size-1;
2561+ break;
2562+ }
2563+ } else {
2564+ max_size = size-1;
2565+ break;
2566+ }
2567+ size++;
2568+ }
2569+ hit += heap->cache_stat[i].hit;
2570+ miss += heap->cache_stat[i].miss;
2571+ fprintf(f, "%2d %8d %8d %9d %8d %8d %8d\n", i, (int)min_size, (int)max_size, ZEND_MM_TRUE_SIZE(max_size), heap->cache_stat[i].max_count, heap->cache_stat[i].hit, heap->cache_stat[i].miss);
2572+ } else {
2573+ break;
2574+ }
2575+ }
2576+ fprintf(f, " %8d %8d\n", hit, miss);
2577+ fprintf(f, " %8d %8d\n", heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit, heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss);
2578+ fclose(f);
2579+ }
2580+ }
2581+#endif
2582+
2583+#if ZEND_DEBUG
2584+ if (!silent) {
2585+ zend_mm_check_leaks(heap TSRMLS_CC);
2586+ }
2587+#endif
2588+
2589+ internal = heap->internal;
2590+ storage = heap->storage;
2591+ segment = heap->segments_list;
2592+ while (segment) {
2593+ prev = segment;
2594+ segment = segment->next_segment;
2595+ ZEND_MM_STORAGE_FREE(prev);
2596+ }
2597+ if (full_shutdown) {
2598+ storage->handlers->dtor(storage);
2599+ if (!internal) {
2600+ free(heap);
2601+ }
2602+ } else {
2603+ if (heap->compact_size &&
2604+ heap->real_peak > heap->compact_size) {
2605+ storage->handlers->compact(storage);
2606+ }
2607+ heap->segments_list = NULL;
2608+ zend_mm_init(heap);
2609+ heap->real_size = 0;
2610+ heap->real_peak = 0;
2611+ heap->size = 0;
2612+ heap->peak = 0;
2613+ if (heap->reserve_size) {
2614+ heap->reserve = _zend_mm_alloc((zend_mm_heap *)heap, heap->reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
2615+ }
2616+ heap->overflow = 0;
2617+ }
2618+}
2619+
2620+static void zend_mm_safe_error(zend_mm_heap_canary *heap,
2621+ const char *format,
2622+ size_t limit,
2623+#if ZEND_DEBUG
2624+ const char *filename,
2625+ uint lineno,
2626+#endif
2627+ size_t size)
2628+{
2629+ if (heap->reserve) {
2630+ _zend_mm_free_canary_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
2631+ heap->reserve = NULL;
2632+ }
2633+ if (heap->overflow == 0) {
2634+ char *error_filename;
2635+ uint error_lineno;
2636+ TSRMLS_FETCH();
2637+ if (zend_is_compiling(TSRMLS_C)) {
2638+ error_filename = zend_get_compiled_filename(TSRMLS_C);
2639+ error_lineno = zend_get_compiled_lineno(TSRMLS_C);
2640+ } else if (EG(in_execution)) {
2641+ error_filename = EG(active_op_array)?EG(active_op_array)->filename:NULL;
2642+ error_lineno = EG(opline_ptr)?(*EG(opline_ptr))->lineno:0;
2643+ } else {
2644+ error_filename = NULL;
2645+ error_lineno = 0;
2646+ }
2647+ if (!error_filename) {
2648+ error_filename = "Unknown";
2649+ }
2650+ heap->overflow = 1;
2651+ zend_try {
2652+ zend_error_noreturn(E_ERROR,
2653+ format,
2654+ limit,
2655+#if ZEND_DEBUG
2656+ filename,
2657+ lineno,
2658+#endif
2659+ size);
2660+ } zend_catch {
2661+ if (heap->overflow == 2) {
2662+ fprintf(stderr, "\nFatal error: ");
2663+ fprintf(stderr,
2664+ format,
2665+ limit,
2666+#if ZEND_DEBUG
2667+ filename,
2668+ lineno,
2669+#endif
2670+ size);
2671+ fprintf(stderr, " in %s on line %d\n", error_filename, error_lineno);
2672+ }
2673+ } zend_end_try();
2674+ } else {
2675+ heap->overflow = 2;
2676+ }
2677+ zend_bailout();
2678+}
2679+
2680+static zend_mm_free_block_canary *zend_mm_search_large_block(zend_mm_heap_canary *heap, size_t true_size)
2681+{
2682+ zend_mm_free_block_canary *best_fit;
2683+ size_t index = ZEND_MM_LARGE_BUCKET_INDEX(true_size);
2684+ size_t bitmap = heap->large_free_bitmap >> index;
2685+ zend_mm_free_block_canary *p;
2686+
2687+ if (bitmap == 0) {
2688+ return NULL;
2689+ }
2690+
2691+ if (UNEXPECTED((bitmap & 1) != 0)) {
2692+ /* Search for best "large" free block */
2693+ zend_mm_free_block_canary *rst = NULL;
2694+ size_t m;
2695+ size_t best_size = -1;
2696+
2697+ best_fit = NULL;
2698+ p = heap->large_free_buckets[index];
2699+ for (m = true_size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
2700+ if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
2701+ return p->next_free_block;
2702+ } else if (ZEND_MM_FREE_BLOCK_SIZE(p) >= true_size &&
2703+ ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
2704+ best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
2705+ best_fit = p;
2706+ }
2707+ if ((m & (ZEND_MM_LONG_CONST(1) << (ZEND_MM_NUM_BUCKETS-1))) == 0) {
2708+ if (p->child[1]) {
2709+ rst = p->child[1];
2710+ }
2711+ if (p->child[0]) {
2712+ p = p->child[0];
2713+ } else {
2714+ break;
2715+ }
2716+ } else if (p->child[1]) {
2717+ p = p->child[1];
2718+ } else {
2719+ break;
2720+ }
2721+ }
2722+
2723+ for (p = rst; p; p = p->child[p->child[0] != NULL]) {
2724+ if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
2725+ return p->next_free_block;
2726+ } else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
2727+ ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
2728+ best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
2729+ best_fit = p;
2730+ }
2731+ }
2732+
2733+ if (best_fit) {
2734+ return best_fit->next_free_block;
2735+ }
2736+ bitmap = bitmap >> 1;
2737+ if (!bitmap) {
2738+ return NULL;
2739+ }
2740+ index++;
2741+ }
2742+
2743+ /* Search for smallest "large" free block */
2744+ best_fit = p = heap->large_free_buckets[index + zend_mm_low_bit(bitmap)];
2745+ while ((p = p->child[p->child[0] != NULL])) {
2746+ if (ZEND_MM_FREE_BLOCK_SIZE(p) < ZEND_MM_FREE_BLOCK_SIZE(best_fit)) {
2747+ best_fit = p;
2748+ }
2749+ }
2750+ return best_fit->next_free_block;
2751+}
2752+
2753+void *_zend_mm_alloc_canary_int(zend_mm_heap_canary *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2754+{
2755+ zend_mm_free_block_canary *best_fit;
2756+ size_t true_size = ZEND_MM_TRUE_SIZE(size);
2757+ size_t block_size;
2758+ size_t remaining_size;
2759+ size_t segment_size;
2760+ zend_mm_segment *segment;
2761+ int keep_rest = 0;
2762+
2763+ if (EXPECTED(ZEND_MM_SMALL_SIZE(true_size))) {
2764+ size_t index = ZEND_MM_BUCKET_INDEX(true_size);
2765+ size_t bitmap;
2766+
2767+ if (UNEXPECTED(true_size < size)) {
2768+ goto out_of_memory;
2769+ }
2770+#if ZEND_MM_CACHE
2771+ if (EXPECTED(heap->cache[index] != NULL)) {
2772+ /* Get block from cache */
2773+#if ZEND_MM_CACHE_STAT
2774+ heap->cache_stat[index].count--;
2775+ heap->cache_stat[index].hit++;
2776+#endif
2777+ best_fit = heap->cache[index];
2778+ heap->cache[index] = best_fit->prev_free_block;
2779+ heap->cached -= true_size;
2780+#if SUHOSIN_PATCH
2781+ SUHOSIN_MM_SET_CANARIES(best_fit);
2782+ ((zend_mm_block_canary*)best_fit)->info.size = size;
2783+ SUHOSIN_MM_SET_END_CANARY(best_fit);
2784+#endif
2785+ ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
2786+ ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
2787+ return ZEND_MM_DATA_OF(best_fit);
2788+ }
2789+#if ZEND_MM_CACHE_STAT
2790+ heap->cache_stat[index].miss++;
2791+#endif
2792+#endif
2793+
2794+ bitmap = heap->free_bitmap >> index;
2795+ if (bitmap) {
2796+ /* Found some "small" free block that can be used */
2797+ index += zend_mm_low_bit(bitmap);
2798+ best_fit = heap->free_buckets[index*2];
2799+#if ZEND_MM_CACHE_STAT
2800+ heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit++;
2801+#endif
2802+ goto zend_mm_finished_searching_for_block;
2803+ }
2804+ }
2805+
2806+#if ZEND_MM_CACHE_STAT
2807+ heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss++;
2808+#endif
2809+
2810+ best_fit = zend_mm_search_large_block(heap, true_size);
2811+
2812+ if (!best_fit && heap->real_size >= heap->limit - heap->block_size) {
2813+ zend_mm_free_block_canary *p = heap->rest_buckets[0];
2814+ size_t best_size = -1;
2815+
2816+ while (p != ZEND_MM_REST_BUCKET(heap)) {
2817+ if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
2818+ best_fit = p;
2819+ goto zend_mm_finished_searching_for_block;
2820+ } else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
2821+ ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
2822+ best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
2823+ best_fit = p;
2824+ }
2825+ p = p->prev_free_block;
2826+ }
2827+ }
2828+
2829+ if (!best_fit) {
2830+ if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
2831+ /* Make sure we add a memory block which is big enough,
2832+ segment must have header "size" and trailer "guard" block */
2833+ segment_size = true_size + ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE;
2834+ segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
2835+ keep_rest = 1;
2836+ } else {
2837+ segment_size = heap->block_size;
2838+ }
2839+
2840+ HANDLE_BLOCK_INTERRUPTIONS();
2841+
2842+ if (segment_size < true_size ||
2843+ heap->real_size + segment_size > heap->limit) {
2844+ /* Memory limit overflow */
2845+#if ZEND_MM_CACHE
2846+ zend_mm_free_cache(heap);
2847+#endif
2848+ HANDLE_UNBLOCK_INTERRUPTIONS();
2849+#if ZEND_DEBUG
2850+ zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %lu bytes)", heap->limit, __zend_filename, __zend_lineno, size);
2851+#else
2852+ zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %lu bytes)", heap->limit, size);
2853+#endif
2854+ }
2855+
2856+ segment = (zend_mm_segment *) ZEND_MM_STORAGE_ALLOC(segment_size);
2857+
2858+ if (!segment) {
2859+ /* Storage manager cannot allocate memory */
2860+#if ZEND_MM_CACHE
2861+ zend_mm_free_cache(heap);
2862+#endif
2863+ HANDLE_UNBLOCK_INTERRUPTIONS();
2864+out_of_memory:
2865+#if ZEND_DEBUG
2866+ zend_mm_safe_error(heap, "Out of memory (allocated %ld) at %s:%d (tried to allocate %lu bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
2867+#else
2868+ zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %lu bytes)", heap->real_size, size);
2869+#endif
2870+ return NULL;
2871+ }
2872+
2873+ heap->real_size += segment_size;
2874+ if (heap->real_size > heap->real_peak) {
2875+ heap->real_peak = heap->real_size;
2876+ }
2877+
2878+ segment->size = segment_size;
2879+ segment->next_segment = heap->segments_list;
2880+ heap->segments_list = segment;
2881+
2882+ best_fit = (zend_mm_free_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
2883+ ZEND_MM_MARK_FIRST_BLOCK(best_fit);
2884+
2885+ block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
2886+
2887+ ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(best_fit, block_size));
2888+
2889+ } else {
2890+zend_mm_finished_searching_for_block:
2891+ /* remove from free list */
2892+ HANDLE_BLOCK_INTERRUPTIONS();
2893+ ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_FREED);
2894+ ZEND_MM_CHECK_COOKIE(best_fit);
2895+ ZEND_MM_CHECK_BLOCK_LINKAGE(best_fit);
2896+ zend_mm_remove_from_free_list(heap, best_fit);
2897+
2898+ block_size = ZEND_MM_FREE_BLOCK_SIZE(best_fit);
2899+ }
2900+
2901+ remaining_size = block_size - true_size;
2902+
2903+ if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
2904+ true_size = block_size;
2905+ ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
2906+ } else {
2907+ zend_mm_free_block_canary *new_free_block;
2908+
2909+ /* prepare new free block */
2910+ ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
2911+ new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(best_fit, true_size);
2912+ ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
2913+
2914+ /* add the new free block to the free list */
2915+ if (EXPECTED(!keep_rest)) {
2916+ zend_mm_add_to_free_list(heap, new_free_block);
2917+ } else {
2918+ zend_mm_add_to_rest_list(heap, new_free_block);
2919+ }
2920+ }
2921+
2922+ ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 1);
2923+
2924+#if SUHOSIN_PATCH
2925+ SUHOSIN_MM_SET_CANARIES(best_fit);
2926+ ((zend_mm_block_canary*)best_fit)->info.size = size;
2927+ SUHOSIN_MM_SET_END_CANARY(best_fit);
2928+#endif
2929+
2930+ heap->size += true_size;
2931+ if (heap->peak < heap->size) {
2932+ heap->peak = heap->size;
2933+ }
2934+
2935+ HANDLE_UNBLOCK_INTERRUPTIONS();
2936+ return ZEND_MM_DATA_OF(best_fit);
2937+}
2938+
2939+
2940+void _zend_mm_free_canary_int(zend_mm_heap_canary *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2941+{
2942+ zend_mm_block_canary *mm_block;
2943+ zend_mm_block_canary *next_block;
2944+ size_t size;
2945+
2946+ if (!ZEND_MM_VALID_PTR(p)) {
2947+ return;
2948+ }
2949+
2950+ mm_block = ZEND_MM_HEADER_OF(p);
2951+ size = ZEND_MM_BLOCK_SIZE(mm_block);
2952+#if SUHOSIN_PATCH
2953+ SUHOSIN_MM_CHECK_CANARIES(mm_block, "efree()");
2954+#endif
2955+ ZEND_MM_CHECK_PROTECTION(mm_block);
2956+
2957+#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
2958+ memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->debug.size);
2959+#endif
2960+#if SUHOSIN_PATCH
2961+ if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY))) {
2962+ memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->info.size);
2963+ }
2964+#endif
2965+#if ZEND_MM_CACHE
2966+ if (EXPECTED(ZEND_MM_SMALL_SIZE(size)) && EXPECTED(heap->cached < ZEND_MM_CACHE_SIZE)) {
2967+ size_t index = ZEND_MM_BUCKET_INDEX(size);
2968+ zend_mm_free_block_canary **cache = &heap->cache[index];
2969+
2970+ ((zend_mm_free_block_canary*)mm_block)->prev_free_block = *cache;
2971+ *cache = (zend_mm_free_block_canary*)mm_block;
2972+ heap->cached += size;
2973+ ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
2974+#if ZEND_MM_CACHE_STAT
2975+ if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
2976+ heap->cache_stat[index].max_count = heap->cache_stat[index].count;
2977+ }
2978+#endif
2979+ return;
2980+ }
2981+#endif
2982+
2983+ HANDLE_BLOCK_INTERRUPTIONS();
2984+
2985+ heap->size -= size;
2986+
2987+ next_block = ZEND_MM_BLOCK_AT(mm_block, size);
2988+ if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
2989+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
2990+ size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
2991+ }
2992+ if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
2993+ mm_block = ZEND_MM_PREV_BLOCK(mm_block);
2994+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) mm_block);
2995+ size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
2996+ }
2997+ if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
2998+ ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(mm_block, size))) {
2999+ zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
3000+ } else {
3001+ ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
3002+ zend_mm_add_to_free_list(heap, (zend_mm_free_block_canary *) mm_block);
3003+ }
3004+ HANDLE_UNBLOCK_INTERRUPTIONS();
3005+}
3006+
3007+void *_zend_mm_realloc_canary_int(zend_mm_heap_canary *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
3008+{
3009+ zend_mm_block_canary *mm_block = ZEND_MM_HEADER_OF(p);
3010+ zend_mm_block_canary *next_block;
3011+ size_t true_size;
3012+ size_t orig_size;
3013+ void *ptr;
3014+
3015+ if (UNEXPECTED(!p) || !ZEND_MM_VALID_PTR(p)) {
3016+ return _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
3017+ }
3018+ mm_block = ZEND_MM_HEADER_OF(p);
3019+ true_size = ZEND_MM_TRUE_SIZE(size);
3020+ orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
3021+#if SUHOSIN_PATCH
3022+ SUHOSIN_MM_CHECK_CANARIES(mm_block, "erealloc()");
3023+#endif
3024+ ZEND_MM_CHECK_PROTECTION(mm_block);
3025+
3026+ if (UNEXPECTED(true_size < size)) {
3027+ goto out_of_memory;
3028+ }
3029+
3030+ if (true_size <= orig_size) {
3031+ size_t remaining_size = orig_size - true_size;
3032+
3033+ if (remaining_size >= ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
3034+ zend_mm_free_block_canary *new_free_block;
3035+
3036+ HANDLE_BLOCK_INTERRUPTIONS();
3037+ next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
3038+ if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
3039+ remaining_size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
3040+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
3041+ }
3042+
3043+ /* prepare new free block */
3044+ ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
3045+ new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(mm_block, true_size);
3046+
3047+ ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
3048+
3049+ /* add the new free block to the free list */
3050+ zend_mm_add_to_free_list(heap, new_free_block);
3051+ heap->size += (true_size - orig_size);
3052+ HANDLE_UNBLOCK_INTERRUPTIONS();
3053+ }
3054+ ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
3055+#if SUHOSIN_PATCH
3056+ SUHOSIN_MM_SET_CANARIES(mm_block);
3057+ ((zend_mm_block_canary*)mm_block)->info.size = size;
3058+ SUHOSIN_MM_SET_END_CANARY(mm_block);
3059+#endif
3060+ return p;
3061+ }
3062+
3063+#if ZEND_MM_CACHE
3064+ if (ZEND_MM_SMALL_SIZE(true_size)) {
3065+ size_t index = ZEND_MM_BUCKET_INDEX(true_size);
3066+
3067+ if (heap->cache[index] != NULL) {
3068+ zend_mm_free_block_canary *best_fit;
3069+ zend_mm_free_block_canary **cache;
3070+
3071+#if ZEND_MM_CACHE_STAT
3072+ heap->cache_stat[index].count--;
3073+ heap->cache_stat[index].hit++;
3074+#endif
3075+ best_fit = heap->cache[index];
3076+ heap->cache[index] = best_fit->prev_free_block;
3077+ ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
3078+ ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
3079+#if SUHOSIN_PATCH
3080+ SUHOSIN_MM_SET_CANARIES(best_fit);
3081+ ((zend_mm_block_canary*)best_fit)->info.size = size;
3082+ SUHOSIN_MM_SET_END_CANARY(best_fit);
3083+#endif
3084+
3085+ ptr = ZEND_MM_DATA_OF(best_fit);
3086+
3087+#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
3088+ memcpy(ptr, p, mm_block->debug.size);
3089+#else
3090+ memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
3091+#endif
3092+
3093+ heap->cached -= true_size - orig_size;
3094+
3095+ index = ZEND_MM_BUCKET_INDEX(orig_size);
3096+ cache = &heap->cache[index];
3097+
3098+ ((zend_mm_free_block_canary*)mm_block)->prev_free_block = *cache;
3099+ *cache = (zend_mm_free_block_canary*)mm_block;
3100+ ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
3101+#if ZEND_MM_CACHE_STAT
3102+ if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
3103+ heap->cache_stat[index].max_count = heap->cache_stat[index].count;
3104+ }
3105+#endif
3106+ return ptr;
3107+ }
3108+ }
3109+#endif
3110+
3111+ next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
3112+
3113+ if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
3114+ ZEND_MM_CHECK_COOKIE(next_block);
3115+ ZEND_MM_CHECK_BLOCK_LINKAGE(next_block);
3116+ if (orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block) >= true_size) {
3117+ size_t block_size = orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block);
3118+ size_t remaining_size = block_size - true_size;
3119+
3120+ HANDLE_BLOCK_INTERRUPTIONS();
3121+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
3122+
3123+ if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
3124+ true_size = block_size;
3125+ ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
3126+ } else {
3127+ zend_mm_free_block_canary *new_free_block;
3128+
3129+ /* prepare new free block */
3130+ ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
3131+ new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(mm_block, true_size);
3132+ ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
3133+
3134+ /* add the new free block to the free list */
3135+ if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
3136+ ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(new_free_block, remaining_size))) {
3137+ zend_mm_add_to_rest_list(heap, new_free_block);
3138+ } else {
3139+ zend_mm_add_to_free_list(heap, new_free_block);
3140+ }
3141+ }
3142+ ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
3143+ heap->size = heap->size + true_size - orig_size;
3144+ if (heap->peak < heap->size) {
3145+ heap->peak = heap->size;
3146+ }
3147+ HANDLE_UNBLOCK_INTERRUPTIONS();
3148+#if SUHOSIN_PATCH
3149+ SUHOSIN_MM_SET_CANARIES(mm_block);
3150+ ((zend_mm_block_canary*)mm_block)->info.size = size;
3151+ SUHOSIN_MM_SET_END_CANARY(mm_block);
3152+#endif
3153+ return p;
3154+ } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
3155+ ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(next_block, ZEND_MM_FREE_BLOCK_SIZE(next_block)))) {
3156+ HANDLE_BLOCK_INTERRUPTIONS();
3157+ zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
3158+ goto realloc_segment;
3159+ }
3160+ } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) && ZEND_MM_IS_GUARD_BLOCK(next_block)) {
3161+ zend_mm_segment *segment;
3162+ zend_mm_segment *segment_copy;
3163+ size_t segment_size;
3164+ size_t block_size;
3165+ size_t remaining_size;
3166+
3167+ HANDLE_BLOCK_INTERRUPTIONS();
3168+realloc_segment:
3169+ /* segment size, size of block and size of guard block */
3170+ if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
3171+ segment_size = true_size+ZEND_MM_ALIGNED_SEGMENT_SIZE+ZEND_MM_ALIGNED_HEADER_SIZE;
3172+ segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
3173+ } else {
3174+ segment_size = heap->block_size;
3175+ }
3176+
3177+ segment_copy = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
3178+ if (segment_size < true_size ||
3179+ heap->real_size + segment_size - segment_copy->size > heap->limit) {
3180+ if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
3181+ zend_mm_add_to_free_list(heap, (zend_mm_free_block_canary *) next_block);
3182+ }
3183+#if ZEND_MM_CACHE
3184+ zend_mm_free_cache(heap);
3185+#endif
3186+ HANDLE_UNBLOCK_INTERRUPTIONS();
3187+#if ZEND_DEBUG
3188+ zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %ld bytes)", heap->limit, __zend_filename, __zend_lineno, size);
3189+#else
3190+ zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %ld bytes)", heap->limit, size);
3191+#endif
3192+ return NULL;
3193+ }
3194+
3195+ segment = ZEND_MM_STORAGE_REALLOC(segment_copy, segment_size);
3196+ if (!segment) {
3197+#if ZEND_MM_CACHE
3198+ zend_mm_free_cache(heap);
3199+#endif
3200+ HANDLE_UNBLOCK_INTERRUPTIONS();
3201+out_of_memory:
3202+#if ZEND_DEBUG
3203+ zend_mm_safe_error(heap, "Out of memory (allocated %ld) at %s:%d (tried to allocate %ld bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
3204+#else
3205+ zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %ld bytes)", heap->real_size, size);
3206+#endif
3207+ return NULL;
3208+ }
3209+ heap->real_size += segment_size - segment->size;
3210+ if (heap->real_size > heap->real_peak) {
3211+ heap->real_peak = heap->real_size;
3212+ }
3213+
3214+ segment->size = segment_size;
3215+
3216+ if (segment != segment_copy) {
3217+ zend_mm_segment **seg = &heap->segments_list;
3218+ while (*seg != segment_copy) {
3219+ seg = &(*seg)->next_segment;
3220+ }
3221+ *seg = segment;
3222+ mm_block = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
3223+ ZEND_MM_MARK_FIRST_BLOCK(mm_block);
3224+ }
3225+
3226+ block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
3227+ remaining_size = block_size - true_size;
3228+
3229+ /* setup guard block */
3230+ ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(mm_block, block_size));
3231+
3232+ if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
3233+ true_size = block_size;
3234+ ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
3235+ } else {
3236+ zend_mm_free_block_canary *new_free_block;
3237+
3238+ /* prepare new free block */
3239+ ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
3240+ new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(mm_block, true_size);
3241+ ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
3242+
3243+ /* add the new free block to the free list */
3244+ zend_mm_add_to_rest_list(heap, new_free_block);
3245+ }
3246+
3247+ ZEND_MM_SET_DEBUG_INFO(mm_block, size, 1, 1);
3248+
3249+ heap->size = heap->size + true_size - orig_size;
3250+ if (heap->peak < heap->size) {
3251+ heap->peak = heap->size;
3252+ }
3253+
3254+ HANDLE_UNBLOCK_INTERRUPTIONS();
3255+#if SUHOSIN_PATCH
3256+ SUHOSIN_MM_SET_CANARIES(mm_block);
3257+ ((zend_mm_block_canary*)mm_block)->info.size = size;
3258+ SUHOSIN_MM_SET_END_CANARY(mm_block);
3259+#endif
3260+ return ZEND_MM_DATA_OF(mm_block);
3261+ }
3262+
3263+ ptr = _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
3264+#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
3265+ memcpy(ptr, p, mm_block->debug.size);
3266+#else
3267+ memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
3268+#endif
3269+ _zend_mm_free_canary_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
3270+ return ptr;
3271+}
3272+
3273+ZEND_API size_t _zend_mm_block_size_canary(zend_mm_heap_canary *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
3274+{
3275+ zend_mm_block_canary *mm_block;
3276+
3277+ if (!ZEND_MM_VALID_PTR(p)) {
3278+ return 0;
3279+ }
3280+ mm_block = ZEND_MM_HEADER_OF(p);
3281+ ZEND_MM_CHECK_PROTECTION(mm_block);
3282+#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
3283+ return mm_block->debug.size;
3284+#else
3285+ return ZEND_MM_BLOCK_SIZE(mm_block);
3286+#endif
3287+}
3288+
3289+#if defined(__GNUC__) && defined(i386)
3290+
3291+static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
3292+{
3293+ size_t res = nmemb;
3294+ unsigned long overflow = 0;
3295+
3296+ __asm__ ("mull %3\n\taddl %4,%0\n\tadcl %1,%1"
3297+ : "=&a"(res), "=&d" (overflow)
3298+ : "%0"(res),
3299+ "rm"(size),
3300+ "rm"(offset));
3301+
3302+ if (UNEXPECTED(overflow)) {
3303+ zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
3304+ return 0;
3305+ }
3306+ return res;
3307+}
3308+
3309+#elif defined(__GNUC__) && defined(__x86_64__)
3310+
3311+static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
3312+{
3313+ size_t res = nmemb;
3314+ unsigned long overflow = 0;
3315+
3316+ __asm__ ("mulq %3\n\taddq %4,%0\n\tadcq %1,%1"
3317+ : "=&a"(res), "=&d" (overflow)
3318+ : "%0"(res),
3319+ "rm"(size),
3320+ "rm"(offset));
3321+
3322+ if (UNEXPECTED(overflow)) {
3323+ zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
3324+ return 0;
3325+ }
3326+ return res;
3327+}
3328+
3329+#elif SIZEOF_SIZE_T == 4 && defined(HAVE_ZEND_LONG64)
3330+
3331+static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
3332+{
3333+ zend_ulong64 res = (zend_ulong64)nmemb * (zend_ulong64)size + (zend_ulong64)offset;
3334+
3335+ if (UNEXPECTED(res > (zend_ulong64)0xFFFFFFFFL)) {
3336+ zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
3337+ return 0;
3338+ }
3339+ return (size_t) res;
3340+}
3341+
3342+#else
3343+
3344+static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
3345+{
3346+ size_t res = nmemb * size + offset;
3347+ double _d = (double)nmemb * (double)size + (double)offset;
3348+ double _delta = (double)res - _d;
3349+
3350+ if (UNEXPECTED((_d + _delta ) != _d)) {
3351+ zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
3352+ return 0;
3353+ }
3354+ return res;
3355+}
3356+#endif
3357+
3358+/*
3359+ * Local variables:
3360+ * tab-width: 4
3361+ * c-basic-offset: 4
3362+ * indent-tabs-mode: t
3363+ * End:
3364+ */
3365+
3366diff -Nura php-5.3.1RC1/Zend/zend_canary.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_canary.c
3367--- php-5.3.1RC1/Zend/zend_canary.c 1970-01-01 01:00:00.000000000 +0100
3368+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_canary.c 2009-09-27 19:04:06.000000000 +0200
3369@@ -0,0 +1,64 @@
3370+/*
3371+ +----------------------------------------------------------------------+
3372+ | Suhosin-Patch for PHP |
3373+ +----------------------------------------------------------------------+
3374+ | Copyright (c) 2004-2009 Stefan Esser |
3375+ +----------------------------------------------------------------------+
3376+ | This source file is subject to version 2.02 of the PHP license, |
3377+ | that is bundled with this package in the file LICENSE, and is |
3378+ | available at through the world-wide-web at |
3379+ | http://www.php.net/license/2_02.txt. |
3380+ | If you did not receive a copy of the PHP license and are unable to |
3381+ | obtain it through the world-wide-web, please send a note to |
3382+ | license@php.net so we can mail you a copy immediately. |
3383+ +----------------------------------------------------------------------+
3384+ | Author: Stefan Esser <stefan.esser@sektioneins.de> |
3385+ +----------------------------------------------------------------------+
3386+ */
3387+/* $Id$ */
3388+
3389+#include "zend.h"
3390+
3391+#include <stdio.h>
3392+#include <stdlib.h>
3393+
3394+
3395+#if SUHOSIN_PATCH
3396+
3397+static size_t last_canary = 0x73625123;
3398+
3399+/* will be replaced later with more compatible method */
3400+ZEND_API size_t zend_canary()
3401+{
3402+ time_t t;
3403+ size_t canary;
3404+ int fd;
3405+
3406+#ifndef PHP_WIN32
3407+ fd = open("/dev/urandom", 0);
3408+ if (fd != -1) {
3409+ int r = read(fd, &canary, sizeof(canary));
3410+ close(fd);
3411+ if (r == sizeof(canary)) {
3412+ return (canary);
3413+ }
3414+ }
3415+#endif
3416+ /* not good but we never want to do this */
3417+ time(&t);
3418+ canary = *(unsigned int *)&t + getpid() << 16 + last_canary;
3419+ last_canary ^= (canary << 5) | (canary >> (32-5));
3420+ return (canary);
3421+}
3422+
3423+#endif
3424+
3425+
3426+/*
3427+ * Local variables:
3428+ * tab-width: 4
3429+ * c-basic-offset: 4
3430+ * End:
3431+ * vim600: sw=4 ts=4 fdm=marker
3432+ * vim<600: sw=4 ts=4
3433+ */
3434diff -Nura php-5.3.1RC1/Zend/zend_compile.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_compile.c
3435--- php-5.3.1RC1/Zend/zend_compile.c 2009-09-03 16:33:11.000000000 +0200
3436+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_compile.c 2009-09-27 19:09:53.000000000 +0200
3437@@ -73,6 +73,11 @@
3438 }
3439 /* }}} */
3440
3441+#if SUHOSIN_PATCH
3442+void *suhosin_zend_destroy_property_info_internal = zend_destroy_property_info_internal;
3443+void *suhosin_zend_destroy_property_info = zend_destroy_property_info;
3444+#endif
3445+
3446 static void build_runtime_defined_function_key(zval *result, const char *name, int name_length TSRMLS_DC) /* {{{ */
3447 {
3448 char char_pos_buf[32];
3449diff -Nura php-5.3.1RC1/Zend/zend_compile.h suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_compile.h
3450--- php-5.3.1RC1/Zend/zend_compile.h 2009-06-06 01:20:59.000000000 +0200
3451+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_compile.h 2009-09-27 19:04:06.000000000 +0200
3452@@ -606,6 +606,11 @@
3453 ZEND_API int zend_auto_global_disable_jit(const char *varname, zend_uint varname_length TSRMLS_DC);
3454 ZEND_API size_t zend_dirname(char *path, size_t len);
3455
3456+#if SUHOSIN_PATCH
3457+extern void *suhosin_zend_destroy_property_info_internal;
3458+extern void *suhosin_zend_destroy_property_info;
3459+#endif
3460+
3461 int zendlex(znode *zendlval TSRMLS_DC);
3462
3463 /* BEGIN: OPCODES */
3464diff -Nura php-5.3.1RC1/Zend/zend_constants.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_constants.c
3465--- php-5.3.1RC1/Zend/zend_constants.c 2009-01-12 22:54:37.000000000 +0100
3466+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_constants.c 2009-09-27 19:04:06.000000000 +0200
3467@@ -113,6 +113,76 @@
3468
3469 REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
3470
3471+#if SUHOSIN_PATCH
3472+ REGISTER_MAIN_LONG_CONSTANT("S_MEMORY", S_MEMORY, CONST_PERSISTENT | CONST_CS);
3473+ REGISTER_MAIN_LONG_CONSTANT("S_VARS", S_VARS, CONST_PERSISTENT | CONST_CS);
3474+ REGISTER_MAIN_LONG_CONSTANT("S_FILES", S_FILES, CONST_PERSISTENT | CONST_CS);
3475+ REGISTER_MAIN_LONG_CONSTANT("S_INCLUDE", S_INCLUDE, CONST_PERSISTENT | CONST_CS);
3476+ REGISTER_MAIN_LONG_CONSTANT("S_SQL", S_SQL, CONST_PERSISTENT | CONST_CS);
3477+ REGISTER_MAIN_LONG_CONSTANT("S_EXECUTOR", S_EXECUTOR, CONST_PERSISTENT | CONST_CS);
3478+ REGISTER_MAIN_LONG_CONSTANT("S_MAIL", S_MAIL, CONST_PERSISTENT | CONST_CS);
3479+ REGISTER_MAIN_LONG_CONSTANT("S_SESSION", S_SESSION, CONST_PERSISTENT | CONST_CS);
3480+ REGISTER_MAIN_LONG_CONSTANT("S_MISC", S_MISC, CONST_PERSISTENT | CONST_CS);
3481+ REGISTER_MAIN_LONG_CONSTANT("S_INTERNAL", S_INTERNAL, CONST_PERSISTENT | CONST_CS);
3482+ REGISTER_MAIN_LONG_CONSTANT("S_ALL", S_ALL, CONST_PERSISTENT | CONST_CS);
3483+
3484+ /* error levels */
3485+ REGISTER_MAIN_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
3486+ REGISTER_MAIN_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
3487+ REGISTER_MAIN_LONG_CONSTANT("LOG_CRIT", LOG_CRIT, CONST_CS | CONST_PERSISTENT); /* critical conditions */
3488+ REGISTER_MAIN_LONG_CONSTANT("LOG_ERR", LOG_ERR, CONST_CS | CONST_PERSISTENT);
3489+ REGISTER_MAIN_LONG_CONSTANT("LOG_WARNING", LOG_WARNING, CONST_CS | CONST_PERSISTENT);
3490+ REGISTER_MAIN_LONG_CONSTANT("LOG_NOTICE", LOG_NOTICE, CONST_CS | CONST_PERSISTENT);
3491+ REGISTER_MAIN_LONG_CONSTANT("LOG_INFO", LOG_INFO, CONST_CS | CONST_PERSISTENT);
3492+ REGISTER_MAIN_LONG_CONSTANT("LOG_DEBUG", LOG_DEBUG, CONST_CS | CONST_PERSISTENT);
3493+ /* facility: type of program logging the message */
3494+ REGISTER_MAIN_LONG_CONSTANT("LOG_KERN", LOG_KERN, CONST_CS | CONST_PERSISTENT);
3495+ REGISTER_MAIN_LONG_CONSTANT("LOG_USER", LOG_USER, CONST_CS | CONST_PERSISTENT); /* generic user level */
3496+ REGISTER_MAIN_LONG_CONSTANT("LOG_MAIL", LOG_MAIL, CONST_CS | CONST_PERSISTENT); /* log to email */
3497+ REGISTER_MAIN_LONG_CONSTANT("LOG_DAEMON", LOG_DAEMON, CONST_CS | CONST_PERSISTENT); /* other system daemons */
3498+ REGISTER_MAIN_LONG_CONSTANT("LOG_AUTH", LOG_AUTH, CONST_CS | CONST_PERSISTENT);
3499+ REGISTER_MAIN_LONG_CONSTANT("LOG_SYSLOG", LOG_SYSLOG, CONST_CS | CONST_PERSISTENT);
3500+ REGISTER_MAIN_LONG_CONSTANT("LOG_LPR", LOG_LPR, CONST_CS | CONST_PERSISTENT);
3501+#ifdef LOG_NEWS
3502+ /* No LOG_NEWS on HP-UX */
3503+ REGISTER_MAIN_LONG_CONSTANT("LOG_NEWS", LOG_NEWS, CONST_CS | CONST_PERSISTENT); /* usenet new */
3504+#endif
3505+#ifdef LOG_UUCP
3506+ /* No LOG_UUCP on HP-UX */
3507+ REGISTER_MAIN_LONG_CONSTANT("LOG_UUCP", LOG_UUCP, CONST_CS | CONST_PERSISTENT);
3508+#endif
3509+#ifdef LOG_CRON
3510+ /* apparently some systems don't have this one */
3511+ REGISTER_MAIN_LONG_CONSTANT("LOG_CRON", LOG_CRON, CONST_CS | CONST_PERSISTENT);
3512+#endif
3513+#ifdef LOG_AUTHPRIV
3514+ /* AIX doesn't have LOG_AUTHPRIV */
3515+ REGISTER_MAIN_LONG_CONSTANT("LOG_AUTHPRIV", LOG_AUTHPRIV, CONST_CS | CONST_PERSISTENT);
3516+#endif
3517+#ifndef PHP_WIN32
3518+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL0", LOG_LOCAL0, CONST_CS | CONST_PERSISTENT);
3519+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL1", LOG_LOCAL1, CONST_CS | CONST_PERSISTENT);
3520+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL2", LOG_LOCAL2, CONST_CS | CONST_PERSISTENT);
3521+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL3", LOG_LOCAL3, CONST_CS | CONST_PERSISTENT);
3522+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL4", LOG_LOCAL4, CONST_CS | CONST_PERSISTENT);
3523+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL5", LOG_LOCAL5, CONST_CS | CONST_PERSISTENT);
3524+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL6", LOG_LOCAL6, CONST_CS | CONST_PERSISTENT);
3525+ REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL7", LOG_LOCAL7, CONST_CS | CONST_PERSISTENT);
3526+#endif
3527+ /* options */
3528+ REGISTER_MAIN_LONG_CONSTANT("LOG_PID", LOG_PID, CONST_CS | CONST_PERSISTENT);
3529+ REGISTER_MAIN_LONG_CONSTANT("LOG_CONS", LOG_CONS, CONST_CS | CONST_PERSISTENT);
3530+ REGISTER_MAIN_LONG_CONSTANT("LOG_ODELAY", LOG_ODELAY, CONST_CS | CONST_PERSISTENT);
3531+ REGISTER_MAIN_LONG_CONSTANT("LOG_NDELAY", LOG_NDELAY, CONST_CS | CONST_PERSISTENT);
3532+#ifdef LOG_NOWAIT
3533+ REGISTER_MAIN_LONG_CONSTANT("LOG_NOWAIT", LOG_NOWAIT, CONST_CS | CONST_PERSISTENT);
3534+#endif
3535+#ifdef LOG_PERROR
3536+ /* AIX doesn't have LOG_PERROR */
3537+ REGISTER_MAIN_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
3538+#endif
3539+#endif
3540+
3541 /* true/false constants */
3542 {
3543 zend_constant c;
3544diff -Nura php-5.3.1RC1/Zend/zend_errors.h suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_errors.h
3545--- php-5.3.1RC1/Zend/zend_errors.h 2008-12-31 12:15:49.000000000 +0100
3546+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_errors.h 2009-09-27 19:04:06.000000000 +0200
3547@@ -41,6 +41,20 @@
3548 #define E_ALL (E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_RECOVERABLE_ERROR | E_DEPRECATED | E_USER_DEPRECATED)
3549 #define E_CORE (E_CORE_ERROR | E_CORE_WARNING)
3550
3551+#if SUHOSIN_PATCH
3552+#define S_MEMORY (1<<0L)
3553+#define S_MISC (1<<1L)
3554+#define S_VARS (1<<2L)
3555+#define S_FILES (1<<3L)
3556+#define S_INCLUDE (1<<4L)
3557+#define S_SQL (1<<5L)
3558+#define S_EXECUTOR (1<<6L)
3559+#define S_MAIL (1<<7L)
3560+#define S_SESSION (1<<8L)
3561+#define S_INTERNAL (1<<29L)
3562+#define S_ALL (S_MEMORY | S_VARS | S_INCLUDE | S_FILES | S_MAIL | S_SESSION | S_MISC | S_SQL | S_EXECUTOR)
3563+#endif
3564+
3565 #endif /* ZEND_ERRORS_H */
3566
3567 /*
3568diff -Nura php-5.3.1RC1/Zend/zend_hash.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_hash.c
3569--- php-5.3.1RC1/Zend/zend_hash.c 2009-06-07 21:28:15.000000000 +0200
3570+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_hash.c 2009-09-27 19:10:51.000000000 +0200
3571@@ -20,6 +20,7 @@
3572 /* $Id$ */
3573
3574 #include "zend.h"
3575+#include "zend_compile.h"
3576
3577 #define CONNECT_TO_BUCKET_DLLIST(element, list_head) \
3578 (element)->pNext = (list_head); \
3579@@ -133,6 +134,191 @@
3580 }
3581
3582
3583+#if SUHOSIN_PATCH
3584+#ifdef ZTS
3585+static MUTEX_T zend_hash_dprot_mx_reader;
3586+static MUTEX_T zend_hash_dprot_mx_writer;
3587+static unsigned int zend_hash_dprot_reader;
3588+#endif
3589+static unsigned int zend_hash_dprot_counter;
3590+static unsigned int zend_hash_dprot_curmax;
3591+static dtor_func_t *zend_hash_dprot_table = NULL;
3592+
3593+static void zend_hash_dprot_begin_read()
3594+{
3595+#ifdef ZTS
3596+ tsrm_mutex_lock(zend_hash_dprot_mx_reader);
3597+ if ((++(zend_hash_dprot_reader)) == 1) {
3598+ tsrm_mutex_lock(zend_hash_dprot_mx_writer);
3599+ }
3600+ tsrm_mutex_unlock(zend_hash_dprot_mx_reader);
3601+#endif
3602+}
3603+
3604+static void zend_hash_dprot_end_read()
3605+{
3606+#ifdef ZTS
3607+ tsrm_mutex_lock(zend_hash_dprot_mx_reader);
3608+ if ((--(zend_hash_dprot_reader)) == 0) {
3609+ tsrm_mutex_unlock(zend_hash_dprot_mx_writer);
3610+ }
3611+ tsrm_mutex_unlock(zend_hash_dprot_mx_reader);
3612+#endif
3613+}
3614+
3615+static void zend_hash_dprot_begin_write()
3616+{
3617+#ifdef ZTS
3618+ tsrm_mutex_lock(zend_hash_dprot_mx_writer);
3619+#endif
3620+}
3621+
3622+static void zend_hash_dprot_end_write()
3623+{
3624+#ifdef ZTS
3625+ tsrm_mutex_unlock(zend_hash_dprot_mx_writer);
3626+#endif
3627+}
3628+
3629+/*ZEND_API void zend_hash_dprot_dtor()
3630+{
3631+#ifdef ZTS
3632+ tsrm_mutex_free(zend_hash_dprot_mx_reader);
3633+ tsrm_mutex_free(zend_hash_dprot_mx_writer);
3634+#endif
3635+ free(zend_hash_dprot_table);
3636+}*/
3637+
3638+static void zend_hash_add_destructor(dtor_func_t pDestructor)
3639+{
3640+ int left, right, mid;
3641+ zend_bool found = 0;
3642+ unsigned long value;
3643+
3644+ if (pDestructor == NULL || pDestructor == ZVAL_PTR_DTOR || pDestructor == ZVAL_INTERNAL_PTR_DTOR
3645+ || pDestructor == ZEND_FUNCTION_DTOR || pDestructor == ZEND_CLASS_DTOR) {
3646+ return;
3647+ }
3648+
3649+ if (zend_hash_dprot_table == NULL) {
3650+#ifdef ZTS
3651+ zend_hash_dprot_mx_reader = tsrm_mutex_alloc();
3652+ zend_hash_dprot_mx_writer = tsrm_mutex_alloc();
3653+ zend_hash_dprot_reader = 0;
3654+#endif
3655+ zend_hash_dprot_counter = 0;
3656+ zend_hash_dprot_curmax = 256;
3657+ zend_hash_dprot_table = (dtor_func_t *) malloc(256 * sizeof(dtor_func_t));
3658+ }
3659+
3660+ zend_hash_dprot_begin_write();
3661+
3662+ if (zend_hash_dprot_counter == 0) {
3663+ zend_hash_dprot_counter++;
3664+ zend_hash_dprot_table[0] = pDestructor;
3665+ } else {
3666+ value = (unsigned long) pDestructor;
3667+ left = 0;
3668+ right = zend_hash_dprot_counter-1;
3669+ mid = 0;
3670+
3671+ while (left < right) {
3672+ mid = (right - left) >> 1;
3673+ mid += left;
3674+ if ((unsigned long)zend_hash_dprot_table[mid] == value) {
3675+ found = 1;
3676+ break;
3677+ }
3678+ if (value < (unsigned long)zend_hash_dprot_table[mid]) {
3679+ right = mid-1;
3680+ } else {
3681+ left = mid+1;
3682+ }
3683+ }
3684+ if ((unsigned long)zend_hash_dprot_table[left] == value) {
3685+ found = 1;
3686+ }
3687+
3688+ if (!found) {
3689+
3690+ if (zend_hash_dprot_counter >= zend_hash_dprot_curmax) {
3691+ zend_hash_dprot_curmax += 256;
3692+ zend_hash_dprot_table = (dtor_func_t *) realloc(zend_hash_dprot_table, zend_hash_dprot_curmax * sizeof(dtor_func_t));
3693+ }
3694+
3695+ if ((unsigned long)zend_hash_dprot_table[left] < value) {
3696+ memmove(zend_hash_dprot_table+left+2, zend_hash_dprot_table+left+1, (zend_hash_dprot_counter-left-1)*sizeof(dtor_func_t));
3697+ zend_hash_dprot_table[left+1] = pDestructor;
3698+ } else {
3699+ memmove(zend_hash_dprot_table+left+1, zend_hash_dprot_table+left, (zend_hash_dprot_counter-left)*sizeof(dtor_func_t));
3700+ zend_hash_dprot_table[left] = pDestructor;
3701+ }
3702+
3703+ zend_hash_dprot_counter++;
3704+ }
3705+ }
3706+
3707+ zend_hash_dprot_end_write();
3708+}
3709+
3710+static void zend_hash_check_destructor(dtor_func_t pDestructor)
3711+{
3712+ unsigned long value;
3713+
3714+ if (pDestructor == NULL || pDestructor == ZVAL_PTR_DTOR || pDestructor == ZVAL_INTERNAL_PTR_DTOR
3715+#ifdef ZEND_ENGINE_2
3716+ || pDestructor == suhosin_zend_destroy_property_info_internal || pDestructor == suhosin_zend_destroy_property_info
3717+#endif
3718+ || pDestructor == ZEND_FUNCTION_DTOR || pDestructor == ZEND_CLASS_DTOR) {
3719+ return;
3720+ }
3721+
3722+ zend_hash_dprot_begin_read();
3723+
3724+ if (zend_hash_dprot_counter > 0) {
3725+ int left, right, mid;
3726+ zend_bool found = 0;
3727+
3728+ value = (unsigned long) pDestructor;
3729+ left = 0;
3730+ right = zend_hash_dprot_counter-1;
3731+
3732+ while (left < right) {
3733+ mid = (right - left) >> 1;
3734+ mid += left;
3735+ if ((unsigned long)zend_hash_dprot_table[mid] == value) {
3736+ found = 1;
3737+ break;
3738+ }
3739+ if (value < (unsigned long)zend_hash_dprot_table[mid]) {
3740+ right = mid-1;
3741+ } else {
3742+ left = mid+1;
3743+ }
3744+ }
3745+ if ((unsigned long)zend_hash_dprot_table[left] == value) {
3746+ found = 1;
3747+ }
3748+
3749+ if (!found) {
3750+ zend_hash_dprot_end_read();
3751+
3752+ zend_suhosin_log(S_MEMORY, "possible memory corruption detected - unknown Hashtable destructor");
3753+ if (SUHOSIN_CONFIG(SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR) == 0) {
3754+ _exit(1);
3755+ }
3756+ return;
3757+ }
3758+
3759+ }
3760+
3761+ zend_hash_dprot_end_read();
3762+}
3763+
3764+#else
3765+#define zend_hash_add_destructor(pDestructor) do {} while(0)
3766+#define zend_hash_check_destructor(pDestructor) do {} while(0)
3767+#endif
3768
3769 ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
3770 {
3771@@ -153,6 +339,7 @@
3772
3773 ht->nTableMask = ht->nTableSize - 1;
3774 ht->pDestructor = pDestructor;
3775+ zend_hash_add_destructor(pDestructor);
3776 ht->arBuckets = NULL;
3777 ht->pListHead = NULL;
3778 ht->pListTail = NULL;
3779@@ -230,6 +417,7 @@
3780 return FAILURE;
3781 }
3782 #endif
3783+ zend_hash_check_destructor(ht->pDestructor);
3784 if (ht->pDestructor) {
3785 ht->pDestructor(p->pData);
3786 }
3787@@ -295,6 +483,7 @@
3788 return FAILURE;
3789 }
3790 #endif
3791+ zend_hash_check_destructor(ht->pDestructor);
3792 if (ht->pDestructor) {
3793 ht->pDestructor(p->pData);
3794 }
3795@@ -370,6 +559,7 @@
3796 return FAILURE;
3797 }
3798 #endif
3799+ zend_hash_check_destructor(ht->pDestructor);
3800 if (ht->pDestructor) {
3801 ht->pDestructor(p->pData);
3802 }
3803@@ -493,6 +683,7 @@
3804 if (ht->pInternalPointer == p) {
3805 ht->pInternalPointer = p->pListNext;
3806 }
3807+ zend_hash_check_destructor(ht->pDestructor);
3808 if (ht->pDestructor) {
3809 ht->pDestructor(p->pData);
3810 }
3811@@ -519,6 +710,7 @@
3812 SET_INCONSISTENT(HT_IS_DESTROYING);
3813
3814 p = ht->pListHead;
3815+ zend_hash_check_destructor(ht->pDestructor);
3816 while (p != NULL) {
3817 q = p;
3818 p = p->pListNext;
3819@@ -545,6 +737,7 @@
3820 SET_INCONSISTENT(HT_CLEANING);
3821
3822 p = ht->pListHead;
3823+ zend_hash_check_destructor(ht->pDestructor);
3824 while (p != NULL) {
3825 q = p;
3826 p = p->pListNext;
3827@@ -607,6 +800,7 @@
3828 ht->nNumOfElements--;
3829 HANDLE_UNBLOCK_INTERRUPTIONS();
3830
3831+ zend_hash_check_destructor(ht->pDestructor);
3832 if (ht->pDestructor) {
3833 ht->pDestructor(p->pData);
3834 }
3835diff -Nura php-5.3.1RC1/Zend/zend_llist.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_llist.c
3836--- php-5.3.1RC1/Zend/zend_llist.c 2008-12-31 12:15:49.000000000 +0100
3837+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_llist.c 2009-09-27 19:11:28.000000000 +0200
3838@@ -23,6 +23,186 @@
3839 #include "zend_llist.h"
3840 #include "zend_qsort.h"
3841
3842+#if SUHOSIN_PATCH
3843+#ifdef ZTS
3844+static MUTEX_T zend_llist_dprot_mx_reader;
3845+static MUTEX_T zend_llist_dprot_mx_writer;
3846+static unsigned int zend_llist_dprot_reader;
3847+#endif
3848+static unsigned int zend_llist_dprot_counter;
3849+static unsigned int zend_llist_dprot_curmax;
3850+static llist_dtor_func_t *zend_llist_dprot_table = NULL;
3851+
3852+static void zend_llist_dprot_begin_read()
3853+{
3854+#ifdef ZTS
3855+ tsrm_mutex_lock(zend_llist_dprot_mx_reader);
3856+ if ((++(zend_llist_dprot_reader)) == 1) {
3857+ tsrm_mutex_lock(zend_llist_dprot_mx_writer);
3858+ }
3859+ tsrm_mutex_unlock(zend_llist_dprot_mx_reader);
3860+#endif
3861+}
3862+
3863+static void zend_llist_dprot_end_read()
3864+{
3865+#ifdef ZTS
3866+ tsrm_mutex_lock(zend_llist_dprot_mx_reader);
3867+ if ((--(zend_llist_dprot_reader)) == 0) {
3868+ tsrm_mutex_unlock(zend_llist_dprot_mx_writer);
3869+ }
3870+ tsrm_mutex_unlock(zend_llist_dprot_mx_reader);
3871+#endif
3872+}
3873+
3874+static void zend_llist_dprot_begin_write()
3875+{
3876+#ifdef ZTS
3877+ tsrm_mutex_lock(zend_llist_dprot_mx_writer);
3878+#endif
3879+}
3880+
3881+static void zend_llist_dprot_end_write()
3882+{
3883+#ifdef ZTS
3884+ tsrm_mutex_unlock(zend_llist_dprot_mx_writer);
3885+#endif
3886+}
3887+
3888+/*ZEND_API void zend_llist_dprot_dtor()
3889+{
3890+#ifdef ZTS
3891+ tsrm_mutex_free(zend_llist_dprot_mx_reader);
3892+ tsrm_mutex_free(zend_llist_dprot_mx_writer);
3893+#endif
3894+ free(zend_llist_dprot_table);
3895+}*/
3896+
3897+static void zend_llist_add_destructor(llist_dtor_func_t pDestructor)
3898+{
3899+ int left, right, mid;
3900+ zend_bool found = 0;
3901+ unsigned long value;
3902+
3903+ if (pDestructor == NULL || pDestructor == ZVAL_PTR_DTOR) {
3904+ return;
3905+ }
3906+
3907+ if (zend_llist_dprot_table == NULL) {
3908+#ifdef ZTS
3909+ zend_llist_dprot_mx_reader = tsrm_mutex_alloc();
3910+ zend_llist_dprot_mx_writer = tsrm_mutex_alloc();
3911+ zend_llist_dprot_reader = 0;
3912+#endif
3913+ zend_llist_dprot_counter = 0;
3914+ zend_llist_dprot_curmax = 256;
3915+ zend_llist_dprot_table = (llist_dtor_func_t *) malloc(256 * sizeof(llist_dtor_func_t));
3916+ }
3917+
3918+ zend_llist_dprot_begin_write();
3919+
3920+ if (zend_llist_dprot_counter == 0) {
3921+ zend_llist_dprot_counter++;
3922+ zend_llist_dprot_table[0] = pDestructor;
3923+ } else {
3924+ value = (unsigned long) pDestructor;
3925+ left = 0;
3926+ right = zend_llist_dprot_counter-1;
3927+ mid = 0;
3928+
3929+ while (left < right) {
3930+ mid = (right - left) >> 1;
3931+ mid += left;
3932+ if ((unsigned long)zend_llist_dprot_table[mid] == value) {
3933+ found = 1;
3934+ break;
3935+ }
3936+ if (value < (unsigned long)zend_llist_dprot_table[mid]) {
3937+ right = mid-1;
3938+ } else {
3939+ left = mid+1;
3940+ }
3941+ }
3942+ if ((unsigned long)zend_llist_dprot_table[left] == value) {
3943+ found = 1;
3944+ }
3945+
3946+ if (!found) {
3947+
3948+ if (zend_llist_dprot_counter >= zend_llist_dprot_curmax) {
3949+ zend_llist_dprot_curmax += 256;
3950+ zend_llist_dprot_table = (llist_dtor_func_t *) realloc(zend_llist_dprot_table, zend_llist_dprot_curmax * sizeof(llist_dtor_func_t));
3951+ }
3952+
3953+ if ((unsigned long)zend_llist_dprot_table[left] < value) {
3954+ memmove(zend_llist_dprot_table+left+2, zend_llist_dprot_table+left+1, (zend_llist_dprot_counter-left-1)*sizeof(llist_dtor_func_t));
3955+ zend_llist_dprot_table[left+1] = pDestructor;
3956+ } else {
3957+ memmove(zend_llist_dprot_table+left+1, zend_llist_dprot_table+left, (zend_llist_dprot_counter-left)*sizeof(llist_dtor_func_t));
3958+ zend_llist_dprot_table[left] = pDestructor;
3959+ }
3960+
3961+ zend_llist_dprot_counter++;
3962+ }
3963+ }
3964+
3965+ zend_llist_dprot_end_write();
3966+}
3967+
3968+static void zend_llist_check_destructor(llist_dtor_func_t pDestructor)
3969+{
3970+ unsigned long value;
3971+
3972+ if (pDestructor == NULL || pDestructor == ZVAL_PTR_DTOR) {
3973+ return;
3974+ }
3975+
3976+ zend_llist_dprot_begin_read();
3977+
3978+ if (zend_llist_dprot_counter > 0) {
3979+ int left, right, mid;
3980+ zend_bool found = 0;
3981+
3982+ value = (unsigned long) pDestructor;
3983+ left = 0;
3984+ right = zend_llist_dprot_counter-1;
3985+
3986+ while (left < right) {
3987+ mid = (right - left) >> 1;
3988+ mid += left;
3989+ if ((unsigned long)zend_llist_dprot_table[mid] == value) {
3990+ found = 1;
3991+ break;
3992+ }
3993+ if (value < (unsigned long)zend_llist_dprot_table[mid]) {
3994+ right = mid-1;
3995+ } else {
3996+ left = mid+1;
3997+ }
3998+ }
3999+ if ((unsigned long)zend_llist_dprot_table[left] == value) {
4000+ found = 1;
4001+ }
4002+
4003+ if (!found) {
4004+ zend_llist_dprot_end_read();
4005+
4006+ zend_suhosin_log(S_MEMORY, "possible memory corruption detected - unknown llist destructor");
4007+ if (SUHOSIN_CONFIG(SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR) == 0) {
4008+ _exit(1);
4009+ }
4010+ return;
4011+ }
4012+
4013+ }
4014+
4015+ zend_llist_dprot_end_read();
4016+}
4017+#else
4018+#define zend_llist_add_destructor(pDestructor) do {} while(0)
4019+#define zend_llist_check_destructor(pDestructor) do {} while(0)
4020+#endif
4021+
4022 ZEND_API void zend_llist_init(zend_llist *l, size_t size, llist_dtor_func_t dtor, unsigned char persistent)
4023 {
4024 l->head = NULL;
4025@@ -30,6 +210,7 @@
4026 l->count = 0;
4027 l->size = size;
4028 l->dtor = dtor;
4029+ zend_llist_add_destructor(dtor);
4030 l->persistent = persistent;
4031 }
4032
4033@@ -81,6 +262,7 @@
4034 } else {\
4035 (l)->tail = (current)->prev;\
4036 }\
4037+ zend_llist_check_destructor((l)->dtor); \
4038 if ((l)->dtor) {\
4039 (l)->dtor((current)->data);\
4040 }\
4041@@ -108,6 +290,7 @@
4042 {
4043 zend_llist_element *current=l->head, *next;
4044
4045+ zend_llist_check_destructor(l->dtor);
4046 while (current) {
4047 next = current->next;
4048 if (l->dtor) {
4049@@ -133,6 +316,7 @@
4050 zend_llist_element *old_tail;
4051 void *data;
4052
4053+ zend_llist_check_destructor((l)->dtor);
4054 if ((old_tail = l->tail)) {
4055 if (old_tail->prev) {
4056 old_tail->prev->next = NULL;
4057diff -Nura php-5.3.1RC1/Zend/zend_operators.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_operators.c
4058--- php-5.3.1RC1/Zend/zend_operators.c 2009-06-04 20:20:45.000000000 +0200
4059+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_operators.c 2009-09-27 19:04:06.000000000 +0200
4060@@ -152,9 +152,14 @@
4061 case IS_STRING:
4062 {
4063 char *strval;
4064+ int strl;
4065
4066 strval = Z_STRVAL_P(op);
4067- if ((Z_TYPE_P(op)=is_numeric_string(strval, Z_STRLEN_P(op), &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) {
4068+ strl = Z_STRLEN_P(op);
4069+#if SUHOSIN_PATCH
4070+ Z_STRLEN_P(op) = 0;
4071+#endif
4072+ if ((Z_TYPE_P(op)=is_numeric_string(strval, strl, &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) {
4073 ZVAL_LONG(op, 0);
4074 }
4075 STR_FREE(strval);
4076@@ -186,7 +191,8 @@
4077 } else { \
4078 switch (Z_TYPE_P(op)) { \
4079 case IS_STRING: \
4080- { \
4081+ { \
4082+ Z_STRLEN(holder) = 0; \
4083 if ((Z_TYPE(holder)=is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL(holder), &Z_DVAL(holder), 1)) == 0) { \
4084 ZVAL_LONG(&(holder), 0); \
4085 } \
4086@@ -228,6 +234,7 @@
4087 Z_LVAL(holder) = zend_dval_to_lval(Z_DVAL_P(op)); \
4088 break; \
4089 case IS_STRING: \
4090+ Z_STRLEN(holder) = 0; \
4091 Z_LVAL(holder) = strtol(Z_STRVAL_P(op), NULL, 10); \
4092 break; \
4093 case IS_ARRAY: \
4094@@ -270,6 +277,7 @@
4095 Z_LVAL(holder) = (Z_DVAL_P(op) ? 1 : 0); \
4096 break; \
4097 case IS_STRING: \
4098+ Z_STRLEN(holder) = 0; \
4099 if (Z_STRLEN_P(op) == 0 \
4100 || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) { \
4101 Z_LVAL(holder) = 0; \
4102@@ -355,6 +363,9 @@
4103 {
4104 char *strval = Z_STRVAL_P(op);
4105
4106+#if SUHOSIN_PATCH
4107+ Z_STRLEN_P(op) = 0;
4108+#endif
4109 Z_LVAL_P(op) = strtol(strval, NULL, base);
4110 STR_FREE(strval);
4111 }
4112@@ -415,6 +426,9 @@
4113 {
4114 char *strval = Z_STRVAL_P(op);
4115
4116+#if SUHOSIN_PATCH
4117+ Z_STRLEN_P(op) = 0;
4118+#endif
4119 Z_DVAL_P(op) = zend_strtod(strval, NULL);
4120 STR_FREE(strval);
4121 }
4122@@ -501,8 +515,14 @@
4123
4124 if (Z_STRLEN_P(op) == 0
4125 || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
4126+#if SUHOSIN_PATCH
4127+ Z_STRLEN_P(op) = 0;
4128+#endif
4129 Z_LVAL_P(op) = 0;
4130 } else {
4131+#if SUHOSIN_PATCH
4132+ Z_STRLEN_P(op) = 0;
4133+#endif
4134 Z_LVAL_P(op) = 1;
4135 }
4136 STR_FREE(strval);
4137@@ -616,6 +636,9 @@
4138 *entry = *op;
4139 INIT_PZVAL(entry);
4140
4141+#if SUHOSIN_PATCH
4142+ Z_STRLEN_P(op) = 0;
4143+#endif
4144 switch (type) {
4145 case IS_ARRAY:
4146 ALLOC_HASHTABLE(Z_ARRVAL_P(op));
4147diff -Nura php-5.3.1RC1/Zend/zend_variables.c suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_variables.c
4148--- php-5.3.1RC1/Zend/zend_variables.c 2008-12-31 12:15:49.000000000 +0100
4149+++ suhosin-patch-5.3.1RC1-0.9.8/Zend/zend_variables.c 2009-09-27 19:04:06.000000000 +0200
4150@@ -34,6 +34,9 @@
4151 case IS_CONSTANT:
4152 CHECK_ZVAL_STRING_REL(zvalue);
4153 STR_FREE_REL(zvalue->value.str.val);
4154+#if SUHOSIN_PATCH
4155+ zvalue->value.str.len = 0;
4156+#endif
4157 break;
4158 case IS_ARRAY:
4159 case IS_CONSTANT_ARRAY: {
4160@@ -78,6 +81,9 @@
4161 case IS_CONSTANT:
4162 CHECK_ZVAL_STRING_REL(zvalue);
4163 free(zvalue->value.str.val);
4164+#if SUHOSIN_PATCH
4165+ zvalue->value.str.len = 0;
4166+#endif
4167 break;
4168 case IS_ARRAY:
4169 case IS_CONSTANT_ARRAY:
4170diff -Nura php-5.3.1RC1/configure suhosin-patch-5.3.1RC1-0.9.8/configure
4171--- php-5.3.1RC1/configure 2009-09-04 01:11:27.000000000 +0200
4172+++ suhosin-patch-5.3.1RC1-0.9.8/configure 2009-09-27 19:04:06.000000000 +0200
4173@@ -17600,6 +17600,9 @@
4174
4175 fi
4176
4177+cat >> confdefs.h <<\EOF
4178+#define SUHOSIN_PATCH 1
4179+EOF
4180
4181 echo $ac_n "checking for declared timezone""... $ac_c" 1>&6
4182 echo "configure:17606: checking for declared timezone" >&5
4183@@ -112443,7 +112446,7 @@
4184 php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
4185 strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c \
4186 network.c php_open_temporary_file.c php_logos.c \
4187- output.c getopt.c; do
4188+ output.c getopt.c suhosin_patch.c ; do
4189
4190 IFS=.
4191 set $ac_src
4192@@ -112647,7 +112650,7 @@
4193 zend_list.c zend_indent.c zend_builtin_functions.c zend_sprintf.c \
4194 zend_ini.c zend_qsort.c zend_multibyte.c zend_ts_hash.c zend_stream.c \
4195 zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c \
4196- zend_closures.c zend_float.c; do
4197+ zend_closures.c zend_float.c zend_canary.c zend_alloc_canary.c ; do
4198
4199 IFS=.
4200 set $ac_src
4201diff -Nura php-5.3.1RC1/configure.in suhosin-patch-5.3.1RC1-0.9.8/configure.in
4202--- php-5.3.1RC1/configure.in 2009-09-03 23:30:56.000000000 +0200
4203+++ suhosin-patch-5.3.1RC1-0.9.8/configure.in 2009-09-27 19:04:06.000000000 +0200
4204@@ -304,6 +304,7 @@
4205 sinclude(TSRM/threads.m4)
4206 sinclude(TSRM/tsrm.m4)
4207
4208+sinclude(main/suhosin_patch.m4)
4209
4210 divert(2)
4211
4212@@ -1407,7 +1408,7 @@
4213 php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
4214 strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c \
4215 network.c php_open_temporary_file.c php_logos.c \
4216- output.c getopt.c)
4217+ output.c getopt.c suhosin_patch.c )
4218
4219 PHP_ADD_SOURCES(main/streams, streams.c cast.c memory.c filter.c \
4220 plain_wrapper.c userspace.c transports.c xp_socket.c mmap.c \
4221@@ -1435,7 +1436,7 @@
4222 zend_list.c zend_indent.c zend_builtin_functions.c zend_sprintf.c \
4223 zend_ini.c zend_qsort.c zend_multibyte.c zend_ts_hash.c zend_stream.c \
4224 zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c \
4225- zend_closures.c zend_float.c)
4226+ zend_closures.c zend_float.c zend_canary.c zend_alloc_canary.c )
4227
4228 if test -r "$abs_srcdir/Zend/zend_objects.c"; then
4229 PHP_ADD_SOURCES(Zend, zend_objects.c zend_object_handlers.c zend_objects_API.c zend_default_classes.c)
4230diff -Nura php-5.3.1RC1/ext/standard/dl.c suhosin-patch-5.3.1RC1-0.9.8/ext/standard/dl.c
4231--- php-5.3.1RC1/ext/standard/dl.c 2009-08-06 03:33:54.000000000 +0200
4232+++ suhosin-patch-5.3.1RC1-0.9.8/ext/standard/dl.c 2009-09-27 19:04:06.000000000 +0200
4233@@ -244,6 +244,23 @@
4234 return FAILURE;
4235 }
4236 }
4237+
4238+#if SUHOSIN_PATCH
4239+ if (strncmp("suhosin", module_entry->name, sizeof("suhosin")-1) == 0) {
4240+ void *log_func;
4241+ /* sucessfully loaded suhosin extension, now check for logging function replacement */
4242+ log_func = (void *) DL_FETCH_SYMBOL(handle, "suhosin_log");
4243+ if (log_func == NULL) {
4244+ log_func = (void *) DL_FETCH_SYMBOL(handle, "_suhosin_log");
4245+ }
4246+ if (log_func != NULL) {
4247+ zend_suhosin_log = log_func;
4248+ } else {
4249+ zend_suhosin_log(S_MISC, "could not replace logging function");
4250+ }
4251+ }
4252+#endif
4253+
4254 return SUCCESS;
4255 }
4256 /* }}} */
4257diff -Nura php-5.3.1RC1/ext/standard/info.c suhosin-patch-5.3.1RC1-0.9.8/ext/standard/info.c
4258--- php-5.3.1RC1/ext/standard/info.c 2009-01-17 03:05:13.000000000 +0100
4259+++ suhosin-patch-5.3.1RC1-0.9.8/ext/standard/info.c 2009-09-27 19:04:06.000000000 +0200
4260@@ -842,6 +842,33 @@
4261
4262 php_info_print_table_end();
4263
4264+ /* Suhosin Patch */
4265+ php_info_print_box_start(0);
4266+ if (expose_php && !sapi_module.phpinfo_as_text) {
4267+ PUTS("<a href=\"http://www.suhosin.org\"><img border=\"0\" src=\"");
4268+ if (SG(request_info).request_uri) {
4269+ char *elem_esc = php_info_html_esc(SG(request_info).request_uri TSRMLS_CC);
4270+ PUTS(elem_esc);
4271+ efree(elem_esc);
4272+ }
4273+ PUTS("?="SUHOSIN_LOGO_GUID"\" alt=\"Suhosin logo\" /></a>\n");
4274+ }
4275+ PUTS("This server is protected with the Suhosin Patch ");
4276+ if (sapi_module.phpinfo_as_text) {
4277+ PUTS(SUHOSIN_PATCH_VERSION);
4278+ } else {
4279+ zend_html_puts(SUHOSIN_PATCH_VERSION, strlen(SUHOSIN_PATCH_VERSION) TSRMLS_CC);
4280+ }
4281+ PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
4282+ if (sapi_module.phpinfo_as_text) {
4283+ PUTS("Copyright (c) 2006-2007 Hardened-PHP Project\n");
4284+ PUTS("Copyright (c) 2007-2009 SektionEins GmbH\n");
4285+ } else {
4286+ PUTS("Copyright (c) 2006-2007 <a href=\"http://www.hardened-php.net/\">Hardened-PHP Project</a>\n");
4287+ PUTS("Copyright (c) 2007-2009 <a href=\"http://www.sektioneins.de/\">SektionEins GmbH</a>\n");
4288+ }
4289+ php_info_print_box_end();
4290+
4291 /* Zend Engine */
4292 php_info_print_box_start(0);
4293 if (expose_php && !sapi_module.phpinfo_as_text) {
4294diff -Nura php-5.3.1RC1/ext/standard/syslog.c suhosin-patch-5.3.1RC1-0.9.8/ext/standard/syslog.c
4295--- php-5.3.1RC1/ext/standard/syslog.c 2008-12-31 12:15:49.000000000 +0100
4296+++ suhosin-patch-5.3.1RC1-0.9.8/ext/standard/syslog.c 2009-09-27 19:04:06.000000000 +0200
4297@@ -42,6 +42,7 @@
4298 */
4299 PHP_MINIT_FUNCTION(syslog)
4300 {
4301+#if !SUHOSIN_PATCH
4302 /* error levels */
4303 REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
4304 REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
4305@@ -97,6 +98,7 @@
4306 /* AIX doesn't have LOG_PERROR */
4307 REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
4308 #endif
4309+#endif
4310 BG(syslog_device)=NULL;
4311
4312 return SUCCESS;
4313diff -Nura php-5.3.1RC1/main/fopen_wrappers.c suhosin-patch-5.3.1RC1-0.9.8/main/fopen_wrappers.c
4314--- php-5.3.1RC1/main/fopen_wrappers.c 2009-07-31 23:09:45.000000000 +0200
4315+++ suhosin-patch-5.3.1RC1-0.9.8/main/fopen_wrappers.c 2009-09-27 19:32:39.000000000 +0200
4316@@ -85,13 +85,8 @@
4317 PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
4318 {
4319 char **p, *pathbuf, *ptr, *end;
4320-#ifndef ZTS
4321- char *base = (char *) mh_arg2;
4322-#else
4323- char *base = (char *) ts_resource(*((int *) mh_arg2));
4324-#endif
4325
4326- p = (char **) (base + (size_t) mh_arg1);
4327+ p = &PG(open_basedir);
4328
4329 if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN || stage == PHP_INI_STAGE_ACTIVATE || stage == PHP_INI_STAGE_DEACTIVATE) {
4330 /* We're in a PHP_INI_SYSTEM context, no restrictions */
4331diff -Nura php-5.3.1RC1/main/main.c suhosin-patch-5.3.1RC1-0.9.8/main/main.c
4332--- php-5.3.1RC1/main/main.c 2009-07-29 02:17:10.000000000 +0200
4333+++ suhosin-patch-5.3.1RC1-0.9.8/main/main.c 2009-09-27 19:04:06.000000000 +0200
4334@@ -90,6 +90,9 @@
4335
4336 #include "SAPI.h"
4337 #include "rfc1867.h"
4338+#if SUHOSIN_PATCH
4339+#include "suhosin_globals.h"
4340+#endif
4341
4342 #if HAVE_SYS_MMAN_H
4343 # include <sys/mman.h>
4344@@ -487,7 +490,7 @@
4345 STD_PHP_INI_ENTRY("extension_dir", PHP_EXTENSION_DIR, PHP_INI_SYSTEM, OnUpdateStringUnempty, extension_dir, php_core_globals, core_globals)
4346 STD_PHP_INI_ENTRY("include_path", PHP_INCLUDE_PATH, PHP_INI_ALL, OnUpdateStringUnempty, include_path, php_core_globals, core_globals)
4347 PHP_INI_ENTRY("max_execution_time", "30", PHP_INI_ALL, OnUpdateTimeout)
4348- STD_PHP_INI_ENTRY("open_basedir", NULL, PHP_INI_ALL, OnUpdateBaseDir, open_basedir, php_core_globals, core_globals)
4349+ PHP_INI_ENTRY("open_basedir", NULL, PHP_INI_ALL, OnUpdateBaseDir)
4350 STD_PHP_INI_ENTRY("safe_mode_exec_dir", PHP_SAFE_MODE_EXEC_DIR, PHP_INI_SYSTEM, OnUpdateString, safe_mode_exec_dir, php_core_globals, core_globals)
4351
4352 STD_PHP_INI_BOOLEAN("file_uploads", "1", PHP_INI_SYSTEM, OnUpdateBool, file_uploads, php_core_globals, core_globals)
4353@@ -1736,6 +1739,10 @@
4354 }
4355 #endif
4356
4357+#if SUHOSIN_PATCH
4358+PHPAPI void suhosin_startup();
4359+#endif
4360+
4361 /* {{{ php_module_startup
4362 */
4363 int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules)
4364@@ -1780,6 +1787,10 @@
4365 tsrm_ls = ts_resource(0);
4366 #endif
4367
4368+#if SUHOSIN_PATCH
4369+ suhosin_startup();
4370+#endif
4371+
4372 module_shutdown = 0;
4373 module_startup = 1;
4374 sapi_initialize_empty_request(TSRMLS_C);
4375@@ -1899,7 +1910,11 @@
4376 REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_SCAN_DIR", PHP_CONFIG_FILE_SCAN_DIR, sizeof(PHP_CONFIG_FILE_SCAN_DIR)-1, CONST_PERSISTENT | CONST_CS);
4377 REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
4378 REGISTER_MAIN_STRINGL_CONSTANT("PHP_EOL", PHP_EOL, sizeof(PHP_EOL)-1, CONST_PERSISTENT | CONST_CS);
4379- REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
4380+#if SUHOSIN_PATCH
4381+ REGISTER_MAIN_LONG_CONSTANT("SUHOSIN_PATCH", 1, CONST_PERSISTENT | CONST_CS);
4382+ REGISTER_MAIN_STRINGL_CONSTANT("SUHOSIN_PATCH_VERSION", SUHOSIN_PATCH_VERSION, sizeof(SUHOSIN_PATCH_VERSION)-1, CONST_PERSISTENT | CONST_CS);
4383+#endif
4384+ REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
4385 REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", LONG_MAX, CONST_PERSISTENT | CONST_CS);
4386 REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", sizeof(long), CONST_PERSISTENT | CONST_CS);
4387
4388diff -Nura php-5.3.1RC1/main/php.h suhosin-patch-5.3.1RC1-0.9.8/main/php.h
4389--- php-5.3.1RC1/main/php.h 2009-06-26 17:44:19.000000000 +0200
4390+++ suhosin-patch-5.3.1RC1-0.9.8/main/php.h 2009-09-27 19:04:06.000000000 +0200
4391@@ -450,6 +450,10 @@
4392 #endif
4393 #endif /* !XtOffsetOf */
4394
4395+#if SUHOSIN_PATCH
4396+#include "suhosin_patch.h"
4397+#endif
4398+
4399 #endif
4400
4401 /*
4402diff -Nura php-5.3.1RC1/main/php_config.h.in suhosin-patch-5.3.1RC1-0.9.8/main/php_config.h.in
4403--- php-5.3.1RC1/main/php_config.h.in 2009-09-04 01:11:30.000000000 +0200
4404+++ suhosin-patch-5.3.1RC1-0.9.8/main/php_config.h.in 2009-09-27 19:04:06.000000000 +0200
4405@@ -833,6 +833,9 @@
4406 /* Define if the target system has /dev/urandom device */
4407 #undef HAVE_DEV_URANDOM
4408
4409+/* Suhosin-Patch for PHP */
4410+#undef SUHOSIN_PATCH
4411+
4412 /* Whether you have AOLserver */
4413 #undef HAVE_AOLSERVER
4414
4415diff -Nura php-5.3.1RC1/main/php_logos.c suhosin-patch-5.3.1RC1-0.9.8/main/php_logos.c
4416--- php-5.3.1RC1/main/php_logos.c 2008-12-31 12:15:49.000000000 +0100
4417+++ suhosin-patch-5.3.1RC1-0.9.8/main/php_logos.c 2009-09-27 19:04:06.000000000 +0200
4418@@ -50,6 +50,10 @@
4419 return zend_hash_del(&phpinfo_logo_hash, logo_string, strlen(logo_string));
4420 }
4421
4422+#if SUHOSIN_PATCH
4423+#include "suhosin_logo.h"
4424+#endif
4425+
4426 int php_init_info_logos(void)
4427 {
4428 if(zend_hash_init(&phpinfo_logo_hash, 0, NULL, NULL, 1)==FAILURE)
4429@@ -58,7 +62,9 @@
4430 php_register_info_logo(PHP_LOGO_GUID , "image/gif", php_logo , sizeof(php_logo));
4431 php_register_info_logo(PHP_EGG_LOGO_GUID, "image/gif", php_egg_logo, sizeof(php_egg_logo));
4432 php_register_info_logo(ZEND_LOGO_GUID , "image/gif", zend_logo , sizeof(zend_logo));
4433-
4434+#if SUHOSIN_PATCH
4435+ php_register_info_logo(SUHOSIN_LOGO_GUID, "image/jpeg", suhosin_logo , sizeof(suhosin_logo));
4436+#endif
4437 return SUCCESS;
4438 }
4439
4440diff -Nura php-5.3.1RC1/main/snprintf.c suhosin-patch-5.3.1RC1-0.9.8/main/snprintf.c
4441--- php-5.3.1RC1/main/snprintf.c 2008-12-31 12:15:49.000000000 +0100
4442+++ suhosin-patch-5.3.1RC1-0.9.8/main/snprintf.c 2009-09-27 19:04:06.000000000 +0200
4443@@ -1091,7 +1091,11 @@
4444
4445
4446 case 'n':
4447+#if SUHOSIN_PATCH
4448+ zend_suhosin_log(S_MISC, "'n' specifier within format string");
4449+#else
4450 *(va_arg(ap, int *)) = cc;
4451+#endif
4452 goto skip_output;
4453
4454 /*
4455diff -Nura php-5.3.1RC1/main/spprintf.c suhosin-patch-5.3.1RC1-0.9.8/main/spprintf.c
4456--- php-5.3.1RC1/main/spprintf.c 2008-12-31 12:15:49.000000000 +0100
4457+++ suhosin-patch-5.3.1RC1-0.9.8/main/spprintf.c 2009-09-27 19:04:06.000000000 +0200
4458@@ -698,7 +698,11 @@
4459
4460
4461 case 'n':
4462+#if SUHOSIN_PATCH
4463+ zend_suhosin_log(S_MISC, "'n' specifier within format string");
4464+#else
4465 *(va_arg(ap, int *)) = xbuf->len;
4466+#endif
4467 goto skip_output;
4468
4469 /*
4470diff -Nura php-5.3.1RC1/main/suhosin_globals.h suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_globals.h
4471--- php-5.3.1RC1/main/suhosin_globals.h 1970-01-01 01:00:00.000000000 +0100
4472+++ suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_globals.h 2009-09-27 19:04:06.000000000 +0200
4473@@ -0,0 +1,61 @@
4474+/*
4475+ +----------------------------------------------------------------------+
4476+ | Suhosin-Patch for PHP |
4477+ +----------------------------------------------------------------------+
4478+ | Copyright (c) 2004-2009 Stefan Esser |
4479+ +----------------------------------------------------------------------+
4480+ | This source file is subject to version 2.02 of the PHP license, |
4481+ | that is bundled with this package in the file LICENSE, and is |
4482+ | available at through the world-wide-web at |
4483+ | http://www.php.net/license/2_02.txt. |
4484+ | If you did not receive a copy of the PHP license and are unable to |
4485+ | obtain it through the world-wide-web, please send a note to |
4486+ | license@php.net so we can mail you a copy immediately. |
4487+ +----------------------------------------------------------------------+
4488+ | Author: Stefan Esser <stefan.esser@sektioneins.de> |
4489+ +----------------------------------------------------------------------+
4490+ */
4491+
4492+#ifndef SUHOSIN_GLOBALS_H
4493+#define SUHOSIN_GLOBALS_H
4494+
4495+typedef struct _suhosin_patch_globals suhosin_patch_globals_struct;
4496+
4497+#ifdef ZTS
4498+# define SPG(v) TSRMG(suhosin_patch_globals_id, suhosin_patch_globals_struct *, v)
4499+extern int suhosin_patch_globals_id;
4500+#else
4501+# define SPG(v) (suhosin_patch_globals.v)
4502+extern struct _suhosin_patch_globals suhosin_patch_globals;
4503+#endif
4504+
4505+
4506+struct _suhosin_patch_globals {
4507+ /* logging */
4508+ int log_syslog;
4509+ int log_syslog_facility;
4510+ int log_syslog_priority;
4511+ int log_sapi;
4512+ int log_script;
4513+ int log_phpscript;
4514+ char *log_scriptname;
4515+ char *log_phpscriptname;
4516+ zend_bool log_phpscript_is_safe;
4517+ zend_bool log_use_x_forwarded_for;
4518+
4519+ /* memory manager canary protection */
4520+ unsigned int canary_1;
4521+ unsigned int canary_2;
4522+ unsigned int canary_3;
4523+ unsigned int dummy;
4524+};
4525+
4526+
4527+#endif /* SUHOSIN_GLOBALS_H */
4528+
4529+/*
4530+ * Local variables:
4531+ * tab-width: 4
4532+ * c-basic-offset: 4
4533+ * End:
4534+ */
4535diff -Nura php-5.3.1RC1/main/suhosin_logo.h suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_logo.h
4536--- php-5.3.1RC1/main/suhosin_logo.h 1970-01-01 01:00:00.000000000 +0100
4537+++ suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_logo.h 2009-09-27 19:04:06.000000000 +0200
4538@@ -0,0 +1,178 @@
4539+static unsigned char suhosin_logo[] =
4540+ "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x48"
4541+ "\x00\x48\x00\x00\xff\xe1\x00\x16\x45\x78\x69\x66\x00\x00\x4d\x4d"
4542+ "\x00\x2a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\xff\xdb\x00\x43"
4543+ "\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
4544+ "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
4545+ "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
4546+ "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
4547+ "\x01\xff\xc0\x00\x0b\x08\x00\x27\x00\x71\x01\x01\x22\x00\xff\xc4"
4548+ "\x00\x1e\x00\x00\x02\x02\x02\x03\x01\x01\x00\x00\x00\x00\x00\x00"
4549+ "\x00\x00\x00\x00\x09\x06\x08\x05\x07\x02\x03\x0a\x01\x04\xff\xc4"
4550+ "\x00\x32\x10\x00\x01\x04\x03\x00\x02\x00\x05\x01\x05\x09\x01\x00"
4551+ "\x00\x00\x00\x05\x02\x03\x04\x06\x01\x07\x08\x00\x09\x11\x12\x13"
4552+ "\x14\x21\x15\x0a\x16\x31\x56\x96\x17\x18\x19\x23\x32\x41\x58\x98"
4553+ "\xd4\xd6\xff\xda\x00\x08\x01\x01\x00\x00\x3f\x00\xf4\xc1\xe1\xe5"
4554+ "\x69\xe9\x3e\xb9\xd1\x7c\x8a\x2e\x9d\x66\xe8\x3b\x29\x4d\x7f\x46"
4555+ "\xba\x58\x55\x54\x8d\xb1\x5f\xaa\xd9\x8d\x51\x2b\xb6\x27\x5a\x69"
4556+ "\xd1\x43\xaf\x16\x1a\xf0\xb2\xb1\xe9\x6d\x9f\xc2\xa4\x36\x18\xb5"
4557+ "\x85\x10\x41\xbe\xfc\x09\xac\x49\x29\x11\xd4\x32\x97\xec\x08\x13"
4558+ "\xc1\x2d\x20\xc3\x59\xeb\x26\x05\xd8\x6b\x76\x31\x43\x8f\x57\xcf"
4559+ "\x84\x9f\x14\xa8\x53\x81\x0b\xc3\x64\x80\xa3\x02\x0a\x41\x75\xf8"
4560+ "\x44\x85\x93\x81\x22\x3c\xd8\x13\xe1\xbe\xf4\x59\x91\x1f\x6a\x44"
4561+ "\x77\x5c\x69\xc4\x2f\x39\x5f\x0f\x2a\x8d\xeb\xba\xf8\xc3\x56\x6c"
4562+ "\x3b\x36\xa7\xda\xbd\x4d\xa1\xb5\x4e\xc6\xa7\xa4\x3a\xec\x15\x2d"
4563+ "\xa5\xb3\xea\x5a\xdc\xac\x46\xac\x01\x60\xd8\x43\xc8\x8e\x8b\xb1"
4564+ "\x40\x4c\x95\x8b\x34\x41\x28\x52\x91\x28\x43\xd3\xa3\xb6\xa7\x55"
4565+ "\x15\xe7\x5a\x96\xcb\xf1\xda\xe5\x55\xee\xfe\x1e\xbd\xd9\x41\xd3"
4566+ "\x28\xfd\x97\xca\x57\x2b\x85\x9c\xa4\x30\x95\xaa\xa5\x57\xa2\x35"
4567+ "\x15\x86\xcb\x61\x34\x41\xe4\xc7\x80\x20\x18\x21\x17\x09\x85\x0b"
4568+ "\x14\x9d\x21\x68\x62\x1c\x08\x11\x64\x4b\x92\xf2\xd2\xd3\x2d\x2d"
4569+ "\x6a\xc2\x73\x6b\x3c\x3c\x8b\x9e\xbc\x52\xaa\xa4\xab\x81\x6c\xf6"
4570+ "\xfa\xbd\x70\xc5\xc6\x7b\xc2\xaa\x22\x4f\x58\x04\x87\x25\x6a\x27"
4571+ "\x1d\xa4\x3d\x20\x75\x72\x01\x09\x71\xe5\x1c\x9e\xc3\x2e\x36\xf3"
4572+ "\xd0\xc6\x35\x2a\x43\x4d\x2d\x0e\x2d\xb4\xa1\x49\xce\x65\x1e\x52"
4573+ "\x9e\xa1\xf6\x09\xcc\xdc\x63\x66\xa8\x01\xe9\x3b\x0d\xd7\x5a\x85"
4574+ "\xbb\xc5\x65\xc0\x7b\x2e\x46\xa9\xd9\x56\x1d\x4c\x92\x72\x26\x4e"
4575+ "\x86\xd5\x68\xae\xc4\xaa\x55\xce\xd7\x83\x59\xb3\x81\xee\xce\x74"
4576+ "\x39\x39\x31\x9f\x8a\x25\xe8\xa5\xa5\xe5\x81\xf2\x11\x23\xcb\xa1"
4577+ "\x1e\x43\x12\xe3\xb1\x2a\x2b\xcd\xc8\x8d\x25\x96\xa4\x47\x7d\x95"
4578+ "\xa5\xc6\x9f\x61\xe4\x25\xc6\x5e\x69\xc4\xe7\x29\x5b\x6e\xb6\xa4"
4579+ "\xad\x0b\x4e\x72\x95\x25\x58\x56\x33\x9c\x67\xce\xef\x0f\x17\xbf"
4580+ "\x4c\x7b\x2d\xe6\xfe\x76\x35\x27\x5a\x07\x97\x67\xe8\xae\x8d\x71"
4581+ "\x0f\xb2\x13\x99\xb9\xbc\x14\xad\xb3\xb7\xe6\x11\x6f\xe0\xda\x58"
4582+ "\xb1\x08\xac\xa6\x6c\x2d\x7f\x05\xb7\x56\xd2\xe6\xcf\xbb\x4d\x0c"
4583+ "\xe3\x50\xb2\xec\x91\xf0\x4a\xb8\xd6\x22\xb8\xa7\xf6\x67\xaf\xcf"
4584+ "\x63\x7e\xd7\xe7\x42\xd8\xbd\xc3\x71\xa1\xf2\x7e\x9b\xa8\x97\x83"
4585+ "\x6e\xd1\xdc\x4b\x06\x11\x2d\xae\x26\x61\x98\x72\x10\xf4\x42\x5d"
4586+ "\x20\x4a\xa3\x73\xd7\xf2\xcd\x3c\x48\x32\xe4\x03\x9f\x80\x37\x08"
4587+ "\x36\x11\xd0\xcb\x97\x6c\x08\xed\x6d\x33\x24\xa2\x1b\xb4\x77\xdf"
4588+ "\x61\x5d\x5f\xc1\x43\xc2\x82\xeb\x0f\x5d\x84\x08\x68\xaa\xa4\x01"
4589+ "\xe1\x19\xdf\xbc\x31\x65\xfe\xd1\xf5\x7d\x7a\xb2\x2a\x33\x50\x21"
4590+ "\x2a\x56\x9d\xb1\x81\xab\xdb\x35\x78\x30\x83\xd9\x89\x1d\x31\xac"
4591+ "\x96\x14\x07\x61\xbc\x20\x68\x42\x85\x33\x19\xac\xbe\xdb\x34\x56"
4592+ "\xf1\xd5\xfd\x29\xa9\x28\xdb\xcb\x4c\x5a\x23\xdc\xf5\x96\xc5\x10"
4593+ "\xa3\x35\x5b\x14\x68\xd3\x61\x62\x64\x76\x26\xcb\x17\x3e\x34\x98"
4594+ "\x04\xa3\xc4\x20\x38\x90\x92\xe3\xc8\x07\x2c\x36\x74\x66\x26\x0e"
4595+ "\x29\x02\x64\x29\x2d\x21\xe6\x16\x9c\x6b\xce\xa3\x89\xd9\x4f\xd3"
4596+ "\xc4\xbd\xc5\x87\x79\x9c\x65\xf6\x39\x45\x60\xe8\xce\x9e\xab\x6d"
4597+ "\x13\x15\x22\xe1\x5e\x4b\x38\x42\xc4\x1e\xd5\x76\xe0\xc5\xeb\x85"
4598+ "\x07\x2d\x0f\xb8\xb6\xa6\xd6\x6d\x71\x0d\xa2\x43\x4c\x25\xea\xfa"
4599+ "\xa1\xae\x4c\xe4\x7d\xbd\x76\xa9\xfb\x06\xc2\x83\x42\xeb\xad\xe7"
4600+ "\xe9\x5f\x68\x6f\xba\xfb\x2f\x07\xce\xb8\x13\xc1\x9b\xeb\xb0\x76"
4601+ "\x45\x57\x28\x7b\xea\xbe\x0f\xf4\x30\x7b\xa0\xed\xe4\x22\x93\x21"
4602+ "\xfc\xbc\xe0\xb9\x75\xc1\x4f\xfc\xef\xb6\xfa\xa1\xfc\x64\xa1\x4a"
4603+ "\x82\xc7\x33\xad\x75\xed\x82\xbd\x3d\xdb\xf7\xa8\xbe\x5e\xbb\x36"
4604+ "\x62\x04\x9a\x2e\xc5\xd9\x9e\x9c\x3a\x0b\x98\x0b\x57\xac\xf1\x24"
4605+ "\x62\x58\x83\x15\x5b\xa6\xf2\xda\x34\x70\x03\xce\x0f\x93\x1b\x12"
4606+ "\xc7\xce\x54\x87\x33\x15\xd6\x53\x25\x1f\x2a\x90\x87\x12\xe3\x78"
4607+ "\xef\x55\x77\x4d\x4a\xd8\x7e\xef\xd2\xfd\xd1\xaf\x3a\xaf\x55\xdb"
4608+ "\x6a\x2d\x3d\x42\xac\x51\x79\xee\x91\xab\xe1\x05\x2d\x3c\x80\xa2"
4609+ "\x43\xad\x22\x2e\xd5\x33\x13\xa4\x9e\x00\xe0\x04\x10\x84\xc8\xf2"
4610+ "\x19\x30\x92\x1f\xaa\xc3\x28\xc9\x76\x30\x3f\xe9\x10\x61\x5e\x79"
4611+ "\xd5\xf7\xdf\xd0\x54\xdb\xae\xb6\xae\xfa\xe8\xa3\x57\xe0\x6c\x2d"
4612+ "\xf7\xbd\x49\xd6\x6e\x76\x79\xcc\x54\x0c\x5f\xff\x00\xbb\x06\x98"
4613+ "\xa6\x9e\x89\x61\xb4\x6f\xc3\xe3\x6a\xc2\x4f\x59\x03\xc9\x80\x2c"
4614+ "\x59\x24\x44\x70\x38\xd5\x96\x6a\x9e\x8b\x81\x64\xe5\xbc\xa0\x3c"
4615+ "\x33\xaf\x17\x9d\xff\x00\x71\x1a\xd1\x3a\x80\x66\xb3\xd9\x31\x77"
4616+ "\x0d\x12\xbd\xae\x29\xb5\x6a\xd6\xcf\x8d\x68\x87\x75\xcd\xe8\x65"
4617+ "\x5a\xbe\x3c\x04\x7b\x34\xdb\x54\x19\xa4\x63\x9c\x2a\x5d\x23\xbe"
4618+ "\xf4\xb1\x1c\x4d\x90\xec\x92\x2f\x49\x71\xf7\x14\xf2\x97\x9f\x15"
4619+ "\x57\xed\x13\x21\x2a\xf5\x33\xd1\x2a\x52\x52\xac\xb7\x62\xd1\xcb"
4620+ "\x46\x73\x8c\x67\x28\x56\x77\x86\xbf\x6f\x2a\x4e\x73\xfe\x95\x65"
4621+ "\x0b\x5a\x3e\x38\xfc\xfc\xaa\x56\x3f\x86\x73\xe3\xb9\x4a\x52\x84"
4622+ "\xa5\x08\x4e\x12\x94\x27\x09\x4a\x53\x8c\x61\x29\x4a\x71\xf0\x4a"
4623+ "\x53\x8c\x7e\x31\x8c\x63\x18\xc6\x31\x8f\xc6\x31\xf8\xc7\x9f\x7c"
4624+ "\xd5\xbb\xae\x5e\xe2\x1f\xab\x6e\x24\x34\x00\x8a\x25\x83\x70\x40"
4625+ "\x1c\xcc\xda\x45\x7f\x66\x4e\x30\x2e\x94\x7e\x74\x49\xf0\xe4\x4e"
4626+ "\x06\x5c\xa8\x2f\x89\x21\x2e\x98\x0e\xd9\x21\xc2\x0b\x21\x0f\xc4"
4627+ "\x16\x6e\x48\xd9\xe4\xe3\x4a\x19\x1e\x64\x67\x54\xff\x00\x3a\x6d"
4628+ "\x4f\x62\xb5\x00\x4a\xaa\x51\xfd\x2d\xe8\x0e\x6c\xaf\xc6\x7d\x6d"
4629+ "\xc8\x88\xc7\x67\xea\x8a\x58\x02\x73\xe3\x65\x4d\xc9\x24\xc0\x3d"
4630+ "\x57\xa3\x2e\x53\x16\x99\x4f\xe5\xe7\x19\x97\x3e\x3b\xcf\xc9\x4b"
4631+ "\x99\x7f\x33\x25\xa5\xdf\xba\x77\x2b\xd3\x3e\xc2\x7b\x8b\x94\x07"
4632+ "\xe9\x52\x5b\x43\x87\x34\x14\x86\x37\xcf\x41\x6b\x8e\x6a\xa5\x22"
4633+ "\xab\xdb\x96\xa2\xcf\x46\xd8\x9b\x45\x93\xef\xd6\xdf\x3e\x99\x9c"
4634+ "\x7e\x29\x10\x6b\x6c\xa2\xb8\x43\x05\x09\x44\x70\x8c\xb8\xaa\x54"
4635+ "\x7c\x30\x36\x5e\x1c\x5e\x5b\x9f\x6c\x0d\x81\xee\xa0\x93\x8d\x67"
4636+ "\x55\xf3\x87\xaf\xaa\x6b\x58\xf9\xbe\xb2\x36\x07\x42\x6e\xbd\x96"
4637+ "\xe3\x9f\x1f\x8f\xc9\xf4\x9d\xae\x6a\x7d\x4c\x96\xbe\x5f\xc7\xcd"
4638+ "\xf3\xb2\xf7\xcd\xf0\xcf\xc3\xe4\xf8\xfe\x37\x4f\x1c\x4d\xf6\x40"
4639+ "\xf1\x6b\x7c\x4e\xe0\xa6\x71\xad\x56\xa7\x1c\x5c\x15\x6b\xfc\xf3"
4640+ "\x01\x5d\xac\xf1\x75\x9a\x72\x6b\xaa\x28\xc5\x88\x6d\xfb\x33\x85"
4641+ "\xe0\x4e\x61\xab\xeb\x31\x2c\x71\x08\x73\x11\x3b\xfc\xb5\xc0\x96"
4642+ "\xcc\x87\x24\x44\xb5\x9b\x9e\xb3\x71\xba\xe9\xed\xb1\x4e\xd7\x76"
4643+ "\x6c\xd2\xb6\x05\xb7\x5a\xde\xeb\x34\x5b\x96\x16\xfb\x59\xa9\x5c"
4644+ "\x4f\x55\xca\x8a\xac\x59\xb0\xe4\x54\x39\x25\xbc\x81\x37\x2a\x09"
4645+ "\x5f\x9e\x3b\x6b\x7d\x1f\x69\xf3\x34\x85\x39\x84\xa7\x28\x0b\xd3"
4646+ "\xfd\xfb\x4b\x7a\xea\xe7\xd2\x3c\xd3\xda\x15\x68\xbc\x73\xd3\x22"
4647+ "\x6f\xd7\x72\x5b\x2b\x66\xee\xa8\x0d\x54\xe8\x5b\xf9\x92\x96\x92"
4648+ "\x93\xea\x97\x4a\xc7\x43\x10\x46\x35\xc5\xc0\x60\x8a\xe4\xc1\xb5"
4649+ "\x36\xc6\xae\xed\xf7\x70\xa5\x86\x99\x3d\x91\xf8\xfd\x4e\x53\xeb"
4650+ "\xbb\xbd\x6d\xec\x8f\xd7\x89\x3d\x31\x7f\xd7\x78\xba\x50\xbb\x74"
4651+ "\x9d\xf6\xac\x4e\xb9\x03\x9c\x79\xd5\xe1\xbd\x17\x68\xd9\x13\x0b"
4652+ "\x45\x75\x88\x00\x1d\x1f\xae\x73\x6a\x1d\x5c\x6e\x44\x9f\xa6\xfa"
4653+ "\x4e\xd8\x25\x8b\xc0\xbc\xb2\x99\xe3\x17\x24\xb3\x23\xe2\x48\x8b"
4654+ "\xfa\x22\xe7\x7e\x8f\xe6\x3f\x5f\x55\x0d\x75\xd3\x51\x0b\xd7\xed"
4655+ "\xd3\x6f\x97\x3b\x85\x42\x80\x7e\x5f\xdc\x1b\xd6\xba\xee\xc4\x80"
4656+ "\xce\x06\xa9\x15\x8c\x97\x5f\x40\x69\xb2\x4d\xc5\xb2\x5c\x1e\x01"
4657+ "\x87\x7e\xe0\x36\x6d\x78\x80\x4e\x3c\x02\xec\x90\x1d\x11\x81\x74"
4658+ "\xa5\x8b\xa4\xa0\x56\x06\xd5\x79\x72\x85\x57\x3b\xb2\x2e\xae\x90"
4659+ "\x18\x8d\x91\xb2\x0e\x44\x19\xaa\xb4\xcc\x08\xed\x46\xfa\xd7\x2b"
4660+ "\x78\x58\x72\x5d\xbb\x5e\x49\xe7\xee\xf3\x8a\x9d\x22\xa4\x19\xc8"
4661+ "\xe7\x08\xc3\x90\x9b\x35\x9a\xa4\x25\x8c\x4b\x9b\xa7\xf8\xbf\x81"
4662+ "\xf5\xdf\x22\x66\xf1\x7e\x9f\x66\x3d\xbb\xfa\x73\x73\x4d\xfd\x67"
4663+ "\x7b\xf4\xce\xc3\x62\x2e\x6f\xbb\x0c\xa2\xdc\x69\xfc\x8a\x17\x0e"
4664+ "\x3a\x9e\x83\x46\xd7\xe3\x5e\x65\x86\xc0\x51\x00\xbb\x91\xe3\xe1"
4665+ "\xc1\x16\xc4\xe9\x65\x5c\x14\x3e\x44\x6a\x6b\xd1\x1e\xb0\x36\xdd"
4666+ "\x0b\x7d\x8a\xeb\xaf\x58\x5b\x64\x3f\x38\xed\x52\x76\xe8\x46\xf7"
4667+ "\x86\x84\xb3\x93\xb1\x0b\xe5\xfd\xfd\x0d\xe9\x6d\xe4\xf1\x1b\x1d"
4668+ "\x56\xb4\x34\xe4\x6a\xf5\xa4\x9c\x2c\xc9\x64\x94\xc1\xf5\x79\x6d"
4669+ "\x12\x96\xf3\x47\xc5\x48\xa8\xdb\xd8\x95\x64\x29\xcf\xf6\x88\xf1"
4670+ "\x95\x7a\x98\xe8\xbc\x27\x19\xce\x73\x61\xd1\xb8\xc6\x31\x8c\xe7"
4671+ "\x39\xce\x77\x9e\xbc\xc6\x31\x8c\x63\xf3\x9c\xe7\x39\xc6\x31\x8f"
4672+ "\xf7\xce\x7e\x1e\x3b\x7f\x0f\x0f\x0f\x13\x57\xb9\x0a\xe1\x0b\x64"
4673+ "\x5f\x58\x40\xc6\xc7\x7a\x4b\xf2\x3d\xbc\x71\xf4\xa7\xd2\xca\x14"
4674+ "\xe2\x98\x1a\x30\x1e\xe0\x26\x5a\x6a\xf0\x9c\x67\x38\x66\x00\xb8"
4675+ "\x72\xe6\xbe\xac\xfe\x12\xd3\x0b\x56\x73\x8c\x63\xc7\x2b\xe1\xe2"
4676+ "\xe8\xdd\x7b\xff\x00\xd8\xe5\x23\x6c\xce\xa8\x69\xcf\x5e\x3a\xef"
4677+ "\x77\xea\xe5\xab\x0e\x82\xdb\xd9\xed\x7a\x9e\xb8\x6d\x51\x32\xdb"
4678+ "\x79\xc3\x36\x9a\x2d\xa3\x50\x39\x65\x0a\x63\x0e\xe5\xd4\x39\x12"
4679+ "\xbf\x8b\x98\xa4\xa1\x2d\xad\xb3\xcf\x65\x6a\x43\x78\xb3\x3b\x07"
4680+ "\xd8\xd5\xea\xae\x76\xad\x6f\xf5\xff\x00\xca\x93\xab\x96\xb0\x64"
4681+ "\xeb\xd6\x4a\xd5\x87\xba\xec\x24\x60\x97\x06\x76\x03\xe3\x4c\x07"
4682+ "\x29\x11\x8e\x34\x25\x02\x64\x29\xf0\x25\x48\x85\x3a\x33\x8b\x7a"
4683+ "\x3c\x86\x1e\x75\xa5\x61\xc6\x97\x9f\x8d\x25\xf5\xc9\xcd\xde\xc9"
4684+ "\x7d\x77\xf2\xc8\x7e\x70\xaf\x73\x5f\x2d\xec\xa2\x51\x2d\x96\xfb"
4685+ "\x89\xad\x80\x57\xb2\x36\x1d\x7d\x83\x45\xac\xf3\xdb\xcc\x6c\x31"
4686+ "\x4f\xcf\x30\x58\xd0\x12\x28\x90\x50\x42\x86\xfb\x48\x16\x3c\xc5"
4687+ "\x9c\xf8\xe7\xcc\x29\x88\xb3\x4a\x4b\x4e\x6c\xbc\xdb\xc7\xbb\xe9"
4688+ "\xb6\xa0\x8b\x11\xa1\x7d\x73\xd7\xe9\xbf\x7e\xc2\x6c\x10\x8d\xee"
4689+ "\x9d\xef\x63\x3a\xe0\xf5\xbe\x8c\x3e\xa1\xc7\xc5\xd1\x00\x44\x1e"
4690+ "\xf3\x51\xf2\xe2\xb0\xe3\xb5\x13\x7f\x32\xf1\x8c\xa6\x22\xfe\x1f"
4691+ "\x49\x4d\xbb\xcf\x3a\x5d\xed\x4c\xd2\xfc\x85\xed\x23\xd6\xc7\x50"
4692+ "\xb6\x5b\x3a\x16\x83\xb8\x6f\xfd\x32\x3f\xaa\x36\x34\xbb\xf5\x96"
4693+ "\xa9\xab\xcf\x9f\x8f\xac\xc3\xca\xd5\x8b\xd8\x48\x9e\x79\xaa\x30"
4694+ "\x87\xca\x58\x4d\x59\x96\xb9\x4f\xc5\x1b\x1c\xd2\xda\x5b\xe6\x57"
4695+ "\x29\xa1\x28\x7a\x2b\x5b\xff\x00\x12\x2f\x5e\x3f\xf3\xbb\x8e\x7f"
4696+ "\xec\xc6\x98\xff\x00\xed\x3c\xa6\xdd\xa9\xdc\x7e\xa0\xf7\xd6\x99"
4697+ "\x31\xa2\xf7\xaf\x6b\xe9\x82\x74\x4b\x3d\x8f\x5e\x58\x0b\x33\xab"
4698+ "\xef\xc3\xaf\x84\x64\xb9\xae\xb6\x25\x5f\x62\x8f\x1c\xe3\xf4\x51"
4699+ "\xb7\x96\xe3\x0e\x30\x42\xa9\x18\x39\xbf\x9e\x2a\x1f\x74\x19\x02"
4700+ "\x2d\x43\x93\x06\x63\xb1\xa7\x47\x6a\xfa\x9b\x6c\xeb\xbd\xe9\xae"
4701+ "\x6a\x7b\x6f\x53\x5a\x60\x5d\xb5\xcd\xe8\x67\xeb\x35\x3b\x48\xc6"
4702+ "\xa6\xb3\x04\xc8\xdf\xb8\x7e\x26\x64\xb0\xc9\x18\xb0\xa7\x33\xf2"
4703+ "\x4a\x8b\x22\x3b\x8d\x4b\x89\x1d\xf6\x9d\x65\xc4\x38\xd2\x54\x9c"
4704+ "\xe3\xcd\x89\xe1\xe1\xe6\x3e\x70\x81\x45\x1d\x18\xf9\x31\x83\xc8"
4705+ "\xbe\x14\x82\x4b\x87\x7a\x74\x28\xd2\xdd\x12\x55\x30\xe6\x0e\x49"
4706+ "\x31\x8e\x48\x69\xc5\xc0\x20\x91\xe4\x48\x41\x4c\xd8\xb9\x6a\x4e"
4707+ "\x21\xce\x99\x1b\x0e\xfd\x09\x4f\xa1\x79\x0f\x0f\x0f\x0f\x0f\x0f"
4708+ "\x0f\x3f\x3c\xb8\x71\x27\xc7\x72\x24\xe8\xb1\xa6\xc5\x7b\x18\xc3"
4709+ "\xb1\xa5\xb0\xd4\x98\xee\xe3\x19\xc6\x71\x87\x19\x79\x2b\x6d\x78"
4710+ "\xc6\x71\x8c\xe3\x0a\x4e\x71\x8c\xe3\x19\xfe\x38\xf2\x3b\xfb\x8b"
4711+ "\x48\xfe\x4e\xaa\xff\x00\x4f\x08\xff\x00\xc7\xe1\xfb\x8b\x48\xfe"
4712+ "\x4e\xaa\xff\x00\x4f\x08\xff\x00\xc7\xe4\x95\x86\x18\x8a\xcb\x31"
4713+ "\xa3\x32\xd4\x78\xf1\xdb\x43\x2c\x47\x61\xb4\x32\xcb\x2c\xb4\x9c"
4714+ "\x21\xb6\x99\x69\xbc\x25\xb6\xdb\x6d\x18\xc2\x10\xda\x12\x94\xa1"
4715+ "\x38\xc2\x53\x8c\x63\x18\xc7\x9d\xbe\x7f\xff\xd9"
4716+ ;
4717diff -Nura php-5.3.1RC1/main/suhosin_patch.c suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_patch.c
4718--- php-5.3.1RC1/main/suhosin_patch.c 1970-01-01 01:00:00.000000000 +0100
4719+++ suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_patch.c 2009-09-27 19:44:00.000000000 +0200
4720@@ -0,0 +1,458 @@
4721+/*
4722+ +----------------------------------------------------------------------+
4723+ | Suhosin Patch for PHP |
4724+ +----------------------------------------------------------------------+
4725+ | Copyright (c) 2004-2006 Stefan Esser |
4726+ +----------------------------------------------------------------------+
4727+ | This source file is subject to version 2.02 of the PHP license, |
4728+ | that is bundled with this package in the file LICENSE, and is |
4729+ | available at through the world-wide-web at |
4730+ | http://www.php.net/license/2_02.txt. |
4731+ | If you did not receive a copy of the PHP license and are unable to |
4732+ | obtain it through the world-wide-web, please send a note to |
4733+ | license@php.net so we can mail you a copy immediately. |
4734+ +----------------------------------------------------------------------+
4735+ | Author: Stefan Esser <sesser@hardened-php.net> |
4736+ +----------------------------------------------------------------------+
4737+ */
4738+/* $Id$ */
4739+
4740+#include "php.h"
4741+
4742+#include <stdio.h>
4743+#include <stdlib.h>
4744+
4745+#if HAVE_UNISTD_H
4746+#include <unistd.h>
4747+#endif
4748+#include "SAPI.h"
4749+#include "php_globals.h"
4750+
4751+#if SUHOSIN_PATCH
4752+
4753+#ifdef HAVE_SYS_SOCKET_H
4754+#include <sys/socket.h>
4755+#endif
4756+
4757+#if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
4758+#undef AF_UNIX
4759+#endif
4760+
4761+#if defined(AF_UNIX)
4762+#include <sys/un.h>
4763+#endif
4764+
4765+#define SYSLOG_PATH "/dev/log"
4766+
4767+#ifdef PHP_WIN32
4768+static HANDLE log_source = 0;
4769+#endif
4770+
4771+#include "snprintf.h"
4772+
4773+#include "suhosin_patch.h"
4774+
4775+#ifdef ZTS
4776+#include "suhosin_globals.h"
4777+int suhosin_patch_globals_id;
4778+#else
4779+struct _suhosin_patch_globals suhosin_patch_globals;
4780+#endif
4781+
4782+/* hack that needs to be fixed */
4783+#ifndef PAGE_SIZE
4784+#define PAGE_SIZE 4096
4785+#endif
4786+
4787+#ifdef ZEND_WIN32
4788+__declspec(align(PAGE_SIZE))
4789+#endif
4790+char suhosin_config[PAGE_SIZE]
4791+#if defined(__GNUC__)
4792+ __attribute__ ((aligned(PAGE_SIZE)))
4793+#endif
4794+;
4795+
4796+static void php_security_log(int loglevel, char *fmt, ...);
4797+
4798+static void suhosin_patch_globals_ctor(suhosin_patch_globals_struct *suhosin_patch_globals TSRMLS_DC)
4799+{
4800+ memset(suhosin_patch_globals, 0, sizeof(*suhosin_patch_globals));
4801+}
4802+
4803+static void suhosin_read_configuration_from_environment()
4804+{
4805+ char *tmp;
4806+
4807+ /* check if canary protection should be activated or not */
4808+ tmp = getenv("SUHOSIN_MM_USE_CANARY_PROTECTION");
4809+ /* default to activated */
4810+ SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) = 1;
4811+ if (tmp) {
4812+ int flag = zend_atoi(tmp, 0);
4813+ SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) = flag;
4814+ }
4815+
4816+ /* check if free memory should be overwritten with 0xFF or not */
4817+ tmp = getenv("SUHOSIN_MM_DESTROY_FREE_MEMORY");
4818+ /* default to deactivated */
4819+ SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY) = 0;
4820+ if (tmp) {
4821+ int flag = zend_atoi(tmp, 0);
4822+ SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY) = flag;
4823+ }
4824+
4825+ /* check if canary violations should be ignored */
4826+ tmp = getenv("SUHOSIN_MM_IGNORE_CANARY_VIOLATION");
4827+ /* default to NOT ignore */
4828+ SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) = 0;
4829+ if (tmp) {
4830+ int flag = zend_atoi(tmp, 0);
4831+ SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) = flag;
4832+ }
4833+
4834+ /* check if invalid hashtable destructors should be ignored */
4835+ tmp = getenv("SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR");
4836+ /* default to NOT ignore */
4837+ SUHOSIN_CONFIG(SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR) = 0;
4838+ if (tmp) {
4839+ int flag = zend_atoi(tmp, 0);
4840+ SUHOSIN_CONFIG(SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR) = flag;
4841+ }
4842+
4843+ /* check if invalid linkedlist destructors should be ignored */
4844+ tmp = getenv("SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR");
4845+ /* default to NOT ignore */
4846+ SUHOSIN_CONFIG(SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR) = 0;
4847+ if (tmp) {
4848+ int flag = zend_atoi(tmp, 0);
4849+ SUHOSIN_CONFIG(SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR) = flag;
4850+ }
4851+
4852+ SUHOSIN_CONFIG(SUHOSIN_CONFIG_SET) = 1;
4853+}
4854+
4855+static void suhosin_write_protect_configuration()
4856+{
4857+#if defined(__GNUC__)
4858+ mprotect(suhosin_config, PAGE_SIZE, PROT_READ);
4859+#endif
4860+}
4861+
4862+PHPAPI void suhosin_startup()
4863+{
4864+#ifdef ZTS
4865+ ts_allocate_id(&suhosin_patch_globals_id, sizeof(suhosin_patch_globals_struct), (ts_allocate_ctor) suhosin_patch_globals_ctor, NULL);
4866+#else
4867+ suhosin_patch_globals_ctor(&suhosin_patch_globals TSRMLS_CC);
4868+#endif
4869+ zend_suhosin_log = php_security_log;
4870+
4871+ if (!SUHOSIN_CONFIG(SUHOSIN_CONFIG_SET)) {
4872+ suhosin_read_configuration_from_environment();
4873+ suhosin_write_protect_configuration();
4874+ }
4875+}
4876+
4877+/*PHPAPI void suhosin_clear_mm_canaries(TSRMLS_D)
4878+{
4879+ zend_alloc_clear_mm_canaries(AG(heap));
4880+ SPG(canary_1) = zend_canary();
4881+ SPG(canary_2) = zend_canary();
4882+ SPG(canary_3) = zend_canary();
4883+}*/
4884+
4885+static char *loglevel2string(int loglevel)
4886+{
4887+ switch (loglevel) {
4888+ case S_FILES:
4889+ return "FILES";
4890+ case S_INCLUDE:
4891+ return "INCLUDE";
4892+ case S_MEMORY:
4893+ return "MEMORY";
4894+ case S_MISC:
4895+ return "MISC";
4896+ case S_SESSION:
4897+ return "SESSION";
4898+ case S_SQL:
4899+ return "SQL";
4900+ case S_EXECUTOR:
4901+ return "EXECUTOR";
4902+ case S_VARS:
4903+ return "VARS";
4904+ default:
4905+ return "UNKNOWN";
4906+ }
4907+}
4908+
4909+static void php_security_log(int loglevel, char *fmt, ...)
4910+{
4911+ int s, r, i=0;
4912+#if defined(AF_UNIX)
4913+ struct sockaddr_un saun;
4914+#endif
4915+#ifdef PHP_WIN32
4916+ LPTSTR strs[2];
4917+ unsigned short etype;
4918+ DWORD evid;
4919+#endif
4920+ char buf[4096+64];
4921+ char error[4096+100];
4922+ char *ip_address;
4923+ char *fname;
4924+ char *alertstring;
4925+ int lineno;
4926+ va_list ap;
4927+ TSRMLS_FETCH();
4928+
4929+ /*SDEBUG("(suhosin_log) loglevel: %d log_syslog: %u - log_sapi: %u - log_script: %u", loglevel, SPG(log_syslog), SPG(log_sapi), SPG(log_script));*/
4930+
4931+ if (SPG(log_use_x_forwarded_for)) {
4932+ ip_address = sapi_getenv("HTTP_X_FORWARDED_FOR", 20 TSRMLS_CC);
4933+ if (ip_address == NULL) {
4934+ ip_address = "X-FORWARDED-FOR not set";
4935+ }
4936+ } else {
4937+ ip_address = sapi_getenv("REMOTE_ADDR", 11 TSRMLS_CC);
4938+ if (ip_address == NULL) {
4939+ ip_address = "REMOTE_ADDR not set";
4940+ }
4941+ }
4942+
4943+
4944+ va_start(ap, fmt);
4945+ ap_php_vsnprintf(error, sizeof(error), fmt, ap);
4946+ va_end(ap);
4947+ while (error[i]) {
4948+ if (error[i] < 32) error[i] = '.';
4949+ i++;
4950+ }
4951+
4952+/* if (SPG(simulation)) {
4953+ alertstring = "ALERT-SIMULATION";
4954+ } else { */
4955+ alertstring = "ALERT";
4956+/* }*/
4957+
4958+ if (zend_is_executing(TSRMLS_C)) {
4959+ if (EG(current_execute_data)) {
4960+ lineno = EG(current_execute_data)->opline->lineno;
4961+ fname = EG(current_execute_data)->op_array->filename;
4962+ } else {
4963+ lineno = zend_get_executed_lineno(TSRMLS_C);
4964+ fname = zend_get_executed_filename(TSRMLS_C);
4965+ }
4966+ ap_php_snprintf(buf, sizeof(buf), "%s - %s (attacker '%s', file '%s', line %u)", alertstring, error, ip_address, fname, lineno);
4967+ } else {
4968+ fname = sapi_getenv("SCRIPT_FILENAME", 15 TSRMLS_CC);
4969+ if (fname==NULL) {
4970+ fname = "unknown";
4971+ }
4972+ ap_php_snprintf(buf, sizeof(buf), "%s - %s (attacker '%s', file '%s')", alertstring, error, ip_address, fname);
4973+ }
4974+
4975+ /* Syslog-Logging disabled? */
4976+ if (((SPG(log_syslog)|S_INTERNAL) & loglevel)==0) {
4977+ goto log_sapi;
4978+ }
4979+
4980+#if defined(AF_UNIX)
4981+ ap_php_snprintf(error, sizeof(error), "<%u>suhosin[%u]: %s\n", (unsigned int)(SPG(log_syslog_facility)|SPG(log_syslog_priority)),getpid(),buf);
4982+
4983+ s = socket(AF_UNIX, SOCK_DGRAM, 0);
4984+ if (s == -1) {
4985+ goto log_sapi;
4986+ }
4987+
4988+ memset(&saun, 0, sizeof(saun));
4989+ saun.sun_family = AF_UNIX;
4990+ strcpy(saun.sun_path, SYSLOG_PATH);
4991+ /*saun.sun_len = sizeof(saun);*/
4992+
4993+ r = connect(s, (struct sockaddr *)&saun, sizeof(saun));
4994+ if (r) {
4995+ close(s);
4996+ s = socket(AF_UNIX, SOCK_STREAM, 0);
4997+ if (s == -1) {
4998+ goto log_sapi;
4999+ }
5000+
5001+ memset(&saun, 0, sizeof(saun));
5002+ saun.sun_family = AF_UNIX;
5003+ strcpy(saun.sun_path, SYSLOG_PATH);
5004+ /*saun.sun_len = sizeof(saun);*/
5005+
5006+ r = connect(s, (struct sockaddr *)&saun, sizeof(saun));
5007+ if (r) {
5008+ close(s);
5009+ goto log_sapi;
5010+ }
5011+ }
5012+ send(s, error, strlen(error), 0);
5013+
5014+ close(s);
5015+#endif
5016+#ifdef PHP_WIN32
5017+ ap_php_snprintf(error, sizeof(error), "suhosin[%u]: %s", getpid(),buf);
5018+
5019+ switch (SPG(log_syslog_priority)) { /* translate UNIX type into NT type */
5020+ case 1: /*LOG_ALERT:*/
5021+ etype = EVENTLOG_ERROR_TYPE;
5022+ break;
5023+ case 6: /*LOG_INFO:*/
5024+ etype = EVENTLOG_INFORMATION_TYPE;
5025+ break;
5026+ default:
5027+ etype = EVENTLOG_WARNING_TYPE;
5028+ }
5029+ evid = loglevel;
5030+ strs[0] = error;
5031+ /* report the event */
5032+ if (log_source == NULL) {
5033+ log_source = RegisterEventSource(NULL, "Suhosin-Patch-" SUHOSIN_PATCH_VERSION);
5034+ }
5035+ ReportEvent(log_source, etype, (unsigned short) SPG(log_syslog_priority), evid, NULL, 1, 0, strs, NULL);
5036+
5037+#endif
5038+log_sapi:
5039+ /* SAPI Logging activated? */
5040+ /*SDEBUG("(suhosin_log) log_syslog: %u - log_sapi: %u - log_script: %u - log_phpscript: %u", SPG(log_syslog), SPG(log_sapi), SPG(log_script), SPG(log_phpscript));*/
5041+ if (((SPG(log_sapi)|S_INTERNAL) & loglevel)!=0) {
5042+ sapi_module.log_message(buf);
5043+ }
5044+
5045+/*log_script:*/
5046+ /* script logging activaed? */
5047+ if (((SPG(log_script) & loglevel)!=0) && SPG(log_scriptname)!=NULL) {
5048+ char cmd[8192], *cmdpos, *bufpos;
5049+ FILE *in;
5050+ int space;
5051+
5052+ ap_php_snprintf(cmd, sizeof(cmd), "%s %s \'", SPG(log_scriptname), loglevel2string(loglevel));
5053+ space = sizeof(cmd) - strlen(cmd);
5054+ cmdpos = cmd + strlen(cmd);
5055+ bufpos = buf;
5056+ if (space <= 1) return;
5057+ while (space > 2 && *bufpos) {
5058+ if (*bufpos == '\'') {
5059+ if (space<=5) break;
5060+ *cmdpos++ = '\'';
5061+ *cmdpos++ = '\\';
5062+ *cmdpos++ = '\'';
5063+ *cmdpos++ = '\'';
5064+ bufpos++;
5065+ space-=4;
5066+ } else {
5067+ *cmdpos++ = *bufpos++;
5068+ space--;
5069+ }
5070+ }
5071+ *cmdpos++ = '\'';
5072+ *cmdpos = 0;
5073+
5074+ if ((in=VCWD_POPEN(cmd, "r"))==NULL) {
5075+ php_security_log(S_INTERNAL, "Unable to execute logging shell script: %s", SPG(log_scriptname));
5076+ return;
5077+ }
5078+ /* read and forget the result */
5079+ while (1) {
5080+ int readbytes = fread(cmd, 1, sizeof(cmd), in);
5081+ if (readbytes<=0) {
5082+ break;
5083+ }
5084+ }
5085+ pclose(in);
5086+ }
5087+/*log_phpscript:*/
5088+ if ((SPG(log_phpscript) & loglevel)!=0 && EG(in_execution) && SPG(log_phpscriptname) && SPG(log_phpscriptname)[0]) {
5089+ zend_file_handle file_handle;
5090+ zend_op_array *new_op_array;
5091+ zval *result = NULL;
5092+
5093+ /*long orig_execution_depth = SPG(execution_depth);*/
5094+ zend_bool orig_safe_mode = PG(safe_mode);
5095+ char *orig_basedir = PG(open_basedir);
5096+
5097+ char *phpscript = SPG(log_phpscriptname);
5098+/*SDEBUG("scriptname %s", SPG(log_phpscriptname));`*/
5099+#ifdef ZEND_ENGINE_2
5100+ if (zend_stream_open(phpscript, &file_handle TSRMLS_CC) == SUCCESS) {
5101+#else
5102+ if (zend_open(phpscript, &file_handle) == SUCCESS && ZEND_IS_VALID_FILE_HANDLE(&file_handle)) {
5103+ file_handle.filename = phpscript;
5104+ file_handle.free_filename = 0;
5105+#endif
5106+ if (!file_handle.opened_path) {
5107+ file_handle.opened_path = estrndup(phpscript, strlen(phpscript));
5108+ }
5109+ new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC);
5110+ zend_destroy_file_handle(&file_handle TSRMLS_CC);
5111+ if (new_op_array) {
5112+ HashTable *active_symbol_table = EG(active_symbol_table);
5113+ zval *zerror, *zerror_class;
5114+
5115+ if (active_symbol_table == NULL) {
5116+ active_symbol_table = &EG(symbol_table);
5117+ }
5118+ EG(return_value_ptr_ptr) = &result;
5119+ EG(active_op_array) = new_op_array;
5120+
5121+ MAKE_STD_ZVAL(zerror);
5122+ MAKE_STD_ZVAL(zerror_class);
5123+ ZVAL_STRING(zerror, buf, 1);
5124+ ZVAL_LONG(zerror_class, loglevel);
5125+
5126+ zend_hash_update(active_symbol_table, "SUHOSIN_ERROR", sizeof("SUHOSIN_ERROR"), (void **)&zerror, sizeof(zval *), NULL);
5127+ zend_hash_update(active_symbol_table, "SUHOSIN_ERRORCLASS", sizeof("SUHOSIN_ERRORCLASS"), (void **)&zerror_class, sizeof(zval *), NULL);
5128+
5129+ /*SPG(execution_depth) = 0;*/
5130+ if (SPG(log_phpscript_is_safe)) {
5131+ PG(safe_mode) = 0;
5132+ PG(open_basedir) = NULL;
5133+ }
5134+
5135+ zend_execute(new_op_array TSRMLS_CC);
5136+
5137+ /*SPG(execution_depth) = orig_execution_depth;*/
5138+ PG(safe_mode) = orig_safe_mode;
5139+ PG(open_basedir) = orig_basedir;
5140+
5141+#ifdef ZEND_ENGINE_2
5142+ destroy_op_array(new_op_array TSRMLS_CC);
5143+#else
5144+ destroy_op_array(new_op_array);
5145+#endif
5146+ efree(new_op_array);
5147+#ifdef ZEND_ENGINE_2
5148+ if (!EG(exception))
5149+#endif
5150+ {
5151+ if (EG(return_value_ptr_ptr)) {
5152+ zval_ptr_dtor(EG(return_value_ptr_ptr));
5153+ EG(return_value_ptr_ptr) = NULL;
5154+ }
5155+ }
5156+ } else {
5157+ php_security_log(S_INTERNAL, "Unable to execute logging PHP script: %s", SPG(log_phpscriptname));
5158+ return;
5159+ }
5160+ } else {
5161+ php_security_log(S_INTERNAL, "Unable to execute logging PHP script: %s", SPG(log_phpscriptname));
5162+ return;
5163+ }
5164+ }
5165+
5166+}
5167+
5168+
5169+#endif
5170+
5171+/*
5172+ * Local variables:
5173+ * tab-width: 4
5174+ * c-basic-offset: 4
5175+ * End:
5176+ * vim600: sw=4 ts=4 fdm=marker
5177+ * vim<600: sw=4 ts=4
5178+ */
5179diff -Nura php-5.3.1RC1/main/suhosin_patch.h suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_patch.h
5180--- php-5.3.1RC1/main/suhosin_patch.h 1970-01-01 01:00:00.000000000 +0100
5181+++ suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_patch.h 2009-09-27 19:40:08.000000000 +0200
5182@@ -0,0 +1,63 @@
5183+/*
5184+ +----------------------------------------------------------------------+
5185+ | Suhosin Patch for PHP |
5186+ +----------------------------------------------------------------------+
5187+ | Copyright (c) 2004-2009 Stefan Esser |
5188+ +----------------------------------------------------------------------+
5189+ | This source file is subject to version 2.02 of the PHP license, |
5190+ | that is bundled with this package in the file LICENSE, and is |
5191+ | available at through the world-wide-web at |
5192+ | http://www.php.net/license/2_02.txt. |
5193+ | If you did not receive a copy of the PHP license and are unable to |
5194+ | obtain it through the world-wide-web, please send a note to |
5195+ | license@php.net so we can mail you a copy immediately. |
5196+ +----------------------------------------------------------------------+
5197+ | Author: Stefan Esser <stefan.esser@sektioneins.de> |
5198+ +----------------------------------------------------------------------+
5199+ */
5200+
5201+#ifndef SUHOSIN_PATCH_H
5202+#define SUHOSIN_PATCH_H
5203+
5204+#if SUHOSIN_PATCH
5205+
5206+#include "zend.h"
5207+
5208+#define SUHOSIN_PATCH_VERSION "0.9.8"
5209+
5210+#define SUHOSIN_LOGO_GUID "SUHO8567F54-D428-14d2-A769-00DA302A5F18"
5211+
5212+#define SUHOSIN_CONFIG(idx) suhosin_config[(idx)]
5213+#define SUHOSIN_MM_USE_CANARY_PROTECTION 0
5214+#define SUHOSIN_MM_DESTROY_FREE_MEMORY 1
5215+#define SUHOSIN_MM_IGNORE_CANARY_VIOLATION 2
5216+#define SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR 3
5217+#define SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR 4
5218+
5219+#define SUHOSIN_CONFIG_SET 100
5220+
5221+#include <sys/types.h>
5222+#include <sys/stat.h>
5223+#include <sys/mman.h>
5224+
5225+#if defined(DARWIN)
5226+#include <mach/vm_param.h>
5227+#endif
5228+
5229+/* hack that needs to be fixed */
5230+#ifndef PAGE_SIZE
5231+#define PAGE_SIZE 4096
5232+#endif
5233+
5234+extern char suhosin_config[PAGE_SIZE];
5235+
5236+#endif
5237+
5238+#endif /* SUHOSIN_PATCH_H */
5239+
5240+/*
5241+ * Local variables:
5242+ * tab-width: 4
5243+ * c-basic-offset: 4
5244+ * End:
5245+ */
5246diff -Nura php-5.3.1RC1/main/suhosin_patch.m4 suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_patch.m4
5247--- php-5.3.1RC1/main/suhosin_patch.m4 1970-01-01 01:00:00.000000000 +0100
5248+++ suhosin-patch-5.3.1RC1-0.9.8/main/suhosin_patch.m4 2009-09-27 19:04:06.000000000 +0200
5249@@ -0,0 +1,8 @@
5250+dnl
5251+dnl $Id$
5252+dnl
5253+dnl This file contains Suhosin Patch for PHP specific autoconf functions.
5254+dnl
5255+
5256+AC_DEFINE(SUHOSIN_PATCH, 1, [Suhosin Patch])
5257+
5258diff -Nura php-5.3.1RC1/sapi/apache/mod_php5.c suhosin-patch-5.3.1RC1-0.9.8/sapi/apache/mod_php5.c
5259--- php-5.3.1RC1/sapi/apache/mod_php5.c 2008-12-31 12:15:49.000000000 +0100
5260+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/apache/mod_php5.c 2009-09-27 19:04:06.000000000 +0200
5261@@ -967,7 +967,11 @@
5262 {
5263 TSRMLS_FETCH();
5264 if (PG(expose_php)) {
5265+#if SUHOSIN_PATCH
5266+ ap_add_version_component("PHP/" PHP_VERSION " with Suhosin-Patch");
5267+#else
5268 ap_add_version_component("PHP/" PHP_VERSION);
5269+#endif
5270 }
5271 }
5272 #endif
5273diff -Nura php-5.3.1RC1/sapi/apache2filter/sapi_apache2.c suhosin-patch-5.3.1RC1-0.9.8/sapi/apache2filter/sapi_apache2.c
5274--- php-5.3.1RC1/sapi/apache2filter/sapi_apache2.c 2008-12-31 12:15:49.000000000 +0100
5275+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/apache2filter/sapi_apache2.c 2009-09-27 19:04:06.000000000 +0200
5276@@ -581,7 +581,11 @@
5277 {
5278 TSRMLS_FETCH();
5279 if (PG(expose_php)) {
5280+#if SUHOSIN_PATCH
5281+ ap_add_version_component(p, "PHP/" PHP_VERSION " with Suhosin-Patch");
5282+#else
5283 ap_add_version_component(p, "PHP/" PHP_VERSION);
5284+#endif
5285 }
5286 }
5287
5288diff -Nura php-5.3.1RC1/sapi/apache2handler/sapi_apache2.c suhosin-patch-5.3.1RC1-0.9.8/sapi/apache2handler/sapi_apache2.c
5289--- php-5.3.1RC1/sapi/apache2handler/sapi_apache2.c 2008-12-31 12:15:49.000000000 +0100
5290+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/apache2handler/sapi_apache2.c 2009-09-27 19:04:06.000000000 +0200
5291@@ -386,7 +386,11 @@
5292 {
5293 TSRMLS_FETCH();
5294 if (PG(expose_php)) {
5295+#if SUHOSIN_PATCH
5296+ ap_add_version_component(p, "PHP/" PHP_VERSION " with Suhosin-Patch");
5297+#else
5298 ap_add_version_component(p, "PHP/" PHP_VERSION);
5299+#endif
5300 }
5301 }
5302
5303diff -Nura php-5.3.1RC1/sapi/apache_hooks/mod_php5.c suhosin-patch-5.3.1RC1-0.9.8/sapi/apache_hooks/mod_php5.c
5304--- php-5.3.1RC1/sapi/apache_hooks/mod_php5.c 2008-12-31 12:15:49.000000000 +0100
5305+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/apache_hooks/mod_php5.c 2009-09-27 19:04:06.000000000 +0200
5306@@ -1256,7 +1256,11 @@
5307 {
5308 TSRMLS_FETCH();
5309 if (PG(expose_php)) {
5310+#if SUHOSIN_PATCH
5311+ ap_add_version_component("PHP/" PHP_VERSION " with Suhosin-Patch");
5312+#else
5313 ap_add_version_component("PHP/" PHP_VERSION);
5314+#endif
5315 }
5316 }
5317 #endif
5318diff -Nura php-5.3.1RC1/sapi/cgi/cgi_main.c suhosin-patch-5.3.1RC1-0.9.8/sapi/cgi/cgi_main.c
5319--- php-5.3.1RC1/sapi/cgi/cgi_main.c 2009-06-22 16:10:40.000000000 +0200
5320+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/cgi/cgi_main.c 2009-09-27 19:04:06.000000000 +0200
5321@@ -1907,11 +1907,19 @@
5322 SG(headers_sent) = 1;
5323 SG(request_info).no_headers = 1;
5324 }
5325+#if SUHOSIN_PATCH
5326+#if ZEND_DEBUG
5327+ php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5328+#else
5329+ php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5330+#endif
5331+#else
5332 #if ZEND_DEBUG
5333 php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5334 #else
5335 php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5336 #endif
5337+#endif
5338 php_request_shutdown((void *) 0);
5339 exit_status = 0;
5340 goto out;
5341diff -Nura php-5.3.1RC1/sapi/cli/php_cli.c suhosin-patch-5.3.1RC1-0.9.8/sapi/cli/php_cli.c
5342--- php-5.3.1RC1/sapi/cli/php_cli.c 2009-09-02 22:02:17.000000000 +0200
5343+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/cli/php_cli.c 2009-09-27 19:04:06.000000000 +0200
5344@@ -829,7 +829,11 @@
5345 }
5346
5347 request_started = 1;
5348- php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2009 The PHP Group\n%s",
5349+ php_printf("PHP %s "
5350+#if SUHOSIN_PATCH
5351+ "with Suhosin-Patch "
5352+#endif
5353+ "(%s) (built: %s %s) %s\nCopyright (c) 1997-2009 The PHP Group\n%s",
5354 PHP_VERSION, sapi_module.name, __DATE__, __TIME__,
5355 #if ZEND_DEBUG && defined(HAVE_GCOV)
5356 "(DEBUG GCOV)",
5357diff -Nura php-5.3.1RC1/sapi/litespeed/lsapi_main.c suhosin-patch-5.3.1RC1-0.9.8/sapi/litespeed/lsapi_main.c
5358--- php-5.3.1RC1/sapi/litespeed/lsapi_main.c 2008-08-27 00:05:17.000000000 +0200
5359+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/litespeed/lsapi_main.c 2009-09-27 19:04:06.000000000 +0200
5360@@ -545,11 +545,19 @@
5361 break;
5362 case 'v':
5363 if (php_request_startup(TSRMLS_C) != FAILURE) {
5364+#if SUHOSIN_PATCH
5365+#if ZEND_DEBUG
5366+ php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5367+#else
5368+ php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5369+#endif
5370+#else
5371 #if ZEND_DEBUG
5372 php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5373 #else
5374 php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5375 #endif
5376+#endif
5377 #ifdef PHP_OUTPUT_NEWAPI
5378 php_output_end_all(TSRMLS_C);
5379 #else
5380diff -Nura php-5.3.1RC1/sapi/milter/php_milter.c suhosin-patch-5.3.1RC1-0.9.8/sapi/milter/php_milter.c
5381--- php-5.3.1RC1/sapi/milter/php_milter.c 2008-12-31 12:15:49.000000000 +0100
5382+++ suhosin-patch-5.3.1RC1-0.9.8/sapi/milter/php_milter.c 2009-09-27 19:04:06.000000000 +0200
5383@@ -1102,7 +1102,11 @@
5384 }
5385 SG(headers_sent) = 1;
5386 SG(request_info).no_headers = 1;
5387+#if SUHOSIN_PATCH
5388+ php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5389+#else
5390 php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
5391+#endif
5392 php_end_ob_buffers(1 TSRMLS_CC);
5393 exit(1);
5394 break;
5395diff -Nura php-5.3.1RC1/win32/build/config.w32 suhosin-patch-5.3.1RC1-0.9.8/win32/build/config.w32
5396--- php-5.3.1RC1/win32/build/config.w32 2009-08-24 16:18:19.000000000 +0200
5397+++ suhosin-patch-5.3.1RC1-0.9.8/win32/build/config.w32 2009-09-27 19:04:06.000000000 +0200
5398@@ -323,7 +323,7 @@
5399 zend_stream.c zend_iterators.c zend_interfaces.c zend_objects.c \
5400 zend_object_handlers.c zend_objects_API.c \
5401 zend_default_classes.c zend_execute.c zend_strtod.c zend_gc.c zend_closures.c \
5402- zend_float.c");
5403+ zend_float.c zend_canary.c zend_alloc_canary.c");
5404
5405 if (VCVERS == 1200) {
5406 AC_DEFINE('ZEND_DVAL_TO_LVAL_CAST_OK', 1);
5407@@ -378,6 +378,7 @@
5408
5409 AC_DEFINE('HAVE_USLEEP', 1);
5410 AC_DEFINE('HAVE_STRCOLL', 1);
5411+AC_DEFINE('SUHOSIN_PATCH', 1);
5412
5413 /* For snapshot builders, where can we find the additional
5414 * files that make up the snapshot template? */
5415diff -Nura php-5.3.1RC1/win32/build/config.w32.h.in suhosin-patch-5.3.1RC1-0.9.8/win32/build/config.w32.h.in
5416--- php-5.3.1RC1/win32/build/config.w32.h.in 2009-06-23 08:56:45.000000000 +0200
5417+++ suhosin-patch-5.3.1RC1-0.9.8/win32/build/config.w32.h.in 2009-09-27 19:04:06.000000000 +0200
5418@@ -149,6 +149,9 @@
5419 /* Win32 supports strcoll */
5420 #define HAVE_STRCOLL 1
5421
5422+/* Suhosin Patch support */
5423+#define SUHOSIN_PATCH 1
5424+
5425 /* Win32 supports socketpair by the emulation in win32/sockets.c */
5426 #define HAVE_SOCKETPAIR 1
5427 #define HAVE_SOCKLEN_T 1
This page took 0.659831 seconds and 4 git commands to generate.