Skip to content

Commit 62a2387

Browse files
committed
Fix #80215: imap_mail_compose() may modify by-val parameters
We separate the input arrays and all sub-arrays to avoid modification of the passed parameters. This should be rewritten to use `zend_string`s for the "master" branch. Closes GH-6316.
1 parent 8bee0fb commit 62a2387

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PHP NEWS
1111

1212
- IMAP:
1313
. Fixed bug #80213 (imap_mail_compose() segfaults on certain $bodies). (cmb)
14+
. Fixed bug #80215 (imap_mail_compose() may modify by-val parameters). (cmb)
1415

1516
- MySQLnd:
1617
. Fixed bug #80115 (mysqlnd.debug doesn't recognize absolute paths with

ext/imap/php_imap.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,7 @@ PHP_FUNCTION(imap_mail_compose)
35443544
int toppart = 0;
35453545
int first;
35463546

3547-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &envelope, &body) == FAILURE) {
3547+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/a/", &envelope, &body) == FAILURE) {
35483548
return;
35493549
}
35503550

@@ -3602,6 +3602,7 @@ PHP_FUNCTION(imap_mail_compose)
36023602
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "custom_headers", sizeof("custom_headers") - 1)) != NULL) {
36033603
if (Z_TYPE_P(pvalue) == IS_ARRAY) {
36043604
custom_headers_param = tmp_param = NULL;
3605+
SEPARATE_ARRAY(pvalue);
36053606
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pvalue), env_data) {
36063607
custom_headers_param = mail_newbody_parameter();
36073608
convert_to_string_ex(env_data);
@@ -3623,6 +3624,7 @@ PHP_FUNCTION(imap_mail_compose)
36233624
php_error_docref(NULL, E_WARNING, "body parameter must be a non-empty array");
36243625
RETURN_FALSE;
36253626
}
3627+
SEPARATE_ARRAY(data);
36263628

36273629
bod = mail_newbody();
36283630
topbod = bod;
@@ -3644,6 +3646,7 @@ PHP_FUNCTION(imap_mail_compose)
36443646
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "type.parameters", sizeof("type.parameters") - 1)) != NULL) {
36453647
if(Z_TYPE_P(pvalue) == IS_ARRAY) {
36463648
disp_param = tmp_param = NULL;
3649+
SEPARATE_ARRAY(pvalue);
36473650
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(pvalue), key, disp_data) {
36483651
if (key == NULL) continue;
36493652
disp_param = mail_newbody_parameter();
@@ -3677,6 +3680,7 @@ PHP_FUNCTION(imap_mail_compose)
36773680
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "disposition", sizeof("disposition") - 1)) != NULL) {
36783681
if (Z_TYPE_P(pvalue) == IS_ARRAY) {
36793682
disp_param = tmp_param = NULL;
3683+
SEPARATE_ARRAY(pvalue);
36803684
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(pvalue), key, disp_data) {
36813685
if (key == NULL) continue;
36823686
disp_param = mail_newbody_parameter();
@@ -3712,6 +3716,7 @@ PHP_FUNCTION(imap_mail_compose)
37123716
}
37133717
} else if (Z_TYPE_P(data) == IS_ARRAY) {
37143718
short type = -1;
3719+
SEPARATE_ARRAY(data);
37153720
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "type", sizeof("type") - 1)) != NULL) {
37163721
type = (short) zval_get_long(pvalue);
37173722
}
@@ -3746,6 +3751,7 @@ PHP_FUNCTION(imap_mail_compose)
37463751
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "type.parameters", sizeof("type.parameters") - 1)) != NULL) {
37473752
if (Z_TYPE_P(pvalue) == IS_ARRAY) {
37483753
disp_param = tmp_param = NULL;
3754+
SEPARATE_ARRAY(pvalue);
37493755
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(pvalue), key, disp_data) {
37503756
if (key == NULL) continue;
37513757
disp_param = mail_newbody_parameter();
@@ -3779,6 +3785,7 @@ PHP_FUNCTION(imap_mail_compose)
37793785
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "disposition", sizeof("disposition") - 1)) != NULL) {
37803786
if (Z_TYPE_P(pvalue) == IS_ARRAY) {
37813787
disp_param = tmp_param = NULL;
3788+
SEPARATE_ARRAY(pvalue);
37823789
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(pvalue), key, disp_data) {
37833790
if (key == NULL) continue;
37843791
disp_param = mail_newbody_parameter();

ext/imap/tests/bug80215.phpt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
Bug #80215 (imap_mail_compose() may modify by-val parameters)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('imap')) die('skip imap extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$envelope = [
10+
"from" => 1,
11+
"to" => 2,
12+
"custom_headers" => [3],
13+
];
14+
$body = [[
15+
"contents.data" => 4,
16+
"type.parameters" => ['foo' => 5],
17+
"disposition" => ['bar' => 6],
18+
], [
19+
"contents.data" => 7,
20+
"type.parameters" => ['foo' => 8],
21+
"disposition" => ['bar' => 9],
22+
]];
23+
imap_mail_compose($envelope, $body);
24+
var_dump($envelope, $body);
25+
?>
26+
--EXPECT--
27+
array(3) {
28+
["from"]=>
29+
int(1)
30+
["to"]=>
31+
int(2)
32+
["custom_headers"]=>
33+
array(1) {
34+
[0]=>
35+
int(3)
36+
}
37+
}
38+
array(2) {
39+
[0]=>
40+
array(3) {
41+
["contents.data"]=>
42+
int(4)
43+
["type.parameters"]=>
44+
array(1) {
45+
["foo"]=>
46+
int(5)
47+
}
48+
["disposition"]=>
49+
array(1) {
50+
["bar"]=>
51+
int(6)
52+
}
53+
}
54+
[1]=>
55+
array(3) {
56+
["contents.data"]=>
57+
int(7)
58+
["type.parameters"]=>
59+
array(1) {
60+
["foo"]=>
61+
int(8)
62+
}
63+
["disposition"]=>
64+
array(1) {
65+
["bar"]=>
66+
int(9)
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)