修改sepolicy編譯
1. 如何設置SElinux 策略規則
[SELinux Policy]如何設置SELinux策略規則?
[Description]
android KK 4.4 版本後,Google 默認啟用了SELinux, 並會把SELinux 審查異常列印在kernel log
或者 android log(L 版本)中,對應的關鍵字是: "avc: denied" 或者"avc: denied"
如一行LOG:
<5>[ 17.285600].(0)[503:idmap]type=1400 audit(1356999072.320:202): avc: denied { create
} for pid=503 comm="idmap" name="overlays.list" scontext=u:r:zygote:s0
tcontext=ubject_r:resource_cache_data_file:s0 tclass=file
即表明idmap 這個process, 使用zygote 的source context, 訪問/data/resource_cache 目錄,並
創建文件時,被SELinux 拒絕訪問。
[Keyword]
android, SELinux, avc: denied, audit
[Solution]
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們
直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc:
denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認
當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
首先, 務必確認對應進程訪問系統資源是否正常, 是否有必要 ?如果本身是異常非法訪問,那麼
就要自行消除訪問。
其次, 如果確認訪問是必要,並且正常的,那麼就要對對應的process/domain 增加新的policy.
1). 簡化方法
1.1 提取所有的avc LOG. 如 adb shell "cat /proc/kmsg | grep avc" > avc_log.txt
1.2 使用 audit2allow tool 直接生成policy. audit2allow -i avc_log.txt 即可自動輸出生成的
policy
1.3 將對應的policy 添加到selinux policy 規則中,對應MTK Solution, 您可以將它們添加在KK:
mediatek/custom/common/sepolicy, L: device/mediatek/common/sepolicy 下面,如
allow zygote resource_cache_data_file:dir rw_dir_perms;
allow zygote resource_cache_data_file:file create_file_perms;
===> mediatek/custom/common/sepolicy/zygote.te (KK)
===> device/mediatek/common/sepolicy/zygote.te (L)
注意audit2allow 它自動機械的幫您將LOG 轉換成policy, 而無法知道你操作的真實意圖,有可能
出現許可權放大問題,經常出現policy 無法編譯通過的情況。
2). 按需確認方法
此方法需要工程人員對SELinux 基本原理,以及SELinux Policy Language 有了解.
2.1 確認是哪個進程訪問哪個資源,具體需要哪些訪問許可權,read ? write ? exec ? create ?
search ?
2.2 當前進程是否已經創建了policy 文件? 通常是process 的執行檔.te,如果沒有,並且它的父
進程即source context 無須訪問對應的資源,則創建新的te 文件.
在L 版本上, Google 要求維護關鍵 security context 的唯一性, 比如嚴禁zygote, netd,
installd, vold, ueventd 等關鍵process 與其它process 共享同一個security context.
2.3 創建文件後,關聯它的執行檔,在file_contexts 中, 關聯相關的執行檔.
比如 /system/bin/idmap 則是 /system/bin/idmap ubject_r:idmap_exec:s0
2.4 填寫policy 到相關的te 文件中
如果沿用原來父進程的te 文件,則直接添加.
如果是新的文件,那麼首先:
#==============================================
# Type Declaration
#==============================================
type idmap, domain;
type idmap_exec, exec_type,file_type;
#==============================================
# Android Policy Rule
#==============================================
#permissive idmap;
domain_auto_trans(zygote, idmap_exec, idmap);
然後添加新的policy
# new policy
allow idmap resource_cache_data_file:dir rw_dir_perms;
allow idmap resource_cache_data_file:file create_file_perms;
3). 許可權放大情況處理
如果直接按照avc: denied 的LOG 轉換出SELinux Policy, 往往會產生許可權放大問題. 比如因為要
訪問某個device, 在這個device 沒有細化SELinux Label 的情況下, 可能出現:
<7>[11281.586780] avc: denied { read write } for pid=1217 comm="mediaserver"
name="tfa9897" dev="tmpfs" ino=4385 scontext=u:r:mediaserver:s0
tcontext=ubject_r:device:s0 tclass=chr_file permissive=0
如果直接按照此LOG 轉換出SELinux Policy: allow mediaserver device:chr_file {read write};
那麼就會放開mediaserver 讀寫所有device 的許可權. 而Google 為了防止這樣的情況, 使用了
neverallow 語句來約束, 這樣你編譯sepolicy 時就無法編譯通過.
為了規避這種許可權放大情況, 我們需要細化訪問目標(Object) 的SELinux Label, 做到按需申請.
通常會由三步構成
3.1 定義相關的SELinux type.
比如上述案例, 在 device/mediatek/common/sepolicy/device.te 添加
type tfa9897_device, dev_type;
3.2 綁定文件與SELinux type.
比如上述案例, 在 device/mediatek/common/sepolicy/file_contexts 添加
/dev/tfa9897(/.*)? ubject_r:tfa9897_device:s0
3.3 添加對應process/domain 的訪問許可權.
比如上述案例, 在 device/mediatek/common/sepolicy/mediaserver.te 添加
allow mediaserver tfa9897_device:chr_file { open read write };
那麼哪些訪問對象通常會遇到此類呢?(以L 版本為例)
* device
-- 類型定義: external/sepolicy/device.te;device/mediatek/common/sepolicy/device.te
-- 類型綁定:
external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts
* File 類型:
-- 類型定義: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te
-- 綁定類型:
external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts
* 虛擬File 類型:
-- 類型定義: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te
-- 綁定類型:
external/sepolicy/genfs_contexts;device/mediatek/common/sepolicy/genfs_contexts
* Service 類型:
-- 類型定義: external/sepolicy/service.te; device/mediatek/common/sepolicy/service.te
-- 綁定類型
:external/sepolicyservice_contexts;device/mediatek/common/sepolicy/service_contexts
* Property 類型:
-- 類型定義: external/sepolicy/property.te;device/mediatek/common/sepolicy/property.te
-- 綁定類型:
external/sepolicy/property_contexts;device/mediatek/common/sepolicy/property_contexts;
通常我們強烈反對更新google default 的policy, 大家可以更新mediatek 下面的相關的policy.
2. 我有一個delphi的源碼,編譯的時候有錯:找不到se_controls.pas
用Delphi編譯的時候,每一個單元都對應一個pas文件,所以你找不到這個pas文件,只能說明幾種情況,你的編譯文件夾下缺失了這個單元文件,或者你在重新編譯另存的時候沒有把這個文件保存過來,你還是找找你這個源碼的來源地,然後把pas文件拷過來,要是沒有pas文件,你就把相應的這部分的代碼源碼保存並命名為se_controls.pas;要是都沒有的話,要麼這個Delphi源碼你放棄好了;要麼在主文件裡面的代碼去掉USE這個單元的代碼。在編譯的主文件中你創建一個新的單元,裡面代碼就會增加一句use unit1,Unit2。。。。。。之類的,去掉那個,還有其他單元裡面有引用到這個單元的代碼也要去掉,不過很麻煩就是了。建議你另找尋新源代碼。
3. seandroid policy update是什麼軟體
第一步:找到需要訪問該內核節點的進程(process),筆者自己這個節點由system_server進程來訪問第二步:打開文件AndroidL/android/external/sepolicy/file_contexts.be仿照這個文件里的寫法,為這個定義一個自己想要的名字:/dev/tegra.*u:object_r:video_device:s0/dev/tf_driveru:object_r:tee_device:s0/dev/ttyu:object_r:owntty_device:s0/dev/tty[0-9]*u:object_r:tty_device:s0/dev/ttyS[0-9]*u:object_r:serial_device:s0/dev/wf_btu:object_r:wf_bt_device:s0wf_bt_device是自定義,其他左右兩邊的內容都和上面的範例一致。第三步:打開文件AndroidL/android/external/sepolicy/device.te仿照這個文件里的寫法,將剛剛第二步寫的wf_bt_device聲明為dev_type:#Devicetypestypedevice,dev_type,fs_type;typealarm_device,dev_type,mlstrustedobject;typeadb_device,dev_type;typeashmem_device,dev_type,mlstrustedobject;typeaudio_device,dev_type;typebinder_device,dev_type,mlstrustedobject;typeblock_device,dev_type;typecamera_device,dev_type;typewf_bt_device,dev_type;第四步:AndroidL/android/external/sepolicy/目錄下很多.te文件都是以進程名來結尾的,比如有針對surfaceflinger進程的surfaceflinger,有針對vold進程的vold.te,剛剛從第一步得到,這個節點是由system_server進程來訪問,所以,找到system_server.te打開,加入允許這個進程對/dev/wf_bt的讀寫許可權,#Read/Writeto/proc/net/xt_qtaguid/ctrlandand/dev/xt_qtaguid.allowsystem_serverqtaguid_proc:filerw_file_perms;allowsystem_serverqtaguid_device:chr_filerw_file_perms;#chr_file表示字元設備文件,如果是普通文件用file,目錄請用dir#rw_file_perms代表讀寫許可權allowsystem_serverwf_bt_device:chr_filerw_file_perms;這句話的意思是:允許system_server進程擁有對wf_bt_device的這個字元設備的讀寫許可權。改了這些之後,就可以makeinstallclean;make-j16編譯image來驗證許可權是否獲取成功。fd=open("/dev/wf_bt",O_RDONLY|O_NOCTTY);
4. 怎麼用android image kitchen解包,並且修改sepolicy文件,方便在MIUI中安裝root。
如果你要獲取root許可權。那何必這么麻煩呀。直接刷miui開發版就可以獲取自帶root許可權了。而且比第三方外root百分之百好。
5. android selinux怎麼修改
如果要恢復運行SELinux則可以運行
# setenforce
1
這條命令會把SELinux設定成Enforcing模式
2.把SELinux永久設定為Permissive模式
這里需要講一下Permissive和Enforcing模式的區別。
SELinux有三種模式:Enforcing, Permissive and Disable.
Enforcing模式就是應用SELinux所設定的Policy,
所有違反Policy的規則(Rules)都會被SELinux拒絕
Permissive和Enforcing的區別就在於它還是會遵循SELinux的Policy,但是對於違反規則的操作只會予以記錄而並不會拒絕操作
Disable 顧名思義就是完全禁用SELinux
如果要永久設定為Permissive模式,我們就要修改SELinux的配置文件
/etc/sysconfig/selinux (在RHEL5下這是一個symbolic link to /etc/selinux/conf)
# This file controls the state of SELinux on the
system.
# SELINUX= can take one of these three
values:
# enforcing - SELinux security policy is
enforced.
# permissive - SELinux prints warnings instead of
enforcing.
# disabled - SELinux is fully disabled.
SELINUX=enforcing
#
SELINUXTYPE= type of policy in use. Possible values are:
# targeted -
Only targeted network daemons are protected.
# strict -
Full SELinux protection.
SELINUXTYPE=targeted
修改SELINUX=permissive,然後重新啟動就可以了
6. 安卓代碼能不能實現關閉SElinux許可權
不可以
1.3 方法1:adb在線修改
關閉 seLinux:
打開seLinux:
Enforcing:seLinux已經打開;
Permissive:seLinux已經關閉;
1.4 方法2: 從kernel中徹底關閉 (用於開機初始化時的seLinux許可權問題,要重編bootimage)
修改LINUX/android/kernel/arch/arm64/configs/XXXdefconfig文件(找相應config文件)
去掉CONFIG_SECURITY_SELINUX=y 的配置項
2. 在sepolicy中添加相應許可權
2.1 修改依據:
log 信息:
avc: denied { 操作許可權 } for pid=7201 comm=「進程名」 scontext=u:r:源類型:s0 tcontext=u:r:目標類型:s0 tclass=訪問類別 permissive=0
2.2 修改步驟:
找相應的「源類型.te 」文件
有兩個位置可能存在相應的te文件:
位置一:LINUX/android/external/sepolicy
位置二:LINUX/android/device/qcom/sepolicy/common
2.3 按如下格式在該文件中添加:
allow 源類型 目標類型:訪問類別 {許可權};
2.4 舉例
Kernel Log:
avc: denied { execheap } for pid=7201 comm="com..input" scontext=u:r:untrusted_app:s0tcontext=u:r:untrusted_app:s0tclass=processpermissive=0
修改:
在LINUX/android/external/sepolicy/untrusted_app.te 中添加:
[java] view plain
<span style="font-size:24px;color:#009900;">allow untrusted_app untrusted_app:process { execheap };</span>
備注:
在這個例子中,由於源類型和目標類型都是untreated_app, 所以也可以寫成:
[java] view plain
<span style="font-size:24px;color:#009900;">allow untrusted_app self:process { execheap };</span>
3. 添加許可權後的neverallowed沖突
3.1 編譯報錯:
libsepol.check_assertion_helper: neverallow on line xxx ofexternal/sepolicy/domain.te ……
3.2 原因:
新添加的sepolicy項目違反了domain.te 中規定的的總策略原則。所以該條許可權策略不能添加,如果強行添加的話有CTS測試失敗的風險。
3.3 解決方法:
1.從運行log中找到要訪問的目標名稱,一般是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.找到相應的*_contexts文件。
一般有file_contexts, genfs_contexts, property_contexts, service_contexts 等文件
3.在contexts文件中指定要訪問的目標為一個「源類型 」有許可權訪問的「目標類型」
如:在file_contexts中添加: /dev/tfa9890 u:object_r:audio_device:s0
3.4 舉例
添加許可權:
在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}
運行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.目標名稱是: tfa9890, 其在系統中的路徑是: /dev/tfa9890, 是audio相關的設備文件
2.源類型是mediaserver, 在mediaserver.te 文件中發現其具有 audio_device 目標類型的許可權
3.所以在file_contexts 中添加 「/dev/tfa9890 u:object_r:audio_device:s0」 可以解決問題
7. 我想修改frameworks中的android.policy.jar文件,用apktool和smali反編譯出來的都是*.smali文件
apktool d -d xxx.apk
用-d選項,同時使用1.3.2版本的apktool
8. 如何用ModelSim se完全編譯Xilinx庫文件
Modlesim 模擬庫的建立:
將Modelsim根目錄下的modelsim.ini的屬性由只讀改為可寫。
新建一個文件夾,比如library(為敘述方便,把它放在modelsim的根目錄下)。D:/modelsim/library.
啟動Modelsim,選擇[File]/[chang Directory],選擇D:/modelsim/library.
選擇[File]/[New]/[library]命令,彈出[Creat a New library],在[lihrary Name]中輸入「simprims_ver」,同時下一欄也自動輸入「simprims_ver」,單擊OK。
在主窗口中選擇[compile]/[Compile]命令,彈出[compile Source Files],在[Library]的下拉列表中選擇「simprims_ver」在[查找范圍]中選中[Xilinx/veriog/src/simprims]目錄下的全部文件,單擊complie進行編譯。(這時可能會花你一些時間,耐心等待編譯完畢)用同樣的方法將unisims和Xilinxcorelib三個模擬庫進行編譯。
這時在D:/modelsim/library 下就有以上三個模擬庫。
7.總結步驟為a:建立庫的放置路徑b:對庫進行編譯c:對庫進行映射。最後重新啟動Modelsim可以在列表中看到建立的三個庫。
那麼這個辦法明顯是比較麻煩的。其實我們可以這樣做;
首先將modelsim.ini文件只讀模式去掉,存檔前面打對勾。
在您安裝ise的目錄下,進入到bin
t目錄下,例如e:ise6in
t,確認有compxlib這個程序
在cmd中運行compxlib -s mti_se -f all -l all -o e:modeltech_6.0xilinx_libs就可以了,e:modeltech_6.0是我安裝modelsim的目錄,您可以作相應的更改。參數也可以按照您的要求作相應的更改。
這樣就可以了。
需要注意的是,千萬記住ise和modelsim的安裝目錄都不要出現空格,最好是直接安裝在根目錄下
modelsim下編譯xilinx庫的方法
這幾天,建庫的問題比較多,寫一個建庫的方法。
所用軟體:ISE7.1i+ModelsimSE6.0
語言:VHDL
首先安裝軟體。注意:ISE,Modelsim的安裝路徑不能含有空格。
另外,Modelsim的安裝路徑可設為「$:/modelsim」,其中$為盤符,不要使用默認的安裝路徑。
將Modelsim根目錄下的modelsim.ini文件的只讀屬性去掉。
在modelsim的命令窗口中輸入命令「compxlib -s mti_se -arch all -l vhdl -w -lib all」,按回車鍵即可。
編譯完成後,將Modelsim根目錄下的modelsim.ini文件的屬性設置為只讀。
關於「compxlib」命令各項參數的含義,請在modelsim的命令窗口中輸入「compxlib -help」查詢
把庫建好後,接下來的事情就是使它成為modelsim的標准庫。這只要修改modelsim安裝目錄下的modelsim.ini文件就可以了。修改後的內容如下:
[Library]
std = $MODEL_TECH/../std
ieee = $MODEL_TECH/../ieee
verilog = $MODEL_TECH/../verilog
vital2000 = $MODEL_TECH/../vital2000
std_developerskit = $MODEL_TECH/../std_developerskit
synopsys = $MODEL_TECH/../synopsys
modelsim_lib = $MODEL_TECH/../modelsim_lib
simprim_ver = G:/EDA/Xilinx/simprim_ver(庫的路徑,以下同)
unisim_ver = G:/EDA/Xilinx/unisim_ver
xilinxcorelib_ver = G:/EDA/Xilinx/xilinxcorelib_ver
注意的是,這個文件是只讀屬性。修改之前要把這個屬性去掉。
第六步:關掉工程,重啟modelsim。查看這3個庫是否在library框裡面。
二、 在ISE環境下,調用synplify,生成後模擬所需要的文件。
之所以要在ISE環境下調用synplify,主要是因為方便!我也嘗試過在synplify環境下綜合設計文件,然後在ISE里編譯synplify生成的edif文件。但是不成功。ISE在第三方工具支持方面做的是比較好的,感覺跟用ISE直接綜合一樣。不過有一個缺點是看不了RTL原理圖。你可以在synplify中打開ISE生成的synplify工程文件,解決在ISE中不方便查看synplify綜合結果的問題。現在,就要開始第二個大步驟了!
第一步:創建ISE工程文件。選擇好器件。注意Design Flow中一定要選擇Synplify Pro Verilog。
第二步:綜合設計文件,也就是verilog文件。
ISE就會自動調用synplify。(如果沒有的話,那可能是你的系統環境變數沒有設置好)。此時會彈出一個對話框,要你選擇synplify的liscense。(這步本來不用說的。如果沒有對話框彈出來的話,也不要緊)隨便選擇一個,就等結果了。
第三步:生成後模擬需要的文件。
我們可以看到在Implement Design中有三個大分支,這對應著三種模擬。按你的需要按下相應的圖標,生成modelsim後模擬所需要的文件,下面對生成的文件和生成這些文件的圖標進行說明。
第三步:在彈出的對話框里,選擇SDF項。把ISE生成的SDF文件添加進出。如下圖:
記住Apply to Region這一項要寫好。它對應的是你的tb文件(就是測試文件)調用的頂層模塊名。(不是頂層模塊名!!)比如你的測試文件是text,例化頂層模塊top為i_top,那你應該這樣填:text/i_top或者/text/i_top。如果是第一種模擬,此步可以省略。
第四步:添加library。
我們創建的那三個庫終於派上用場了!我們要添加的就是這3個。選擇library項,添加這3個庫。你的庫建在哪裡,就去哪裡找!這個也不用說了吧
第五步:選擇要模擬的模塊。
你先不要急,看清楚再選。(有些朋友性子急,駕輕就熟就選了)
我們要選的模擬模塊可不止一個,如下圖,用CTRL鍵實現!!選了之後點0k!!
9. 安卓關閉selinux好處
你好朋友
1. 禁止selinux
1.1 在內核中關閉selinux編譯選項CONFIG_SECURITY_SELINUX
1.2 還可以在system.prop中定義ro.boot.selinux=disable
這兩種方法都可以禁用selinux,也可以設置成ro.boot.selinux=permissive
寬容模式
1.3 可以通過setenforce1開啟enforce模式,setenforce 0為permissive模式
getenforce獲取當前模式
2. 所有安全策略最終編譯成sepolicy文件放在root目錄下,init進程啟動後會讀取/sepolicy策略文件,並通過/sys/fs/selinux/load節點
把策略文件內容寫入內核
3 安全上下文存放root目錄
/etc/security/mac_permissions.xml
/file_contexts //系統中所有file_contexts安全上下文
/seapp_contexts //app安全上下文
/property_contexts //屬性的安全上下文
/service_contexts //service文件安全上下文
genfs_contexts //虛擬文件系統安全上下文
4. app在/data/data/文件的安全上下文設置過程
1. 根據uid,pkgname,seinfo在seapp_contexts中匹配.
2. 根據匹配到的contexts,重新設置給相對應文件
5. 系統中所有的object class 定義在external/sepolicy/security_classes中.
object class使用在allow語句中,object class所具有的操作定義在external/sepolicy/access_vectors
文件中
6 allow語句
allow語句用來許可權設置
rule_name source_type target_type : class perm_set
rule_name : 有allow,neverallow
source_type : 許可權主體,表示source_type對target_type有perm_set描述的許可權
如:
allow zygote init:process sigchld
允許zygote域裡面的進程可對init域的進程發送sigchld信號
typeattribute表示把屬性和type關聯起來
7 role定義
Android系統中的role定義在external/sepolicy/roles中,
目前只定義了r
8 socket使用
以/data/misc/wifi/sockets/wlan0 socket來說明使用方法
1. 定義socket type
type wpa_socket ,file_type
2. 指定安全上下文
/data/misc/wifi/sockets(/.*)? u:object_r:wpa_socket:s0
給/data/misc/wifi/sockets目錄下所有的文件統一指定安全上下文為wpa_socket
3.聲明socket使用許可權
在進程te中使用unix_socket_send(clientdomain, wpa, serverdomain)即可建立socket連接
9binder使用
在使用binder進程的te中根據情況使用如下宏:
binder_use(domain)//允許domain域中的進程使用binder通信
binder_call(clientdomain, serverdomain)//允許clientdomain和serverdomain域中的進程通信
binder_service(domain)//標志domain為service端
10 文件的使用
以/dev/wmtWifi來說明:
1.定義type
type wmtWifi_device dev_type //dev_type用來標志/dev/下的文件
2.給/dev/wmtWifi指定完全上下文
/dev/wmtWifi(/.*)? u:object_r:wmtWifi_device:s0
3.進程許可權設置
在進程te文件中allow許可權
allow netd wmtWifi_device:chr_file { write open };
11 property 屬性設置
以藍牙的各種屬性來說明
1.定義type
type bluetooth_prop, property_type;
2設置安全上下文
bluetooth. u:object_r:bluetooth_prop:s0
3進程許可權設置
allow bluetooth bluetooth_prop:property_service set;
5 專業詞彙
MLS :Multi-Level Security
RBAC :Role Based Access Control
DAC :Discretionary Access Control
MAC :Mandatory Access Control
TEAC :Type Enforcement Accesc Control
望採納祝你好運