From 0fda85780bd51c982622f3baf70701d4d032bbc6 Mon Sep 17 00:00:00 2001 From: midipix Date: Fri, 19 Jun 2020 22:24:12 +0000 Subject: internals: __ntapi_init_solib_fork_child_finalize() [rare use-case support]. --- src/internal/ntapi.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/internal/ntapi.c b/src/internal/ntapi.c index b1d282d..7562b35 100644 --- a/src/internal/ntapi.c +++ b/src/internal/ntapi.c @@ -588,6 +588,88 @@ static int32_t __fastcall __ntapi_init_once(ntapi_vtbl ** pvtbl) return NT_STATUS_SUCCESS; } +static int32_t __ntapi_init_solib_fork_child_finalize(void) +{ + int32_t status; + int page; + nt_rtdata * rtdata; + nt_oa oa; + ntapi_internals * __internals; + + /* detect the rare scenario where libntapi.so is (indirectly) */ + /* used by a hosted process, in which case post-fork adjustments */ + /* to internal structures would take place in the copy of ntapi */ + /* that is statically linked into the system call layer */ + /* library, thereby requiring similar adjustmets to be made */ + /* once here as well. */ + + __internals = __ntapi_internals(); + rtdata = __internals->rtdata; + + if (rtdata->cid_self.process_id == pe_get_current_process_id()) + return NT_STATUS_SUCCESS; + + rtdata->cid_parent.process_id = rtdata->cid_self.process_id; + rtdata->cid_parent.thread_id = rtdata->cid_self.thread_id; + + rtdata->cid_self.process_id = pe_get_current_process_id(); + rtdata->cid_self.thread_id = pe_get_current_thread_id(); + + if (rtdata->hparent) + __ntapi->zw_close(rtdata->hparent); + + oa.len = sizeof(oa); + oa.root_dir = 0; + oa.obj_name = 0; + oa.obj_attr = 0; + oa.sec_desc = &__internals->seq_desc; + oa.sec_qos = &__internals->seq_qos; + + status = __ntapi->zw_open_process( + &__internals->hprocess, + NT_PROCESS_ALL_ACCESS, + &oa,&rtdata->cid_self); + + rtdata->hparent = rtdata->hself; + rtdata->hself = __internals->hprocess; + + if ((status = dalist_init_ex( + &__internals->ipc_conns, + sizeof(nt_ipc_conn), + NT_ALLOCATION_GRANULARITY, + __ntapi_ipc_page_alloc, + DALIST_MEMFN_CUSTOM))) + return status; + + dalist_deposit_memory_block( + &__internals->ipc_conns, + __internals->ntapi_img_sec_bss->ipc_buffer, + __NT_BSS_IPC_BUFFER_SIZE); + + for (page=0; page<__internals->ipc_page; page++) + dalist_deposit_memory_block( + &__internals->ipc_conns, + __internals->ipc_pages[page], + NT_ALLOCATION_GRANULARITY); + + rtdata->hsemctl = 0; + rtdata->hsempid = 0; + + rtdata->hmsqctl = 0; + rtdata->hmsqpid = 0; + + rtdata->haflctl = 0; + rtdata->haflpid = 0; + + rtdata->ipc_keys[0] = 0; + rtdata->ipc_keys[1] = 0; + rtdata->ipc_keys[2] = 0; + rtdata->ipc_keys[3] = 0; + rtdata->ipc_keys[4] = 0; + rtdata->ipc_keys[5] = 0; + + return 0; +} static int32_t __fastcall __ntapi_init_pending(ntapi_vtbl ** pvtbl) { @@ -598,7 +680,7 @@ static int32_t __fastcall __ntapi_init_pending(ntapi_vtbl ** pvtbl) static int32_t __fastcall __ntapi_init_completed(ntapi_vtbl ** pvtbl) { *pvtbl = &___ntapi_shadow; - return NT_STATUS_SUCCESS; + return __ntapi_init_solib_fork_child_finalize(); }; -- cgit v1.2.3