mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Remove realpath/getcwd from loaders (#1024)
This implements proposals 1 and 2a from this gist: https://gist.github.com/mrdomino/2222cab61715fd527e82e036ba4156b1 The only reason to use realpath from the loader was to try to prevent a TOCTOU between the loader and the binary. But this is only a real issue in set-id contexts, and in those cases there is already a canonical way to do it: `/dev/fd`, passed by the kernel to the loader, so all we have to do is pass that along to the binary. Aside from realpath, there is no reason to absolutize the path we supply to the binary, since it can call `getcwd` as well as we can, and on non- M1 the binary is in a much better position to make that call. Since we no longer absolutize the path, the binary does need to do this, so we make its argv-parsing code generic and apply that to the different possible places the path could come from. This means that `_` is finally usable as a relative path, as a nice side benefit. The M1 realpath code had a significant bug - it uses the wrong offset to truncate the `.ape` in the `$prog.ape` case. This PR also fixes a regression in `ape $progname` out of `$PATH` on the two BSDs (Free and Net) that did not implement `RealPath`.
This commit is contained in:
parent
f73576ab8a
commit
2a11a09d98
3 changed files with 69 additions and 96 deletions
53
ape/loader.c
53
ape/loader.c
|
@ -545,40 +545,6 @@ __attribute__((__noreturn__)) static void Pexit(int os, const char *c, int rc,
|
|||
Exit(127, os);
|
||||
}
|
||||
|
||||
#define PSFD "/proc/self/fd/"
|
||||
|
||||
static int RealPath(int os, int fd, char *path, char **resolved) {
|
||||
char buf[PATH_MAX];
|
||||
int rc;
|
||||
if (IsLinux()) {
|
||||
char psfd[sizeof(PSFD) + 19];
|
||||
MemMove(psfd, PSFD, sizeof(PSFD) - 1);
|
||||
Utoa(psfd + sizeof(PSFD) - 1, fd);
|
||||
rc = SystemCall(-100, (long)psfd, (long)buf, PATH_MAX, 0, 0, 0,
|
||||
IsAarch64() ? 78 : 267);
|
||||
if (rc >= 0) {
|
||||
if (rc == PATH_MAX) {
|
||||
rc = -36;
|
||||
} else {
|
||||
buf[rc] = 0;
|
||||
}
|
||||
}
|
||||
} else if (IsXnu()) {
|
||||
rc = SystemCall(fd, 50, (long)buf, 0, 0, 0, 0, 92 | 0x2000000);
|
||||
} else if (IsOpenbsd()) {
|
||||
rc = SystemCall((long)path, (long)buf, 0, 0, 0, 0, 0, 115);
|
||||
} else {
|
||||
*resolved = 0;
|
||||
return 0;
|
||||
}
|
||||
if (rc >= 0) {
|
||||
MemMove(path, buf, StrLen(buf) + 1);
|
||||
*resolved = path;
|
||||
rc = 0;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static char AccessCommand(struct PathSearcher *ps, unsigned long pathlen) {
|
||||
if (pathlen + 1 + ps->namelen + 1 > sizeof(ps->path)) return 0;
|
||||
if (pathlen && ps->path[pathlen - 1] != '/') ps->path[pathlen++] = '/';
|
||||
|
@ -644,9 +610,8 @@ static char *Commandv(struct PathSearcher *ps, int os, const char *name,
|
|||
}
|
||||
}
|
||||
|
||||
__attribute__((__noreturn__)) static void Spawn(int os, const char *exe,
|
||||
char *path, int fd, long *sp,
|
||||
unsigned long pagesz,
|
||||
__attribute__((__noreturn__)) static void Spawn(int os, char *exe, int fd,
|
||||
long *sp, unsigned long pagesz,
|
||||
struct ElfEhdr *e,
|
||||
struct ElfPhdr *p) {
|
||||
long rc;
|
||||
|
@ -803,12 +768,12 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe,
|
|||
Msyscall(dynbase + code, codesize, os);
|
||||
|
||||
/* call program entrypoint */
|
||||
Launch(IsFreebsd() ? sp : 0, dynbase + e->e_entry, path, sp, os);
|
||||
Launch(IsFreebsd() ? sp : 0, dynbase + e->e_entry, exe, sp, os);
|
||||
}
|
||||
|
||||
static const char *TryElf(struct ApeLoader *M, union ElfEhdrBuf *ebuf,
|
||||
const char *exe, char *path, int fd, long *sp,
|
||||
long *auxv, unsigned long pagesz, int os) {
|
||||
char *exe, int fd, long *sp, long *auxv,
|
||||
unsigned long pagesz, int os) {
|
||||
long i, rc;
|
||||
unsigned size;
|
||||
struct ElfEhdr *e;
|
||||
|
@ -923,7 +888,7 @@ static const char *TryElf(struct ApeLoader *M, union ElfEhdrBuf *ebuf,
|
|||
}
|
||||
|
||||
/* we're now ready to load */
|
||||
Spawn(os, exe, path, fd, sp, pagesz, e, p);
|
||||
Spawn(os, exe, fd, sp, pagesz, e, p);
|
||||
}
|
||||
|
||||
__attribute__((__noreturn__)) static void ShowUsage(int os, int fd, int rc) {
|
||||
|
@ -1081,8 +1046,6 @@ EXTERN_C __attribute__((__noreturn__)) void ApeLoader(long di, long *sp,
|
|||
Pexit(os, prog, 0, "not found (maybe chmod +x or ./ needed)");
|
||||
} else if ((fd = Open(exe, O_RDONLY, 0, os)) < 0) {
|
||||
Pexit(os, exe, fd, "open");
|
||||
} else if ((rc = RealPath(os, fd, exe, &prog)) < 0) {
|
||||
Pexit(os, exe, rc, "realpath");
|
||||
} else if ((rc = Pread(fd, ebuf->buf, sizeof(ebuf->buf), 0, os)) < 0) {
|
||||
Pexit(os, exe, rc, "read");
|
||||
} else if ((unsigned long)rc < sizeof(ebuf->ehdr)) {
|
||||
|
@ -1122,9 +1085,9 @@ EXTERN_C __attribute__((__noreturn__)) void ApeLoader(long di, long *sp,
|
|||
}
|
||||
}
|
||||
if (i >= sizeof(ebuf->ehdr)) {
|
||||
TryElf(M, ebuf, exe, prog, fd, sp, auxv, pagesz, os);
|
||||
TryElf(M, ebuf, exe, fd, sp, auxv, pagesz, os);
|
||||
}
|
||||
}
|
||||
}
|
||||
Pexit(os, exe, 0, TryElf(M, ebuf, exe, prog, fd, sp, auxv, pagesz, os));
|
||||
Pexit(os, exe, 0, TryElf(M, ebuf, exe, fd, sp, auxv, pagesz, os));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue