Skip to content

Commit c900603

Browse files
committed
Fix GH-10737: PHP 8.1.16 segfaults on line 597 of sapi/apache2handler/sapi_apache2.c
The TSRM keeps a hashtable mapping the thread IDs to the thread resource pointers. It's possible that the thread disappears without us knowing, and then another thread gets spawned some time later with the same ID as the disappeared thread. Note that since it's a new thread the TSRM key pointer and cached pointer will be NULL. The Apache request handler `php_handler()` will try to fetch some fields from the SAPI globals. It uses a lazy thread resource allocation by calling `ts_resource(0);`. This allocates a thread resource and sets up the TSRM pointers if they haven't been set up yet. At least, that's what's supposed to happen. But since we are in a situation where the thread ID still has the resources of the *old* thread associated in the hashtable, the loop in `ts_resource_ex` will find that thread resource and assume the thread has been setup already. But this is not the case since this thread is actually a new thread, just reusing the ID of the old one, without any relation whatsoever to the old thread. Because of this assumption, the TSRM pointers will not be setup, leading to a NULL pointer dereference when trying to access the SAPI globals. We can easily detect this scenario: if we're in the fallback path, and the pointer is NULL, and we're looking for our own thread resource, we know we're actually reusing a thread ID. In that case, we'll free up the old thread resources gracefully (gracefully because there might still be resources open like database connection which need to be shut down cleanly). After freeing the resources, we'll create the new resources for this thread as if the stale resources never existed in the first place. From that point forward, it is as if that situation never occurred. The fact that this situation happens isn't that bad because a child process containing threads will eventually be respawned anyway by the SAPI, so the stale thread resources won't remain forever. Note that we can't simply assign our own TSRM pointers to the existing thread resource for our ID, since it was actually from a different thread (just with the same ID!). Furthermore, the dynamically loaded extensions have their own pointer, which is only set when their constructor is called, so we'd have to call their constructor anyway... I also tried to call the dtor and then the ctor again for those resources on the pre-existing thread resource to reuse storage, but that didn't work properly because other code doesn't expect something like that to happen, which breaks assumptions, and this in turn caused Valgrind to (rightfully) complain about memory bugs. Note 2: I also had to fix a bug in the core globals destruction because it always assumed that the thread destroying them was the owning thread, which on TSRM shutdown isn't always the case. A similar bug was fixed recently with the JIT globals.
1 parent c4c8d6c commit c900603

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

TSRM/TSRM.c

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,13 @@ TSRM_API ts_rsrc_id ts_allocate_fast_id(ts_rsrc_id *rsrc_id, size_t *offset, siz
367367
return *rsrc_id;
368368
}/*}}}*/
369369

370+
static void set_thread_local_storage_resource_to(tsrm_tls_entry *thread_resource)
371+
{
372+
tsrm_tls_set(thread_resource);
373+
TSRMLS_CACHE = thread_resource;
374+
}
370375

376+
/* Must be called with tsmm_mutex held */
371377
static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_T thread_id)
372378
{/*{{{*/
373379
int i;
@@ -383,8 +389,7 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
383389
(*thread_resources_ptr)->next = NULL;
384390

385391
/* Set thread local storage to this new thread resources structure */
386-
tsrm_tls_set(*thread_resources_ptr);
387-
TSRMLS_CACHE = *thread_resources_ptr;
392+
set_thread_local_storage_resource_to(*thread_resources_ptr);
388393

389394
if (tsrm_new_thread_begin_handler) {
390395
tsrm_new_thread_begin_handler(thread_id);
@@ -407,17 +412,27 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
407412
if (tsrm_new_thread_end_handler) {
408413
tsrm_new_thread_end_handler(thread_id);
409414
}
410-
411-
tsrm_mutex_unlock(tsmm_mutex);
412415
}/*}}}*/
413416

417+
static void ts_free_resources(tsrm_tls_entry *thread_resources)
418+
{
419+
for (int i = 0; i < thread_resources->count; i++) {
420+
if (resource_types_table[i].dtor) {
421+
resource_types_table[i].dtor(thread_resources->storage[i]);
422+
}
423+
if (!resource_types_table[i].fast_offset) {
424+
free(thread_resources->storage[i]);
425+
}
426+
}
427+
free(thread_resources->storage);
428+
}
414429

415430
/* fetches the requested resource for the current thread */
416431
TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
417432
{/*{{{*/
418433
THREAD_T thread_id;
419434
int hash_value;
420-
tsrm_tls_entry *thread_resources;
435+
tsrm_tls_entry *thread_resources, **last_thread_resources;
421436

422437
if (!th_id) {
423438
/* Fast path for looking up the resources for the current
@@ -448,16 +463,20 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
448463

449464
if (!thread_resources) {
450465
allocate_new_resource(&tsrm_tls_table[hash_value], thread_id);
466+
tsrm_mutex_unlock(tsmm_mutex);
451467
return ts_resource_ex(id, &thread_id);
452468
} else {
469+
last_thread_resources = &tsrm_tls_table[hash_value];
453470
do {
454471
if (thread_resources->thread_id == thread_id) {
455472
break;
456473
}
474+
last_thread_resources = &thread_resources->next;
457475
if (thread_resources->next) {
458476
thread_resources = thread_resources->next;
459477
} else {
460478
allocate_new_resource(&thread_resources->next, thread_id);
479+
tsrm_mutex_unlock(tsmm_mutex);
461480
return ts_resource_ex(id, &thread_id);
462481
/*
463482
* thread_resources = thread_resources->next;
@@ -466,7 +485,40 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
466485
}
467486
} while (thread_resources);
468487
}
488+
489+
/* It's possible that the current thread resources are requested, and that we get here.
490+
* This means that the TSRM key pointer and cached pointer are NULL, but there is still
491+
* a thread resource associated with this ID in the hashtable. This can occur if a thread
492+
* goes away, but its resources are never cleaned up, and then that thread ID is reused.
493+
* Since we don't always have a way to know when a thread goes away, we can't clean up
494+
* the thread's resources before the new thread spawns.
495+
* To solve this issue, we'll free up the old thread resources gracefully (gracefully
496+
* because there might still be resources open like database connection which need to
497+
* be shut down cleanly). After freeing up, we'll create the new resources for this thread
498+
* as if the stale resources never existed in the first place. From that point forward,
499+
* it is as if that situation never occurred.
500+
* The fact that this situation happens isn't that bad because a child process containing
501+
* threads will eventually be respawned anyway by the SAPI, so the stale threads won't last
502+
* forever. */
503+
TSRM_ASSERT(thread_resources->thread_id == thread_id);
504+
if (thread_id == tsrm_thread_id() && !tsrm_tls_get()) {
505+
tsrm_tls_entry *next = thread_resources->next;
506+
/* In case that extensions don't use the pointer passed from the dtor, but incorrectly
507+
* use the global pointer, we need to setup the global pointer temporarily here. */
508+
set_thread_local_storage_resource_to(thread_resources);
509+
/* Free up the old resource from the old thread instance */
510+
ts_free_resources(thread_resources);
511+
free(thread_resources);
512+
/* Allocate a new resource at the same point in the linked list, and relink the next pointer */
513+
allocate_new_resource(last_thread_resources, thread_id);
514+
thread_resources = *last_thread_resources;
515+
thread_resources->next = next;
516+
/* We don't have to tail-call ts_resource_ex, we can take the fast path to the return
517+
* because we already have the correct pointer. */
518+
}
519+
469520
tsrm_mutex_unlock(tsmm_mutex);
521+
470522
/* Read a specific resource from the thread's resources.
471523
* This is called outside of a mutex, so have to be aware about external
472524
* changes to the structure as we read it.
@@ -479,7 +531,6 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
479531
void ts_free_thread(void)
480532
{/*{{{*/
481533
tsrm_tls_entry *thread_resources;
482-
int i;
483534
THREAD_T thread_id = tsrm_thread_id();
484535
int hash_value;
485536
tsrm_tls_entry *last=NULL;
@@ -492,17 +543,7 @@ void ts_free_thread(void)
492543

493544
while (thread_resources) {
494545
if (thread_resources->thread_id == thread_id) {
495-
for (i=0; i<thread_resources->count; i++) {
496-
if (resource_types_table[i].dtor) {
497-
resource_types_table[i].dtor(thread_resources->storage[i]);
498-
}
499-
}
500-
for (i=0; i<thread_resources->count; i++) {
501-
if (!resource_types_table[i].fast_offset) {
502-
free(thread_resources->storage[i]);
503-
}
504-
}
505-
free(thread_resources->storage);
546+
ts_free_resources(thread_resources);
506547
if (last) {
507548
last->next = thread_resources->next;
508549
} else {

main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ static void core_globals_dtor(php_core_globals *core_globals)
19361936
free(core_globals->php_binary);
19371937
}
19381938

1939-
php_shutdown_ticks();
1939+
php_shutdown_ticks(core_globals);
19401940
}
19411941
/* }}} */
19421942

main/php_ticks.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ void php_deactivate_ticks(void)
3434
zend_llist_clean(&PG(tick_functions));
3535
}
3636

37-
void php_shutdown_ticks(void)
37+
void php_shutdown_ticks(php_core_globals *core_globals)
3838
{
39-
zend_llist_destroy(&PG(tick_functions));
39+
zend_llist_destroy(&core_globals->tick_functions);
4040
}
4141

4242
static int php_compare_tick_functions(void *elem1, void *elem2)

main/php_ticks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
int php_startup_ticks(void);
2121
void php_deactivate_ticks(void);
22-
void php_shutdown_ticks(void);
22+
void php_shutdown_ticks(php_core_globals *core_globals);
2323
void php_run_ticks(int count);
2424

2525
BEGIN_EXTERN_C()

0 commit comments

Comments
 (0)