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 Dec 2, 2023
1 parent 321d65c commit 8bef966
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions libc/proc/fexecve.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -196,15 +197,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) && IsApeLoadable(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) && IsApeLoadable(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 @@ -287,7 +294,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 8bef966

Please sign in to comment.