7

My phone is a Galaxy S3, not rooted. I have "ES File Explorer" and "Terminal Emulator" installed.

I downloaded an Android build of 'wget', intending to execute it from the Terminal Emulator app. The file currently exists in /storage/sdcard0/Download (which is an internal partition, not a physical SD card) as:

-rw-rw-r-- root sdcard_rw 241974 2016-03-11 wget

./wget gives Permission denied. chmod o+x wget gives "Bad mode"; this is apparently because Android's chmod only accepts octal digits; chmod 665 wget gives "Operation not permitted". I can copy the file with "cp", so the filesystem is writable.

Other Q&A's here give several possible reasons for chmod failing on Android. One is SD cards formatted with FAT which do not support permission bits; but as I say this is not actually an SD card. Another is the filesystem being mounted with "noexec". My Android/Linux knowledge is way too lacking to check such things.

Is Android blocking me from executing wget by design? Is there a way to execute it without rooting my phone?

4
  • Use the sh command before invoking your script. See here: Android Enthusiasts Shell Script Permissions Mar 11, 2016 at 8:55
  • 1
    @Chungzuwalla : If you want the wget command without rooting, you might be intersted by this busybox version of wget. Mar 11, 2016 at 15:37
  • Thanks @AaronGillion, I tried using sh but since wget is a binary it just gives a syntax error. It seems what @MatijaNalis wrote below about this is correct. Mar 14, 2016 at 2:25
  • Copy it to the folder of your terminal application and then you can run it.
    – TJJ
    Mar 26, 2019 at 23:14

3 Answers 3

6

Yes, this is by design. You can't execute binaries from the SD card without system or superuser privileges (and remounting it without the noexec option) because otherwise apps could just download and execute malicious code at will. In older versions of Android the SD card also did not have emulated permissions so chmod would naturally fail.

Some devices/older versions of Android might allow execution from /data/local/tmp/, so you could try putting it there. Otherwise, you'll probably need root.

4
  • Thanks! I had expected that it was the app permissions system that controlled what an app could do, so downloading and executing code would be fine if it didn't require any privileges above what the app already has. Anyway I don't have /local at all, or at least I can't see it. I think the structure of UN*X filesystems is going to remain a mystery to me for a long while yet. Mar 14, 2016 at 2:31
  • Apps can execute their own binaries, though. Since u installed the terminal app, any binary of this app can be executed. Therefor, copy the binary into the app folder of the terminal app and you can run it from there. I tried it, it works.
    – TJJ
    Mar 26, 2019 at 23:17
  • @TJJ That's clever! Mar 27, 2019 at 15:17
  • But yes, to my understanding it will be limited to the permissions of the 'parent' app, i.e. the terminal app. It requests internet permission, so wget will work. More sophisticated stuff might not. Other option is to write your own apk with all the permissions needed (afaik Terminal Emulator is open source on github).
    – TJJ
    Mar 28, 2019 at 0:36
5

Android, just as Linux, prevents any user but root, to change the permission bits of any file not owned by such user. That said, chmod is ineffective, unless you have root privileges.

By the way, even if you had such privileges, you wouldn't have been able to change permissions, as long as the target path is /storage/sdcard0 or /storage/emulated/0. This depends upon a security policy, which is implemented in the form of the FUSE filesystem, and can be circumvented by moving to /data/media/0 or /data/media/emulated/0, and chmoding the file from this position. Please notice that this procedure will let you execute a script only if you launch it from one of the abovementioned locations.

Lastly, it should be noted that the Android port of chmod does not support symbolic editing of the flags (+rwx and so on), so you'll be forced to edit them via their corresponding octal values. You'll also do need root privileges, in order to be able to access /data/media. Trying to cd to such directory by using user privileges results in a Permission denied error.

2
  • I think this is where I get really confused. So when I downloaded the wget binary (using some browser, can't remember which one now) it was saved as owned by root. If I copy it with cp in Terminal Emulator, the copy is also owned by root. I can delete the copy. But the Terminal Emulator shell is not a root shell. There's plenty about this that confuses me. But that's a whole other question... Mar 14, 2016 at 3:20
  • @Chungzuwalla Copying a file is an operation that's generally allowed, provided that the file to copy has not been locked (like the /etc/shadow on Linux). Changing permissions, though, is a dangerous operation, since, if perpetrated by some untrusted user, can forbid the access to almost any file (chmod 000).
    – Grimoire
    Mar 14, 2016 at 14:48
0
  • first, to make file executable, you should do chmod 775 wget (and not 665)
  • but, you can only change modes of file if you are it its owner (or root). In your case, owner is root (and group sdcard_rw, as indicated by your ls -l), so only root can change permissions. As you are not root (you can check with id command, but as you say you haven't got a rooted device, so you can't use su to become root) you cannot change its permissions.
  • Also, modes can only be changed if filesystem containing file supports UN*X permissions. Your /storage/sdcard0/Download is probably FAT, which doesn't support it (you can check filesystem type with cat /proc/mounts)

Having said all that, if the file was shell script, you could execute it even with missing x mode with sh /storage/sdcard0/Download/script.

But if it is ELF binary (as it is the case with your wget), you are probably out of luck on Android (on desktop GNU/Linux system, you can execute ELF binaries indirectly via dynamic linker /lib64/ld-linux-x86-64.so.2 /mnt/Download/wget, but I don't think Android allow the same with its /system/bin/linker - I could be wrong on that one, though)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .