Skip to content

Commit 5582d1f

Browse files
authored
darwin-module: Handle special lib ordinal values (#653)
In this way, if an import's module can't be resolved by direct ordinal index, the import address is resolved immediately using dlsym() and the corresponding module path is resolved anyway at emit time (like it's always been) with a new fallback on dladdr() when ModuleMap fails to resolve it.
1 parent 15f163c commit 5582d1f

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

gum/backend-darwin/gumprocess-darwin.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (C) 2010-2022 Ole André Vadla Ravnås <oleavr@nowsecure.com>
33
* Copyright (C) 2015 Asger Hautop Drewsen <asgerdrewsen@gmail.com>
4+
* Copyright (C) 2022 Francesco Tamagni <mrmacete@protonmail.ch>
45
*
56
* Licence: wxWindows Library Licence, Version 3.1
67
*/
@@ -1894,17 +1895,23 @@ gum_emit_import (const GumImportDetails * details,
18941895

18951896
if (d.module == NULL)
18961897
{
1897-
d.address = GUM_ADDRESS (dlsym (RTLD_DEFAULT, d.name));
1898+
if (details->address != 0)
1899+
d.address = details->address;
1900+
else
1901+
d.address = GUM_ADDRESS (dlsym (RTLD_DEFAULT, d.name));
18981902

18991903
if (d.address != 0)
19001904
{
19011905
const GumModuleDetails * module_details;
1906+
Dl_info info;
19021907

19031908
if (ctx->module_map == NULL)
19041909
ctx->module_map = gum_module_map_new ();
19051910
module_details = gum_module_map_find (ctx->module_map, d.address);
19061911
if (module_details != NULL)
19071912
d.module = module_details->path;
1913+
else if (dladdr (GSIZE_TO_POINTER (d.address), &info) != 0)
1914+
d.module = info.dli_fname;
19081915
}
19091916
}
19101917

@@ -1936,6 +1943,12 @@ gum_resolve_export (const char * module_name,
19361943
GumEnumerateImportsContext * ctx = user_data;
19371944
GumDarwinModule * module;
19381945

1946+
if (module_name == NULL)
1947+
{
1948+
const char * name = gum_symbol_name_from_darwin (symbol_name);
1949+
return GUM_ADDRESS (dlsym (RTLD_DEFAULT, name));
1950+
}
1951+
19391952
module = gum_darwin_module_resolver_find_module (ctx->resolver, module_name);
19401953
if (module != NULL)
19411954
{

gum/gumdarwinmodule-priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2020 Ole André Vadla Ravnås <oleavr@nowsecure.com>
3+
* Copyright (C) 2022 Francesco Tamagni <mrmacete@protonmail.ch>
34
*
45
* Licence: wxWindows Library Licence, Version 3.1
56
*/
@@ -34,6 +35,7 @@
3435
#define GUM_BIND_SPECIAL_DYLIB_SELF 0
3536
#define GUM_BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE -1
3637
#define GUM_BIND_SPECIAL_DYLIB_FLAT_LOOKUP -2
38+
#define GUM_BIND_SPECIAL_DYLIB_WEAK_LOOKUP -3
3739

3840
G_BEGIN_DECLS
3941

gum/gumdarwinmodule.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2022 Ole André Vadla Ravnås <oleavr@nowsecure.com>
3+
* Copyright (C) 2022 Francesco Tamagni <mrmacete@protonmail.ch>
34
*
45
* Licence: wxWindows Library Licence, Version 3.1
56
*/
@@ -1570,7 +1571,7 @@ gum_emit_chained_imports (const GumDarwinChainedFixupsDetails * details,
15701571
for (imp_index = 0; imp_index != fixups_header->imports_count; imp_index++)
15711572
{
15721573
guint name_offset;
1573-
gint lib_ordinal;
1574+
gint8 lib_ordinal;
15741575
GumImportDetails * d;
15751576
gpointer key;
15761577

@@ -1896,11 +1897,23 @@ const gchar *
18961897
gum_darwin_module_get_dependency_by_ordinal (GumDarwinModule * self,
18971898
gint ordinal)
18981899
{
1899-
gint i = ordinal - 1;
1900+
gint i;
19001901

19011902
if (!gum_darwin_module_ensure_image_loaded (self, NULL))
19021903
return NULL;
19031904

1905+
switch (ordinal)
1906+
{
1907+
case GUM_BIND_SPECIAL_DYLIB_SELF:
1908+
return self->name;
1909+
case GUM_BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
1910+
case GUM_BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
1911+
case GUM_BIND_SPECIAL_DYLIB_WEAK_LOOKUP:
1912+
return NULL;
1913+
}
1914+
1915+
i = ordinal - 1;
1916+
19041917
if (i < 0 || i >= (gint) self->dependencies->len)
19051918
return NULL;
19061919

0 commit comments

Comments
 (0)