From 0784d41e07c35441b29893f7499376b49df791e5 Mon Sep 17 00:00:00 2001 From: Gavin Hayes Date: Sat, 2 Sep 2023 20:52:13 -0400 Subject: [PATCH] fexecve: fix zipos when executable is not an APE --- libc/calls/fexecve.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/libc/calls/fexecve.c b/libc/calls/fexecve.c index 39317270370..471c1bfcb6a 100644 --- a/libc/calls/fexecve.c +++ b/libc/calls/fexecve.c @@ -44,6 +44,7 @@ #include "libc/sysv/consts/s.h" #include "libc/sysv/consts/shm.h" #include "libc/sysv/errfuns.h" +#include "libc/zip.internal.h" static bool IsAPEFd(const int fd) { char buf[8]; @@ -195,15 +196,21 @@ static int fd_to_mem_fd(const int infd, char *path) { ssize_t readRc; readRc = pread(infd, space, st.st_size, 0); bool success = readRc != -1; - if (success && (st.st_size > 8) && IsAPEMagic(space)) { - int flags = fcntl(fd, F_GETFD); - if (success = (flags != -1) && - (fcntl(fd, F_SETFD, flags & (~FD_CLOEXEC)) != -1) && - ape_to_elf(space, st.st_size)) { - const int newfd = fcntl(fd, F_DUPFD, 9001); - if (newfd != -1) { - close(fd); - fd = newfd; + if (success) { + int ziperror; + if ((st.st_size > 8) && IsAPEMagic(space)) { + success = ape_to_elf(space, st.st_size); + } + // we need to preserve the fd over exec if there's zipos + if (success && _weaken(GetZipEocd) && _weaken(GetZipEocd)(space, st.st_size, &ziperror)) { + int flags = fcntl(fd, F_GETFD); + if (success = (flags != -1) && + (fcntl(fd, F_SETFD, flags & (~FD_CLOEXEC)) != -1)) { + const int newfd = fcntl(fd, F_DUPFD, 9001); + if (newfd != -1) { + close(fd); + fd = newfd; + } } } }