Search code examples
androidshelladb

Android shell user has no access to /data/local/tmp even though it is the owner of that folder


Some background info: My phone is on android 9 and is rooted.

I am trying to build a flutter android app. When I execute flutter run, it fails with this error:

❯ flutter run
adb shell failed to write the SHA hash: ProcessException: Process "/home/wscp/Android/Sdk/platform-tools/adb" exited abnormally:
/system/bin/sh: can't create /data/local/tmp/sky.com.example.test_app.sha1: Permission denied
  Command: /home/wscp/Android/Sdk/platform-tools/adb -s da0d543 shell echo -n 75cf727e0e3b43efd656faf8cb2e4c779755b261 >
  /data/local/tmp/sky.com.example.test_app.sha1.
Error launching application on Redmi Note 7.

When I try running adb shell independently, I find that I cannot access /data/local/tmp.

I was able to perform the following actions since I have rooted my phone:

❯ adb shell   
lavender:/ $ whoami
shell                                                                                                                                         
lavender:/ $ ls -la /data/local/tmp
ls: /data/local/tmp: Permission denied
1|lavender:/ $ su
lavender:/ # ls -la /data/local/tmp
total 24
drwxrwxrwx 2 shell shell 4096 2019-10-25 10:23 .             <====== !!!
drwxr-x--x 4 root  root  4096 2019-10-12 16:29 ..
-rw-rw-rw- 1 root  root  9726 2019-10-12 16:29 build.prop
-rw-rw-rw- 1 root  root   968 2019-10-12 16:29 cmdline

The directory /data/local/tmp seems to belong to shell but shell itself is not able to access that directory!

I also attempted to use the scrcpy tool, which I have successfully used on another device of mine, and it resulted in a similar error:

❯ scrcpy
INFO: scrcpy 1.11 <https://github.com/Genymobile/scrcpy>
adb: error: failed to copy '/usr/local/share/scrcpy/scrcpy-server' to '/data/local/tmp/scrcpy-server': remote couldn't create file: Permission denied
/usr/local/share/scrcpy/scrcpy-server: 0 files pushed. 2.0 MB/s (25454 bytes in 0.012s)
ERROR: "adb push" returned with value 1

Why does this happen, and is there a fix?


Solution

  • It turns out to a selinux issue. I have having a similar problem with flutter and scrcpy. So, I went around online finding a solution and ended up on this gist, https://gist.github.com/AwaisKing/009a64d92d476da33036efd61c961a7a

    The trick is to rename the selinux context from system_data_file to shell_data_file, You will need a rooted android device for it. The exact command that worked for me was,

    chcon -R u:object_r:shell_data_file:s0 /data/local/tmp