186 lines
8.7 KiB
C
186 lines
8.7 KiB
C
#include "pipe_admission_snapshot.h"
|
|
#include <string.h>
|
|
|
|
#define GW_ORIGINAL_OWNER_GLOBAL ((uintptr_t)0x145e7a098ULL)
|
|
#define GW_ORIGINAL_CONNECTION_TABLE ((uint64_t)0x1450396b0ULL)
|
|
#define GW_ORIGINAL_PIPE_TABLE ((uint64_t)0x144e59fe8ULL)
|
|
#define GW_ORIGINAL_CLOSE ((uint64_t)0x1434db6b0ULL)
|
|
#define GW_ORIGINAL_WRITE ((uint64_t)0x1434db7a0ULL)
|
|
|
|
static const uint64_t original_pipe_methods[9] = {
|
|
0x141e4ca60ULL, 0x141e4d120ULL, 0x1401d9200ULL,
|
|
0x141e4e8e0ULL, 0x14091e6e0ULL, 0x1409e95f0ULL,
|
|
0x141e4c8f0ULL, 0x141e4c9e0ULL, 0x141e4ca20ULL
|
|
};
|
|
|
|
static int read_exact(const struct gw_pipe_admission_input *in,
|
|
uintptr_t address, void *destination, size_t bytes) {
|
|
if (!address || !destination || !bytes || address > UINTPTR_MAX - bytes)
|
|
return 0;
|
|
return in->read(in->read_context, address, destination, bytes) == 1;
|
|
}
|
|
|
|
static uint64_t field64(const unsigned char *object, size_t offset) {
|
|
uint64_t value;
|
|
memcpy(&value, object + offset, sizeof(value));
|
|
return value;
|
|
}
|
|
|
|
static uint32_t field32(const unsigned char *object, size_t offset) {
|
|
uint32_t value;
|
|
memcpy(&value, object + offset, sizeof(value));
|
|
return value;
|
|
}
|
|
|
|
int gw_pipe_admission_snapshot(const struct gw_pipe_admission_input *in,
|
|
struct gw_pipe_admission_output *out) {
|
|
unsigned char owner[0x230], connection[0x30], pipe[24], listener[16];
|
|
uint64_t global_owner, queue_owner, holder_pipe, connection_methods[2];
|
|
uint64_t original_table[10], current_owner_vtable, queue, connection_ptr;
|
|
uint64_t listener_ptr, holder, write_callback, read_callback;
|
|
uint32_t generation, i;
|
|
const struct gw_pipe_control *control;
|
|
const struct gw_private_pipe_tables *tables;
|
|
struct gw_pipe_admission_output candidate;
|
|
unsigned char pristine_control[sizeof(struct gw_pipe_control)];
|
|
const uint64_t *existing_row;
|
|
|
|
if (!in || !out || !in->read || !in->executable || !in->writable ||
|
|
!in->expected_owner || !in->expected_bound_owner_vtable ||
|
|
!in->current_thread || in->current_thread != in->primary_thread ||
|
|
in->auth_state != 2 || in->server_proof_verified != 1 ||
|
|
in->transport_prepared != 1 ||
|
|
!in->credentials_generation ||
|
|
in->credentials_generation != in->pipe_generation ||
|
|
in->pipe_generation > 64 ||
|
|
in->callback_module_begin >= in->callback_module_end ||
|
|
!in->current_control || !in->current_tables ||
|
|
!in->epoch_callbacks || !in->retired_receivers)
|
|
return -1;
|
|
|
|
generation = in->pipe_generation;
|
|
if (in->epoch_callbacks_abi != 1 || in->epoch_callbacks_size != 1040 ||
|
|
in->epoch_callbacks_count != 64 || in->epoch_callbacks_reserved ||
|
|
in->retired_receivers_abi != 1 || in->retired_receivers_size != 528 ||
|
|
in->retired_receivers_count != 64 || in->retired_receivers_reserved)
|
|
return -2;
|
|
|
|
control = in->current_control;
|
|
tables = in->current_tables;
|
|
memset(pristine_control, 0, sizeof(pristine_control));
|
|
memcpy(pristine_control, &control->abi, sizeof(uint32_t) * 2);
|
|
if (control->abi != 1 || control->size != sizeof(*control) ||
|
|
memcmp(control, pristine_control, sizeof(*control)) ||
|
|
tables->abi != 1 || tables->size != sizeof(*tables) ||
|
|
tables->count != 64 || tables->reserved)
|
|
return -3;
|
|
existing_row = tables->methods[generation - 1];
|
|
for (i = 0; i < 10; ++i)
|
|
if (existing_row[i]) return -4;
|
|
|
|
write_callback = in->epoch_callbacks[generation - 1][0];
|
|
read_callback = in->epoch_callbacks[generation - 1][1];
|
|
if (!write_callback || !read_callback ||
|
|
write_callback < in->callback_module_begin ||
|
|
write_callback >= in->callback_module_end ||
|
|
read_callback < in->callback_module_begin ||
|
|
read_callback >= in->callback_module_end ||
|
|
!in->executable(in->read_context, (uintptr_t)write_callback) ||
|
|
!in->executable(in->read_context, (uintptr_t)read_callback))
|
|
return -5;
|
|
|
|
if (!read_exact(in, GW_ORIGINAL_OWNER_GLOBAL, &global_owner, 8) ||
|
|
global_owner != in->expected_owner ||
|
|
!read_exact(in, in->expected_owner, owner, sizeof(owner)))
|
|
return -6;
|
|
current_owner_vtable = field64(owner, 0);
|
|
if (current_owner_vtable != in->expected_bound_owner_vtable ||
|
|
field32(owner, 0x98) != 4)
|
|
return -7;
|
|
queue = field64(owner, 0xa0);
|
|
connection_ptr = field64(owner, 0x1a0);
|
|
listener_ptr = field64(owner, 0x220);
|
|
holder = field64(owner, 0x228);
|
|
if (!queue || !connection_ptr || !listener_ptr || !holder ||
|
|
!read_exact(in, (uintptr_t)queue, &queue_owner, 8) ||
|
|
queue_owner != in->expected_owner ||
|
|
!read_exact(in, (uintptr_t)connection_ptr, connection,
|
|
sizeof(connection)) ||
|
|
field64(connection, 0) != GW_ORIGINAL_CONNECTION_TABLE ||
|
|
field64(connection, 0x28) != listener_ptr ||
|
|
!read_exact(in, (uintptr_t)listener_ptr, listener,
|
|
sizeof(listener)) ||
|
|
field64(listener, 8) != in->expected_owner)
|
|
return -8;
|
|
if (!read_exact(in, GW_ORIGINAL_CONNECTION_TABLE + 17 * 8,
|
|
connection_methods, sizeof(connection_methods)) ||
|
|
connection_methods[0] != GW_ORIGINAL_CLOSE ||
|
|
connection_methods[1] != GW_ORIGINAL_WRITE)
|
|
return -9;
|
|
if (!read_exact(in, (uintptr_t)holder, &holder_pipe, 8) ||
|
|
!holder_pipe ||
|
|
!in->writable(in->read_context, (uintptr_t)holder_pipe, 8) ||
|
|
!read_exact(in, (uintptr_t)holder_pipe, pipe, sizeof(pipe)) ||
|
|
field64(pipe, 0) != GW_ORIGINAL_PIPE_TABLE ||
|
|
field64(pipe, 8) != in->expected_owner ||
|
|
field64(pipe, 16))
|
|
return -10;
|
|
if (!read_exact(in, GW_ORIGINAL_PIPE_TABLE - 8, original_table,
|
|
sizeof(original_table)) || original_table[0] != 0 ||
|
|
memcmp(original_table + 1, original_pipe_methods,
|
|
sizeof(original_pipe_methods)))
|
|
return -11;
|
|
/* The original allocator can reuse a fully retired pipe address. The
|
|
* current owner/holder/connection chain above and the generation-specific
|
|
* callbacks establish the live object; an address alone does not. Keep
|
|
* the current epoch slot empty so one epoch is published only once. */
|
|
if (in->retired_receivers[generation - 1]) return -12;
|
|
|
|
memset(&candidate, 0, sizeof(candidate));
|
|
candidate.holder = holder;
|
|
candidate.epoch_row[0] = original_table[0];
|
|
candidate.epoch_row[1] = write_callback;
|
|
candidate.epoch_row[2] = read_callback;
|
|
memcpy(candidate.epoch_row + 3, original_pipe_methods + 2,
|
|
sizeof(uint64_t) * 7);
|
|
candidate.control_template.abi = 1;
|
|
candidate.control_template.size = sizeof(struct gw_pipe_control);
|
|
candidate.control_template.stage = 1;
|
|
candidate.control_template.owner = in->expected_owner;
|
|
candidate.control_template.pipe = holder_pipe;
|
|
candidate.control_template.connection = connection_ptr;
|
|
candidate.control_template.queue = queue;
|
|
candidate.control_template.original_table = GW_ORIGINAL_PIPE_TABLE;
|
|
candidate.control_template.original_read = original_pipe_methods[1];
|
|
candidate.control_template.original_write = original_pipe_methods[0];
|
|
memcpy(candidate.control_template.methods, candidate.epoch_row,
|
|
sizeof(candidate.epoch_row));
|
|
|
|
candidate.prevalidated.pipe_vptr = (uintptr_t *)holder_pipe;
|
|
candidate.prevalidated.owner_global = global_owner;
|
|
candidate.prevalidated.queue_owner = queue_owner;
|
|
candidate.prevalidated.pipe_owner = field64(pipe, 8);
|
|
candidate.prevalidated.holder_pipe = holder_pipe;
|
|
candidate.prevalidated.owner_connection = connection_ptr;
|
|
candidate.prevalidated.connection_listener = field64(connection, 0x28);
|
|
candidate.prevalidated.owner_listener = listener_ptr;
|
|
candidate.prevalidated.listener_owner = field64(listener, 8);
|
|
candidate.prevalidated.owner_vtable = current_owner_vtable;
|
|
candidate.prevalidated.expected_owner_vtable = in->expected_bound_owner_vtable;
|
|
candidate.prevalidated.connection_vtable = field64(connection, 0);
|
|
candidate.prevalidated.original_rtti = original_table[0];
|
|
candidate.prevalidated.pipe_reserved = field64(pipe, 16);
|
|
candidate.prevalidated.owner_state = field32(owner, 0x98);
|
|
candidate.prevalidated.current_thread = in->current_thread;
|
|
candidate.prevalidated.primary_thread = in->primary_thread;
|
|
candidate.prevalidated.generation = generation;
|
|
candidate.prevalidated.active_generation = in->pipe_generation;
|
|
candidate.prevalidated.authenticated = true;
|
|
candidate.prevalidated.transport_prepared = true;
|
|
candidate.prevalidated.receiver_fresh = true;
|
|
candidate.prevalidated.callback_write = write_callback;
|
|
candidate.prevalidated.callback_read = read_callback;
|
|
*out = candidate;
|
|
return 0;
|
|
}
|