libxcrypt icon indicating copy to clipboard operation
libxcrypt copied to clipboard

skip-if-exec-format-error doesn't work

Open zackw opened this issue 3 years ago • 0 comments

I have discovered that skip-if-exec-format-error doesn't work reliably. Observe:

$ ./m4/skip-if-exec-format-error ./wrong-arch-true; echo $?
./m4/skip-if-exec-format-error:line 17: ./wrong-arch-true: cannot execute binary file: Exec format error
77

but

$ dd if=wrong-arch-true of=test.1 bs=6 count=1 status=none
$ chmod +x test.1
$ ./m4/skip-if-exec-format-error ./test.1; echo $?
./test.1: line 1: $'\177ELF\002\001': command not found
127

What on earth? Turns out that on most Unixes, the C library routine execvp will run /bin/sh argv... if the attempt to invoke argv[0] fails with ENOEXEC. If there are NULs in the first few dozen bytes of the file, /bin/sh will generate the "Exec format error" message, but if they're aren't, it'll try to run the sucker as a shell script.

There isn't, AFAICT, a good way to prevent this behavior, short of reimplementing execvp by hand in C, which wouldn't be that bad except that, when cross-compiling (which is the only time skip-if-exec-format-error gets used) we'd need a C compiler for the build machine as well as the host. I'm arguing with myself about whether it's worth the hassle.

zackw avatar Dec 15 '20 01:12 zackw