Skip to content

Commit 5049ef2

Browse files
cmb69smalyshev
authored andcommitted
Fix #73549: Use after free when stream is passed to imagepng
If a stream is passed to imagepng() or other image output functions, opposed to a filename, we must not close this stream.
1 parent 2a80758 commit 5049ef2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ?? 2017, PHP 5.6.30
44

5+
- GD:
6+
. Fixed bug #73549 (Use after free when stream is passed to imagepng). (cmb)
7+
58
08 Dec 2016, PHP 5.6.29
69

710
- Mbstring:

ext/gd/gd_ctx.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
6161
}
6262

6363
static void _php_image_stream_ctxfree(struct gdIOCtx *ctx)
64+
{
65+
if(ctx->data) {
66+
ctx->data = NULL;
67+
}
68+
if(ctx) {
69+
efree(ctx);
70+
}
71+
} /* }}} */
72+
73+
static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
6474
{
6575
TSRMLS_FETCH();
6676

@@ -87,6 +97,7 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
8797
gdIOCtx *ctx = NULL;
8898
zval *to_zval = NULL;
8999
php_stream *stream;
100+
int close_stream = 1;
90101

91102
/* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
92103
* The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
@@ -123,6 +134,7 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
123134
if (stream == NULL) {
124135
RETURN_FALSE;
125136
}
137+
close_stream = 0;
126138
} else if (Z_TYPE_P(to_zval) == IS_STRING) {
127139
if (CHECK_ZVAL_NULL_PATH(to_zval)) {
128140
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
@@ -159,7 +171,11 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
159171
ctx = emalloc(sizeof(gdIOCtx));
160172
ctx->putC = _php_image_stream_putc;
161173
ctx->putBuf = _php_image_stream_putbuf;
162-
ctx->gd_free = _php_image_stream_ctxfree;
174+
if (close_stream) {
175+
ctx->gd_free = _php_image_stream_ctxfreeandclose;
176+
} else {
177+
ctx->gd_free = _php_image_stream_ctxfree;
178+
}
163179
ctx->data = (void *)stream;
164180
}
165181

ext/gd/tests/bug73549.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #73549 (Use after free when stream is passed to imagepng)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$stream = fopen(__DIR__ . DIRECTORY_SEPARATOR . 'bug73549.png', 'w');
10+
$im = imagecreatetruecolor(8, 8);
11+
var_dump(imagepng($im, $stream));
12+
var_dump($stream);
13+
?>
14+
===DONE===
15+
--EXPECTF--
16+
bool(true)
17+
resource(%d) of type (stream)
18+
===DONE===
19+
--CLEAN--
20+
<?php
21+
unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug73549.png');
22+
?>

0 commit comments

Comments
 (0)