Discussion:
[android-porting] Permission issue on sysfs access from android application ( .java source code )
himanshu
2017-04-03 15:00:20 UTC
Permalink
Android application in java having permission issues when accessing sysfs
entry.

try{
FileOutputStream fos = new FileOutputStream("/sys/class/shift_reg/shift_reg/value");
byte mybyte = 1;
fos.write(mybyte);
fos.close();} catch ( Exception e) {
Log.d(TAG, "Failed in writing to Shift Register"); <------- I am always getting this exception.}


1) Have tried putting 'setenforce 0'. Does't work. Even though enforce
level is permissive.

2) Do I have to access using JNI way: ( android-app -> jni -> driver )

3) I have set file permission as 0777 for the sysfs node file.


Dmesg Error message:

[ 417.176302] type=1400 audit(418.589:63): avc: denied { write } for
pid=3164 comm="com.android.cam" name="value" dev="sysfs" ino=9749
scontext=u:r:untrusted_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file
permissive=1

ps -Z

u:r:untrusted_app:s0 u0_a67 4360 333
com.android.cam
--
--
unsubscribe: android-porting+***@googlegroups.com
website: http://groups.google.com/group/android-porting

---
You received this message because you are subscribed to the Google Groups "android-porting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-porting+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
M***@Cirrus.com
2018-03-26 10:15:25 UTC
Permalink
Android does not allow apps to access the hardware directly.
The error you see is SE Linux filtering out the access since the sys-fs you
are trying to access is not allowed to access.
You will need to modify the SE Linux policy for the device you are using
and ensure it allows access to below sys-fs.
Post by himanshu
Android application in java having permission issues when accessing sysfs
entry.
try{
FileOutputStream fos = new FileOutputStream("/sys/class/shift_reg/shift_reg/value");
byte mybyte = 1;
fos.write(mybyte);
fos.close();} catch ( Exception e) {
Log.d(TAG, "Failed in writing to Shift Register"); <------- I am always getting this exception.}
1) Have tried putting 'setenforce 0'. Does't work. Even though enforce
level is permissive.
2) Do I have to access using JNI way: ( android-app -> jni -> driver )
3) I have set file permission as 0777 for the sysfs node file.
[ 417.176302] type=1400 audit(418.589:63): avc: denied { write } for
pid=3164 comm="com.android.cam" name="value" dev="sysfs" ino=9749
scontext=u:r:untrusted_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file
permissive=1
ps -Z
u:r:untrusted_app:s0 u0_a67 4360 333
com.android.cam
--
--
unsubscribe: android-porting+***@googlegroups.com
website: http://groups.google.com/group/android-porting

---
You received this message because you are subscribed to the Google Groups "android-porting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-porting+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Renjith Rajagopal
2018-05-20 07:42:46 UTC
Permalink
You need .te file for your module even though it is permissive mode.
Also try move app to /system/priv-app/ which will remove untrusted_app erro
Post by himanshu
Android application in java having permission issues when accessing sysfs
entry.
try{
FileOutputStream fos = new FileOutputStream("/sys/class/shift_reg/shift_reg/value");
byte mybyte = 1;
fos.write(mybyte);
fos.close();} catch ( Exception e) {
Log.d(TAG, "Failed in writing to Shift Register"); <------- I am always getting this exception.}
1) Have tried putting 'setenforce 0'. Does't work. Even though enforce
level is permissive.
2) Do I have to access using JNI way: ( android-app -> jni -> driver )
3) I have set file permission as 0777 for the sysfs node file.
[ 417.176302] type=1400 audit(418.589:63): avc: denied { write } for
pid=3164 comm="com.android.cam" name="value" dev="sysfs" ino=9749
scontext=u:r:untrusted_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file
permissive=1
ps -Z
u:r:untrusted_app:s0 u0_a67 4360 333
com.android.cam
--
--
unsubscribe: android-porting+***@googlegroups.com
website: http://groups.google.com/group/android-porting

---
You received this message because you are subscribed to the Google Groups "android-porting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-porting+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
r***@gmail.com
2018-05-23 00:42:37 UTC
Permalink
This post might be inappropriate. Click to display it.
Sooraj Sizon
2018-05-24 06:18:29 UTC
Permalink
Hello all,

Here is how i was able to access , read , write to sysfs from an android
application .

Ramdisk / device specific changes

#init.{hardware}.rc
on property:sys.boot_completed=1
#UART5 on my device
chown root root /dev/ttymxc4
chmod 0777 /dev/ttymxc4
#GPIO5_IO14 (gpio_bank - 1) * 32 + gpio_bitwrite /sys/class/gpio/export
142
write /sys/class/gpio/gpio142/edge "both"
#ueventd.{hardware}.rc
+/dev/ttymxc* 0666 system system
Then using JNI i was able to access uart and gpio pin .
Here are both application source code
UART: https://github.com/sooorajjj/BluemoonUART
<https://www.google.com/url?q=https://github.com/sooorajjj/BluemoonUART&sa=D&source=hangouts&ust=1527226851456000&usg=AFQjCNGparXoQ2yPbiFH74V9DUo9uXzIVA>
GPIO: https://github.com/sooorajjj/BluemoonGPIO
Note these application were not created by me , its available as example on
google i just had to make a few changes for it to work on my device .

GPIO led blink example and reading input from button is what i have tested
and works.
UART reading and writing using serial console works .
Android application in java having permission issues when accessing sysfs
entry.
try{
FileOutputStream fos = new FileOutputStream("/sys/class/shift_reg/shift_reg/value");
byte mybyte = 1;
fos.write(mybyte);
fos.close();} catch ( Exception e) {
Log.d(TAG, "Failed in writing to Shift Register"); <------- I am always getting this exception.}
1) Have tried putting 'setenforce 0'. Does't work. Even though enforce
level is permissive.
2) Do I have to access using JNI way: ( android-app -> jni -> driver )
3) I have set file permission as 0777 for the sysfs node file.
[ 417.176302] type=1400 audit(418.589:63): avc: denied { write } for
pid=3164 comm="com.android.cam" name="value" dev="sysfs" ino=9749
scontext=u:r:untrusted_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file
permissive=1
ps -Z
u:r:untrusted_app:s0 u0_a67 4360 333
com.android.cam
--
--
unsubscribe: android-porting+***@googlegroups.com
website: http://groups.google.com/group/android-porting

---
You received this message because you are subscribed to the Google Groups "android-porting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-porting+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
r***@gmail.com
2018-05-24 17:28:18 UTC
Permalink
Hi Sooraj. Thanks for sharing your codes.

I think your code just need the uevent.{hardware}.rc part:

/dev/ttymxc* 0666 root root

which its the same as:

adb shell
su
chown root:root /dev/ttyXXX
chmod +rw /dev/ttyXXX

except that the .rc part is permanent after each reboot.
So the init.{hardware}.rc part is not necessary. Besides, your chown is
wrong, it should be with "root:root" instead of "root root".

Anyway, the problem persist if you don't put "setenforce 0" as you
mentioned me in your gtalk msg: "Though i did set selinux to permissive for
testing".

Thanks for the example apps too.

Have a nice day all.

Rolando.
--
--
unsubscribe: android-porting+***@googlegroups.com
website: http://groups.google.com/group/android-porting

---
You received this message because you are subscribed to the Google Groups "android-porting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-porting+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...