title: SELinux rules for i2c files in sysfs in Android
body: <p>I created kernel driver as loadable module for one of my I2C devices. The driver creates few sysfs file under I2C corresponding folder <code>(/sys/devices/i2c/i2c-0/0-0008/)</code> using instantiation through new_device file <code>(/sys/devices/i2c/i2c-0/new_device)</code>.</p>
  
  <p>Lollipop enforced SELinux so I need to create rules for my applications that need access to the device's sysfs file. Mostly these are system applications (they fall into definition of platform_app in the Android SELinux). Problem is that applications in any application domain are not allowed to write to sysfs files:</p>
  
  <pre><code>neverallow { appdomain -bluetooth -nfc }
      sysfs:dir_file_class_set write;
  </code></pre>
  
  <p>So I decided to create file context exclusively for my device:</p>
  
  <pre><code>file_context:
  
  /sys/devices/i2c-0/0-0008(/.*)?                     u:object_r:sysfs_mydeviceic:s0
  </code></pre>
  
  <p>The result is interesting: default driver files and folders like name and uevent etc. get the proper context but not the files created by the sysfs part of the I2C driver:</p>
  
  <pre><code>root@android:/sys/devices/i2c-0/0-0008 # ls -Z
  --w--w--w- root     root              u:object_r:sysfs:s0 data
  lrwxrwxrwx root     root              u:object_r:sysfs_mydeviceic:s0 driver -&gt; ../../../bus/i2c/drivers/mydevice
  -rw-rw-rw- root     root              u:object_r:sysfs:s0 locked
  -r--r--r-- root     root              u:object_r:sysfs_mydeviceic:s0 modalias
  -r--r--r-- root     root              u:object_r:sysfs_mydeviceic:s0 name
  drwxr-xr-x root     root              u:object_r:sysfs_mydeviceic:s0 power
  -rw-rw-rw- root     root              u:object_r:sysfs:s0 protection
  -rw-rw-rw- root     root              u:object_r:sysfs:s0 state
  lrwxrwxrwx root     root              u:object_r:sysfs_mydeviceic:s0 subsystem -&gt; ../../../bus/i2c
  -rw-r--r-- root     root              u:object_r:sysfs_mydeviceic:s0 uevent
  </code></pre>
  
  <p>I'm looking for help how to proceed with this problem: if I still want to convert sysfs context into sysfs_mydeviceic for the rest of the files, then how to do this? Or is there other way to enable writing to sysfs files by the applications?</p>