diff --git a/tools/bionic-runtime/steamshm.c b/tools/bionic-runtime/steamshm.c index 16eb686b99..7ee22d75f9 100644 --- a/tools/bionic-runtime/steamshm.c +++ b/tools/bionic-runtime/steamshm.c @@ -8,6 +8,7 @@ #define _GNU_SOURCE #include #include +#include #include #include #include @@ -547,3 +548,55 @@ EXPORT uid_t getuid(void) } return (uid_t)raw_getuid(); } + +static bool read_at(int fd, void *buf, size_t n, off_t at) +{ + return pread(fd, buf, n, at) == (ssize_t)n; +} + +static void load_needed_of(const char *path) +{ + Elf64_Ehdr e; + Elf64_Phdr ph[32]; + Elf64_Dyn dyn[128]; + uint64_t strtab = 0, strsz = 0, needed[32]; + size_t n_dyn = 0, n_needed = 0; + off_t names_at = -1; + char *names = NULL; + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return; + if (!read_at(fd, &e, sizeof e, 0) || memcmp(e.e_ident, ELFMAG, SELFMAG) || e.e_ident[EI_CLASS] != ELFCLASS64 || + e.e_phentsize != sizeof *ph || e.e_phnum > 32 || !read_at(fd, ph, e.e_phnum * sizeof *ph, (off_t)e.e_phoff)) + goto out; + for (int i = 0; i < e.e_phnum; i++) { + if (ph[i].p_type != PT_DYNAMIC) continue; + n_dyn = ph[i].p_filesz / sizeof *dyn; + if (n_dyn > 128) n_dyn = 128; + if (!read_at(fd, dyn, n_dyn * sizeof *dyn, (off_t)ph[i].p_offset)) goto out; + break; + } + for (size_t i = 0; i < n_dyn && dyn[i].d_tag != DT_NULL; i++) { + if (dyn[i].d_tag == DT_STRTAB) strtab = dyn[i].d_un.d_ptr; + else if (dyn[i].d_tag == DT_STRSZ) strsz = dyn[i].d_un.d_val; + else if (dyn[i].d_tag == DT_NEEDED && n_needed < 32) needed[n_needed++] = dyn[i].d_un.d_val; + } + for (int i = 0; i < e.e_phnum; i++) + if (ph[i].p_type == PT_LOAD && strtab >= ph[i].p_vaddr && strtab - ph[i].p_vaddr + strsz <= ph[i].p_filesz) + names_at = (off_t)(ph[i].p_offset + strtab - ph[i].p_vaddr); + if (names_at < 0 || !strsz || strsz > (16u << 20) || !(names = malloc(strsz + 1)) || + !read_at(fd, names, strsz, names_at)) + goto out; + names[strsz] = 0; + for (size_t i = 0; i < n_needed; i++) + if (needed[i] < strsz && !dlopen(names + needed[i], RTLD_NOW)) + say("steamshm: %s, needed by %s, did not load: %s\n", names + needed[i], path, dlerror()); +out: + free(names); + close(fd); +} + +__attribute__((constructor)) static void link_steamclient_needs_before_it(void) +{ + const char *path = getenv("WINESTEAMCLIENTPATH64"); + if (path && *path) load_needed_of(path); +} diff --git a/tools/tests/test_bionic_steamclient_needs.py b/tools/tests/test_bionic_steamclient_needs.py new file mode 100644 index 0000000000..156b11165b --- /dev/null +++ b/tools/tests/test_bionic_steamclient_needs.py @@ -0,0 +1,94 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +STEAMSHM = Path(__file__).resolve().parents[1] / "bionic-runtime/steamshm.c" + +SOURCES = { + "libsystemxml.so": ("int xml_copy(void) { return 1; }\n", []), + "libvendor.so": ("int xml_copy(void);\nint vendor_xml_copy(void) { return xml_copy(); }\n", ["systemxml"]), + "libframework.so": ("int framework(void) { return 0; }\n", ["vendor"]), + "libsteam.so": ("int xml_copy(void) { return 2; }\nint CreateInterface(void) { return 0; }\n", ["framework"]), +} + +LOADER_PRELUDE = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define say(...) fprintf(stderr, __VA_ARGS__) +""" + + +def steamclient_loader(): + source = STEAMSHM.read_text() + return LOADER_PRELUDE + source[source.index("static bool read_at("):] + + +PROGRAM = r""" +#include +#include +int main(void) { + if (!dlopen("libsteam.so", RTLD_NOW)) { puts(dlerror()); return 1; } + void *vendor = dlopen("libvendor.so", RTLD_NOW | RTLD_NOLOAD); + int (*copy)(void) = vendor ? (int (*)(void))dlsym(vendor, "vendor_xml_copy") : 0; + if (!copy) return 1; + printf("%d\n", copy()); + return 0; +} +""" + + +@unittest.skipUnless(shutil.which("cc"), "needs a C compiler") +class SteamClientNeedsTest(unittest.TestCase): + def setUp(self): + self.dir = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.dir, True) + for name, (code, needs) in SOURCES.items(): + src = self.dir / (name + ".c") + src.write_text(code) + subprocess.run(["cc", "-shared", "-fPIC", "-o", str(self.dir / name), str(src), "-L", str(self.dir), + "-Wl,--no-as-needed", *("-l" + n for n in needs)], check=True) + (self.dir / "main.c").write_text(PROGRAM) + subprocess.run(["cc", "-o", str(self.dir / "main"), str(self.dir / "main.c"), "-ldl"], check=True) + loader = self.dir / "loader.c" + loader.write_text(steamclient_loader()) + self.preload = self.dir / "libsteamshm.so" + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-fvisibility=hidden", + str(loader), "-ldl", "-o", str(self.preload)], check=True) + + def vendor_binding(self, preload=True, steam=None): + env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "WINESTEAMCLIENTPATH64")} + env["LD_LIBRARY_PATH"] = str(self.dir) + if steam: + env["WINESTEAMCLIENTPATH64"] = steam + if preload: + env["LD_PRELOAD"] = str(self.preload) + run = subprocess.run([str(self.dir / "main")], env=env, capture_output=True, text=True, check=True) + return run.stdout.strip() + + def test_without_the_preload_a_framework_library_binds_to_steams_copy(self): + self.assertEqual(self.vendor_binding(preload=False), "2") + + def test_the_libraries_steam_needs_link_to_their_own_copy_first(self): + self.assertEqual(self.vendor_binding(steam=str(self.dir / "libsteam.so")), "1") + + def test_without_a_steam_library_path_nothing_is_loaded_early(self): + self.assertEqual(self.vendor_binding(), "2") + + def test_a_missing_or_foreign_steam_library_is_ignored(self): + self.assertEqual(self.vendor_binding(steam=str(self.dir / "missing.so")), "2") + self.assertEqual(self.vendor_binding(steam=str(self.dir / "main.c")), "2") + + +if __name__ == "__main__": + unittest.main()