selinux contexts 安全上下文的臨時更改


Androiod 安全上下文的臨時更改,命令:

chcon       安全上下文  文件
chcon  -R   安全上下文   目錄    (-R是遞歸更改)

 

舉例:

原始信息:

xxx:/system/app # ls -lZ
total 199
...... drwxr-xr-x 4 root root u:object_r:system_file:s0 4096 2009-01-01 08:00 Bluetooth drwxr-xr-x 1 root root u:object_r:system_file:s0 3488 2021-07-06 04:11 Stk drwxr-xr-x 3 root root u:object_r:system_file:s0 4096 2009-01-01 08:00 Traceur .....

運行命令:chcon -R u:object_r:vendor_app_file:s0 Stk/

運行后結果:

130|xxx:/system/app # chcon -R u:object_r:vendor_app_file:s0 Stk/
E6A:/system/app # ls -lZ
total 199......
drwxr-xr-x 3 root root u:object_r:system_file:s0     4096 2009-01-01 08:00 SoterService
drwxr-xr-x 1 root root u:object_r:vendor_app_file:s0 3488 2021-07-06 04:11 Stk
drwxr-xr-x 3 root root u:object_r:system_file:s0     4096 2009-01-01 08:00 Traceur
......

130|xxx:/system/app/Stk # ls -lZ
total 275
-rw-r--r-- 1 root root u:object_r:vendor_app_file:s0 277735 2009-01-01 08:00 Stk.apk drwxr-xr-x 1 root root u:object_r:vendor_app_file:s0 3488 2021-07-06 04:11 oat

恢復命令: restorecon -R Stk/   若不生效則重啟手機

 

 

 

 

 

https://www.jianshu.com/p/d10a132df49a

SELinux增強了Linux系統的安全,但是對於初學者來說很容易造成各種各樣的錯誤,經常被搞的莫名其妙不知所措,最后只能祭出大招把SELinux一關了之。筆者也是初學者,也沒有搞清楚其中的道理,只是了解點皮毛而已。本文只從chcon命令的使用結合幾個例子簡單回顧一下,更加深入的知識有待繼續學習。

使用selinux contexts

SELinux增強了Linux系統的安全,但是對於初學者來說很容易造成各種各樣的錯誤,經常被搞的莫名其妙不知所措,最后只能祭出大招把SELinux一關了之。筆者也是初學者,也沒有搞清楚其中的道理,只是了解點皮毛而已。本文只從chcon命令的使用結合幾個例子簡單回顧一下,更加深入的知識有待繼續學習。



chcon man page

NAME chcon - change security context SYNOPSIS chcon [OPTION]... CONTEXT FILE... chcon [OPTION]... --reference=RFILE FILE... DESCRIPTION Change the security context of each FILE to CONTEXT. -c, --changes like verbose but report only when a change is made -h, --no-dereference affect symbolic links instead of any referenced file (available only on systems with lchown system call) -f, --silent, --quiet suppress most error messages -l, --range set range RANGE in the target security context --reference=RFILE use RFILE’s context instead of using a CONTEXT value -R, --recursive change files and directories recursively  -r, --role set role ROLE in the target security context -t, --type set type TYPE in the target security context -u, --user set user USER in the target security context -v, --verbose output a diagnostic for every file processed --help display this help and exit --version output version information and exit REPORTING BUGS Report bugs to <email@host.com>. SEE ALSO The full documentation for chcon is maintained as a Texinfo manual. If the info and chcon programs are properly installed at your site, the command info chcon should give you access to the complete manual. 

SELinux 上下文 簡介

在運行這SELinux的系統上,所有的進程和文件都以標記着一種安全相關的信息,這種信息就是SELinux Context(上下文)。可以使用下面的命令查看文件的上下文:

ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 

在這個例子里面,SELinux提供了一個user (unconfined_u), 一個role (object_r), 一個type (user_home_t), 以及一個level (s0)。這個信息用來進行控制訪問。SELinux實現了強制訪問控制(MAC)的安全模式。在標准的Linux環境中開啟了自由訪問控制(Discretionary Access Control,DAC)后,這一機制就會在Linux內核中對所有操作的安全性進行檢查。在DAC體系中,訪問通過Linux用戶ID和組ID進行控制,SELinux策略規則會在DAC規則之后進行檢查。也就是說,如果DAC規則已經拒絕訪問了,那么SELinux策略規則就不使用了。

有許多命令可以管理文件的SELnux上下文,例如chcon,semanage fcontext和restorecon。

chcon臨時的改變

chcon命令可以臨時的改變文件的上下文,也就是說如果文件系統relabel一下或者執行一下restorecon命令,上下文的改變就實效了。SELinux策略控制着用戶能否修改一個文件的上下文。在適用chcon命令的時候用戶要指定所有的或者部分的要改變的SELinux上下文。

改變一個文件或者文件夾的類型

下面這段只說明一下如何改變SELinux中的類型。這個我們假設file1是一個文件。當然,如果file1是一個文件夾的話也是適用的。

查看文件的上下文
~]# ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 改變文件的上下文 ~]# chcon -t samba_share_t file1 ~]# ls -Z file1 -rw-rw-r-- user1 group1 unconfined_u:object_r:samba_share_t:s0 file1 恢復文件的上下文 ~]# restorecon -v file1 restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0 

在使用RHEL6中默認的SELinux策略的時候,restore命令會讀取/etc/selinux/targeted/contexts/files/文件夾中的文件,來查看文件應該擁有哪個SELinux上下文。

改變一個文件夾及其內容的類型

下面這個例子說明了如何把一個文件夾及其內容的類型改成Apache HTTP服務器使用的類型。當你想讓Apache HTTP服務器使用一個不同的root文件夾的時候(默認的是/var/www/html/),這個配置就有派上用場了。

新建文件夾/web,並在其中新建三個文件file{1,2,3}: ~]# mkdir /web ~]# cd /web ~]# touch file{1,2,3} /web/文件夾和里面的文件都具有default_t類型的標簽: ~]# ls -dZ /web drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web ~]# ls -lZ /web -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3 使用chcon命令把/web/文件夾及其內容的類型改成httpd_sys_content_t: ~]# chcon -R -t httpd_sys_content_t /web/ 使用restorecon命令來恢復默認的上下文: ~]# restorecon -R -v /web/ restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 

參考其他的文件來創建上下文

使用參數--reference來指定其他的文件
chcon -R --reference=RFILE FILE

參考文獻

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Working_with_SELinux-SELinux_Contexts_Labeling_Files.html
http://wiki.centos.org/HowTos/SELinux
http://www.searchsv.com.cn/showcontent_44629.htm




 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM