天天看點

Android 5.x SEAndroid/SElinux核心節點的讀寫權限

Android 5.0下,因為采取了SEAndroid/SElinux的安全機制,即使擁有root權限,或者對某核心節點設定為777的權限,仍然無法在JNI層通路。 本文将以使用者自定義的核心節點 /dev/wf_bt為例,手把手教會讀者如何在JNI層獲得對該節點的通路權限。

第一步:找到需要通路該核心節點的程序(process),筆者自己這個節點由system_server程序來通路

第二步:打開檔案AndroidL/android/external/sepolicy/file_contexts.be 仿照這個檔案裡的寫法,為你的節點定義一個你想要的名字:

/dev/tegra.* u:object_r:video_device:s0
/dev/tf_driver u:object_r:tee_device:s0
/dev/tty u:object_r:owntty_device:s0
/dev/tty[0-9]* u:object_r:tty_device:s0
# We add here
/dev/wf_bt              u:object_r:wf_bt_device:s0
           

wf_bt_device是自定義,其他左右兩邊的内容都和上面的範例一緻。

第三步:打開檔案AndroidL/android/external/sepolicy/device.te 仿照這個檔案裡的寫法,将剛剛第二步寫的wf_bt_device聲明為dev_type:

# Device types
type device, dev_type, fs_type;
type alarm_device, dev_type, mlstrustedobject;
type adb_device, dev_type;
type ashmem_device, dev_type, mlstrustedobject;
type audio_device, dev_type;
type binder_device, dev_type, mlstrustedobject;
type block_device, dev_type;
# We add here
type wf_bt_device, dev_type;
           

第四步: AndroidL/android/external/sepolicy/目錄下很多.te檔案都是以程序名來結尾的,比如有針對surfaceflinger程序的surfaceflinger,有針對vold程序的vold.te, 剛剛從第一步得到,這個節點是由system_server程序來通路,是以,我們找到system_server.te打開,加入允許這個程序對/dev/wf_bt的讀寫權限,

# Read/Write to /proc/net/xt_qtaguid/ctrl and and /dev/xt_qtaguid.
allow system_server qtaguid_proc:file rw_file_perms;
allow system_server qtaguid_device:chr_file rw_file_perms;

# chr_file表示字元裝置檔案,如果是普通檔案用file,目錄請用dir
# rw_file_perms代表讀寫權限
allow system_server wf_bt_device:chr_file rw_file_perms;
           

這句話的意思是:允許system_server程序擁有對wf_bt_device的這個字元裝置的讀寫權限。 改了這些之後,你就可以make installclean;make -j16編譯image來驗證權限是否擷取成功。

fd =open("/dev/wf_bt",O_RDONLY | O_NOCTTY); 絕對成功!!!!!

鳴謝:感謝Joly_xie

繼續閱讀