diff options
author | Ron Yorston <rmy@pobox.com> | 2024-06-16 08:51:04 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-06-16 08:51:04 +0100 |
commit | 51a4aaa27d280bae24c8fbdeb056fb01a6d5e940 (patch) | |
tree | 7e532edf58438166896fdac72b8198bc3fd7cefd | |
parent | eb376b5d196358dafe454a4778746e46bad48ee7 (diff) | |
download | busybox-w32-51a4aaa27d280bae24c8fbdeb056fb01a6d5e940.tar.gz busybox-w32-51a4aaa27d280bae24c8fbdeb056fb01a6d5e940.tar.bz2 busybox-w32-51a4aaa27d280bae24c8fbdeb056fb01a6d5e940.zip |
win32: detect Actually Portable Executable binaries
Check for the signature of Actually Portable Executable binaries
and treat them as executable.
Adds 40-64 bytes.
(GitHub issue #424)
-rw-r--r-- | win32/mingw.c | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index f96b5b49a..26b046f1a 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -472,22 +472,30 @@ static int has_exec_format(const char *name) | |||
472 | * Poke about in file to see if it's a PE binary. I've just copied | 472 | * Poke about in file to see if it's a PE binary. I've just copied |
473 | * the magic from the file command. | 473 | * the magic from the file command. |
474 | */ | 474 | */ |
475 | if (buf[0] == 'M' && buf[1] == 'Z' && n > 0x3f) { | 475 | if (buf[0] == 'M' && buf[1] == 'Z') { |
476 | offset = (buf[0x19] << 8) + buf[0x18]; | 476 | /* Actually Portable Executable */ |
477 | if (offset > 0x3f) { | 477 | /* See ape/ape.S at https://github.com/jart/cosmopolitan */ |
478 | offset = (buf[0x3f] << 24) + (buf[0x3e] << 16) + | 478 | if (n > 9 && memcmp(buf + 2, "qFpD='\n", 7) == 0) |
479 | (buf[0x3d] << 8) + buf[0x3c]; | 479 | return 1; |
480 | if (offset < sizeof(buf)-100) { | 480 | |
481 | if (memcmp(buf+offset, "PE\0\0", 4) == 0) { | 481 | if (n > 0x3f) { |
482 | sig = (buf[offset+25] << 8) + buf[offset+24]; | 482 | offset = (buf[0x19] << 8) + buf[0x18]; |
483 | if (sig == 0x10b || sig == 0x20b) { | 483 | if (offset > 0x3f) { |
484 | sig = (buf[offset+23] << 8) + buf[offset+22]; | 484 | offset = (buf[0x3f] << 24) + (buf[0x3e] << 16) + |
485 | if ((sig & 0x2000) != 0) { | 485 | (buf[0x3d] << 8) + buf[0x3c]; |
486 | /* DLL */ | 486 | if (offset < sizeof(buf)-100) { |
487 | return 0; | 487 | if (memcmp(buf+offset, "PE\0\0", 4) == 0) { |
488 | sig = (buf[offset+25] << 8) + buf[offset+24]; | ||
489 | if (sig == 0x10b || sig == 0x20b) { | ||
490 | sig = (buf[offset+23] << 8) + buf[offset+22]; | ||
491 | if ((sig & 0x2000) != 0) { | ||
492 | /* DLL */ | ||
493 | return 0; | ||
494 | } | ||
495 | sig = buf[offset+92]; | ||
496 | return (sig == 1 || sig == 2 || sig == 3 | ||
497 | || sig == 7); | ||
488 | } | 498 | } |
489 | sig = buf[offset+92]; | ||
490 | return (sig == 1 || sig == 2 || sig == 3 || sig == 7); | ||
491 | } | 499 | } |
492 | } | 500 | } |
493 | } | 501 | } |