DynamicLoader: Don't leak fds

This commit is contained in:
Bananymous 2025-04-20 18:18:32 +03:00
parent 16dbfbb267
commit 2cbfe70a28
1 changed files with 4 additions and 5 deletions

View File

@ -700,11 +700,7 @@ static void handle_dynamic(LoadedElf& elf)
if (auto ret = syscall(SYS_REALPATH, path_buffer, realpath); ret < 0) if (auto ret = syscall(SYS_REALPATH, path_buffer, realpath); ret < 0)
print_error_and_exit("realpath", ret); print_error_and_exit("realpath", ret);
int library_fd = syscall(SYS_OPENAT, AT_FDCWD, realpath, O_RDONLY); const auto& loaded_elf = load_elf(realpath, -1);
if (library_fd < 0)
print_error_and_exit("could not open library", library_fd);
const auto& loaded_elf = load_elf(realpath, library_fd);
dynamic.d_un.d_ptr = reinterpret_cast<uintptr_t>(&loaded_elf); dynamic.d_un.d_ptr = reinterpret_cast<uintptr_t>(&loaded_elf);
} }
@ -844,6 +840,9 @@ static LoadedElf& load_elf(const char* path, int fd)
if (strcmp(s_loaded_files[i].path, path) == 0) if (strcmp(s_loaded_files[i].path, path) == 0)
return s_loaded_files[i]; return s_loaded_files[i];
if (fd == -1 && (fd = syscall(SYS_OPENAT, AT_FDCWD, path, O_RDONLY)) < 0)
print_error_and_exit("could not open library", fd);
ElfNativeFileHeader file_header; ElfNativeFileHeader file_header;
if (auto ret = syscall(SYS_READ, fd, &file_header, sizeof(file_header)); ret != sizeof(file_header)) if (auto ret = syscall(SYS_READ, fd, &file_header, sizeof(file_header)); ret != sizeof(file_header))
print_error_and_exit("could not read file header", ret); print_error_and_exit("could not read file header", ret);