GetZipCdir: prevent integer underflow. posix_spawn_test.c: stop attempting to load zipos from /usr/bin/ape. (#758)

This commit is contained in:
Gavin Hayes 2023-03-05 17:29:38 -05:00 committed by GitHub
parent 22fcab131c
commit c5de653b98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View file

@ -55,11 +55,12 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
continue;
}
}
while (magic = READ32LE(p + i),
magic != kZipCdir64LocatorMagic && magic != kZipCdirHdrMagic &&
i + 0x10000 + 0x1000 >= n) --i;
if (magic == kZipCdir64LocatorMagic &&
i + kZipCdir64LocatorSize <= n &&
while (magic = READ32LE(p + i), magic != kZipCdir64LocatorMagic &&
magic != kZipCdirHdrMagic &&
i + 0x10000 + 0x1000 >= n && i > 0) {
--i;
}
if (magic == kZipCdir64LocatorMagic && i + kZipCdir64LocatorSize <= n &&
IsZipCdir64(p, n, ZIP_LOCATE64_OFFSET(p + i))) {
return p + ZIP_LOCATE64_OFFSET(p + i);
} else if (magic == kZipCdirHdrMagic && IsZipCdir32(p, n, i)) {
@ -73,6 +74,6 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
} while (j-- && i - j < 128);
return p + i;
}
} while (i-- + 0x10000 + 0x1000 >= n);
} while (i > 0 && i-- + 0x10000 + 0x1000 >= n);
return 0;
}

View file

@ -51,7 +51,7 @@ __attribute__((__constructor__)) static void init(void) {
TEST(posix_spawn, test) {
int rc, ws, pid;
char *prog = GetProgramExecutableName();
char *args[] = {program_invocation_name, NULL};
char *args[] = {prog, NULL};
char *envs[] = {"THE_DOGE=42", NULL};
EXPECT_EQ(0, posix_spawn(&pid, prog, NULL, NULL, args, envs));
EXPECT_NE(-1, waitpid(pid, &ws, 0));