1

I have cross-compiled a binary on Linux using the arm toochain. I uploaded the binary to a non-rooted andnroid phone.

What do I need to do in order to run this binary on the non-rooted phone ?

EDIT: I tried to run via terminal emulator, but I get the following error.

$ pwd
/mnt/sdcard/external_sd
$ ls -l hello
-rwxrwxr-x system   sdcard_rw     8420 2013-12-31 22:12 hello
$ ./hello
./hello: permission denied
$
aseaudi
  • 111
  • 1
  • 1
  • 3
  • I suppose you could install a terminal emulator, navigate to the binary, and use ./binary to invoke it. Making sure permissions are properly set, of course. – dotVezz Dec 31 '13 at 18:56
  • 1
    I tried this, but the file is not executable on sdcard, i tried chmod +x, but it does nothing. I tried to copy binary to /data folder on phone, but got permission error. How can i run it ? – aseaudi Dec 31 '13 at 19:14
  • Can you [edit] your question and give us the output of ls -l [file]? You won't be able to move anything to /data/ because it's owned by root. – dotVezz Dec 31 '13 at 19:23
  • check my edit above – aseaudi Dec 31 '13 at 20:19
  • 2
    sdcard is probably (usually) FAT32/exFat, so it doesn't have Linux permission system, which is why you can't execute or chmod anything on it. I would try to find a "rw" partition using mount (e.g. /cache) and trying to execute from there. – Chahk Dec 31 '13 at 20:38
  • As mentioned above, I tried to copy to /data (mounted rw) but I still get permission denied error, same with /cache. – aseaudi Dec 31 '13 at 20:50
  • Try sh hello while in the same directory as your executable. That even works on SDcard. – Izzy Dec 31 '13 at 22:01
  • sh is for running shell scripts, right ? not for running compiled binaries, right ? Anyway, it gives me ""hello:1:Syntax error: word unexpected (expecting")")"", because it is expecting a shell text script file, not a binary. – aseaudi Dec 31 '13 at 22:11
  • Well, it might be a bit difficult to create a little shell script wrapper :) OK, so a little more detailed: echo "./hello" > hey && sh hey will do. IOW: first create a shell script that does nothing but execute the binary, and then let sh run that shell script. Sorry, thought that was clear – obviously I was a little cryptic ;) – Izzy Dec 31 '13 at 23:48
  • It gives me : "hey: ./hello: permission denied". – aseaudi Jan 01 '14 at 04:30
  • @Izzy, Even though sh can run the script, doesn't anything it runs in turn still have to have the proper permissions? – dotVezz Jan 01 '14 at 16:31
  • /data/local should be writable typically, even though other portions of /data are not. Try moving the binary there, marking it executable with chmod (if needed), and then running it. – eldarerathis Jan 03 '14 at 05:10
  • I tried to copy "cp hello /data/local", it gave me "cp:/data/local/hello: Permission denied". – aseaudi Jan 04 '14 at 13:13
  • @dotVezz Not necessarily. I didn't try it with a binary recently (so you might be right concerning that), but shell scripts do not need to be executable when passed as argument to sh. aseaudi: That error is strange. Can you do a ls -l /data/local (to see whether the file already exists, but is owned by a different user)? – Izzy Jan 06 '14 at 11:16
  • @Izzy, "ls -l /data/local" gives me "opendir failed, Permission denied" – aseaudi Jan 07 '14 at 00:41
  • My bad, /data/local is owned by system:system, and not world-readable. But unless a similar permission issue exists there as well, ls /data/local/hello should work. If it does, please also cross-check with id to see whether the user matches. – Izzy Jan 07 '14 at 08:00

2 Answers2

4

You can download termux, copy your binary to the termux home folder and run it

cp /storage/emulated/0/mybinary .
chmod +x mybinary
./mybinary

I haven't actually managed to run something because I don't have an android binary handy and a quick google doesn't bring up any android hello world binaries, but it should work.

qwazix
  • 141
  • 3
  • A hello world binary on android isn't any different than on any other system. Try this one, https://github.com/leachim6/hello-world/blob/master/c/c.c – oxr463 Mar 10 '19 at 23:05
  • 1
    Tried this with Android Terminal Emulator and it works! – TJJ Mar 26 '19 at 23:07
1

Removable media, as well as several other (user writable) partitions are mounted NOEXEC, meaning you can't execute binaries on these partitiions. See the output of 'mount' for details. Modern android builds generally prevent you from copying binaries to partitions mounted EXEC as a security precaution.

Short of rooting your device, you can't run this binary.

buggerer
  • 19
  • 1
  • This one's correct. – iBug Feb 28 '17 at 03:24
  • is it possible to overcome this? for example by requesting a permission to write in a exec zone? – Daniel May 06 '20 at 12:42
  • 1
    You need to pack the binary into an app now, and execute it via the app (possibly with NDK?). Or if you're lucky, you can copy it to /data/local/tmp/ then chmod +x it, and execute it from there. – Mark K Cowan Mar 13 '21 at 21:54
  • mount | grep -v noexec to find which partiotions are not noexec so that's possible to run binary files, or maybe better way mount | grep -v noexec | grep ext4 – a55 Mar 12 '23 at 16:29