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 a338eee commit 0784d41
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 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 @@ -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;
}
}
}
}
Expand Down

0 comments on commit 0784d41

Please sign in to comment.