28

Is there a way to run an executable binary file under Linux which does not have the execute bit set? chmod +x is not an option.

E.g. it's permissions may be r--r--r-- only.

Executing scripts is possible without setting the execute bit and putting in a shebang by passing the source to the interpreter, e.g. bash script.sh or python script.py.

So is there something like execute abinaryfile that will load the object code into memory and run it?

2
  • 3
    make a copy and chmod an option?
    – TiCL
    Sep 29, 2011 at 23:45
  • No, I'm wondering if there is a way to execute a binary in an environment where the permission bit can't be set (on the given file or any copies of it...)
    – Tom
    Sep 30, 2011 at 1:34

3 Answers 3

42

You can use /lib/ld*.so as an ELF interpreter, like so:

cp /bin/ls /tmp/ls
chmod a-x /tmp/ls
/lib/ld-linux.so.2 /tmp/ls

The actual name differs from architecture to architecture. Some names include /lib/ld-linux.so.2, /lib/ld-linux-x86-64.so.2 and /lib/ld-2.7.so. You can probably find it singularly as /lib/ld*.

5
  • 1
    great info.....anyways what does ld stands for? Nov 14, 2011 at 11:43
  • @VineetMenon ld is the program linker/loader. It finds and loads the shared libraries used by the program, and then executes it. ld-linux handles ELF binaries.
    – Daniel Beck
    Nov 14, 2011 at 11:57
  • Ok, what if /lib/ld-linux.so.2 isn't executable (is that even possible?)
    – LawrenceC
    Nov 14, 2011 at 12:30
  • @ultrasawblade AFAIK, only executable .sos can be loaded, and ld-linux is kind of important.
    – Daniel Beck
    Nov 14, 2011 at 12:41
  • 1
    On 64-bit Ubuntu it is /lib64/ld-linux-x86-64.so.2 Jul 14, 2016 at 10:12
1

No. At least, not in the same way. You are still executing a binary when you do the python thing. Python is +x. You would need to compile something that can load a file and execute it.

TiCL should make his/her response an answer because it is the best way to go.

2
  • Yeah, I understand "python" is a binary in that instance. What I was hoping for is that the "compile something that can load a file and execute it" already existed...
    – Tom
    Sep 30, 2011 at 1:35
  • @Tom You'll find C programs that can read an binary into memory and execute it. I don't know if Python is low level enough to do it in the C way, though.
    – new123456
    Sep 30, 2011 at 1:52
0

The exec syscall of the Linux kernel fails with EACCES if the file is not executable

While you can do sh myprog.sh, trying to run the program as ./myprog.sh cannot work, since when you do that:

This can be verified with main.c:

#define _XOPEN_SOURCE 700
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int main(void) {
    char *argv[] = {"myprog", NULL};
    char *envp[] = {NULL};
    int ret;
    ret = execve("myprog.sh", argv, envp);
    perror("execve");
    printf("%d\n", errno);
    printf("%d\n", EACCES);
}

and myprog.sh:

#!/bin/sh
echo worked

If myprog.sh is not executable, main fails with:

execve: Permission denied
13
13

Tested in Ubuntu 17.10, gcc -std=c99.

POSIX 7 mentions that at:

The exec functions, except for fexecve(), shall fail if:

[EACCES] Search permission is denied for a directory listed in the new process image file's path prefix, or the new process image file denies execution permission.

Further rationale can be found at: https://security.stackexchange.com/questions/66550/unix-execute-permission-can-be-easily-bypassed-is-it-superfluous-or-whats-the

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.