File tree Expand file tree Collapse file tree 4 files changed +64
-10
lines changed Expand file tree Collapse file tree 4 files changed +64
-10
lines changed Original file line number Diff line number Diff line change @@ -2153,15 +2153,15 @@ PHP_FUNCTION(openssl_x509_parse)
2153
2153
/* Can return NULL on error or memory allocation failure */
2154
2154
if (!bn_serial ) {
2155
2155
php_openssl_store_errors ();
2156
- RETURN_FALSE ;
2156
+ goto err ;
2157
2157
}
2158
2158
2159
2159
hex_serial = BN_bn2hex (bn_serial );
2160
2160
BN_free (bn_serial );
2161
2161
/* Can return NULL on error or memory allocation failure */
2162
2162
if (!hex_serial ) {
2163
2163
php_openssl_store_errors ();
2164
- RETURN_FALSE ;
2164
+ goto err ;
2165
2165
}
2166
2166
2167
2167
str_serial = i2s_ASN1_INTEGER (NULL , asn1_serial );
@@ -2233,19 +2233,15 @@ PHP_FUNCTION(openssl_x509_parse)
2233
2233
bio_out = BIO_new (BIO_s_mem ());
2234
2234
if (bio_out == NULL ) {
2235
2235
php_openssl_store_errors ();
2236
- RETURN_FALSE ;
2236
+ goto err_subitem ;
2237
2237
}
2238
2238
if (nid == NID_subject_alt_name ) {
2239
2239
if (openssl_x509v3_subjectAltName (bio_out , extension ) == 0 ) {
2240
2240
BIO_get_mem_ptr (bio_out , & bio_buf );
2241
2241
add_assoc_stringl (& subitem , extname , bio_buf -> data , bio_buf -> length );
2242
2242
} else {
2243
- zend_array_destroy (Z_ARR_P (return_value ));
2244
2243
BIO_free (bio_out );
2245
- if (cert_str ) {
2246
- X509_free (cert );
2247
- }
2248
- RETURN_FALSE ;
2244
+ goto err_subitem ;
2249
2245
}
2250
2246
}
2251
2247
else if (X509V3_EXT_print (bio_out , extension , 0 , 0 )) {
@@ -2260,6 +2256,16 @@ PHP_FUNCTION(openssl_x509_parse)
2260
2256
if (cert_str ) {
2261
2257
X509_free (cert );
2262
2258
}
2259
+ return ;
2260
+
2261
+ err_subitem :
2262
+ zval_ptr_dtor (& subitem );
2263
+ err :
2264
+ zend_array_destroy (Z_ARR_P (return_value ));
2265
+ if (cert_str ) {
2266
+ X509_free (cert );
2267
+ }
2268
+ RETURN_FALSE ;
2263
2269
}
2264
2270
/* }}} */
2265
2271
Original file line number Diff line number Diff line change @@ -291,8 +291,13 @@ PHPAPI zend_result php_session_reset_id(void);
291
291
zend_ulong num_key; \
292
292
zval *struc;
293
293
294
+ /* Do not use a return statement in `code` because that may leak memory.
295
+ * Break out of the loop instead. */
294
296
#define PS_ENCODE_LOOP (code ) do { \
295
- HashTable *_ht = Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars))); \
297
+ zval _zv; \
298
+ /* protect against user interference */ \
299
+ ZVAL_COPY (& _zv , Z_REFVAL (PS (http_session_vars ))); \
300
+ HashTable * _ht = Z_ARRVAL (_zv ); \
296
301
ZEND_HASH_FOREACH_KEY (_ht , num_key , key ) { \
297
302
if (key == NULL ) { \
298
303
php_error_docref (NULL , E_WARNING , \
@@ -303,6 +308,7 @@ PHPAPI zend_result php_session_reset_id(void);
303
308
code ; \
304
309
} \
305
310
} ZEND_HASH_FOREACH_END (); \
311
+ zval_ptr_dtor (& _zv ); \
306
312
} while (0 )
307
313
308
314
PHPAPI ZEND_EXTERN_MODULE_GLOBALS (ps )
Original file line number Diff line number Diff line change @@ -1079,6 +1079,7 @@ PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */
1079
1079
{
1080
1080
smart_str buf = {0 };
1081
1081
php_serialize_data_t var_hash ;
1082
+ bool fail = false;
1082
1083
PS_ENCODE_VARS ;
1083
1084
1084
1085
PHP_VAR_SERIALIZE_INIT (var_hash );
@@ -1088,12 +1089,17 @@ PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */
1088
1089
if (memchr (ZSTR_VAL (key ), PS_DELIMITER , ZSTR_LEN (key ))) {
1089
1090
PHP_VAR_SERIALIZE_DESTROY (var_hash );
1090
1091
smart_str_free (& buf );
1091
- return NULL ;
1092
+ fail = true;
1093
+ break ;
1092
1094
}
1093
1095
smart_str_appendc (& buf , PS_DELIMITER );
1094
1096
php_var_serialize (& buf , struc , & var_hash );
1095
1097
);
1096
1098
1099
+ if (fail ) {
1100
+ return NULL ;
1101
+ }
1102
+
1097
1103
smart_str_0 (& buf );
1098
1104
1099
1105
PHP_VAR_SERIALIZE_DESTROY (var_hash );
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ GH-16590 (UAF in session_encode())
3
+ --EXTENSIONS--
4
+ session
5
+ --SKIPIF--
6
+ <?php include ('skipif.inc ' ); ?>
7
+ --INI--
8
+ session.use_cookies=0
9
+ session.cache_limiter=
10
+ session.serialize_handler=php
11
+ session.save_handler=files
12
+ --FILE--
13
+ <?php
14
+
15
+ class C {
16
+ function __serialize () {
17
+ $ _SESSION = [];
18
+ return [];
19
+ }
20
+ }
21
+
22
+ session_start ();
23
+
24
+ $ _SESSION ['Lz ' ] = new C ;
25
+ for ($ i = 0 ; $ i < 2 ; $ i ++) {
26
+ $ _SESSION [$ i ] = $ i ;
27
+ }
28
+
29
+ var_dump (session_encode ());
30
+
31
+ ?>
32
+ --EXPECTF--
33
+ Warning: session_encode(): Skipping numeric key 0 in %s on line %d
34
+
35
+ Warning: session_encode(): Skipping numeric key 1 in %s on line %d
36
+ string(15) "Lz|O:1:"C":0:{}"
You can’t perform that action at this time.
0 commit comments