1

Is there any way to run binary file from stdin? In Android system I have /data/test in RW but noexec partition

test.c

#include <stdio.h>
void main()
{
    printf("%s\n","hi!");
}

compile: gcc test.c -o test

run:

./test
hi!

I want to run from stdin (without bash) not from file

cat test | sh
sh: 1: ELF@
           @8: not found
sh: 10: Syntax error: EOF in backquote substitution

I tried

/apex/com.android.runtime/bin/linker64 /data/test
error: "/data/test" has unexpected e_type: 2

How can I do that ?

6
  • 2
    Why? Is this an XY problem? If, and this is a guess, that the file system that test resides on is mounted noexec, there are other ways around the problem. Commented Oct 22 at 19:24
  • @doneal24 yes, I cannot copy that file and that partition is read only (cannot remount ) . so I want to run it from stdin if that possible Commented Oct 22 at 19:28
  • 2
    Having the partition mounted read-only will not affect your being able to run the program. You might try /lib64/ld-linux-x86-64.so.2 ./test, which will execute the program even if the partition is mounted noexec. If the partition is read-only, how did the compile succeed in creating ./test? Commented Oct 22 at 19:34
  • 1
    Please edit your post to include the output of ldd /data/test. I don't know what software is in /apex/ but you seem to be using the standard gcc toolkit. Did you try the command I suggested? Commented Oct 22 at 19:47
  • 1
    Please edit your question and describe the actual problem. You say you cannot copy, but then you claim you can do cat test which means you can copy (cat test > newfile). But if you explain what the limitations are, I'm sure we can find a way to help you. It just won't be running a binary from stdin. Commented Oct 23 at 11:41

1 Answer 1

2
cat test | {whatever program}

How can I do that ?

Not at all like that; the Linux exec syscalls (and all its variants) intentionally only allow you to execute independent programs from paths or file descriptors; you need a file. And upon accessing that file, the executable bit needs to be set.

Now, on a "PC-style" Linux environment, there might be tricks like memfd (which, by the way, I haven't been able to make work for this purpose, I think that might actually also be on purpose!), but I'm pretty certain Android won't let you do that.

So, you need to learn how on Linux a process actually gets initialized and launched: you can write a loader that takes data, e.g. from stdin, puts it in a memory buffer, analyzes the loaded data, prepares a memory map, jumps into the ldloader within that buffer, let it load libraries and set up the memory mappings, before jumping to the loaded binary's startup code.

That's essentially what /lib[64]/ld-linux-{platform}.so.2 does: load an elf library and jump to its beginning; it seems the runtime linker thing you chose to use instead (linker64) it will not work with an executable ELF (which is what you have). Question here becomes whether your code itself must reside in an executable? It would seem to me you could simple compile it into a shared object instead and load that into any other process.

Implementing this honestly sounds a bit out of scope, considering the way you asked this question. I think you're really suffering from an X/Y problem: you ask about X, which is hard, complicated, and not at all done like you think it works, but what you really want to solve is something else, Y, which you forgo to ask. For example, the trivial answer here seems to be "copy over your executable to somewhere else"; I'm sure you have a reason that isn't an option, but you don't let us into what you're doing in the bigger picture, so we can't extrapolate about constraints you don't mention.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.