Skip to content

Commit

Permalink
fexecve: fix zipos when executable is not an APE
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi committed Sep 3, 2023
1 parent be53771 commit 3ed188f
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions libc/calls/fexecve.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -194,15 +195,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;
}
}
}
}
Expand Down Expand Up @@ -285,7 +292,6 @@ int fexecve(int fd, char *const argv[], char *const envp[]) {
}
size_t numenvs;
for (numenvs = 0; envp[numenvs];) ++numenvs;
// const size_t desenvs = min(500, max(numenvs + 1, 2));
static _Thread_local char *envs[500];
memcpy(envs, envp, numenvs * sizeof(char *));
envs[numenvs] = path;
Expand Down

0 comments on commit 3ed188f

Please sign in to comment.