Magellan Linux

Annotation of /trunk/php5/patches/php5-5.3.2-suhosin-0.9.9.1.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1006 - (hide annotations) (download)
Sun Mar 7 13:14:17 2010 UTC (14 years, 3 months ago) by niro
File size: 196370 byte(s)
suhison patch 0.9.9.1 against php-5.3.2

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