Android Enthusiasts Stack Exchange is a question and answer site for enthusiasts and power users of the Android operating system. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Use the sh command before invoking your script. See here: Android Enthusiasts Shell Script Permissions – Aaron Gillion Mar 11 at 8:55
1  
@Chungzuwalla : If you want the wget command without rooting, you might be intersted by this busybox version of wget. – user2284570 Mar 11 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. – Chungzuwalla Mar 14 at 2:25
up vote 4 down vote accepted

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.

share|improve this answer
    
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. – Chungzuwalla Mar 14 at 2:31

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.

share|improve this answer
    
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... – Chungzuwalla Mar 14 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). – Death Mask Salesman Mar 14 at 14:48
  • 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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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