Skip to content

Commit f3d24af

Browse files
committed
Fix #81407: shmop_open won't attach and causes php to crash
We need to allocate buffers for the file mapping names which are large enough for all potential keys (`key_t` is defined as `int` on Windows). Regarding the test: it's probably never a good idea to use hard-coded keys (should always use `ftok()` instead), but to reliably reproduce this Windows specific issue we need to, and it shouldn't be an issue on that OS. Closes GH-7448.
1 parent 8c292a2 commit f3d24af

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ PHP NEWS
1616
. Fixed bug #81353 (segfault with preloading and statically bound closure).
1717
(Nikita)
1818

19+
- Shmop:
20+
. Fixed bug #81407 (shmop_open won't attach and causes php to crash). (cmb)
21+
1922
- Standard:
2023
. Fixed bug #71542 (disk_total_space does not work with relative paths). (cmb)
2124
. Fixed bug #81400 (Unterminated string in dns_get_record() results). (cmb)

TSRM/tsrm_win32.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,20 @@ TSRM_API int pclose(FILE *stream)
611611
return termstat;
612612
}/*}}}*/
613613

614+
#define SEGMENT_PREFIX "TSRM_SHM_SEGMENT:"
615+
#define DESCRIPTOR_PREFIX "TSRM_SHM_DESCRIPTOR:"
616+
#define INT_MIN_AS_STRING "-2147483648"
617+
614618
TSRM_API int shmget(key_t key, size_t size, int flags)
615619
{/*{{{*/
616620
shm_pair *shm;
617-
char shm_segment[26], shm_info[29];
621+
char shm_segment[sizeof(SEGMENT_PREFIX INT_MIN_AS_STRING)], shm_info[sizeof(DESCRIPTOR_PREFIX INT_MIN_AS_STRING)];
618622
HANDLE shm_handle = NULL, info_handle = NULL;
619623
BOOL created = FALSE;
620624

621625
if (key != IPC_PRIVATE) {
622-
snprintf(shm_segment, sizeof(shm_segment), "TSRM_SHM_SEGMENT:%d", key);
623-
snprintf(shm_info, sizeof(shm_info), "TSRM_SHM_DESCRIPTOR:%d", key);
626+
snprintf(shm_segment, sizeof(shm_segment), SEGMENT_PREFIX "%d", key);
627+
snprintf(shm_info, sizeof(shm_info), DESCRIPTOR_PREFIX "%d", key);
624628

625629
shm_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_segment);
626630
info_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_info);

ext/shmop/tests/bug81407.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #81407 (shmop_open won't attach and causes php to crash)
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
6+
if (!extension_loaded("shmop")) die("skip shmop extension not available");
7+
?>
8+
--FILE--
9+
<?php
10+
$a = shmop_open(367504384, 'n', 0664, 262144);
11+
$b = shmop_open(367504385, 'n', 0664, 65536);
12+
if ($b == false) {
13+
$b = shmop_open(367504385, 'w', 0664, 65536);
14+
}
15+
var_dump($a !== false, $b !== false);
16+
?>
17+
--EXPECT--
18+
bool(true)
19+
bool(true)

0 commit comments

Comments
 (0)