Magellan Linux

Contents of /trunk/php5/patches/php5-5.3.3-suhosin-0.9.10.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1064 - (show annotations) (download)
Mon Jul 26 09:54:38 2010 UTC (13 years, 9 months ago) by niro
File size: 197499 byte(s)
added new suhosin patch

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