My device is running Android 8.0, rooted using Magisk as the su manager. I log into it using SimpleSSHD.

I'm trying to do some actions which require root access, and in the process I'm creating some files to be later used by normal users. But for some reason what I'm used to in "classical" Linux distributions doesn't appear to work: root-created files can't be read by normal users, whatever chown/chgrp/chmod actions I try. See the following session:

$ su -c "echo hello > file.txt; chown $(whoami) file.txt; chgrp $(whoami) file.txt; chmod a+r file.txt"
$ cat file.txt
cat: file.txt: Permission denied
$ echo readable > my.txt
$ su -c 'cp -a my.txt my-copy.txt'
$ cat my-copy.txt
cat: my-copy.txt: Permission denied
$ ls -l *.txt
-rw-r--r-- 1 u0_a182 u0_a182 6 2018-08-20 15:21 file.txt
-rw------- 1 u0_a182 u0_a182 9 2018-08-20 15:23 my-copy.txt
-rw------- 1 u0_a182 u0_a182 9 2018-08-20 15:22 my.txt
$ whoami
u0_a182

What's happening here? Why are permission bits, owner and group ignored for files I created as root? How to properly change owner to normal user?

  • Could you please describe how did you set up SSH server on Android phone? Я тоже хочу) – Suncatcher Aug 20 at 13:47
  • 1
    @Suncatcher simply installed SimpleSSHD, and its config is trivial (works out of the box, just be sure to connect via port 2222 instead of the usual 22, and then enter the password it prints in its activity). – Ruslan Aug 20 at 14:56
  • Termux comes with openssh package, sshd works fine with limited options. Or use a statically built sshd binary. That works just as on Linux. Port 22 can also be used if you have root – Irfan Latif Aug 23 at 6:36
up vote 1 down vote accepted

Android uses SELinux to manage file access. The files created by root and non-root users differ by SELinux context:

$ ls -lZ *.txt
-rw-r--r-- 1 u0_a182 u0_a182 u:object_r:app_data_file:s0           6 2018-08-20 16:01 file.txt
-rw------- 1 u0_a182 u0_a182 u:object_r:app_data_file:s0           9 2018-08-20 16:01 my-copy.txt
-rw------- 1 u0_a182 u0_a182 u:object_r:app_data_file:s0:c512,c768 9 2018-08-20 16:01 my.txt

To make the file really belong to the non-root user, one has to set the correct SELinux context:

$ su -c 'chcon u:object_r:app_data_file:s0:c512,c768 file.txt'
$ cat file.txt
hello

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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