fix(native): the hook finds libraries, preloads and itself through a sandbox's mounts, and ldconfig -C makes its cache

This commit is contained in:
Kurt Himebauch committed 2026-11-09 14:14:47 -05:00
1 parent 9e24ede7c2
commit 8b90d3f9e0
4 files changed
+40 -25

No files matched your search

+8 -5
View File
@@ -6,6 +6,8 @@
#ifndef DDNATV_HOST_TEST
static const char *ddnatv_hook_library(char *buf, size_t bufsz);
static char **clean_env_for_system(char *const envp[]) {
int count = 0;
while (envp && envp[count]) count++;
@@ -173,10 +175,12 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
}
}
if (!has_preload && g_ddnatv_root[0] != '\0') {
static const char hook_rel[] = "/usr/lib/libddnatv-hook.so";
char hook_buf[PATH_MAX];
const char *hook = (!has_preload && g_ddnatv_root[0] != '\0') ? ddnatv_hook_library(hook_buf, sizeof(hook_buf)) : NULL;
if (hook) {
size_t hook_len = strlen(hook);
size_t host_len = g_preload_host ? strlen(g_preload_host) + 1 : 0;
size_t size = 11 + host_len + g_ddnatv_root_len + sizeof(hook_rel);
size_t size = 11 + host_len + hook_len + 1;
char *str = malloc(size);
if (str) {
size_t n = ddnatv_append(str, size, 0, "LD_PRELOAD=", 11);
@@ -184,8 +188,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
n = ddnatv_append(str, size, n, g_preload_host, host_len - 1);
n = ddnatv_append(str, size, n, ":", 1);
}
n = ddnatv_append(str, size, n, g_ddnatv_root, g_ddnatv_root_len);
ddnatv_append(str, size, n, hook_rel, sizeof(hook_rel) - 1);
ddnatv_append(str, size, n, hook, hook_len);
new_env[dst++] = str;
}
}
+18 -7
View File
@@ -145,13 +145,24 @@ static int run_execve(orig_execve_f_type f, const char *path, char *const argv[]
// ldconfig does nothing in The native userland (its cache is for paths that ld.so opens without the hook), but the
// exec code reads /etc/ld.so.conf(.d) again when /etc/ld.so.cache changes: "sudo ldconfig" after an
// edit of a .conf file marks it changed, as the real ldconfig would by writing it.
static void ldconfig_mark_cache(void) {
// edit of a .conf file marks it changed, as the real ldconfig would by writing it. A cache asked
// for with -C is made (empty): Flatpak mounts the one it generates over /etc/ld.so.cache.
static void ldconfig_mark_cache(char *const argv[]) {
if (g_ddnatv_root[0] == '\0') return;
char path[PATH_MAX];
size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len);
ddnatv_append(path, sizeof(path), n, "/etc/ld.so.cache", 16);
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
const char *cache = NULL;
for (int i = 1; argv && argv[i]; i++) {
if (strcmp(argv[i], "-C") == 0 && argv[i + 1]) cache = argv[++i];
else if (strncmp(argv[i], "-C", 2) == 0 && argv[i][2] != '\0') cache = argv[i] + 2;
}
const char *target = path;
if (cache) {
target = rewrite_path_in_root(cache, path, sizeof(path));
} else {
size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len);
ddnatv_append(path, sizeof(path), n, "/etc/ld.so.cache", 16);
}
int fd = (int)syscall(SYS_openat, AT_FDCWD, target, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
if (fd < 0) return;
syscall(SYS_utimensat, fd, NULL, NULL, 0);
syscall(SYS_close, fd);
@@ -218,7 +229,7 @@ static int ddnatv_execve(const char *filename, char *const argv[], char *const e
strcmp(prog_name, "update-desktop-database") == 0 ||
strcmp(prog_name, "install-info") == 0 ||
strcmp(prog_name, "install-sgmlcatalog") == 0) {
if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache();
if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache(argv);
_exit(0);
}
if (strcmp(f_base, "mountpoint") == 0 || strcmp(prog_name, "mountpoint") == 0) {
@@ -756,7 +767,7 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path,
strcmp(prog_name, "start-stop-daemon") == 0) {
pid_t child = fork();
if (child == 0) {
if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache();
if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache(argv);
_exit(0);
} else if (child > 0) {
if (pid) *pid = child;
+6 -9
View File
@@ -498,17 +498,14 @@ static int lib_dir_list_has(const char *list, const char *dir, size_t dir_len) {
// is not found. Returns the new length.
static size_t append_rootfs_lib_dirs_missing_from(char *out, size_t outsz, size_t used, int separate, const char *have) {
for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) {
size_t dir_len = strlen(k_rootfs_lib_dirs[i]);
if (have && have[0] != '\0') {
char full[PATH_MAX];
size_t n = ddnatv_append(full, sizeof(full), 0, g_ddnatv_root, g_ddnatv_root_len);
n = ddnatv_append(full, sizeof(full), n, k_rootfs_lib_dirs[i], dir_len);
if (lib_dir_list_has(have, full, n)) continue;
}
// a Flatpak sandbox mounts /usr from its runtime
char buf[PATH_MAX];
const char *full = rewrite_path_in_root(k_rootfs_lib_dirs[i], buf, sizeof(buf));
size_t n = strlen(full);
if (have && have[0] != '\0' && lib_dir_list_has(have, full, n)) continue;
if (separate) used = ddnatv_append(out, outsz, used, ":", 1);
separate = 1;
used = ddnatv_append(out, outsz, used, g_ddnatv_root, g_ddnatv_root_len);
used = ddnatv_append(out, outsz, used, k_rootfs_lib_dirs[i], dir_len);
used = ddnatv_append(out, outsz, used, full, n);
}
return used;
}
+8 -4
View File
@@ -38,7 +38,9 @@ static void init_ddnatv_hook(void) {
if (hostname && hostname[0] && strlen(hostname) < sizeof(g_hostname)) ddnatv_copy_cstr(g_hostname, sizeof(g_hostname), hostname);
const char *preload = getenv("DDNATV_PRELOAD");
if (preload && preload[0] && g_ddnatv_root_len > 0) {
size_t cap = strlen(preload) + (g_ddnatv_root_len + 1) * 16 + 1;
size_t libs = 1;
for (const char *p = preload; *p; p++) libs += *p == ':';
size_t cap = libs * (PATH_MAX + 1);
char *host = malloc(cap);
if (host) {
size_t used = 0;
@@ -47,10 +49,12 @@ static void init_ddnatv_hook(void) {
char *save = NULL;
for (char *lib = copy ? strtok_r(copy, ":", &save) : NULL; lib; lib = strtok_r(NULL, ":", &save)) {
if (lib[0] != '/') continue;
if (used + g_ddnatv_root_len + strlen(lib) + 2 > cap) break;
char mapped_buf[PATH_MAX];
const char *mapped = rewrite_path_in_root(lib, mapped_buf, sizeof(mapped_buf));
size_t mapped_len = strlen(mapped);
if (used + mapped_len + 2 > cap) break;
if (used) host[used++] = ':';
used = ddnatv_append(host, cap, used, g_ddnatv_root, g_ddnatv_root_len);
used = ddnatv_append(host, cap, used, lib, strlen(lib));
used = ddnatv_append(host, cap, used, mapped, mapped_len);
}
free(copy);
g_preload_host = host;