日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

android中SELINUX规则分析和语法简介

發(fā)布時間:2023/12/15 综合教程 50 生活家
生活随笔 收集整理的這篇文章主要介紹了 android中SELINUX规则分析和语法简介 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. SELINUX是可以理解為一種android上面的安全機(jī)制,是有美國國家安全局和一些公司設(shè)計的一個針對linux的安全加強(qiáng)系統(tǒng)
我們可以通過配置SELINUX的相關(guān)policy,來定制自己的手機(jī)的一些權(quán)限,比如,我們可以完全讓root用戶沒有任何的權(quán)限和user一樣
2. 在android里面,有兩個類型,一種是文件,一種是進(jìn)程。
針對這兩種類型,我們可以先來看看他們的不同。
在android上面,adb shell之后進(jìn)入手機(jī),ps -Z可以查看當(dāng)前進(jìn)程所擁有的selinux的權(quán)限。

舉例:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

LABEL USER PID PPID NAME
u:r:init:s0 root 1 0 /init
u:r:kernel:s0 root 2 0 kthreadd
...
u:r:kernel:s0 root 258 2 irq/322-HPH_R O
u:r:logd:s0 logd 259 1 /system/bin/logd
u:r:healthd:s0 root 260 1 /sbin/healthd
u:r:lmkd:s0 root 261 1 /system/bin/lmkd
u:r:servicemanager:s0 system 262 1 /system/bin/servicemanager
u:r:vold:s0 root 263 1 /system/bin/vold
u:r:surfaceflinger:s0 system 264 1 /system/bin/surfaceflinger
u:r:tctd:s0 root 265 1 /system/bin/tctd
u:r:rfs_access:s0 system 268 1 /system/bin/rfs_access
u:r:tee:s0 system 271 1 /system/bin/qseecomd
u:r:kernel:s0 root 280 2 kworker/3:1H
u:r:kernel:s0 root 290 2 kauditd
u:r:rmt_storage:s0 nobody 291 1 /system/bin/rmt_storage
u:r:shell:s0 shell 292 1 /system/bin/sh
u:r:netd:s0 root 295 1 /system/bin/netd
u:r:debuggerd:s0 root 296 1 /system/bin/debuggerd
u:r:tee:s0 system 297 271 /system/bin/qseecomd

在這個例子中,我們可以進(jìn)行分析。
在android中,只定義了一個user即為u. 另外,如果是進(jìn)程的話,都會統(tǒng)一定義為r,如果是文件的話,會被定義為object_r. 第三個是這個進(jìn)程type,在andorid里面,定義了100多個type.按照目前我的理解,這個是進(jìn)程所屬的>類型。第四個是s0,這個是一個安全的等級。但是暫時還沒有接觸到配置這個的地方。

另外就是文件,文件想要查看相關(guān)SELINUX權(quán)限的話,需要去執(zhí)行l(wèi)s -Z

?

1
2
3
4
5
6
7
8

drwxr-x--x root sdcard_r u:object_r:rootfs:s0 storage
drwx--x--x root root u:object_r:tmpfs:s0 synthesis
dr-xr-xr-x root root u:object_r:sysfs:s0 sys
drwxr-xr-x root root u:object_r:system_file:s0 system
drwxrwxr-x system tctpersist u:object_r:tct_persist_file:s0 tctpersist
lrwxrwxrwx root root u:object_r:rootfs:s0 tombstones -> /data/tombstones
-rw-r--r-- root root u:object_r:rootfs:s0 ueventd.qcom.rc
-rw-r--r-- root root u:object_r:rootfs:s0 ueventd.rc

在這個例子中,結(jié)合上面的分析,我們知道了object_r是代表的文件,u是android的唯一的用戶,rootfs是這個文件所對應(yīng)的類型,s0是一個安全的等級限制。

3. 如何配置selinux
首先,按照Google的官方文檔:
需要linux內(nèi)核首先是支持selinux的,另外需要android的selinux的配置文件,也就是extern/sepolicy里面的內(nèi)容。
然后就是修改BoardConfig.mk
Google的nexus的sepolicy的支持就放在了device/lge/mako/sepolicy
首先會包含廠商定制的sepolicy的文件夾:BOARD_SEPOLICY_DIRS
然后將規(guī)則添加到了sepolicy中:BOARD_SEPOLICY_DIRS

這樣的話,我們編譯出來的image其實就是具有了selinux的功能。
其實如果沒有廠商定制的話,也是會編譯到external/sepolicy的,這樣的話,就是使用andriod所有默認(rèn)的sepolicy(It defines the domains and types for the AOSP services and apps common to all devices. )

然后理解了這個之后,我們可以看到其實很多的廠商也是有自己的配置規(guī)則在device/***/***/sepolicy下面的.

4. selinux的配置規(guī)則:
首先要了解sepolicy的結(jié)構(gòu):
a. App進(jìn)程 -> mac_permissions.xml
b. App數(shù)據(jù)文件 -> seapp_contexts
c. 系統(tǒng)文件 -> file_contexts
d. 系統(tǒng)屬性 -> property_contexts

在te文件中,我們一般遇到的語法是這樣的:
rule_name source_type target_type:class perm_set
解讀為: 為source_type設(shè)置一個rule_name的規(guī)則,規(guī)則是對target_type的class 進(jìn)行 perm_set的操作。

然后是一些特殊的配置文件:
a. external/sepolicy/attributes -> 所有定義的attributes都在這個文件
b. external/sepolicy/access_vectors -> 對應(yīng)了每一個class可以被允許執(zhí)行的命令
c. external/sepolicy/roles ->Android中只定義了一個role,名字就是r,將r和attribute domain關(guān)聯(lián)起來
d. external/sepolicy/users -> 其實是將user與roles進(jìn)行了關(guān)聯(lián),設(shè)置了user的安全級別,s0為最低級是默認(rèn)的級別,mls_systemHigh是最高的級別
e. external/sepolicy/security_classes -> 指的是上文命令中的class,個人認(rèn)為這個class的內(nèi)容是指在android運(yùn)行過程中,程序或者系統(tǒng)可能用到的操作的模塊
f. external/sepolicy/te_macros -> 系統(tǒng)定義的宏全在te_macros文件
g. external/sepolicy/***.te -> 一些配置的文件,包含了各種運(yùn)行的規(guī)則

另外,selinux有兩種工作模式:
“permissive”:所有操作都被允許(即沒有MAC),但是如果有違反權(quán)限的話,會記錄日志
“enforcing”:所有操作都會進(jìn)行權(quán)限檢查

最后,type的命令如下:
type type_id [alias alias_id,] [attribute_id] # 將type_id(別名為alias)關(guān)聯(lián)到attribute. 這樣的話,方便用attribute來管理不同的type中包含相同的屬性的部分。

class命令的格式為:
class class_name [ inherits common_name ] { permission_name ... }
inherits表示繼承了common定義的權(quán)限,然后自己額外實現(xiàn)了permission_name的權(quán)限

在te文件中常見的四種命名的規(guī)則:
allow:賦予某項權(quán)限。
allowaudit:audit含義就是記錄某項操作。默認(rèn)情況下是SELinux只記錄那些權(quán)限檢查失敗的操作。allowaudit則使得權(quán)限檢查成功的操作也被記錄。注意,allowaudit只是允許記錄,它和賦予權(quán)限沒關(guān)系。賦予權(quán)限必須且只能使
用allow語句。
dontaudit:對那些權(quán)限檢查失敗的操作不做記錄。
neverallow:前面講過,用來檢查安全策略文件中是否有違反該項規(guī)則的allow語句。如例子5所示:

舉例:

?

1

type init, domain;

將init關(guān)聯(lián)到domain,即將domain設(shè)置為init類型的屬性

?

1

allow init unlabeled:filesystem mount;

允許init類型對unlabeled類型的filesystem進(jìn)行mount的操作

?

1

allow init fotad:unix_stream_socket { bind create };

允許init類型對fotad類型的unix_stream_socket 進(jìn)行bind和create的操作

?

1
2

allow appdomain anr_data_file:dir search;
allow appdomain anr_data_file:file { open append };

首先appdomain是定義在te_macros里面的一個宏,很多的app規(guī)則會使用類似app_domain(shell)的命令將其添加進(jìn)去
這兩句話的意思是:1. 允許app去對anr_data_file類型的目錄進(jìn)行查找的操作
2. 允許app對anr_data_file類型的file進(jìn)行打開和添加操作 其實就是規(guī)定了出現(xiàn)anr時候,app往/data/anr/里面寫入的權(quán)限限制

?

1

neverallow { appdomain -unconfineddomain } kmem_device:chr_file { read write };

絕對不允許app(除了有unconfineddomain屬性的app)對kmem_device類型的字符設(shè)備進(jìn)行讀寫的操作

?

1

neverallow { appdomain -unconfineddomain } self:capability2 *;

絕對不允許除了unconfineddomain以外的app對self類型的capability2進(jìn)行任何的操作

?

1

type httpd_user_content_t, file_type, httpdcontent;

聲明一個httpd_user_content_t的類型,具有file_type和httpdcontent的屬性

?

1
2

type httpd_user_content_t;
typeattribute httpd_user_content_t file_type, httpdcontent;

聲明一個httpd_user_content_t的類型
定義httpd_user_content_t具有file_type, httpdcontent的屬性

?

1

allow appdomain self:rawip_socket create_socket_perms;

所有可以設(shè)置類型的地方其實都可以設(shè)置為屬性。
比如這個例子,我們允許所有具有app屬性的內(nèi)容可以去對self屬性的rawip_socket進(jìn)行create的操作

?

1

allow {user_t domain} {bin_t file_type sbin_t}:file execute ;

允許user_t和domain屬性的類對bin_t, file_type, sbin_t類型的file進(jìn)行可執(zhí)行的操作

?

1
2

allow user_t user_t:process signal;
allow user_t self:process signal;

這兩條語句的表述其實是一致的,其實self指的是目標(biāo)的類型和發(fā)起人的類型是一致的
所以不能聲明一個類型或者屬性叫做self

?

1

allow user_t bin_t:file ~{ write setattr ioctl };

允許user_t對bin_t類型的file進(jìn)行除了write setattr ioctl相關(guān)的操作

?

1

type_transition system wifi_data_file:sock_file system_wpa_socket;

當(dāng)一個類型為system的類別去進(jìn)行wifi_data_file類型的sock_file訪問時,類型默認(rèn)切換到system_wpa_socket

如果下面這條語句想要執(zhí)行成功
type_transition init_t apache_exec_t:process apache_t;
至少首先聲明下面的三條規(guī)則:
allow init_t apache_exec_t:file execute;
allow init_t apache_t:process transition;
allow apache_t apache_exec_t:file entrypoint;

type_transition和type_change的語法規(guī)則是一樣的, type_change規(guī)則的影響不會在內(nèi)核中生效,而是依賴于用戶空間應(yīng)用程序,如login或sshd

Android SeLinux權(quán)限問題和解決方法

1. 確認(rèn) seLinux導(dǎo)致權(quán)限問題

1.1 標(biāo)志性log 格式:

avc: denied { 操作權(quán)限} for pid=7201comm=“進(jìn)程名” scontext=u:r:源類型:s0 tcontext=u:r:目標(biāo)類型:s0 tclass=訪問類別 permissive=0

1.2 舉例:

Kenel log:

avc: denied { execheap } for pid=7201 comm="com.baidu.input" scontext=u:r:untrusted_app:s0tcontext=u:r:untrusted_app:s0tclass=processpermissive=0

Logcat log:

com.baidu.input:
type=1400audit(0.0:29): avc: denied { execheap } for
scontext=u:r:untrusted_app:s0tcontext=u:r:untrusted_app:s0tclass=processpermissive=0

1.3 方法1adb在線修改

關(guān)閉 seLinux:

打開seLinux:

Enforcing:seLinux已經(jīng)打開;

Permissive:seLinux已經(jīng)關(guān)閉;

1.4 方法2: 從kernel中徹底關(guān)閉 (用于開機(jī)初始化時的seLinux權(quán)限問題,要重編bootimage)


修改LINUX/android/kernel/arch/arm64/configs/XXXdefconfig文件(找相應(yīng)config文件)
去掉CONFIG_SECURITY_SELINUX=y 的配置項

2. 在sepolicy中添加相應(yīng)權(quán)限

2.1 修改依據(jù):

log 信息:

avc: denied { 操作權(quán)限 } for pid=7201 comm=“進(jìn)程名” scontext=u:r:源類型:s0 tcontext=u:r:目標(biāo)類型:s0
tclass=訪問類別 permissive=0

2.2 修改步驟:

找相應(yīng)的“源類型.te ”文件


有兩個位置可能存在相應(yīng)的te文件:


位置一:LINUX/android/external/sepolicy
位置二:LINUX/android/device/qcom/sepolicy/common

2.3 按如下格式在該文件中添加:

allow 源類型 目標(biāo)類型:訪問類別 {權(quán)限};

2.4 舉例

Kernel Log:

avc: denied { execheap } for pid=7201
comm="com.baidu.input"
scontext=u:r:untrusted_app:s0tcontext=u:r:untrusted_app:s0tclass=processpermissive=0

修改:

在LINUX/android/external/sepolicy/untrusted_app.te 中添加:

allow untrusted_app untrusted_app:process { execheap };


備注:

在這個例子中,由于源類型和目標(biāo)類型都是untreated_app, 所以也可以寫成:

allow untrusted_app self:process { execheap };


3. 添加權(quán)限后的neverallowed沖突

3.1 編譯報錯:

libsepol.check_assertion_helper: neverallow on line xxx ofexternal/sepolicy/domain.te ……

3.2 原因:

新添加的sepolicy項目違反了domain.te 中規(guī)定的的總策略原則。所以該條權(quán)限策略不能添加,如果強(qiáng)行添加的話有CTS測試失敗的風(fēng)險。

3.3 解決方法:

1.從運(yùn)行l(wèi)og中找到要訪問的目標(biāo)名稱,一般是name字段后的名稱

avc: denied { read write } for pid=303 comm="mediaserver" name="tfa9890"dev="tmpfs" ino=3880 scontext=u:r:mediaserver:s0tcontext=u:object_r:device:s0tclass=chr_file permissive=0

2.找到相應(yīng)的*_contexts文件。

一般有file_contexts,genfs_contexts, property_contexts, service_contexts 等文件

3.在contexts文件中指定要訪問的目標(biāo)為一個“源類型 ”有權(quán)限訪問的“目標(biāo)類型”

如:在file_contexts中添加: /dev/tfa9890u:object_r:audio_device:s0

3.4 舉例

添加權(quán)限:

在mediaserver.te中添加allow mediaserver device:chr_file { read write open};


編譯報錯:

libsepol.check_assertion_helper:
neverallow on line 258 ofexternal/sepolicy/domain.te (or line 5252 of
policy.conf) violated byallow mediaserver device:chr_file { read write
open};


違反了domain.te 258的:

neverallow {domain –unconfineddomain –ueventd } device:chr_file { open read write}

運(yùn)行Log:

avc: denied { read write } for pid=303
comm="mediaserver"name="tfa9890" dev="tmpfs" ino=3880
scontext=u:r:mediaserver:s0 tcontext=u:object_r:device:s0tclass=chr_file
permissive=0


修改步驟:

1.目標(biāo)名稱是: tfa9890, 其在系統(tǒng)中的路徑是: /dev/tfa9890, 是audio相關(guān)的設(shè)備文件
2.源類型是mediaserver, 在mediaserver.te 文件中發(fā)現(xiàn)其具有 audio_device 目標(biāo)類型的權(quán)限
3.所以在file_contexts 中添加 “/dev/tfa9890 u:object_r:audio_device:s0” 可以解決問題0

userdebug_or_eng關(guān)鍵字可以限定版本,例如:

userdebug_or_eng(`
allow system_app sysfs_battery_supply:dir search;
allow system_app sysfs_battery_supply:file {read open getattr};
allow system_app sysfs_usb_supply:dir search;
allow system_app sysfs_usb_supply:file {read open getattr};
allow system_app sysfs:file {read write open getattr};
allow system_app sysfs_leds:dir search;
allow system_app sysfs_leds:file write;
allow system_app system_data_file:file {write create setattr};
allow system_app system_data_file:dir {write add_name};
allow system_app vendor_audioftm_exec:file {getattr execute read open execute_no_trans};
allow system_app vendor_default_prop:file {read open getattr};
allow system_app proc_asound:dir search;
allow system_app proc_audiod:file {read write open getattr execute};
allow system_app audio_device:chr_file { read write open ioctl };
allow system_app audio_device:dir { search };
allow system_app proc:file { read open getattr };
allow system_app vendor_file:file {read open};
allow system_app sysfs_graphics:file rw_file_perms;
allow system_app sysfs_graphics:dir search;
allow system_app vendor_gles_data_file:dir search;
allow system_app vendor_camera_prop:file { read open getattr };
')

userdebug_or_eng(`permissive ft_diag;') //打開ft_diag在userdebug或者eng版本上的所有關(guān)于se_Android的權(quán)限

內(nèi)置一個腳本,調(diào)用該腳本時不成功,需要在file_contexts中定義該腳本的類型,然后在其他程序的te文件中加入對該腳本的執(zhí)行權(quán)限。

1. device/xxx/common/sepolicy/file_contexts 添加

/system/bin/preinstall.sh u:object_r:preinstall_exec:s0

要添加該腳本對其他的權(quán)限

2.創(chuàng)建device/amlogic/common/sepolicy/preinstall.te
type preinstall, domain;
type preinstall_exec, exec_type, file_type;
3.init.rc添加腳本運(yùn)行服務(wù)
service preinstall /system/bin/preinstall.sh
user root
group root
disabled
oneshot
seclabel u:r:preinstall:s0

解決seAndroid權(quán)限,可以適用audit2allow工具

作用:可以根據(jù)avc錯誤日志自動生成需要添加te權(quán)限
目錄:external/selinux/prebuilts/bin/
適用條件:依賴libselinux.so庫,所以需要代碼全編譯完成才可以用
適用方法:將avc錯誤日志拷貝到文本文檔中,執(zhí)行命令:audit2allow –i test.txt >test.te

分析過程:

缺少什么權(quán)限: { write }權(quán)限,
誰缺少權(quán)限: scontext=u:r:kernel:s0,
對哪個文件缺少權(quán)限:tcontext=u:object_r:block_device
什么類型的文件: tclass=blk_file
解決方法:kernel.te

allow kernel block_device:blk_file write;

對于類型文件可以在system/sepolicy/public/global_macros來查看

.text-only, .text-card-text { white-space: pre }
.rich-text-paragraph { min-height: 15px }

總結(jié)

以上是生活随笔為你收集整理的android中SELINUX规则分析和语法简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。