8000 linux: Refuse to attach to processes frozen by the cgroup freezer by kkuehl · Pull Request #1317 · frida/frida-core · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions src/linux/frida-helper-backend.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,9 @@ namespace Frida {
}

private async bool init_async (int io_priority, Cancellable? cancellable) throws Error, IOError {
if (on_init == INTERRUPT && query_frozen (pid))
throw new Error.INVALID_OPERATION ("Unable to attach to process frozen by the cgroup freezer");

PtraceOptions options = PtraceOptions.TRACESYSGOOD | PtraceOptions.TRACEEXEC;

bool was_stopped = seize_supported && query_process_state (tid) == 'T';
Expand Down Expand Up @@ -2071,6 +2074,29 @@ namespace Frida {
return true;
}

private static bool query_frozen (uint pid) {
try {
string cgroup;
FileUtils.get_contents ("/proc/%u/cgroup".printf (pid), out cgroup);

string? unified_path = null;
foreach (unowned string line in cgroup.split ("\n")) {
if (line.has_prefix ("0::")) {
unified_path = line.substring (3);
break;
}
}
if (unified_path == null)
return false;

string events;
FileUtils.get_contents ("/sys/fs/cgroup%s/cgroup.events".printf (unified_path), out events);
return "frozen 1" in events.split ("\n");
} catch (FileError e) {
}
return false;
}

private static char query_process_state (uint tid) {
try {
string stat;
Expand Down Expand Up @@ -2122,6 +2148,20 @@ namespace Frida {
ptrace (CONT, tid);
}

public async void restore_saved_registers () {
try {
set_regs (saved_regs);
return;
} catch (Error e) {
}

try {
yield suspend (null);
set_regs (saved_regs);
} catch (GLib.Error e) {
}
}

public void step () throws Error {
ptrace (SINGLESTEP, tid);
}
Expand Down Expand Up @@ -2758,22 +2798,16 @@ namespace Frida {
int sig = -1;
var result_regs = GPRegs ();
session.set_regs (regs);
bool restored = false;
try {
session.resume ();
sig = yield session.wait_for_signals ({ SEGV, STOP }, cancellable);

session.get_regs (&result_regs);

restored = true;
session.set_regs (session.saved_registers);
} finally {
if (!restored) {
try {
session.set_regs (session.saved_registers);
} catch (Error e) {
}
}
} catch (GLib.Error e) {
yield session.restore_saved_registers ();
throw_api_error (e);
}

RemoteCallStatus status = (result_regs.program_counter == DUMMY_RETURN_ADDRESS)
Expand Down
16 changes: 14 additions & 2 deletions src/windows/frida-helper-backend-glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ struct _FridaRemoteWorkerContext
gpointer argument;
};

typedef struct _FridaProcessBasicInformation FridaProcessBasicInformation;

struct _FridaProcessBasicInformation
{
NTSTATUS exit_status;
PVOID peb_base_address;
ULONG_PTR affinity_mask;
LONG base_priority;
ULONG_PTR unique_process_id;
ULONG_PTR inherited_from_unique_process_id;
};

typedef struct _RtlClientId RtlClientId;

struct _RtlClientId
Expand Down Expand Up @@ -551,7 +563,7 @@ frida_create_loader_token (HANDLE process, GError ** error)
HANDLE result = NULL;
const gchar * failed_operation;
NTSTATUS (NTAPI * query_information_process) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
PROCESS_BASIC_INFORMATION basic_info;
FridaProcessBasicInformation basic_info;
HANDLE parent = NULL, parent_token = NULL, restricted_token = NULL, process_token = NULL;
guint i;
const WELL_KNOWN_SID_TYPE restricting_sid_types[] = {
Expand All @@ -568,7 +580,7 @@ frida_create_loader_token (HANDLE process, GError ** error)
query_information_process = (gpointer) GetProcAddress (GetModuleHandleW (L"ntdll.dll"), "NtQueryInformationProcess");
query_information_process (process, ProcessBasicInformation, &basic_info, sizeof (basic_info), NULL);

parent = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, FALSE, (DWORD) GPOINTER_TO_SIZE (basic_info.Reserved3));
parent = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, FALSE, (DWORD) basic_info.inherited_from_unique_process_id);
CHECK_OS_RESULT (parent, !=, NULL, "OpenProcess");

CHECK_OS_RESULT (OpenProcessToken (parent, TOKEN_DUPLICATE | TOKEN_QUERY, &parent_token), !=, FALSE, "OpenProcessToken");
Expand Down
Loading
0