8000 linux: Fix attach when /proc/<pid>/maps has non-UTF-8 bytes by washedbeast · Pull Request #1284 · 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
2 changes: 1 addition & 1 deletion src/linux/frida-helper-backend.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,7 @@ namespace Frida {

Gum.Linux.enumerate_ranges ((Posix.pid_t) session.pid, READ | EXECUTE, d => {
unowned Gum.FileMapping? file = d.file;
if (file != null && file.path.has_prefix ("memfd:"))
if (file != null && file.path.has_prefix ("/memfd:"))
return true;
if (d.range.size >= code.length) {
code_start = d.range.base_address + d.range.size - round_size_to_page_size (code.length);
Expand Down
6 changes: 5 additions & 1 deletion src/linux/linux-host-session.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,11 @@ namespace Frida {
}

MatchInfo info;
if (!/^([0-9a-f]+)-([0-9a-f]+) rw-p 00000000 00:00 0 *$/m.match (raw_maps, 0, out info)) {
try {
var anon_rw_pattern = new Regex ("^([0-9a-f]+)-([0-9a-f]+) rw-p 00000000 00:00 0 *$", MULTILINE | RAW);
if (!anon_rw_pattern.match (raw_maps, 0, out info))
assert_not_reached ();
} catch (RegexError e) {
assert_not_reached ();
}
assert (info != null);
Expand Down
9 changes: 7 additions & 2 deletions src/linux/proc-maps.vala
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,13 @@ namespace Frida {
return;
}

if (!/^([0-9a-f]+)-([0-9a-f]+) (\S{4}) ([0-9a-f]+) ([0-9a-f]{2,}:[0-9a-f]{2,}) (\d+) *([^\n]*)$/m.match (
contents, 0, out info)) {
try {
var line_pattern = new Regex (
"""^([0-9a-f]+)-([0-9a-f]+) (\S{4}) ([0-9a-f]+) ([0-9a-f]{2,}:[0-9a-f]{2,}) (\d+) *([^\n]*)$""",
MULTILINE | RAW);
if (!line_pattern.match (contents, 0, out info))
assert_not_reached ();
} catch (RegexError e) {
assert_not_reached ();
}
}
Expand Down
49 changes: 48 additions & 1 deletion tests/test-injector.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ namespace Frida.InjectorTest {

GLib.Test.add_func ("/Injector/resource-leaks", test_resource_leaks);

#if LINUX
GLib.Test.add_func ("/Injector/Linux/inject-with-non-utf8-mapping", test_injection_with_non_utf8_mapping);
#endif

#if DARWIN
GLib.Test.add_func ("/Injector/suspended-injection-current-arch", () => {
test_suspended_injection (Frida.Test.Arch.CURRENT);
Expand Down Expand Up @@ -149,6 +153,45 @@ namespace Frida.InjectorTest {
rat.close ();
}

#if LINUX
private void test_injection_with_non_utf8_mapping () {
var logfile = File.new_for_path (Frida.Test.path_to_temporary_file ("non-utf8-mapping.log"));
try {
logfile.delete ();
} catch (GLib.Error delete_error) {
}
var envp = new string[] {
"FRIDA_LABRAT_LOGFILE=" + logfile.get_path ()
};

var sleeper = File.new_for_path (Frida.Test.Labrats.path_to_executable ("sleeper"));
var sleeper_copy = File.new_for_path (Frida.Test.path_to_temporary_file ("sleeper-\xff"));
try {
sleeper.copy (sleeper_copy, OVERWRITE);
} catch (GLib.Error e) {
assert_not_reached ();
}
FileUtils.chmod (sleeper_copy.get_path (), 0700);

var rat = new Labrat.from_path (sleeper_copy.get_path (), envp);

rat.inject ("simple-agent", "");
rat.wait_for_uninject ();
assert_true (content_of (logfile) == ">m<");

try {
rat.process.kill ();

logfile.delete ();
sleeper_copy.delete ();
} catch (GLib.Error e) {
assert_not_reached ();
}

rat.close ();
}
#endif

#if DARWIN
private void test_suspended_injection (Frida.Test.Arch arch) {
var logfile = File.new_for_path (Frida.Test.path_to_temporary_file ("suspended-injection.log"));
Expand Down Expand Up @@ -193,8 +236,12 @@ namespace Frida.InjectorTest {
private PendingUninject? pending_uninject;

public Labrat (string name, string[] envp, Frida.Test.Arch arch = Frida.Test.Arch.CURRENT) {
this.from_path (Frida.Test.Labrats.path_to_executable (name), envp, arch);
}

public Labrat.from_path (string path, string[] envp, Frida.Test.Arch arch = Frida.Test.Arch.CURRENT) {
try {
process = Frida.Test.Process.start (Frida.Test.Labrats.path_to_executable (name), null, envp, arch);
process = Frida.Test.Process.start (path, null, envp, arch);
} catch (Error e) {
printerr ("\nFAIL: %s\n\n", e.message);
assert_not_reached ();
Expand Down
0