File: 120-CVE-2007-1453-MOPB-18.patch

package info (click to toggle)
php5 5.2.0%2Bdfsg-8%2Betch16
  • links: PTS
  • area: main
  • in suites: etch
  • size: 58,940 kB
  • ctags: 45,388
  • sloc: ansic: 533,605; sh: 17,835; php: 11,336; cpp: 4,289; xml: 3,809; yacc: 2,446; lex: 2,174; makefile: 1,150; tcl: 1,128; awk: 693; perl: 71; sql: 22; pascal: 15
file content (166 lines) | stat: -rw-r--r-- 4,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--- old/ext/filter/sanitizing_filters.c	2006/10/03 11:42:23	1.11.2.5
+++ new/ext/filter/sanitizing_filters.c	2006/12/18 15:02:16	1.11.2.7
@@ -27,52 +27,29 @@
 /* }}} */
 
 /* {{{ HELPER FUNCTIONS */
-static void php_filter_encode_html(zval *value, const char* chars, int encode_nul)
+static void php_filter_encode_html(zval *value, const unsigned char *chars)
 {
-	register int x, y;
 	smart_str str = {0};
 	int len = Z_STRLEN_P(value);
-	char *s = Z_STRVAL_P(value);
+	unsigned char *s = (unsigned char *)Z_STRVAL_P(value);
+	unsigned char *e = s + len;
 
 	if (Z_STRLEN_P(value) == 0) {
 		return;
 	}
 
-	for (x = 0, y = 0; len--; x++, y++) {
-		if (strchr(chars, s[x]) || (encode_nul && s[x] == 0)) {
+	while (s < e) {
+		if (chars[*s]) {
 			smart_str_appendl(&str, "&#", 2);
-			smart_str_append_long(&str, s[x]);
+			smart_str_append_unsigned(&str, (unsigned long)*s);
 			smart_str_appendc(&str, ';');
 		} else {
-			smart_str_appendc(&str, s[x]);
+			/* XXX: this needs to be optimized to work with blocks of 'safe' chars */
+			smart_str_appendc(&str, *s);
 		}
+		s++;
 	}
-	smart_str_0(&str);
-	efree(Z_STRVAL_P(value));
-	Z_STRVAL_P(value) = str.c;
-	Z_STRLEN_P(value) = str.len;
-}
-
-static void php_filter_encode_html_high_low(zval *value, long flags)
-{
-	register int x, y;
-	smart_str str = {0};
-	int len = Z_STRLEN_P(value);
-	unsigned char *s = (unsigned char *)Z_STRVAL_P(value);
 
-	if (Z_STRLEN_P(value) == 0) {
-		return;
-	}
-	
-	for (x = 0, y = 0; len--; x++, y++) {
-		if (((flags & FILTER_FLAG_ENCODE_LOW) && (s[x] < 32)) || ((flags & FILTER_FLAG_ENCODE_HIGH) && (s[x] > 127))) {
-			smart_str_appendl(&str, "&#", 2);
-			smart_str_append_unsigned(&str, s[x]);
-			smart_str_appendc(&str, ';');
-		} else {
-			smart_str_appendc(&str, s[x]);
-		}
-	}
 	smart_str_0(&str);
 	efree(Z_STRVAL_P(value));
 	Z_STRVAL_P(value) = str.c;
@@ -181,9 +158,28 @@
 void php_filter_string(PHP_INPUT_FILTER_PARAM_DECL)
 {
 	size_t new_len;
-	
+	unsigned char enc[256] = {0};
+
+	/* strip high/strip low ( see flags )*/
+	php_filter_strip(value, flags);
+
+	if (!(flags & FILTER_FLAG_NO_ENCODE_QUOTES)) {
+		enc['\''] = enc['"'] = 1;
+	}
+	if (flags & FILTER_FLAG_ENCODE_AMP) {
+		enc['&'] = 1;
+	}
+	if (flags & FILTER_FLAG_ENCODE_LOW) {
+		memset(enc, 1, 32);
+	}
+	if (flags & FILTER_FLAG_ENCODE_HIGH) {
+		memset(enc + 127, 1, sizeof(enc) - 127);
+	}
+
+	php_filter_encode_html(value, enc);
+
 	/* strip tags, implicitly also removes \0 chars */
-	new_len = php_strip_tags(Z_STRVAL_P(value), Z_STRLEN_P(value), NULL, NULL, 0);
+	new_len = php_strip_tags_ex(Z_STRVAL_P(value), Z_STRLEN_P(value), NULL, NULL, 0, 1);
 	Z_STRLEN_P(value) = new_len;
 
 	if (new_len == 0) {
@@ -191,21 +187,6 @@
 		ZVAL_EMPTY_STRING(value);
 		return;
 	}
-
-	if (! (flags & FILTER_FLAG_NO_ENCODE_QUOTES)) {
-		/* encode ' and " to numerical entity */
-		php_filter_encode_html(value, "'\"", 0);
-	}
-	/* strip high/strip low ( see flags )*/
-	php_filter_strip(value, flags);
-
-	/* encode low/encode high flags */
-	php_filter_encode_html_high_low(value, flags);
-
-	/* also all the flags - & encode as %xx */
-	if (flags & FILTER_FLAG_ENCODE_AMP) {
-		php_filter_encode_html(value, "&", 0);
-	}
 }
 /* }}} */
 
@@ -222,11 +203,21 @@
 /* {{{ php_filter_special_chars */
 void php_filter_special_chars(PHP_INPUT_FILTER_PARAM_DECL)
 {
+	unsigned char enc[256] = {0};
+
+	php_filter_strip(value, flags);
+
 	/* encodes ' " < > & \0 to numerical entities */
-	php_filter_encode_html(value, "'\"<>&", 1);
+	enc['\''] = enc['"'] = enc['<'] = enc['>'] = enc['&'] = enc[0] = 1;
+
 	/* if strip low is not set, then we encode them as &#xx; */
-	php_filter_strip(value, flags);
-	php_filter_encode_html_high_low(value, FILTER_FLAG_ENCODE_LOW | flags);
+	memset(enc, 1, 32);
+
+	if (flags & FILTER_FLAG_ENCODE_HIGH) {
+		memset(enc + 127, 1, sizeof(enc) - 127);
+	}
+	
+	php_filter_encode_html(value, enc);	
 }
 /* }}} */
 
@@ -235,11 +226,21 @@
 {
 	/* Only if no flags are set (optimization) */
 	if (flags != 0 && Z_STRLEN_P(value) > 0) {
+		unsigned char enc[256] = {0};
+
 		php_filter_strip(value, flags);
+
 		if (flags & FILTER_FLAG_ENCODE_AMP) {
-			php_filter_encode_html(value, "&", 0);
+			enc['&'] = 1;
 		}
-		php_filter_encode_html_high_low(value, flags);
+		if (flags & FILTER_FLAG_ENCODE_LOW) {
+			memset(enc, 1, 32);
+		}
+		if (flags & FILTER_FLAG_ENCODE_HIGH) {
+			memset(enc + 127, 1, sizeof(enc) - 127);
+		}
+
+		php_filter_encode_html(value, enc);	
 	}
 }
 /* }}} */